001: package org.claros.intouch.webmail.services;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005: import java.util.Collections;
006: import java.util.List;
007:
008: import javax.servlet.ServletException;
009: import javax.servlet.http.HttpServletRequest;
010: import javax.servlet.http.HttpServletResponse;
011:
012: import org.claros.commons.auth.models.AuthProfile;
013: import org.claros.commons.mail.comparator.ComparatorDate;
014: import org.claros.commons.mail.models.ConnectionMetaHandler;
015: import org.claros.commons.mail.models.ConnectionProfile;
016: import org.claros.commons.mail.models.EmailHeader;
017: import org.claros.commons.utility.Utility;
018: import org.claros.intouch.common.services.BaseService;
019: import org.claros.intouch.webmail.controllers.FolderController;
020: import org.claros.intouch.webmail.controllers.InboxController;
021: import org.claros.intouch.webmail.factory.FolderControllerFactory;
022: import org.claros.intouch.webmail.factory.InboxControllerFactory;
023:
024: public class ListNewMailHeadersService extends BaseService {
025:
026: /**
027: *
028: */
029: private static final long serialVersionUID = 6686687365451641031L;
030:
031: /**
032: * The doGet method of the servlet. <br>
033: *
034: * This method is called when a form has its tag value method equals to get.
035: *
036: * @param request the request send by the client to the server
037: * @param response the response send by the server to the client
038: * @throws ServletException if an error occurred
039: * @throws IOException if an error occurred
040: */
041: public void doGet(HttpServletRequest request,
042: HttpServletResponse response) throws ServletException,
043: IOException {
044:
045: response.setHeader("Expires", "-1");
046: response.setHeader("Pragma", "no-cache");
047: response.setHeader("Cache-control", "no-cache");
048: response.setHeader("Content-Type", "text/xml; charset=utf-8");
049: PrintWriter out = response.getWriter();
050:
051: out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
052: out.write("<data>");
053: out.print("<items>");
054:
055: AuthProfile auth = getAuthProfile(request);
056:
057: // prepare variables
058: List headers = null;
059: ConnectionMetaHandler handler = getConnectionHandler(request);
060: ConnectionProfile profile = getConnectionProfile(request);
061:
062: FolderControllerFactory foldFact = null;
063: FolderController folderCont = null;
064: String currFolder = org.claros.commons.mail.utility.Constants
065: .FOLDER_INBOX(profile);
066:
067: try {
068: if (auth == null) {
069: throw new org.claros.commons.exception.NoPermissionException();
070: }
071: InboxControllerFactory inFact = new InboxControllerFactory(
072: auth, profile, handler);
073: InboxController inCont = inFact.getInboxController();
074: handler = inCont.checkEmail();
075: request.getSession().setAttribute("handler", handler);
076: foldFact = new FolderControllerFactory(auth, profile,
077: handler);
078: folderCont = foldFact.getFolderController();
079:
080: // time to fetch the headers
081: headers = folderCont.getHeadersByFolder(currFolder);
082:
083: // sort the headers
084: Collections.sort(headers, new ComparatorDate(false));
085:
086: // organize and generate XML from the headers.
087: if (headers != null) {
088: EmailHeader tmp = null;
089: for (int i = 0; i < headers.size(); i++) {
090: tmp = (EmailHeader) headers.get(i);
091: if (tmp.getUnread().booleanValue() == true) {
092: out.print("<item>");
093: out.print("<id> " + tmp.getMessageId()
094: + "</id>");
095: out.print("<from> "
096: + Utility.htmlSpecialChars(tmp
097: .getFromShown()) + "</from>");
098: out.print("<subject> "
099: + Utility.htmlSpecialChars(tmp
100: .getSubject()) + "</subject>");
101: out.print("</item>");
102: }
103: }
104: }
105: out.print("</items>");
106: out.print("<result>0</result>");
107: } catch (Exception e) {
108: out.print("</items>");
109: out.print("<result>1</result>");
110: }
111: out.print("</data>");
112: }
113:
114: }
|