- refactor out common things from AjaxServlet so it can be used elsewhere

- fix some guice stuff
This commit is contained in:
Andy Janata 2011-12-22 14:19:22 -08:00
parent c0fe506fc5
commit 727f1cf3c0
4 changed files with 118 additions and 47 deletions

View File

@ -16,9 +16,6 @@ import javax.servlet.http.HttpSession;
import net.socialgamer.cah.handlers.Handler;
import net.socialgamer.cah.handlers.Handlers;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import com.google.inject.Guice;
import com.google.inject.Injector;
@ -29,7 +26,7 @@ import com.google.inject.Injector;
* This servlet is only used for client actions, not for long-polling.
*/
@WebServlet("/AjaxServlet")
public class AjaxServlet extends HttpServlet {
public class AjaxServlet extends CahServlet {
private static final long serialVersionUID = 1L;
private final Injector injector;
@ -42,8 +39,8 @@ public class AjaxServlet extends HttpServlet {
public AjaxServlet() {
super();
this.server = new Server();
injector = Guice.createInjector(new CahModule(server));
injector = Guice.createInjector();
this.server = injector.getInstance(Server.class);
}
/**
@ -55,22 +52,14 @@ public class AjaxServlet extends HttpServlet {
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
* @see CahServlet#doPost(HttpServletRequest request, HttpServletResponse response, HttpSession
* hSession)
*/
@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
protected void handleRequest(final HttpServletRequest request,
final HttpServletResponse response, final HttpSession hSession) throws ServletException,
IOException {
final PrintWriter out = response.getWriter();
final HttpSession hSession = request.getSession(true);
if (hSession.isNew()) {
// they should have gotten a session from the index page.
// they probably don't have cookies on.
returnError(out, "Session not detected. Make sure you have cookies enabled.");
return;
}
int serial = -1;
if (request.getParameter("serial") != null) {
try {
@ -94,25 +83,10 @@ public class AjaxServlet extends HttpServlet {
returnError(out, "Invalid operation.", serial);
return;
}
final Map<String, Object> returnData = handler.handle(request.getParameterMap(), hSession);
returnData.put("serial", serial);
returnData(out, returnData);
final Map<String, Object> data = handler.handle(request.getParameterMap(), hSession);
data.put("serial", serial);
returnData(out, data);
return;
}
private void returnError(final PrintWriter writer, final String message) {
returnError(writer, message, -1);
}
@SuppressWarnings("unchecked")
private void returnError(final PrintWriter writer, final String message, final int serial) {
final JSONObject ret = new JSONObject();
ret.put("error", Boolean.TRUE);
ret.put("error_message", message);
writer.println(ret.toJSONString());
}
private void returnData(final PrintWriter writer, final Map<String, Object> data) {
writer.println(JSONValue.toJSONString(data));
}
}

View File

@ -1,22 +1,14 @@
package net.socialgamer.cah;
import net.socialgamer.cah.data.ConnectedUsers;
import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
public class CahModule extends AbstractModule {
private final Server server;
public CahModule(final Server server) {
this.server = server;
}
@Override
protected void configure() {
// TODO Auto-generated method stub
bind(ConnectedUsers.class).toInstance(server.getConnectedUsers());
bind(Server.class).in(Singleton.class);
}
}

View File

@ -0,0 +1,101 @@
package net.socialgamer.cah;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
/**
* Servlet implementation class CahServlet
*/
// @WebServlet("/CahServlet")
public abstract class CahServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CahServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
final HttpSession hSession = request.getSession(true);
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.");
} else {
handleRequest(request, response, hSession);
}
}
/**
* Handles a request from a CAH client. A session is guaranteed to exist at this point.
*
* @param request
* @param response
* @param hSession
* @throws ServletException
* @throws IOException
*/
protected abstract void handleRequest(final HttpServletRequest request,
final HttpServletResponse response, final HttpSession hSession) throws ServletException,
IOException;
/**
* Return an error to the client. Prefer to use the PrintWriter,String,int version if you know the
* request serial number.
*
* @param writer
* @param message
*/
protected void returnError(final PrintWriter writer, final String message) {
returnError(writer, message, -1);
}
/**
* Return an error to the client.
*
* @param writer
* @param message
* @param serial
*/
@SuppressWarnings("unchecked")
protected void returnError(final PrintWriter writer, final String message, final int serial) {
final JSONObject ret = new JSONObject();
ret.put("error", Boolean.TRUE);
ret.put("error_message", message);
writer.println(ret.toJSONString());
}
/**
* Return response data to the client.
*
* @param writer
* Writer for the response.
* @param data
* Key-value data to return as the response.
*/
protected void returnData(final PrintWriter writer, final Map<String, Object> data) {
writer.println(JSONValue.toJSONString(data));
}
}

View File

@ -2,7 +2,10 @@ package net.socialgamer.cah;
import net.socialgamer.cah.data.ConnectedUsers;
import com.google.inject.Singleton;
@Singleton
public class Server {
private final ConnectedUsers users;
@ -10,6 +13,7 @@ public class Server {
users = new ConnectedUsers();
}
// TODO figure out if I can just get this to inject directly
public ConnectedUsers getConnectedUsers() {
return this.users;
}