refactor Handler:

- abstract class
- so I could make an error() function to create the Map in one place

RegisterHandler also checks nicks against a regex
This commit is contained in:
Andy Janata 2011-12-24 19:34:22 -08:00
parent 87c77993af
commit c1e033cd65
4 changed files with 30 additions and 25 deletions

View File

@ -15,7 +15,7 @@ import net.socialgamer.cah.data.User;
* @author ajanata
*
*/
public class FirstLoadHandler implements Handler {
public class FirstLoadHandler extends Handler {
public static final String OP = "firstload";

View File

@ -1,5 +1,6 @@
package net.socialgamer.cah.handlers;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
@ -11,6 +12,13 @@ import javax.servlet.http.HttpSession;
* @author ajanata
*
*/
public interface Handler {
public Map<String, Object> handle(Map<String, String[]> parameters, HttpSession session);
public abstract class Handler {
public abstract Map<String, Object> handle(Map<String, String[]> parameters, HttpSession session);
protected Map<String, Object> error(final String message) {
final Map<String, Object> data = new HashMap<String, Object>();
data.put("error", Boolean.TRUE);
data.put("error_message", message);
return data;
}
}

View File

@ -2,6 +2,7 @@ package net.socialgamer.cah.handlers;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import javax.servlet.http.HttpSession;
@ -12,17 +13,20 @@ import net.socialgamer.cah.data.User;
import com.google.inject.Inject;
public class RegisterHandler implements Handler {
public class RegisterHandler extends Handler {
public static final String OP = "register";
private static final Pattern validName = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_]{2,29}");
private final ConnectedUsers users;
// @Inject
// public RegisterHandler(final ConnectedUsers users) {
// this.users = users;
// }
/**
* I don't want to have to inject the entire server here, but I couldn't figure out how to
* properly bind ConnectedUsers in Guice, so here we are for now.
*
* @param server
*/
@Inject
public RegisterHandler(final Server server) {
this.users = server.getConnectedUsers();
@ -31,33 +35,26 @@ public class RegisterHandler implements Handler {
@Override
public Map<String, Object> handle(final Map<String, String[]> parameters,
final HttpSession session) {
final HashMap<String, Object> ret = new HashMap<String, Object>();
final Map<String, Object> data = new HashMap<String, Object>();
if (!parameters.containsKey("nickname") || parameters.get("nickname").length != 1) {
ret.put("error", Boolean.TRUE);
ret.put("error_message", "No nickname specified.");
return error("No nickname specified.");
} else {
final String nick = parameters.get("nickname")[0].trim();
if (nick.length() < 3) {
ret.put("error", Boolean.TRUE);
ret.put("error_message", "Nickname " + nick
+ " is too short. Minimum length is 3 characters.");
} else if (nick.length() > 30) {
ret.put("error", Boolean.TRUE);
ret.put("error_message", "Nickname " + nick
+ " is too long. Maximum length is 30 characters.");
if (!validName.matcher(nick).matches()) {
return error("Nickname must contain only upper and lower case letters, numbers, or"
+ " underscores, must be 3 to 30 characters long, and must not start with a number.");
} else if (users.hasUser(nick)) {
ret.put("error", Boolean.TRUE);
ret.put("error_message", "Nickname " + nick + " already in use.");
return error("Nickname " + nick + " already in use.");
} else {
// TODO this will get cluttered until we do pinging
final User user = new User(nick);
users.newUser(user);
session.setAttribute("user", user);
ret.put("nickname", nick);
data.put("nickname", nick);
}
}
return ret;
return data;
}
}

View File

@ -5,7 +5,7 @@ import java.util.Map;
import javax.servlet.http.HttpSession;
public class TestHandler implements Handler {
public class TestHandler extends Handler {
public static final String OP = "test";