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: request.getSession().setAttribute("defaultDomain", null);
061: try {
062: XMPPConnection connection = null;
063: if (server == null || server.equals("Google Talk")) {
064: request.getSession().setAttribute("defaultDomain",
065: "gmail.com");
066: try {
067: connection = new GoogleTalkConnection();
068: if (username.indexOf("@") > 0) {
069: username = username.substring(0, username
070: .indexOf("@"));
071: }
072: } catch (Exception e1) {
073: throw e1;
074: }
075: } else {
076: try {
077: request.getSession().setAttribute("defaultDomain",
078: server);
079: connection = new XMPPConnection(server, 5222);
080: } catch (Exception e1) {
081: throw e1;
082: }
083: }
084:
085: try {
086: connection.login(username, password);
087: log.debug("connection established for user: "
088: + username);
089: } catch (XMPPException e) {
090: log.info(e);
091: // maybe user doesn't exist so try to create one.
092: try {
093: connection.getAccountManager().createAccount(
094: username, password);
095: connection.login(username, password);
096: } catch (Exception e1) {
097: log.info(e1);
098: throw e1;
099: }
100: }
101: request.getSession().setAttribute("conn", connection);
102:
103: // start the user's chat listener thread
104: String user = connection.getUser();
105: if (request.getSession().getAttribute("listener") == null) {
106: ChatListener list = new ChatListener(user, connection);
107: TrafficController.addListener(user, list);
108: list.start();
109: log
110: .debug("listener created and added to the traffic controller for user: "
111: + username);
112: }
113:
114: if (request.getSession().getAttribute("sender") == null) {
115: // start user's chat sender thread
116: String dd = (String) request.getSession().getAttribute(
117: "defaultDomain");
118: ChatSender sender = new ChatSender(username,
119: connection, dd);
120: TrafficController.addSender(user, sender);
121: sender.start();
122: log
123: .debug("sender created and added to the traffic controller for user: "
124: + username);
125: }
126:
127: out.print("ok");
128: } catch (Exception e) {
129: out.print("fail");
130: }
131: log
132: .debug("authentication complete finishing servlet for user: "
133: + username);
134: }
135: }
|