package webIM; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; /** * Title: Chat * Description: This servlet is invoked when the Heartbeat servlet has detected * a new file submission and issued a JavaScript location.reload() * command. Checks are made to ensure all needed session and servlet * context variables are present, then control is forwarded to the * createChatDisplay JavaServer page. * Copyright: Copyright (c) 2001 * Company: UCCS Computer Science Department * @author Merlin Vincent * @version 1.0 */ public class Chat extends HttpServlet { private static final String CONTENT_TYPE = "text/html"; private static String endl = System.getProperty("line.separator"); private int count = 0; private String localhostAddress = null; // localhost.domain.naem /**Initialize global variables*/ public void init(ServletConfig config) throws ServletException { super.init(config); } /** * Process the HTTP Get request. The Heartbeat servlet discovers when the * newFilesList has grown, and commands the Chat frame to reload itself, i.e., * to issue a GET request to this servlet. */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(false); ServletContext context = getServletContext(); // Retrieve the existing session and its variables. if (session == null) throw new ServletException("Chat GET: no HttpSession"); String userName = (String) session.getAttribute("username"); if (userName == null) throw new ServletException("Chat GET: no username in session"); String nickName = (String) session.getAttribute("nickname"); if (nickName == null) throw new ServletException("Chat GET: no nickname in session"); String buddyName = (String) context.getAttribute(nickName+"selectedBuddy"); if (buddyName == null) throw new ServletException("Chat GET: no buddy name in session"); if (buddyName.equals("none")) { // No submissions from anywhere - return an empty document. out.print(""); return; } // Also need the buddy's username and hostname as filename components. String buddyUsername = (String) session.getAttribute("selectedBuddyUser"); String buddyHostname = (String) session.getAttribute("selectedBuddyHost"); if (buddyUsername == null || buddyHostname == null) { try { String buddyKey = buddyName.replace(' ','_'); buddyUsername = Validate.getCustomerInfo(userName, buddyKey+"_username"); session.setAttribute("selectedBuddyUser", buddyUsername); buddyHostname = Validate.getCustomerInfo(userName, buddyKey+"_hostname"); session.setAttribute("selectedBuddyHost", buddyHostname); } catch (Exception e) { throw new ServletException( "Chat GET: resource: "+ userName +".properties, key: "+ buddyName+"_username"+ e); } } /** * Make sure we have the newFilesList and historyFilesList. */ ArrayList historyFilesList = (ArrayList) session.getAttribute("historyChatFiles"); if (historyFilesList == null) { historyFilesList = new ArrayList(); session.setAttribute("historyChatFiles", historyFilesList); } ArrayList newFilesList = (ArrayList) context.getAttribute( nickName+buddyName+"NewChat"); if (newFilesList == null) throw new ServletException("Chat GET: no newFilesList in context"); // Now forward control to the JSP that will build the chat window. RequestDispatcher dispatcher = getServletContext().getRequestDispatcher( "/createChatDisplay.jsp" ); dispatcher.forward( request, response ); } /**Clean up resources*/ public void destroy() { } }