/[projects]/WebConfig/src/dk/thoerup/webconfig/ConfigServlet.java
ViewVC logotype

Contents of /WebConfig/src/dk/thoerup/webconfig/ConfigServlet.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1326 - (show annotations) (download)
Tue Apr 19 21:07:58 2011 UTC (13 years, 1 month ago) by torben
File size: 4327 byte(s)
Ensure 'true' or 'false' for booleans
1 package dk.thoerup.webconfig;
2
3 import java.io.IOException;
4 import java.lang.annotation.Annotation;
5 import java.lang.reflect.Field;
6
7 import javax.servlet.ServletException;
8 import javax.servlet.http.HttpServlet;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 public class ConfigServlet extends HttpServlet {
13 private static final long serialVersionUID = 1L;
14
15 private Object configObject;
16
17 public void setConfigObject(Object obj) {
18 configObject = obj;
19 }
20
21
22 @Override
23 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
24
25
26 resp.setDateHeader("Expires", 0);
27 resp.setHeader("Pragma", "no-cache");
28 resp.setHeader("Cache-Control", "no-cache");
29 resp.setContentType("text/html");
30
31 StringBuilder sb = new StringBuilder();
32
33 sb.append("<html><body><h3>WebConfig:</h3><table border='1' cellspacing='0'>");
34 sb.append("<tr><th>Field name</th><th>Type</th><th>Description</th><th>Value</th><th>&nbsp;</th></tr>\n");
35
36
37 Field fields[] = configObject.getClass().getDeclaredFields();
38
39 try {
40 for(Field field : fields) {
41 field.setAccessible(true);
42
43 ConfigVariable anno = field.getAnnotation(ConfigVariable.class);
44 if (anno == null)
45 continue;
46
47 sb.append("<tr>");
48 sb.append("<td>").append( field.getName() ).append("</td>");
49 sb.append("<td>").append( field.getType().getName() ).append("</td>");
50 sb.append("<td>").append( anno.description() ).append("&nbsp;</td>");
51
52 sb.append("<form action='").append( req.getRequestURI() ).append("' method='post'>");
53 sb.append("<input type='hidden' name='varname' value='").append( field.getName() ).append("' />");
54
55
56
57
58 if (anno.readonly() == false) {
59 sb.append("<td><input type='text' name='varvalue' value='").append( ""+ field.get(configObject) ).append("' /></td>");
60 sb.append("<td><input type='submit' name='Set' value='Set'></td>\n");
61 } else {
62 sb.append("<td>").append( ""+ field.get(configObject) ).append("&nbsp;</td>");
63 sb.append("<td>&nbsp;</td>");
64 }
65
66 sb.append("</form></tr>\n");
67
68
69
70
71 field.getType().getName();
72 }
73 } catch (IllegalAccessException e) {
74 throw new ServletException(e);
75 }
76
77 sb.append("</table></body></html>");
78
79 resp.getWriter().print( sb.toString() );
80 }
81
82 @Override
83 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
84 String name = req.getParameter("varname");
85 String value = req.getParameter("varvalue").trim();
86
87 try {
88 Field field = configObject.getClass().getDeclaredField(name);
89 field.setAccessible(true);
90 ConfigVariable anno = field.getAnnotation(ConfigVariable.class);
91
92 if (anno == null) {
93 resp.sendError(500, "Field is not annotated");
94 return;
95 }
96
97 if (anno.readonly() == true) {
98 resp.sendError(500, "Can't write a read-only field");
99 return;
100 }
101
102
103
104 if (field.getType().getName().equals("int") ) {
105 field.setInt(configObject, Integer.parseInt(value) );
106 } else if (field.getType().getName().equals("boolean") ) {
107 value = value.toLowerCase();
108 if (! (value.equals("true") || value.equals("false"))) {
109 resp.sendError(500, "Only 'true' or 'false' as values for boolean field");
110 return;
111 }
112 field.setBoolean(configObject, Boolean.parseBoolean(value) );
113 } else if (field.getType().getName().equals("long") ) {
114 field.setLong(configObject, Long.parseLong(value) );
115 } else if (field.getType().getName().equals("short") ) {
116 field.setShort(configObject, Short.parseShort(value) );
117 } else if (field.getType().getName().equals("double") ) {
118 field.setDouble(configObject, Double.parseDouble(value) );
119 } else if (field.getType().getName().equals("float") ) {
120 field.setFloat(configObject, Float.parseFloat(value) );
121 } else if (field.getType().getName().equals("char") ) {
122 field.setFloat(configObject, value.charAt(0) );
123 } else {
124 field.set(configObject, value );
125 }
126
127
128 } catch (NoSuchFieldException e) {
129 resp.sendError(500, "No such field: " + name);
130 } catch (IllegalAccessException e) { //this can't happen
131 throw new ServletException(e);
132 }
133
134 resp.sendRedirect( req.getRequestURI() );
135 }
136
137 }

  ViewVC Help
Powered by ViewVC 1.1.20