001: package org.claros.chat.ajax;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005:
006: import javax.servlet.ServletException;
007: import javax.servlet.http.HttpServlet;
008: import javax.servlet.http.HttpServletRequest;
009: import javax.servlet.http.HttpServletResponse;
010:
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013: import org.claros.chat.controllers.TrafficController;
014: import org.claros.chat.threads.ChatListener;
015: import org.claros.chat.threads.ChatSender;
016: import org.jivesoftware.smack.GoogleTalkConnection;
017: import org.jivesoftware.smack.XMPPConnection;
018: import org.jivesoftware.smack.XMPPException;
019:
020: public class Authenticate extends HttpServlet {
021: private static Log log = LogFactory.getLog(Authenticate.class);
022:
023: /**
024: *
025: */
026: private static final long serialVersionUID = 6002125607784963740L;
027:
028: /**
029: * Constructor of the object.
030: */
031: public Authenticate() {
032: super ();
033: }
034:
035: /**
036: * The doPost method of the servlet. <br>
037: *
038: * This method is called when a form has its tag value method equals to post.
039: *
040: * @param request the request send by the client to the server
041: * @param response the response send by the server to the client
042: * @throws ServletException if an error occurred
043: * @throws IOException if an error occurred
044: */
045: public void doPost(HttpServletRequest request,
046: HttpServletResponse response) throws ServletException,
047: IOException {
048: response.setHeader("Expires", "-1");
049: response.setHeader("Pragma", "no-cache");
050: response.setHeader("Cache-control", "no-cache");
051:
052: response.setContentType("text/html");
053: PrintWriter out = response.getWriter();
054:
055: String username = request.getParameter("username");
056: request.getSession().setAttribute("user", username);
057: String password = request.getParameter("password");
058: String server = request.getParameter("server");
059:
060: try {
061: XMPPConnection connection = null;
062: if (server == null || server.equals("Google Talk")) {
063: try {
064: connection = new GoogleTalkConnection();
065: if (username.indexOf("@") > 0) {
066: username = username.substring(0, username
067: .indexOf("@"));
068: }
069: } catch (Exception e1) {
070: throw e1;
071: }
072: } else {
073: try {
074: connection = new XMPPConnection(server, 5222);
075: } catch (Exception e1) {
076: throw e1;
077: }
078: }
079:
080: try {
081: connection.login(username, password);
082: log.debug("connection established for user: "
083: + username);
084: } catch (XMPPException e) {
085: throw e;
086: }
087: request.getSession().setAttribute("conn", connection);
088:
089: // start the user's chat listener thread
090: String user = connection.getUser();
091: if (request.getSession().getAttribute("listener") == null) {
092: ChatListener list = new ChatListener(user, connection);
093: TrafficController.addListener(user, list);
094: list.start();
095: log
096: .debug("listener created and added to the traffic controller for user: "
097: + username);
098: }
099:
100: if (request.getSession().getAttribute("sender") == null) {
101: // start user's chat sender thread
102: ChatSender sender = new ChatSender(username, connection);
103: TrafficController.addSender(user, sender);
104: sender.start();
105: log
106: .debug("sender created and added to the traffic controller for user: "
107: + username);
108: }
109:
110: out.print("ok");
111: } catch (Exception e) {
112: out.print("fail");
113: }
114: log
115: .debug("authentication complete finishing servlet for user: "
116: + username);
117: // out.flush();
118: // out.close();
119: }
120: }
|