- refactor out common things from AjaxServlet so it can be used elsewhere
- fix some guice stuff
This commit is contained in:
parent
c0fe506fc5
commit
727f1cf3c0
|
@ -16,9 +16,6 @@ import javax.servlet.http.HttpSession;
|
||||||
import net.socialgamer.cah.handlers.Handler;
|
import net.socialgamer.cah.handlers.Handler;
|
||||||
import net.socialgamer.cah.handlers.Handlers;
|
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.Guice;
|
||||||
import com.google.inject.Injector;
|
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.
|
* This servlet is only used for client actions, not for long-polling.
|
||||||
*/
|
*/
|
||||||
@WebServlet("/AjaxServlet")
|
@WebServlet("/AjaxServlet")
|
||||||
public class AjaxServlet extends HttpServlet {
|
public class AjaxServlet extends CahServlet {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private final Injector injector;
|
private final Injector injector;
|
||||||
|
@ -42,8 +39,8 @@ public class AjaxServlet extends HttpServlet {
|
||||||
public AjaxServlet() {
|
public AjaxServlet() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.server = new Server();
|
injector = Guice.createInjector();
|
||||||
injector = Guice.createInjector(new CahModule(server));
|
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
|
@Override
|
||||||
protected void doPost(final HttpServletRequest request, final HttpServletResponse response)
|
protected void handleRequest(final HttpServletRequest request,
|
||||||
throws ServletException, IOException {
|
final HttpServletResponse response, final HttpSession hSession) throws ServletException,
|
||||||
response.setContentType("application/json");
|
IOException {
|
||||||
final PrintWriter out = response.getWriter();
|
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;
|
int serial = -1;
|
||||||
if (request.getParameter("serial") != null) {
|
if (request.getParameter("serial") != null) {
|
||||||
try {
|
try {
|
||||||
|
@ -94,25 +83,10 @@ public class AjaxServlet extends HttpServlet {
|
||||||
returnError(out, "Invalid operation.", serial);
|
returnError(out, "Invalid operation.", serial);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final Map<String, Object> returnData = handler.handle(request.getParameterMap(), hSession);
|
final Map<String, Object> data = handler.handle(request.getParameterMap(), hSession);
|
||||||
returnData.put("serial", serial);
|
data.put("serial", serial);
|
||||||
returnData(out, returnData);
|
returnData(out, data);
|
||||||
return;
|
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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,14 @@
|
||||||
package net.socialgamer.cah;
|
package net.socialgamer.cah;
|
||||||
|
|
||||||
import net.socialgamer.cah.data.ConnectedUsers;
|
|
||||||
|
|
||||||
import com.google.inject.AbstractModule;
|
import com.google.inject.AbstractModule;
|
||||||
|
import com.google.inject.Singleton;
|
||||||
|
|
||||||
|
|
||||||
public class CahModule extends AbstractModule {
|
public class CahModule extends AbstractModule {
|
||||||
|
|
||||||
private final Server server;
|
|
||||||
|
|
||||||
public CahModule(final Server server) {
|
|
||||||
this.server = server;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure() {
|
protected void configure() {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
bind(ConnectedUsers.class).toInstance(server.getConnectedUsers());
|
bind(Server.class).in(Singleton.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -2,7 +2,10 @@ package net.socialgamer.cah;
|
||||||
|
|
||||||
import net.socialgamer.cah.data.ConnectedUsers;
|
import net.socialgamer.cah.data.ConnectedUsers;
|
||||||
|
|
||||||
|
import com.google.inject.Singleton;
|
||||||
|
|
||||||
|
|
||||||
|
@Singleton
|
||||||
public class Server {
|
public class Server {
|
||||||
private final ConnectedUsers users;
|
private final ConnectedUsers users;
|
||||||
|
|
||||||
|
@ -10,6 +13,7 @@ public class Server {
|
||||||
users = new ConnectedUsers();
|
users = new ConnectedUsers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO figure out if I can just get this to inject directly
|
||||||
public ConnectedUsers getConnectedUsers() {
|
public ConnectedUsers getConnectedUsers() {
|
||||||
return this.users;
|
return this.users;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue