package dk.thoerup.webconfig;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ConfigServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Object configObject;
public void setConfigObject(Object obj) {
configObject = obj;
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setDateHeader("Expires", 0);
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setContentType("text/html");
StringBuilder sb = new StringBuilder();
sb.append("
WebConfig:
");
sb.append("Field name | Type | Description | Value | |
\n");
Field fields[] = configObject.getClass().getDeclaredFields();
try {
for(Field field : fields) {
field.setAccessible(true);
ConfigVariable anno = field.getAnnotation(ConfigVariable.class);
if (anno == null)
continue;
sb.append("");
sb.append("").append( field.getName() ).append(" | ");
sb.append("").append( field.getType().getName() ).append(" | ");
sb.append("").append( anno.description() ).append(" | ");
sb.append("
\n");
field.getType().getName();
}
} catch (IllegalAccessException e) {
throw new ServletException(e);
}
sb.append("
");
resp.getWriter().print( sb.toString() );
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("varname");
String value = req.getParameter("varvalue");
try {
Field field = configObject.getClass().getDeclaredField(name);
field.setAccessible(true);
ConfigVariable anno = field.getAnnotation(ConfigVariable.class);
if (anno == null)
throw new ServletException("Field is not annotated");
if (anno.readonly() == true)
throw new ServletException("Can't write a read-only field");
if (field.getType().getName().equals("int") ) {
field.setInt(configObject, Integer.parseInt(value) );
} else if (field.getType().getName().equals("boolean") ) {
field.setBoolean(configObject, Boolean.parseBoolean(value) );
} else if (field.getType().getName().equals("long") ) {
field.setLong(configObject, Long.parseLong(value) );
} else if (field.getType().getName().equals("short") ) {
field.setShort(configObject, Short.parseShort(value) );
} else if (field.getType().getName().equals("double") ) {
field.setDouble(configObject, Double.parseDouble(value) );
} else if (field.getType().getName().equals("float") ) {
field.setFloat(configObject, Float.parseFloat(value) );
} else if (field.getType().getName().equals("char") ) {
field.setFloat(configObject, value.charAt(0) );
} else {
field.set(configObject, value );
}
} catch (NoSuchFieldException e) {
throw new ServletException(e);
} catch (IllegalAccessException e) { //this can't happen
throw new ServletException(e);
}
resp.sendRedirect( req.getRequestURI() );
}
}