1 |
torben |
1319 |
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> </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(" </td>"); |
51 |
|
|
|
52 |
|
|
sb.append("<form action='").append( req.getRequestURI() ).append("' method='post'>"); |
53 |
|
|
sb.append("<td><input type='text' name='varvalue' value='").append( ""+ field.get(configObject) ).append("' /></td>"); |
54 |
|
|
sb.append("<input type='hidden' name='varname' value='").append( field.getName() ).append("' />"); |
55 |
|
|
|
56 |
|
|
if (anno.readonly() == false) |
57 |
|
|
sb.append("<td><input type='submit' name='Set' value='Set'></td>\n"); |
58 |
|
|
else |
59 |
|
|
sb.append("<td> </td>"); |
60 |
|
|
|
61 |
|
|
sb.append("</form></tr>\n"); |
62 |
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
|
|
field.getType().getName(); |
67 |
|
|
} |
68 |
|
|
} catch (IllegalAccessException e) { |
69 |
|
|
throw new ServletException(e); |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
sb.append("</table></body></html>"); |
73 |
|
|
|
74 |
|
|
resp.getWriter().print( sb.toString() ); |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
@Override |
78 |
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
79 |
|
|
String name = req.getParameter("varname"); |
80 |
|
|
String value = req.getParameter("varvalue"); |
81 |
|
|
|
82 |
|
|
try { |
83 |
|
|
Field field = configObject.getClass().getDeclaredField(name); |
84 |
|
|
field.setAccessible(true); |
85 |
|
|
ConfigVariable anno = field.getAnnotation(ConfigVariable.class); |
86 |
|
|
|
87 |
torben |
1321 |
if (anno == null) { |
88 |
|
|
resp.sendError(500, "Field is not annotated"); |
89 |
|
|
return; |
90 |
|
|
} |
91 |
torben |
1319 |
|
92 |
torben |
1321 |
if (anno.readonly() == true) { |
93 |
|
|
resp.sendError(500, "Can't write a read-only field"); |
94 |
|
|
return; |
95 |
|
|
} |
96 |
|
|
|
97 |
torben |
1319 |
|
98 |
|
|
|
99 |
|
|
if (field.getType().getName().equals("int") ) { |
100 |
|
|
field.setInt(configObject, Integer.parseInt(value) ); |
101 |
|
|
} else if (field.getType().getName().equals("boolean") ) { |
102 |
|
|
field.setBoolean(configObject, Boolean.parseBoolean(value) ); |
103 |
|
|
} else if (field.getType().getName().equals("long") ) { |
104 |
|
|
field.setLong(configObject, Long.parseLong(value) ); |
105 |
|
|
} else if (field.getType().getName().equals("short") ) { |
106 |
|
|
field.setShort(configObject, Short.parseShort(value) ); |
107 |
|
|
} else if (field.getType().getName().equals("double") ) { |
108 |
|
|
field.setDouble(configObject, Double.parseDouble(value) ); |
109 |
|
|
} else if (field.getType().getName().equals("float") ) { |
110 |
|
|
field.setFloat(configObject, Float.parseFloat(value) ); |
111 |
|
|
} else if (field.getType().getName().equals("char") ) { |
112 |
|
|
field.setFloat(configObject, value.charAt(0) ); |
113 |
|
|
} else { |
114 |
|
|
field.set(configObject, value ); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
|
118 |
|
|
} catch (NoSuchFieldException e) { |
119 |
torben |
1321 |
resp.sendError(500, "No such field: " + name); |
120 |
torben |
1319 |
} catch (IllegalAccessException e) { //this can't happen |
121 |
|
|
throw new ServletException(e); |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
resp.sendRedirect( req.getRequestURI() ); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
} |