package com.phancyte2.servlet.ajax; import java.io.IOException; import java.util.Date; import java.util.logging.Logger; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.phancyte2.database.phancyte.pojo.User; import com.phancyte2.servlet.BaseServlet; import com.phancyte2.struts.ajax.ChatModel; import com.phancyte2.struts.ajax.Message; import com.phancyte2.util.XStreamUtil; import com.phancyte2.util.string.StringUtil; import com.phancyte2.web.bean.session.UserViewBean; import com.sun.enterprise.web.connector.grizzly.comet.CometContext; import com.sun.enterprise.web.connector.grizzly.comet.CometEngine; import com.sun.enterprise.web.connector.grizzly.comet.CometEvent; import com.sun.enterprise.web.connector.grizzly.comet.CometHandler; public class CometChat2 extends BaseServlet { private static final Logger sLogger = Logger.getLogger(CometChat2.class.getName()); public static final String CHAT_CONTEXT_PATH = "/chat"; protected ChatModel chatModel = ChatModel.getInstance(); @Override public void init(ServletConfig config) throws ServletException { // called only once on servlet instantiation/loading sLogger.info("[CometChat2]\tSetting up servlet"); CometEngine engine = CometEngine.getEngine(); CometContext cometContext = engine.register(CHAT_CONTEXT_PATH); cometContext.setExpirationDelay(1000 * 60 * 60); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { //when user client first loads the chat program CometChatHandler handler = new CometChatHandler(); handler.attach(res); //get user UserViewBean userViewBean = getUserViewBean(req); if(!chatModel.containsUser(userViewBean.getUser().getNickName())) { sLogger.info("[CometChat2]\tUser logging to chat: " + userViewBean.getUser().getNickName()); chatModel.addUser(userViewBean.getUser().getNickName(), userViewBean, handler); CometEngine engine = CometEngine.getEngine(); CometContext context = engine.getCometContext(CHAT_CONTEXT_PATH); Message message = getMessage(req); int hashCode = context.addCometHandler(handler); sLogger.info("[CometChat2]\tHashCode for user: " + userViewBean.getUser().getNickName() + " is: " + hashCode); //notify all buddies that user has logged in if(message != null) { notifyLoggedIn(message, userViewBean); Message msgToSelf = new Message( userViewBean.getUser().getNickName(), userViewBean.getUser().getNickName(), "You successfully logged into the chat system", Message.Type.LOGIN.toString()); msgToSelf.setDate(new Date().toString()); sendMessageToSelf(msgToSelf); } } else { sLogger.info("[CometChat2]\tUser has already logged in: " + userViewBean.getUser().getNickName()); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { //sending messages Message message = getMessage(req); if(message != null) { sendMessageToUser(message); } else { sLogger.info("[CometChat2]\tfMessage is null, cannot send message"); } } /** * Payload of request should be in json format and parameter name holding the info * should be jsonMSG. * @param req * @return */ protected Message getMessage(HttpServletRequest req) { //printRequestParams(req); //printHeaders(req); String jsonMSG = req.getParameter("jsonMSG"); sLogger.info("getMessage - jsonMSG: " + jsonMSG); if(!StringUtil.isEmpty(jsonMSG)) { try { return XStreamUtil.jsonToObject("jsonMSG", Message.class, req.getParameter("jsonMSG")); } catch(Exception e) { e.printStackTrace(); } } return null; } public void notifyLoggedIn(Message message, UserViewBean bean) { CometEngine engine = CometEngine.getEngine(); CometContext context = engine.getCometContext(CHAT_CONTEXT_PATH); for(User buddy : bean.getBuddies()) { CometHandler handler = chatModel.getHandler(bean.getUser().getNickName()).getSecond(); if(handler != null && context.isActive(handler)) { int hashCode = handler.hashCode(); try { sLogger.info("[CometChat2]\tNotifying " + buddy.getNickName() + " that " + bean.getUser().getNickName() + "has logged in"); context.notify(message, CometEvent.NOTIFY, hashCode); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { sLogger.info("[CometChat]\tHandler is null for notifying that user: " + message.getFrom() + " has logged in for: " + message.getTo()); } } } public void notifyLoggedOut(Message message, UserViewBean bean) { } public void sendMessageToUser(Message message) { sendMessage(message, chatModel.getHandler(message.getTo()).getSecond()); } public void sendMessageToSelf(Message message) { sendMessage(message, chatModel.getHandler(message.getFrom()).getSecond()); } public void sendMessage(Message message, CometHandler handler) { if(handler != null) { try { CometEngine engine = CometEngine.getEngine(); CometContext context = engine.getCometContext(CHAT_CONTEXT_PATH); int hashCode = handler.hashCode(); sLogger.info("[CometChat2]\tSending message to: " + message.getTo() + " from: " + message.getFrom() + " handler hashcode: " + hashCode); context.notify(message, CometEvent.NOTIFY, hashCode); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { sLogger.severe("[CometChat]\tHandler is null for sending message to: " + message.getTo()); } } }