//tomcat character encoding fix ...
resp.setCharacterEncoding(req.getCharacterEncoding());
// Backyard API
Backyard backyard = new Backyard(req, resp);
backyard.setServlet(this);
// json obj
String data = req.getParameter("data");
try {
log.debug("Parsing JSON: " + data);
JSONObject json = new JSONObject(data);
// get function from request json
String function = json.getString("fn");
if (function.matches("handshake")) {
log.debug("Handshake");
// TODO: GENERATE AN ID OR SEND THE SESSION ID TO THE CLIENT
PrintWriter pw = ev.getHttpServletResponse().getWriter();
resp.getWriter().print(
"{\"status\":\"OK\",\"id\":\""
+ req.getSession().getId()
+ "\",\"websocket\":\""
+ Backyard.isWebsocketSupport() + "\"}");
pw.flush();
pw.close();
return;
}
if (function.matches("comet")) {
log.debug("comet");
// if the implementation is a non Tomcat implementation invoke
// the normal
if (req.getAttribute("de.jochenbrissier.byw.comet") != null
&& req.getAttribute("de.jochenbrissier.byw.comet")
.equals("nonTomcat")) {
backyard.startAsync();
} else {
backyard.startAsync(ev);
}
}
if (function.matches("listen")) {
log.debug("listen");
listenChannel(req, resp, json, backyard);
ev.close();
}
if (function.matches("send")) {
log.debug("sendtochannel");
JSONObject channeldata = json.getJSONObject("channel");
try {
backyard.getChannel(channeldata.getString("channel_name"))
.sendMessage(json.getString("data"));
} catch (Exception e) {
e.printStackTrace();
}