package com.phancyte2.servlet.ajax; import java.io.IOException; import java.io.PrintWriter; import java.util.logging.Logger; import javax.servlet.http.HttpServletResponse; import com.phancyte2.struts.ajax.Message; import com.phancyte2.util.XStreamUtil; import com.sun.enterprise.web.connector.grizzly.comet.CometEvent; import com.sun.enterprise.web.connector.grizzly.comet.CometHandler; public class CometChatHandler implements CometHandler { private static final Logger sLogger = Logger .getLogger(CometChatHandler.class.getName()); protected HttpServletResponse response; @Override public void attach(HttpServletResponse response) { this.response = response; this.response.setCharacterEncoding("UTF-8"); } @Override public void onEvent(CometEvent event) throws IOException { // TODO Auto-generated method stub sLogger.info("[CometChatHandler]\tEvent type is: " + event.getType()); if(event.getType() == CometEvent.NOTIFY) { Object obj = event.attachment(); if(obj != null && obj instanceof Message) { PrintWriter writer = response.getWriter(); writer.write(XStreamUtil.objectToJSON("jsonMSG", obj)); writer.flush(); //event.getCometContext().resumeCometHandler(this); event.getCometContext().registerAsyncRead(this); event.getCometContext().registerAsyncWrite(this); } else { sLogger.severe("[CometChatHandler]\tEvent attachment is not a Message obj: " + obj.getClass().getName()); } } } @Override public void onInitialize(CometEvent arg0) throws IOException { // TODO Auto-generated method stub } @Override public void onInterrupt(CometEvent arg0) throws IOException { // TODO Auto-generated method stub } @Override public void onTerminate(CometEvent arg0) throws IOException { // TODO Auto-generated method stub } }