001: package org.claros.intouch.webmail.services;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.IOException;
006: import java.io.PrintWriter;
007: import java.net.URLDecoder;
008: import java.util.ArrayList;
009:
010: import javax.servlet.ServletException;
011: import javax.servlet.ServletOutputStream;
012: import javax.servlet.http.HttpServletRequest;
013: import javax.servlet.http.HttpServletResponse;
014:
015: import org.claros.commons.auth.models.AuthProfile;
016: import org.claros.commons.mail.models.ConnectionMetaHandler;
017: import org.claros.commons.mail.models.ConnectionProfile;
018: import org.claros.commons.mail.models.Email;
019: import org.claros.commons.mail.models.EmailPart;
020: import org.claros.commons.mail.parser.HTMLMessageParser;
021: import org.claros.commons.utility.Utility;
022: import org.claros.intouch.common.services.BaseService;
023: import org.claros.intouch.webmail.controllers.MailController;
024: import org.claros.intouch.webmail.factory.MailControllerFactory;
025:
026: import org.htmlcleaner.HtmlCleaner;
027:
028: public class DumpPartService extends BaseService {
029:
030: /**
031: *
032: */
033: private static final long serialVersionUID = -3929161932612674112L;
034:
035: /**
036: * The doGet method of the servlet. <br>
037: *
038: * This method is called when a form has its tag value method equals to get.
039: *
040: * @param request the request send by the client to the server
041: * @param response the response send by the server to the client
042: * @throws ServletException if an error occurred
043: * @throws IOException if an error occurred
044: */
045: public void doGet(HttpServletRequest request,
046: HttpServletResponse response) throws ServletException,
047: IOException {
048:
049: int partId = 0;
050: boolean download = false;
051: try {
052: partId = Integer.parseInt((String) request
053: .getParameter("partid"));
054: } catch (Exception e) {
055: }
056:
057: try {
058: String dl = request.getParameter("dl");
059: if (dl != null && dl.equals("true")) {
060: download = true;
061: }
062: } catch (Exception e) {
063: }
064:
065: boolean modifyOutput = true;
066: try {
067: String strModify = request.getParameter("modify");
068: if (strModify != null && strModify.equals("false")) {
069: modifyOutput = false;
070: }
071: } catch (Exception e) {
072: }
073:
074: Email email = (Email) request.getSession()
075: .getAttribute("email");
076:
077: try {
078: // if mail is not in session try to fetch from parameters
079: if (email == null) {
080: String msgId = request.getParameter("msgId");
081: String folder = URLDecoder.decode(request
082: .getParameter("folder"), "UTF-8");
083:
084: AuthProfile auth = getAuthProfile(request);
085: ConnectionMetaHandler handler = (ConnectionMetaHandler) request
086: .getSession().getAttribute("handler");
087: ConnectionProfile profile = (ConnectionProfile) request
088: .getSession().getAttribute("profile");
089:
090: MailControllerFactory factory = new MailControllerFactory(
091: auth, profile, handler, folder);
092: MailController mailCont = factory.getMailController();
093: email = mailCont.getEmailById(new Long(msgId));
094: }
095:
096: if (partId == -1) {
097: String format = "html";
098: if (format == null || format.equals("html")) {
099: partId = findHtmlBody(email.getParts());
100: }
101: if (partId == -1) {
102: partId = findTextBody(email.getParts());
103: }
104: }
105:
106: if (partId != -1) {
107: EmailPart part = (EmailPart) email.getParts().get(
108: partId);
109: response.setContentType(part.getContentType());
110: response.setHeader("Pragma", "public");
111: response.setHeader("Cache-Control", "must-revalidate");
112: response.setDateHeader("Expires", 0);
113:
114: String fn = part.getFilename();
115: if (fn != null) {
116: if (fn.equals("Text Body")) {
117: fn = Utility
118: .replaceAllOccurances(email
119: .getBaseHeader().getSubject(),
120: " ", "_")
121: + ".txt";
122: } else if (fn.equals("Html Body")) {
123: fn = Utility
124: .replaceAllOccurances(email
125: .getBaseHeader().getSubject(),
126: " ", "_")
127: + ".html";
128: }
129: }
130:
131: if (download) {
132: response.setHeader("Content-disposition",
133: "attachment; filename=\"" + fn + "\"");
134: } else {
135: response.setHeader("Content-disposition",
136: "inline; filename=\"" + fn + "\"");
137: }
138:
139: if (part.getContentType().toLowerCase().startsWith(
140: "text/plain")
141: || part.isPlainText()) {
142: PrintWriter out = response.getWriter();
143: String content = "";
144: Object obj = part.getContent();
145: if (null != obj)
146: content = obj.toString();
147: if (!download) {
148: response.setHeader("Content-Type", "text/html");
149: HtmlCleaner cleaner = new HtmlCleaner(content);
150: cleaner.setOmitXmlDeclaration(true);
151: cleaner.setOmitXmlnsAttributes(true);
152: cleaner.setUseCdataForScriptAndStyle(false);
153: if (modifyOutput) {
154: cleaner.clean(true, true);
155: } else {
156: cleaner.clean(false, true);
157: }
158: content = cleaner.getXmlAsString();
159: } else {
160: response.setContentType(part.getContentType());
161: }
162: out.print(content);
163: } else if (part.getContentType().toLowerCase()
164: .startsWith("text/html")
165: || part.isHTMLText()) {
166: PrintWriter out = response.getWriter();
167: String content = "";
168: Object obj = part.getContent();
169: if (null != obj)
170: content = obj.toString();
171: if (!download) {
172: response.setHeader("Content-Type", "text/html");
173: HtmlCleaner cleaner = new HtmlCleaner(content);
174: cleaner.setOmitXmlDeclaration(true);
175: cleaner.setOmitXmlnsAttributes(true);
176: cleaner.setUseCdataForScriptAndStyle(false);
177: cleaner.clean(false, true);
178: content = cleaner.getCompactXmlAsString();
179: if (modifyOutput) {
180: content = HTMLMessageParser
181: .prepareInlineHTMLContent(email,
182: content);
183: }
184: } else {
185: response.setContentType(part.getContentType());
186: }
187: out.write(content);
188: } else {
189: String tmpContType = (part.getContentType() == null) ? "application/octet-stream"
190: : part.getContentType();
191: int pos = tmpContType.indexOf(";");
192: if (pos >= 0) {
193: tmpContType = tmpContType.substring(0, pos);
194: }
195: response.setContentType(tmpContType);
196:
197: Object obj = part.getContent();
198: if (obj instanceof ByteArrayOutputStream) {
199: ServletOutputStream sos = response
200: .getOutputStream();
201: ByteArrayOutputStream baos = (ByteArrayOutputStream) obj;
202: byte[] b = baos.toByteArray();
203: sos.write(b);
204: sos.close();
205: } else if (obj instanceof String) {
206: PrintWriter out = response.getWriter();
207: String content = part.getContent().toString();
208: out.write(content);
209: } else if (obj instanceof ByteArrayInputStream) {
210: ServletOutputStream sos = response
211: .getOutputStream();
212: ByteArrayInputStream bais = (ByteArrayInputStream) obj;
213: int i = -1;
214: int len = 0;
215: while ((i = bais.read()) != -1) {
216: sos.write(i);
217: len++;
218: }
219: sos.close();
220: bais.close();
221: }
222: }
223: } else {
224: PrintWriter out = response.getWriter();
225: out.print("<!-- No body part -->.");
226: }
227: } catch (Exception e) {
228: e.printStackTrace();
229: }
230: }
231:
232: /**
233: * The doPost method of the servlet. <br>
234: *
235: * This method is called when a form has its tag value method equals to post.
236: *
237: * @param request the request send by the client to the server
238: * @param response the response send by the server to the client
239: * @throws ServletException if an error occurred
240: * @throws IOException if an error occurred
241: */
242: public void doPost(HttpServletRequest request,
243: HttpServletResponse response) throws ServletException,
244: IOException {
245: doGet(request, response);
246: }
247:
248: /**
249: * @param list
250: * @return
251: */
252: private int findHtmlBody(ArrayList parts) {
253: for (int i = 0; i < parts.size(); i++) {
254: EmailPart body = (EmailPart) parts.get(i);
255: String cType = body.getContentType();
256: if (cType.toLowerCase().startsWith("text/html")) {
257: return i;
258: }
259: }
260: return -1;
261: }
262:
263: private int findTextBody(ArrayList parts) {
264: for (int i = 0; i < parts.size(); i++) {
265: EmailPart body = (EmailPart) parts.get(i);
266: String cType = body.getContentType();
267: if (cType.toLowerCase().startsWith("text/plain")) {
268: return i;
269: }
270: }
271: return -1;
272: }
273:
274: }
|