View Javadoc

1   /*
2    * $Id: SetProperties.java,v 1.2 2005/03/21 18:45:55 mch Exp $
3    *
4    * (C) Copyright Astrogrid...
5    */
6   
7   package org.astrogrid.webapp;
8   
9   import java.io.IOException;
10  import java.net.URL;
11  import javax.servlet.ServletException;
12  import javax.servlet.http.HttpServletRequest;
13  import javax.servlet.http.HttpServletResponse;
14  import org.astrogrid.cfg.ConfigFactory;
15  
16  /***
17   * A servlet that can be called from JSPs etc to set properties.  BEWARE that
18   * this obviously can create a nasty huge security hole.  Make sure that all
19   * your setup stuff - but particularly this servlet, which is used to make the
20   * actual changes - are suitably locked away behind a login
21   * <p>
22   * Also provides a convenience routine for generating the input fields with
23   * the right names and values that will make suitable parameters for the servlet
24   *
25   * @author M Hill
26   */
27  
28  public class SetProperties extends DefaultServlet
29  {
30     /***
31      * Takes the multi-value parameter 'set' which has values corresponding to
32      * the property keys to set.  Each parameter key should then reference a value.
33      * Not sure how to get descriptions in here yet...
34      * <p>
35      * set the 'forwardTo' parameter to set which page should be automatically
36      * loaded when this servlet has finished
37      */
38     public void doGet(HttpServletRequest request,
39                       HttpServletResponse response) throws ServletException, IOException {
40  
41        String[] sets = request.getParameterValues("set");
42        
43        for (int i = 0; i < sets.length; i++) {
44           String key = sets[i];
45           ConfigFactory.getCommonConfig().setProperty(key, request.getParameter(key));
46        }
47        
48        //forward
49        String targetUrl = request.getParameter("returnTo");
50        request.getRequestDispatcher(targetUrl).forward(request, response);
51  //      response.getWriter().write("<META HTTP-EQUIV='Refresh' CONTENT='0;URL="+targetUrl+"'>");
52     }
53     
54     public static String makePropertyInput(String key, String description, String defaultValue) {
55  
56        if ((description == null) || (description.trim().length()==0)) {
57           //look in JNDI for description?
58        }
59        
60        String value = ConfigFactory.getCommonConfig().getString(key, defaultValue);
61        
62        return
63           "<input type='checkbox' name='set' value='"+key+"' />"+
64           "<input type='text' name='key' value='"+key+"' readonly='readonly' />"+
65           "<input type='text' name='description' value='"+description+"' readonly='readonly' />"+
66           "<input type='text' name='"+key+"' value='"+value+"'/>";
67    
68     }
69     
70  }
71