001: package org.claros.intouch.webmail.services;
002:
003: import java.io.BufferedOutputStream;
004: import java.io.ByteArrayInputStream;
005: import java.io.ByteArrayOutputStream;
006: import java.io.File;
007: import java.io.FileOutputStream;
008: import java.io.IOException;
009: import java.io.PrintWriter;
010: import java.util.List;
011:
012: import javax.activation.DataSource;
013: import javax.servlet.ServletException;
014: import javax.servlet.http.HttpServletRequest;
015: import javax.servlet.http.HttpServletResponse;
016:
017: import org.claros.commons.mail.models.Email;
018: import org.claros.commons.mail.models.EmailPart;
019: import org.claros.commons.utility.MD5;
020: import org.claros.intouch.common.services.BaseService;
021: import org.claros.intouch.common.utility.Constants;
022:
023: public class AttForwardArrangeService extends BaseService {
024:
025: /**
026: *
027: */
028: private static final long serialVersionUID = -6975510930334000147L;
029: private static String tmpDir = Constants.tmpDir;
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/html; charset=utf-8");
049:
050: PrintWriter out = response.getWriter();
051:
052: request.getSession().setAttribute("attachments", null);
053: Email email = (Email) request.getSession()
054: .getAttribute("email");
055: String res = "";
056: if (email != null) {
057: List parts = email.getParts();
058: if (parts != null) {
059: EmailPart part = null;
060: DataSource ds = null;
061: byte data[] = null;
062: for (int i = 0; i < parts.size(); i++) {
063: part = (EmailPart) parts.get(i);
064:
065: String tmpName = MD5.getHashString(request
066: .getSession().getId()
067: + part.getFilename().toLowerCase());
068: File f = new File(tmpDir + "/" + tmpName);
069:
070: ds = part.getDataSource();
071: if (ds == null) {
072: if (part.getContent() instanceof ByteArrayOutputStream) {
073: ByteArrayOutputStream bos = (ByteArrayOutputStream) part
074: .getContent();
075: data = bos.toByteArray();
076: bos.close();
077: } else if (part.getContent() instanceof ByteArrayInputStream) {
078: ByteArrayInputStream bis = (ByteArrayInputStream) part
079: .getContent();
080: ByteArrayOutputStream bos = new ByteArrayOutputStream();
081: int j = -1;
082: while ((j = bis.read()) != -1) {
083: bos.write(j);
084: }
085: data = bos.toByteArray();
086: bos.close();
087: bis.close();
088: } else if (part.getContent() instanceof String) {
089: data = ((String) part.getContent())
090: .getBytes();
091: }
092: }
093: if (data != null) {
094: BufferedOutputStream bos = new BufferedOutputStream(
095: new FileOutputStream(f));
096: bos.write(data);
097: bos.close();
098:
099: part.setDisposition(tmpDir + "/" + tmpName);
100:
101: // get the file name
102: String fn = part.getFilename();
103: int pos = fn.lastIndexOf('\\');
104: if (pos >= 0) {
105: fn = fn.substring(pos + 1);
106: }
107: pos = fn.lastIndexOf('/');
108: if (pos >= 0) {
109: fn = fn.substring(pos + 1);
110: }
111: fn = fn.toLowerCase();
112:
113: res += "<li size=\""
114: + part.getSize()
115: + "\"><img src=\"images/attachment.gif\"/><span>"
116: + part.getFilename()
117: + " ("
118: + part.getSizeReadable()
119: + ")</span> <a href=\"javascript:removeAttach('"
120: + fn + "')\" attid=\"" + i
121: + "\" style='color:#5A799E;'>"
122: + getText(request, "remove")
123: + "</a></li>";
124: }
125: }
126: }
127: request.getSession().setAttribute("attachments", parts);
128: }
129: out.print(res);
130: }
131: }
|