Map<?, ?> parameters = request.getParameterMap();
String userName = request.getParameter("user");
String passwd = request.getParameter("password");
String isAdminStr = request.getParameter("isAdmin");
String description = request.getParameter("description") + "";
IUserPersistence userHandler = PersistenceFactory.getUserPersistence();
if (parameters.containsKey("Create")) {
try {
if (userHandler.getUserByName(userName) != null) {
out.println("<p><i>User already exists!</i></p>");
logger.debug("event=user_already_exists user_name=" + userName);
} else {
Boolean isAdmin = Boolean.parseBoolean(isAdminStr);
userHandler.createUser(userName, passwd, isAdmin, description);
logger.debug("event=create_user user_name=" + userName);
}
} catch (PersistenceException ex) {
logger.error("event=create_user user_name=" + userName, ex);
throw new ServletException(ex);
}
} else if (parameters.containsKey("Delete")) {
try {
userHandler.deleteUser(userName);
logger.debug("event=delete_user user_name=" + userName);
} catch (PersistenceException ex) {
logger.error("event=delete_user user_name=" + userName, ex);
throw new ServletException(ex);
}
}
out.println("<html>");
header(request, out, "All Users");
out.println("<body>");
out.println("<h2>All Users</h2>");
out.print("<form action=\"/webui\" method=POST>");
out.print("<table><tr><td>Username:</td><td>Password:</td><td>Description:</td><td></td></tr>");
out.print("<tr>" +
"<td><input type='text' name='user'/></td>" +
"<td><input type='password' name='password'></td>" +
"<td><input type='text' name='description'/></td>" +
"<td><input type='checkbox' id='isAdmin' name='isAdmin' value='true'><label for='isAdmin'>Is Admin</label></td>" +
"<td><input type='submit' value='Create' name='Create' /></td></tr>");
out.print("</table>");
out.print("</form>");
List<User> users = new ArrayList<User>();
try {
users = userHandler.getAllUsers();
Collections.sort(users, new Comparator() {
public int compare(Object o1, Object o2) {
User u1 = (User) o1;
User u2 = (User) o2;
return u1.getUserName().compareToIgnoreCase(u2.getUserName());