}
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
final RequestHelper requestHelper = new RequestHelper((HttpServletRequest)request,(HttpServletResponse)response);
if (jettyServlet != null) {
// use the Jetty Bayeux implementation if available
jettyServlet.service(request, response);
return;
}
response.setContentType("application/json");
Object json = JSON.parse(request.getParameter("message"));
Client eventStream = null;
if (json instanceof HashMap) {
List array = new ArrayList();
array.add(json);
json = array;
}
List array = (List) json;
boolean respondImmediately = false;
for (int i = 0;i < array.size(); i++) {
Map obj = (Map) array.get(i);
String channel = (String) obj.get("channel");
String clientId = (String) requestHelper.getRequest().getSession().getAttribute("clientId");
if (clientId != null) {
obj.put("clientId", clientId);
eventStream = (Client) request.getSession().getAttribute(clientId);
}
obj.remove("ext");
if (channel.startsWith("/meta")) {
obj.put("successful", true);
if (channel.equals("/meta/handshake")) {
obj.put("authSuccessful", true);
clientId = Math.random() + "";
request.getSession().setAttribute(clientId,eventStream = new Client(clientId));
eventStream.connectionId = clientId;
requestHelper.getRequest().getSession().setAttribute("clientId", clientId);
obj.put("clientId", clientId);
Map advice = new HashMap();
advice.put("reconnect", "retry");
obj.put("advice", advice);
List connectionTypes = new ArrayList();
connectionTypes.add("rest-channels");
obj.put("supportedConnectionTypes", connectionTypes);
respondImmediately = true;
}
else if (channel.equals("/meta/connect")) {
obj.put("error","");
Map advice = new HashMap();
advice.put("reconnect", "retry");
obj.put("advice", advice);
}
else if (channel.equals("/meta/disconnect")) {
respondImmediately = true;
}
else if (channel.equals("/meta/subscribe")) {
//TODO: Need to implement this copying from PersevereFilter
}
else if (channel.equals("/meta/unsubscribe")) {
//TODO: Need to implement this
}
else
throw new RuntimeException("Unknown meta channel");
}
else {
// publish event
List target = (List) Client.getCurrentObjectResponse().requestData(channel, false);
Object appendingItem = requestHelper.convertJsonStringToObject(obj.get("instances").toString());
target.add(appendingItem);
}
}
ServletOutputStream outStream = response.getOutputStream();
if (respondImmediately) {