001: package org.claros.intouch.webmail.services;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005: import java.net.URLDecoder;
006: import java.util.Collections;
007: import java.util.List;
008: import java.util.Locale;
009: import java.util.StringTokenizer;
010:
011: import javax.servlet.ServletException;
012: import javax.servlet.http.Cookie;
013: import javax.servlet.http.HttpServletRequest;
014: import javax.servlet.http.HttpServletResponse;
015:
016: import org.apache.commons.logging.Log;
017: import org.apache.commons.logging.LogFactory;
018: import org.claros.commons.auth.models.AuthProfile;
019: import org.claros.commons.mail.comparator.ComparatorDate;
020: import org.claros.commons.mail.comparator.ComparatorFrom;
021: import org.claros.commons.mail.comparator.ComparatorSize;
022: import org.claros.commons.mail.comparator.ComparatorSubject;
023: import org.claros.commons.mail.comparator.ComparatorTo;
024: import org.claros.commons.mail.models.ConnectionMetaHandler;
025: import org.claros.commons.mail.models.ConnectionProfile;
026: import org.claros.commons.mail.models.EmailHeader;
027: import org.claros.commons.mail.utility.Constants;
028: import org.claros.intouch.common.services.BaseService;
029: import org.claros.intouch.webmail.controllers.FolderController;
030: import org.claros.intouch.webmail.controllers.InboxController;
031: import org.claros.intouch.webmail.factory.FolderControllerFactory;
032: import org.claros.intouch.webmail.factory.InboxControllerFactory;
033: import org.claros.intouch.webmail.models.FolderDbObject;
034: import org.claros.intouch.common.utility.Utility;
035:
036: public class ListHeadersService extends BaseService {
037:
038: /**
039: *
040: */
041: private static final long serialVersionUID = -38711675148470029L;
042: private static Log log = LogFactory
043: .getLog(ListHeadersService.class);
044: private static Locale loc;
045:
046: static {
047: if (org.claros.intouch.common.utility.Constants.charset
048: .indexOf("_") < 0) {
049: loc = new Locale(
050: org.claros.intouch.common.utility.Constants.charset);
051: } else {
052: StringTokenizer token = new StringTokenizer(
053: org.claros.intouch.common.utility.Constants.charset,
054: "_");
055: loc = new Locale(token.nextToken(), token.nextToken());
056: }
057: }
058:
059: /**
060: * The doPost method of the servlet. <br>
061: *
062: * This method is called when a form has its tag value method equals to post.
063: *
064: * @param request the request send by the client to the server
065: * @param response the response send by the server to the client
066: * @throws ServletException if an error occurred
067: * @throws IOException if an error occurred
068: */
069: public void doPost(HttpServletRequest request,
070: HttpServletResponse response) throws ServletException,
071: IOException {
072: response.setHeader("Expires", "-1");
073: response.setHeader("Pragma", "no-cache");
074: response.setHeader("Cache-control", "no-cache");
075: response.setHeader("Content-Type", "text/html; charset=utf-8");
076:
077: PrintWriter out = response.getWriter();
078:
079: AuthProfile auth = getAuthProfile(request);
080: // get folder and set it into sesssion
081: String sFolder = URLDecoder.decode((String) getVariable(
082: request, "folder"), "UTF-8");
083:
084: // prepare variables
085: List headers = null;
086: ConnectionMetaHandler handler = getConnectionHandler(request);
087: ConnectionProfile profile = getConnectionProfile(request);
088:
089: FolderControllerFactory foldFact = null;
090: FolderController folderCont = null;
091: String currFolder = org.claros.commons.mail.utility.Constants
092: .FOLDER_INBOX(profile);
093:
094: try {
095: if (auth == null) {
096: throw new org.claros.commons.exception.NoPermissionException();
097: }
098: // if folder name is empty or it is inbox then do mail filtering. It is done by inbox controller
099: if (sFolder == null || sFolder.equals("")
100: || sFolder.equals(currFolder)) {
101: try {
102: InboxControllerFactory inFact = new InboxControllerFactory(
103: auth, profile, handler);
104: InboxController inCont = inFact
105: .getInboxController();
106: handler = inCont.checkEmail();
107: request.getSession().setAttribute("handler",
108: handler);
109: foldFact = new FolderControllerFactory(auth,
110: profile, handler);
111: folderCont = foldFact.getFolderController();
112: } catch (Exception e) {
113: log.debug("minor error while checking new mail", e);
114: }
115:
116: // get the id(pop3) or the mail folder name (imap)
117: if (profile.getProtocol().equals(
118: org.claros.commons.mail.utility.Constants.POP3)) {
119: currFolder = folderCont.getInboxFolder().getId()
120: .toString();
121: } else {
122: currFolder = folderCont.getInboxFolder()
123: .getFolderName();
124: }
125: } else {
126: currFolder = sFolder;
127: handler = (ConnectionMetaHandler) request.getSession()
128: .getAttribute("handler");
129: foldFact = new FolderControllerFactory(auth, profile,
130: handler);
131: folderCont = foldFact.getFolderController();
132: }
133: request.getSession().setAttribute("folder", currFolder);
134:
135: // time to fetch the headers
136: headers = folderCont.getHeadersByFolder(currFolder);
137:
138: // get info about the current folder
139: FolderDbObject myFolder = folderCont.getFolder(currFolder);
140:
141: // get and set sort parameters
142: String mailSort = (String) getVariable(request, "mailSort");
143: if (null == mailSort)
144: mailSort = "date";
145: String mailSortDirection = (String) getVariable(request,
146: "mailSortDirection");
147: if (null == mailSortDirection)
148: mailSortDirection = "desc";
149: request.getSession().setAttribute("mailSort", mailSort);
150: request.getSession().setAttribute("mailSortDirection",
151: mailSortDirection);
152:
153: // get and set pageNo
154: int pageNo = 1;
155: try {
156: pageNo = Integer
157: .parseInt(getVariable(request, "pageNo")
158: .toString());
159: } catch (Exception e) {
160: }
161: request.getSession().setAttribute("pageNo",
162: new Integer(pageNo));
163:
164: Cookie c1 = new Cookie("mailSort", mailSort);
165: c1.setMaxAge(Integer.MAX_VALUE);
166: Cookie c2 = new Cookie("mailSortDirection",
167: mailSortDirection);
168: c2.setMaxAge(Integer.MAX_VALUE);
169: Cookie c3 = new Cookie("pageNo", String.valueOf(pageNo));
170: c3.setMaxAge(Integer.MAX_VALUE);
171: response.addCookie(c1);
172: response.addCookie(c2);
173: response.addCookie(c3);
174:
175: boolean isAscending = false;
176: if (mailSortDirection != null
177: && mailSortDirection.equals("asc")) {
178: isAscending = true;
179: }
180:
181: // organize em
182: String fromSort = "";
183: String dateSort = "";
184: String sizeSort = "";
185: String subjectSort = "";
186:
187: if (mailSort == null || mailSort.equals("date")) {
188: if (isAscending)
189: dateSort = "asc";
190: else
191: dateSort = "desc";
192: } else if (mailSort.equals("from")) {
193: if (isAscending)
194: fromSort = "asc";
195: else
196: fromSort = "desc";
197: } else if (mailSort.equals("subject")) {
198: if (isAscending)
199: subjectSort = "asc";
200: else
201: subjectSort = "desc";
202: } else if (mailSort.equals("to")) {
203: if (isAscending)
204: fromSort = "asc";
205: else
206: fromSort = "desc";
207: } else if (mailSort.equals("size")) {
208: if (isAscending)
209: sizeSort = "asc";
210: else
211: sizeSort = "desc";
212: }
213:
214: out
215: .print("<div id=\"ieHolder\" class=\"ieHolder\"><div class=\"ie7Holder\" id=\"ie7Holder\">");
216:
217: // at least show the title bar also with sort arrows
218: if (profile.getProtocol().equals(Constants.IMAP)) {
219: out
220: .print("<p class='title' id='mailtitle' displayName='"
221: + myFolder.getFolderName()
222: + "' folderid='"
223: + myFolder.getId()
224: + "' foldername='mailFolder"
225: + myFolder.getFolderName()
226: + "' type='"
227: + myFolder.getFolderType()
228: + "'>"
229: + " <span class='flag' style='background-image:url(images/apply.gif);background-repeat:no-repeat;' onclick='clickAll();'><a href='javascript:;'> </a></span>");
230: } else {
231: out
232: .print("<p class='title' id='mailtitle' displayName='"
233: + myFolder.getFolderName()
234: + "' folderid='"
235: + myFolder.getId()
236: + "' foldername='mailFolder"
237: + myFolder.getId()
238: + "' type='"
239: + myFolder.getFolderType()
240: + "'>"
241: + " <span class='flag' style='background-image:url(images/apply.gif);background-repeat:no-repeat;' onclick='clickAll();'><a href='javascript:;'> </a></span>");
242: }
243: out
244: .print("<span class='attributes'><a href='javascript:;'><span style='cursor:hand'><img alt='' src='images/priority-title.gif' align='absmiddle'><img alt='' src='images/sensitivity-title.gif' align='absmiddle'></span></a></span>");
245:
246: if (myFolder
247: .getFolderType()
248: .equals(
249: org.claros.intouch.common.utility.Constants.FOLDER_TYPE_SENT)) {
250: out
251: .print(" <span class='from "
252: + fromSort
253: + "' onclick=\"sortColumn(this, 'to');\" sort='"
254: + fromSort
255: + "'><a href='javascript:;'><span style='cursor:hand'>"
256: + getText(request, "to")
257: + "</span></a></span>");
258: if (mailSort != null && mailSort.equals("from")) {
259: mailSort = "to";
260: }
261: } else {
262: out
263: .print(" <span class='from "
264: + fromSort
265: + "' onclick=\"sortColumn(this, 'from');\" sort='"
266: + fromSort
267: + "'><a href='javascript:;'><span style='cursor:hand'>"
268: + getText(request, "from")
269: + "</span></a></span>");
270: if (mailSort != null && mailSort.equals("to")) {
271: mailSort = "from";
272: }
273: }
274: out
275: .print(" <span class='subject "
276: + subjectSort
277: + "' onclick=\"sortColumn(this, 'subject');\" sort='"
278: + subjectSort
279: + "'><a href='javascript:;'><span style='cursor:hand'>"
280: + getText(request, "subject")
281: + "</span></a></span>"
282: + " <span class='date "
283: + dateSort
284: + "' onclick=\"sortColumn(this, 'date');\" sort='"
285: + dateSort
286: + "'><a href='javascript:;'><span style='cursor:hand'>"
287: + getText(request, "date")
288: + "</span></a></span>"
289: + " <span class='size "
290: + sizeSort
291: + "' onclick=\"sortColumn(this, 'size');\" sort='"
292: + sizeSort
293: + "'><a href='javascript:;'><span style='cursor:hand'>"
294: + getText(request, "size")
295: + "</span></a></span>"
296: + " <span class='attach'><a href='javascript:;'> </a></span>"
297: + "</p>");
298: // sort the headers
299: if (mailSort == null || mailSort.equals("date")) {
300: Collections.sort(headers, new ComparatorDate(
301: isAscending));
302: } else if (mailSort.equals("from")) {
303: Collections.sort(headers, new ComparatorFrom(
304: isAscending, loc));
305: } else if (mailSort.equals("subject")) {
306: Collections.sort(headers, new ComparatorSubject(
307: isAscending, loc));
308: } else if (mailSort.equals("to")) {
309: Collections.sort(headers, new ComparatorTo(isAscending,
310: loc));
311: } else if (mailSort.equals("size")) {
312: Collections.sort(headers, new ComparatorSize(
313: isAscending));
314: }
315:
316: // organize and generate XML from the headers.
317: if (headers != null) {
318: EmailHeader tmp = null;
319: String subject = null;
320: String from = null;
321: int pageSize = 25;
322: int messageCount = headers.size();
323: int pageCount = messageCount / pageSize;
324: if ((messageCount % pageSize) > 0)
325: pageCount++;
326: if (pageNo > pageCount)
327: pageNo = pageCount;
328: int startIdx = (pageNo - 1) * pageSize;
329: if (startIdx < 0)
330: startIdx = 0;
331: int endIdx = startIdx + pageSize;
332: if (endIdx > messageCount)
333: endIdx = messageCount;
334:
335: out
336: .print("<span id=\"pagerParams\" style=\"display:none\">"
337: + pageNo
338: + ":"
339: + pageCount
340: + ":"
341: + messageCount + "</span>");
342:
343: for (int i = startIdx; i < endIdx; i++) {
344: tmp = (EmailHeader) headers.get(i);
345: subject = tmp.getSubject();
346: if (null == subject || 0 == subject.length())
347: subject = getText(request, "no.subject");
348: from = tmp.getFromShown();
349: if (null == from || 0 == from.length())
350: from = getText(request, "unknown.sender");
351: String priority = org.claros.commons.mail.models.EmailPriority
352: .toStringValue(tmp.getPriority());
353: short sensitivity = tmp.getSensitivity();
354:
355: out
356: .print("<p "
357: + ((tmp.isMultipart()) ? " class='attach' "
358: : "")
359: + "id='mail"
360: + tmp.getMessageId()
361: + "' "
362: + ((tmp.getUnread().booleanValue()) ? "style='font-weight:bold;'"
363: : "")
364: + ">"
365: + "<span class='flag' onclick='mailListClick(event, this, false, true);'> </span>");
366: out
367: .print("<span class='attributes' onclick='mailListClick(event, this, true, false);'><img alt='' src='images/priority-"
368: + priority.toLowerCase()
369: + ".gif' align='absmiddle'><img alt='' src='images/sensitivity-"
370: + sensitivity
371: + ".gif' align='absmiddle'></span>");
372: if (myFolder
373: .getFolderType()
374: .equals(
375: org.claros.intouch.common.utility.Constants.FOLDER_TYPE_SENT)) {
376: out
377: .print("<span class='from' onclick='mailListClick(event, this, true, false);'>"
378: + org.claros.commons.utility.Utility
379: .convertTRCharsToHtmlSafe(Utility
380: .htmlCheck(sbjTrim(tmp
381: .getToShown())))
382: + "</span>");
383: } else {
384: out
385: .print("<span class='from' onclick='mailListClick(event, this, true, false);'>"
386: + org.claros.commons.utility.Utility
387: .convertTRCharsToHtmlSafe(Utility
388: .htmlCheck(sbjTrim(from)))
389: + "</span>");
390: }
391: out
392: .print("<span class='subject' onclick='mailListClick(event, this, true, false);' title=\""
393: + org.claros.commons.utility.Utility
394: .convertTRCharsToHtmlSafe(Utility
395: .htmlCheck(subject))
396: + "\">"
397: + org.claros.commons.utility.Utility
398: .convertTRCharsToHtmlSafe(Utility
399: .htmlCheck(sbjTrim(subject)))
400: + "</span>"
401: + "<span class='date' onclick='mailListClick(event, this, true, false);'>"
402: + Utility.htmlCheck(tmp
403: .getDateShown())
404: + "</span>"
405: + "<span class='size' onclick='mailListClick(event, this, true, false);'>"
406: + Utility.htmlCheck(tmp
407: .getSizeShown())
408: + "</span>"
409: + "<span class='attach' onclick='mailListClick(event, this, true, false);'> </span>"
410: + "</p>");
411: }
412: }
413:
414: out.print("</div></div>");
415: } catch (Exception e) {
416: out
417: .print("<p class='title'>"
418: + "<span class='flag' style='background-image:url(images/apply.gif);background-repeat:no-repeat;' onclick='clickAll();'><a href='javascript:;'> </a></span>"
419: + "<span class='attributes'><a href='javascript:;'><span> </span></a></span>"
420: + "<span class='from asc'><a href='javascript:;'><span>"
421: + getText(request, "from")
422: + "</span></a></span>"
423: + "<span class='subject'><a href='javascript:;'><span>"
424: + getText(request, "subject")
425: + "</span></a></span>"
426: + "<span class='date'><a href='javascript:;'><span>"
427: + getText(request, "date")
428: + "</span></a></span>"
429: + "<span class='size'><a href='javascript:;'><span>"
430: + getText(request, "size")
431: + "</span></a></span>"
432: + "<span class='attach'><a href='javascript:;'> </a></span>"
433: + "</p>");
434:
435: }
436: }
437:
438: public void doGet(HttpServletRequest request,
439: HttpServletResponse response) throws ServletException,
440: IOException {
441: doPost(request, response);
442: }
443:
444: private String sbjTrim(String str) {
445: if (str != null) {
446: if (str.length() > 40) {
447: str = str.substring(0, 40) + "...";
448: }
449: }
450: return str;
451: }
452: }
|