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.ConnectionConfiguration;
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: private static final ConnectionConfiguration googleConfig = new ConnectionConfiguration(
023: "talk.google.com", 5222, "gmail.com");
024:
025: /**
026: *
027: */
028: private static final long serialVersionUID = 6002125607784963740L;
029:
030: /**
031: * Constructor of the object.
032: */
033: public Authenticate() {
034: super ();
035: }
036:
037: /**
038: * The doPost method of the servlet. <br>
039: *
040: * This method is called when a form has its tag value method equals to post.
041: *
042: * @param request the request send by the client to the server
043: * @param response the response send by the server to the client
044: * @throws ServletException if an error occurred
045: * @throws IOException if an error occurred
046: */
047: public void doPost(HttpServletRequest request,
048: HttpServletResponse response) throws ServletException,
049: IOException {
050: response.setHeader("Expires", "-1");
051: response.setHeader("Pragma", "no-cache");
052: response.setHeader("Cache-control", "no-cache");
053:
054: response.setContentType("text/html");
055: PrintWriter out = response.getWriter();
056:
057: String username = request.getParameter("username");
058: request.getSession().setAttribute("user", username);
059: String password = request.getParameter("password");
060: String server = request.getParameter("server");
061:
062: request.getSession().setAttribute("defaultDomain", null);
063: try {
064: XMPPConnection connection = null;
065: if (server == null || server.equals("Google Talk")) {
066: request.getSession().setAttribute("defaultDomain",
067: "gmail.com");
068: try {
069: if (log.isDebugEnabled()) {
070: // googleConfig.setDebuggerEnabled(true);
071: }
072: googleConfig.setReconnectionAllowed(true);
073:
074: connection = new XMPPConnection(googleConfig);
075:
076: if (username.indexOf("@") > 0) {
077: username = username.substring(0, username
078: .indexOf("@"));
079: }
080: } catch (Exception e1) {
081: throw e1;
082: }
083: } else {
084: try {
085: int port = 5222;
086: if (server.indexOf(":") > 0) {
087: try {
088: port = Integer
089: .parseInt(server.substring(server
090: .indexOf(":") + 1));
091: server = server.substring(0, server
092: .indexOf(":"));
093: } catch (Exception e) {
094: }
095: }
096: request.getSession().setAttribute("defaultDomain",
097: server);
098:
099: ConnectionConfiguration config = new ConnectionConfiguration(
100: server, port, "claros.org");
101: config.setExpiredCertificatesCheckEnabled(false);
102: config.setNotMatchingDomainCheckEnabled(false);
103: config.setSelfSignedCertificateEnabled(true);
104: config.setReconnectionAllowed(true);
105:
106: if (log.isDebugEnabled()) {
107: // config.setDebuggerEnabled(true);
108: }
109:
110: connection = new XMPPConnection(config);
111: } catch (Exception e1) {
112: throw e1;
113: }
114: }
115:
116: try {
117: connection.connect();
118: connection.login(username, password);
119: log.debug("connection established for user: "
120: + username);
121: } catch (XMPPException e) {
122: log.info(e);
123: // maybe user doesn't exist so try to create one.
124: try {
125: connection.connect();
126: connection.getAccountManager().createAccount(
127: username, password);
128: connection.login(username, password);
129: } catch (Exception e1) {
130: log.info(e1);
131: throw e1;
132: }
133: }
134: request.getSession().setAttribute("conn", connection);
135:
136: // start the user's chat listener thread
137: String user = connection.getUser();
138: if (request.getSession().getAttribute("listener") == null) {
139: ChatListener list = new ChatListener(user, connection);
140: TrafficController.addListener(user, list);
141: list.start();
142: log
143: .debug("listener created and added to the traffic controller for user: "
144: + username);
145: }
146:
147: if (request.getSession().getAttribute("sender") == null) {
148: // start user's chat sender thread
149: String dd = (String) request.getSession().getAttribute(
150: "defaultDomain");
151: ChatSender sender = new ChatSender(username,
152: connection, dd);
153: TrafficController.addSender(user, sender);
154: sender.start();
155: log
156: .debug("sender created and added to the traffic controller for user: "
157: + username);
158: }
159:
160: out.print("ok");
161: } catch (Exception e) {
162: out.print("fail");
163: }
164: log
165: .debug("authentication complete finishing servlet for user: "
166: + username);
167: }
168: }
|