001: package org.claros.intouch.webmail.services;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005: import java.util.ArrayList;
006: import java.util.List;
007: import java.util.Locale;
008:
009: import javax.servlet.ServletException;
010: import javax.servlet.http.HttpServletRequest;
011: import javax.servlet.http.HttpServletResponse;
012:
013: import org.claros.commons.auth.models.AuthProfile;
014: import org.claros.commons.mail.models.ConnectionMetaHandler;
015: import org.claros.commons.mail.models.ConnectionProfile;
016: import org.claros.commons.mail.models.Email;
017: import org.claros.commons.mail.models.EmailPart;
018: import org.claros.commons.mail.utility.Constants;
019: import org.claros.intouch.common.services.BaseService;
020: import org.claros.intouch.webmail.controllers.IconController;
021: import org.claros.intouch.webmail.controllers.MailController;
022: import org.claros.intouch.webmail.factory.MailControllerFactory;
023: import org.claros.intouch.common.utility.Utility;
024:
025: public class FetchMailService extends BaseService {
026: private static Locale loc = new Locale("en", "US");
027: private static final long serialVersionUID = 2997611737645527623L;
028:
029: /**
030: * The doPost method of the servlet. <br>
031: *
032: * This method is called when a form has its tag value method equals to post.
033: *
034: * @param request the request send by the client to the server
035: * @param response the response send by the server to the client
036: * @throws ServletException if an error occurred
037: * @throws IOException if an error occurred
038: */
039: public void doPost(HttpServletRequest request,
040: HttpServletResponse response) throws ServletException,
041: IOException {
042:
043: response.setHeader("Expires", "-1");
044: response.setHeader("Pragma", "no-cache");
045: response.setHeader("Cache-control", "no-cache");
046: response.setHeader("Content-Type", "text/html; charset=utf-8");
047:
048: PrintWriter out = response.getWriter();
049:
050: String msgId = request.getParameter("msgId");
051: String folder = request.getParameter("folder");
052:
053: AuthProfile auth = getAuthProfile(request);
054: ConnectionMetaHandler handler = (ConnectionMetaHandler) request
055: .getSession().getAttribute("handler");
056: ConnectionProfile profile = (ConnectionProfile) request
057: .getSession().getAttribute("profile");
058:
059: MailControllerFactory factory = new MailControllerFactory(auth,
060: profile, handler, folder);
061: MailController mailCont = factory.getMailController();
062:
063: try {
064: Email email = mailCont.getEmailById(new Long(msgId));
065: request.getSession().setAttribute("email", email);
066:
067: String format = "html";
068: int i = -1;
069: if (format == null || format.equals("html")) {
070: i = findHtmlBody(email.getParts());
071: }
072: if (i == -1) {
073: i = findTextBody(email.getParts());
074: }
075:
076: String from = Utility
077: .htmlCheck(org.claros.commons.utility.Utility
078: .updateTRChars(email.getBaseHeader()
079: .getFromShown()));
080: String to = Utility
081: .htmlCheck(org.claros.commons.utility.Utility
082: .updateTRChars(email.getBaseHeader()
083: .getToShown()));
084: String cc = Utility
085: .htmlCheck(org.claros.commons.utility.Utility
086: .updateTRChars(email.getBaseHeader()
087: .getCcShown()));
088: String date = Utility.htmlCheck(email.getBaseHeader()
089: .getDateShown());
090: String subject = Utility
091: .htmlCheck(org.claros.commons.utility.Utility
092: .updateTRChars(email.getBaseHeader()
093: .getSubject()));
094: Boolean sendReceiptNotification = email.getBaseHeader()
095: .getRequestReceiptNotification();
096: String sendReceiptNotificationEmail = email.getBaseHeader()
097: .getReceiptNotificationEmail();
098: String notificationEmail = "";
099:
100: if (sendReceiptNotification != null
101: && sendReceiptNotification.booleanValue()
102: && sendReceiptNotificationEmail != null
103: && email.getBaseHeader().getUnread().booleanValue()) {
104: notificationEmail = org.claros.commons.utility.Utility
105: .convertTRCharsToHtmlSafe(sendReceiptNotificationEmail);
106: }
107:
108: if (profile.getProtocol().equals(Constants.POP3)) {
109: mailCont.markAsRead(new Long(msgId));
110: }
111:
112: if (from == null || from.equals("")) {
113: from = getText(request, "unknown.sender");
114: }
115: if (subject == null || subject.equals("")) {
116: subject = getText(request, "no.subject");
117: }
118:
119: out
120: .print("<div id='mailViewTitle' notificationEmail='"
121: + notificationEmail
122: + "' class='title' msgid='"
123: + msgId
124: + "' folder='"
125: + folder
126: + "'>"
127: + "<div class='buttons'>"
128: + "<table border='0' cellspacing='0' cellpadding='0'><tr><td nowrap='nowrap'>"
129: + " <div><a style='text-indent:0px;width:auto;overflow:visible;cursor:pointer;color:#5A799E;' href='javascript:;' onclick='toggleHeader()' id='headerChoose'>"
130: + getText(request, "show.more.headers")
131: + "</a> <img src='images/welcome-border.gif' height='10'/> </div>"
132: + "</td><td nowrap='nowrap'>"
133: + " <a href='javascript:;' class='toggle on' title='"
134: + getText(request, "toggle.list.on")
135: + "'>"
136: + getText(request, "toggle.list.on")
137: + "</a>"
138: + " <a href='javascript:;' class='toggle off' title='"
139: + getText(request, "toggle.list.off")
140: + "'>"
141: + getText(request, "toggle.list.off")
142: + "</a><br/>"
143: + "</td></tr></table>"
144: + "</div>");
145: out.print("<p id='subjectHeader'><span class='subtitle'>"
146: + getText(request, "subject")
147: + ":</span><span>"
148: + org.claros.commons.utility.Utility
149: .convertTRCharsToHtmlSafe(subject)
150: + "</span></p>"
151: + "<p id='fromHeader'><span class='subtitle'>"
152: + getText(request, "from")
153: + ":</span><span>"
154: + org.claros.commons.utility.Utility
155: .convertTRCharsToHtmlSafe(from)
156: + "</span></p>");
157:
158: if (date != null && !date.equals("")) {
159: out
160: .print("<p style='display:none' id='dateHeader'><span class='subtitle'>"
161: + getText(request, "date")
162: + ":</span><span>"
163: + date
164: + "</span></p>");
165: }
166: if (to != null && !to.equals("")) {
167: out
168: .print("<p style='display:none' id='toHeader'><span class='subtitle'>"
169: + getText(request, "to")
170: + ":</span><span>" + to + "</span></p>");
171: }
172: if (cc != null && !cc.equals("")) {
173: out
174: .print("<p style='display:none' id='ccHeader'><span class='subtitle'>"
175: + getText(request, "cc")
176: + ":</span><span>" + cc + "</span></p>");
177: }
178:
179: // parts begin
180: List parts = email.getParts();
181: if (parts != null) {
182: if (parts.size() > 1 || i == -1) {
183: out.println("<p style='float:left'>");
184: EmailPart tmp = null;
185: String mime = null;
186: for (int j = 0; j < parts.size(); j++) {
187: tmp = (EmailPart) parts.get(j);
188:
189: mime = tmp.getContentType();
190: mime = mime.toLowerCase(loc).trim();
191: if (mime.indexOf(";") > 0) {
192: mime = mime.substring(0, mime.indexOf(";"));
193: }
194: if (mime.indexOf(" ") > 0) {
195: mime = mime.substring(0, mime.indexOf(" "));
196: }
197: String fileName = org.claros.commons.utility.Utility
198: .updateTRChars(tmp.getFilename());
199: out
200: .println("<img src='images/attachment-small.gif'/> <a href=\"javascript:;\" onclick=\"Dom.get('msgTextIframe').src = 'webmail/dumpPart.service?dl=false&partid="
201: + j
202: + "';menuLayers.hide();\" onmouseout=\"menuLayers.hide()\" onmouseover=\"menuLayers.show('attachmenu"
203: + j
204: + "', event)\" style='color:#5A799E;font-weight:bold;'>"
205: + fileName + "</a> ");
206: out.print("<div id='attachmenu" + j
207: + "' class='menu'>");
208: out
209: .print("<img src='images/mime/"
210: + IconController
211: .findIconByMime(tmp
212: .getContentType())
213: + "' style='float:left;padding-bottom:20px;padding-right:8px;'/>");
214: out.print("<strong>"
215: + getText(request, "file.name")
216: + ":</strong>" + fileName + "<br/>");
217: out.print("<strong>"
218: + getText(request, "mime.type")
219: + ":</strong>" + mime + "<br/>");
220: out.print("<strong>" + getText(request, "size")
221: + ":</strong>" + tmp.getSizeReadable()
222: + "<br/><br/>");
223: out
224: .print("<table border='0' cellspacing='1' cellpadding='5'>"
225: + "<tr><td>"
226: + "<table height='23' border='0' cellspacing='0' cellpadding='0' width='50'>"
227: + " <tr>"
228: + " <td width='1%'><img src='images/button-left-bg.gif' width='9' height='23'/></td>"
229: + " <td nowrap='nowrap' style='background-image: url(images/button-bg.gif);text-align:center;padding-left:15px;padding-right:15px;cursor:pointer' width='98%' height='23' onclick=\"menuLayers.hide();Dom.get('msgTextIframe').src = 'webmail/dumpPart.service?dl=true&partid="
230: + j
231: + "';\"'>"
232: + getText(request, "save")
233: + "</td>"
234: + " <td width='1%'><img src='images/button-right-bg.gif' width='9' height='23'/></td>"
235: + " </tr>"
236: + "</table>"
237: + "</td>"
238: + "<td>"
239: + "<table height='23' border='0' cellspacing='0' cellpadding='0' width='50'>"
240: + " <tr>"
241: + " <td width='1%'><img src='images/button-left-bg.gif' width='9' height='23'/></td>"
242: + " <td nowrap='nowrap' style='background-image: url(images/button-bg.gif);text-align:center;padding-left:15px;padding-right:15px;cursor:pointer' width='98%' height='23' onclick=\"menuLayers.hide();Dom.get('msgTextIframe').src = 'webmail/dumpPart.service?dl=false&partid="
243: + j
244: + "';\">"
245: + getText(request, "open")
246: + "</td>"
247: + " <td width='1%'><img src='images/button-right-bg.gif' width='9' height='23'/></td>"
248: + " </tr>"
249: + "</table>"
250: + "</td>"
251: + "</tr>"
252: + "</table>");
253: out.print("</div>");
254: }
255: out.println("</p>");
256: }
257: }
258: // parts end
259:
260: out
261: .print("</div>"
262: + "<div id='msgText'>"
263: + "<iframe id='msgTextIframe' align='center' frameborder='0' height='250' width='100%' style='font-size: 11px;font: arial, sans-serif;' scrolling='auto' src='webmail/dumpPart.service?partid="
264: + i + "' width='100%' border='0'/>"
265: + "</div>");
266:
267: } catch (Exception e) {
268: e.printStackTrace();
269: }
270: }
271:
272: /**
273: * @param list
274: * @return
275: */
276: private int findHtmlBody(ArrayList parts) {
277: for (int i = 0; i < parts.size(); i++) {
278: EmailPart body = (EmailPart) parts.get(i);
279: String cType = body.getContentType();
280: if (cType.toLowerCase().startsWith("text/html")) {
281: return i;
282: }
283: }
284: return -1;
285: }
286:
287: private int findTextBody(ArrayList parts) {
288: for (int i = 0; i < parts.size(); i++) {
289: EmailPart body = (EmailPart) parts.get(i);
290: String cType = body.getContentType();
291: if (cType.toLowerCase().startsWith("text/plain")) {
292: return i;
293: }
294: }
295: return -1;
296: }
297:
298: }
|