001: package org.claros.intouch.profiling.services;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005: import java.util.HashMap;
006: import java.util.Set;
007:
008: import javax.servlet.ServletException;
009: import javax.servlet.http.HttpServlet;
010: import javax.servlet.http.HttpServletRequest;
011: import javax.servlet.http.HttpServletResponse;
012:
013: import org.apache.commons.logging.Log;
014: import org.apache.commons.logging.LogFactory;
015: import org.claros.commons.auth.MailAuth;
016: import org.claros.commons.auth.exception.LoginInvalidException;
017: import org.claros.commons.auth.models.AuthProfile;
018: import org.claros.commons.exception.SystemException;
019: import org.claros.commons.mail.exception.ServerDownException;
020: import org.claros.commons.mail.models.ConnectionMetaHandler;
021: import org.claros.commons.mail.models.ConnectionProfile;
022: import org.claros.commons.mail.models.ConnectionProfileList;
023: import org.claros.intouch.webmail.controllers.FolderController;
024: import org.claros.intouch.webmail.factory.FolderControllerFactory;
025:
026: public class LoginService extends HttpServlet {
027: private static final long serialVersionUID = -7565470602365037845L;
028: private static Log log = LogFactory.getLog(LoginService.class);
029:
030: /**
031: * The doPost method of the servlet. <br>
032: *
033: * This method is called when a form has its tag value method equals to post.
034: *
035: * @param request the request send by the client to the server
036: * @param response the response send by the server to the client
037: * @throws ServletException if an error occurred
038: * @throws IOException if an error occurred
039: */
040: public void doPost(HttpServletRequest request,
041: HttpServletResponse response) throws ServletException,
042: IOException {
043:
044: response.setHeader("Expires", "-1");
045: response.setHeader("Pragma", "no-cache");
046: response.setHeader("Cache-control", "no-cache");
047: response.setHeader("Content-Type", "text/html; charset=utf-8");
048:
049: PrintWriter out = response.getWriter();
050:
051: log
052: .debug("Fetching the server profile from the config.xml file");
053:
054: try {
055: HashMap map = ConnectionProfileList.getConList();
056: if (map != null) {
057: Set set = map.keySet();
058: if (set == null) {
059: log
060: .fatal("Mail server profile is not accessable. Can't continue. :( Sorry. Please check your config.xml file.1");
061: throw new SystemException();
062: }
063: Object arr[] = set.toArray();
064: if (arr == null || arr.length <= 0) {
065: log
066: .fatal("Mail server profile is not accessable. Can't continue. :( Sorry. Please check your config.xml file.2");
067: throw new SystemException();
068: }
069: ConnectionProfile profile = ConnectionProfileList
070: .getProfileByShortName((String) arr[0]);
071: if (profile == null) {
072: log
073: .fatal("Mail server profile is not accessable. Can't continue. :( Sorry. Please check your config.xml file.3");
074: throw new SystemException();
075: }
076: log
077: .debug("I've got the mail server profile. Keep on... "
078: + profile.toString());
079:
080: String username = request.getParameter("username");
081: String password = request.getParameter("password");
082:
083: if (username != null && password != null) {
084: AuthProfile auth = new AuthProfile();
085: auth.setUsername(username);
086: auth.setPassword(password);
087: ConnectionMetaHandler handler = (ConnectionMetaHandler) request
088: .getSession().getAttribute("handler");
089: log.debug("Starting authentication");
090:
091: try {
092: handler = MailAuth.authenticate(profile, auth,
093: handler);
094: if (handler != null) {
095: log
096: .debug("Authentication was successful... :) Good news!");
097:
098: request.getSession().setAttribute(
099: "handler", handler);
100: request.getSession().setAttribute("auth",
101: auth);
102: request.getSession().setAttribute(
103: "profile", profile);
104:
105: // create default mailboxes if not exists
106: try {
107: log
108: .debug("Trying to create default mailbox folders if they don't exist.");
109:
110: FolderControllerFactory factory = new FolderControllerFactory(
111: auth, profile, handler);
112: FolderController foldCont = factory
113: .getFolderController();
114: foldCont.createDefaultFolders();
115: } catch (Exception e) {
116: log
117: .error(
118: "unable to create default folders!!!! It will probably fail but giving a try",
119: e);
120: }
121:
122: log
123: .debug("Everything was fine. Sending an OK signal to the UI");
124: out.print("ok");
125: } else {
126: log
127: .debug("Can't authenticate. username and password is most probably wrong.1");
128: out.print("no");
129: }
130: } catch (LoginInvalidException e) {
131: log
132: .debug("Can't authenticate. username and password is most probably wrong.3");
133: out.print("no");
134: } catch (ServerDownException e) {
135: log
136: .error("Can't reach the server. Please make sure everything is fine at config.xml file and server is up and running.");
137: out.print("system");
138: }
139: } else {
140: log
141: .debug("Can't authenticate. username and password is most probably wrong.2");
142: out.print("no");
143: }
144: } else {
145: throw new SystemException();
146: }
147: } catch (SystemException e) {
148: log
149: .error(
150: "Can't get mail server list. Please make sure everything is fine at config.xml file.",
151: e);
152: out.print("system");
153: } catch (Throwable e) {
154: log
155: .error(
156: "Unknown error. Please check logs for more information. ",
157: e);
158: out.print("system");
159: }
160: }
161: }
|