From 1b9caf3bef7768d19d2a19fac49b38507b5a3341 Mon Sep 17 00:00:00 2001 From: Andy Janata Date: Thu, 22 Dec 2011 18:44:03 -0800 Subject: [PATCH] rework the initial request error checking in the servlets to be a bit better, and return an error code to the client that the client can then act on (instead of just a string) --- src/net/socialgamer/cah/AjaxServlet.java | 9 +++++---- src/net/socialgamer/cah/CahServlet.java | 23 +++++++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/net/socialgamer/cah/AjaxServlet.java b/src/net/socialgamer/cah/AjaxServlet.java index d7d3e56..0423441 100644 --- a/src/net/socialgamer/cah/AjaxServlet.java +++ b/src/net/socialgamer/cah/AjaxServlet.java @@ -65,14 +65,15 @@ public class AjaxServlet extends CahServlet { try { serial = Integer.parseInt(request.getParameter("serial")); } catch (final NumberFormatException nfe) { - returnError(out, "Bad request"); + returnError(out, "bad_req", "Bad request"); return; } } final String op = request.getParameter("op"); - if (op.equals("") || !Handlers.LIST.containsKey(op)) { - returnError(out, "Operation not specified.", serial); + // !Handlers.LIST.containsKey(op) + if (op == null || op.equals("")) { + returnError(out, "op_not_spec", "Operation not specified.", serial); return; } @@ -80,7 +81,7 @@ public class AjaxServlet extends CahServlet { try { handler = injector.getInstance(Handlers.LIST.get(op)); } catch (final Exception e) { - returnError(out, "Invalid operation.", serial); + returnError(out, "bad_op", "Invalid operation.", serial); return; } final Map data = handler.handle(request.getParameterMap(), hSession); diff --git a/src/net/socialgamer/cah/CahServlet.java b/src/net/socialgamer/cah/CahServlet.java index 405f361..faa568e 100644 --- a/src/net/socialgamer/cah/CahServlet.java +++ b/src/net/socialgamer/cah/CahServlet.java @@ -38,10 +38,16 @@ public abstract class CahServlet extends HttpServlet { response.setContentType("application/json"); final HttpSession hSession = request.getSession(true); + final String op = request.getParameter("op"); + final boolean skipSessionUserCheck = op != null + && (op.equals("register") || op.equals("firstload")); if (hSession.isNew()) { // they should have gotten a session from the index page. // they probably don't have cookies on. - returnError(response.getWriter(), "Session not detected. Make sure you have cookies enabled."); + returnError(response.getWriter(), "no_session", + "Session not detected. Make sure you have cookies enabled."); + } else if (!skipSessionUserCheck && hSession.getAttribute("user") == null) { + returnError(response.getWriter(), "not_registered", "Not registered. Refresh the page."); } else { handleRequest(request, response, hSession); } @@ -61,14 +67,17 @@ public abstract class CahServlet extends HttpServlet { IOException; /** - * Return an error to the client. Prefer to use the PrintWriter,String,int version if you know the - * request serial number. + * Return an error to the client. Prefer to use the PrintWriter,String,String,int version if you + * know the request serial number. * * @param writer + * @param code + * Error code that the js code knows how to handle. * @param message + * User-visible error message. */ - protected void returnError(final PrintWriter writer, final String message) { - returnError(writer, message, -1); + protected void returnError(final PrintWriter writer, final String code, final String message) { + returnError(writer, code, message, -1); } /** @@ -79,9 +88,11 @@ public abstract class CahServlet extends HttpServlet { * @param serial */ @SuppressWarnings("unchecked") - protected void returnError(final PrintWriter writer, final String message, final int serial) { + protected void returnError(final PrintWriter writer, final String code, final String message, + final int serial) { final JSONObject ret = new JSONObject(); ret.put("error", Boolean.TRUE); + ret.put("error_code", code); ret.put("error_message", message); writer.println(ret.toJSONString()); }