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.ArrayList;
011: import java.util.List;
012:
013: import javax.activation.DataSource;
014: import javax.servlet.ServletException;
015: import javax.servlet.http.HttpServletRequest;
016: import javax.servlet.http.HttpServletResponse;
017:
018: import org.claros.commons.mail.models.Email;
019: import org.claros.commons.mail.models.EmailPart;
020: import org.claros.commons.utility.MD5;
021: import org.claros.intouch.common.services.BaseService;
022: import org.claros.intouch.common.utility.Constants;
023:
024: public class AttForwardArrangeService extends BaseService {
025:
026: /**
027: *
028: */
029: private static final long serialVersionUID = -6975510930334000147L;
030: private static String tmpDir = Constants.tmpDir;
031:
032: /**
033: * The doGet method of the servlet. <br>
034: *
035: * This method is called when a form has its tag value method equals to get.
036: *
037: * @param request the request send by the client to the server
038: * @param response the response send by the server to the client
039: * @throws ServletException if an error occurred
040: * @throws IOException if an error occurred
041: */
042: public void doGet(HttpServletRequest request,
043: HttpServletResponse response) throws ServletException,
044: IOException {
045:
046: response.setHeader("Expires", "-1");
047: response.setHeader("Pragma", "no-cache");
048: response.setHeader("Cache-control", "no-cache");
049: response.setHeader("Content-Type", "text/html; charset=utf-8");
050:
051: PrintWriter out = response.getWriter();
052:
053: request.getSession().setAttribute("attachments", null);
054: Email email = (Email) request.getSession()
055: .getAttribute("email");
056: String res = "";
057: if (email != null) {
058: List parts = email.getParts();
059: List newParts = new ArrayList();
060: if (parts != null) {
061: EmailPart part = null;
062: DataSource ds = null;
063: byte data[] = null;
064: for (int i = 0; i < parts.size(); i++) {
065: part = (EmailPart) parts.get(i);
066:
067: if (part.getShortname().indexOf("Body") >= 0) {
068: continue;
069: }
070:
071: String tmpName = MD5.getHashString(request
072: .getSession().getId()
073: + part.getFilename().toLowerCase());
074: File f = new File(tmpDir + "/" + tmpName);
075:
076: ds = part.getDataSource();
077: if (ds == null) {
078: if (part.getContent() instanceof ByteArrayOutputStream) {
079: ByteArrayOutputStream bos = (ByteArrayOutputStream) part
080: .getContent();
081: data = bos.toByteArray();
082: bos.close();
083: } else if (part.getContent() instanceof ByteArrayInputStream) {
084: ByteArrayInputStream bis = (ByteArrayInputStream) part
085: .getContent();
086: ByteArrayOutputStream bos = new ByteArrayOutputStream();
087: int j = -1;
088: while ((j = bis.read()) != -1) {
089: bos.write(j);
090: }
091: data = bos.toByteArray();
092: bos.close();
093: bis.close();
094: } else if (part.getContent() instanceof String) {
095: data = ((String) part.getContent())
096: .getBytes();
097: }
098: }
099: if (data != null) {
100: BufferedOutputStream bos = new BufferedOutputStream(
101: new FileOutputStream(f));
102: bos.write(data);
103: bos.close();
104:
105: part.setDisposition(tmpDir + "/" + tmpName);
106:
107: // get the file name
108: String fn = part.getFilename();
109: int pos = fn.lastIndexOf('\\');
110: if (pos >= 0) {
111: fn = fn.substring(pos + 1);
112: }
113: pos = fn.lastIndexOf('/');
114: if (pos >= 0) {
115: fn = fn.substring(pos + 1);
116: }
117: fn = fn.toLowerCase();
118:
119: res += "<li size=\""
120: + part.getSize()
121: + "\"><img src=\"images/attachment.gif\"/><span>"
122: + part.getFilename()
123: + " ("
124: + part.getSizeReadable()
125: + ")</span> <a href=\"javascript:removeAttach('"
126: + fn + "')\" attid=\"" + i
127: + "\" style='color:#5A799E;'>"
128: + getText(request, "remove")
129: + "</a></li>";
130: }
131:
132: newParts.add(part);
133: }
134: }
135: request.getSession().setAttribute("attachments", newParts);
136: }
137: out.print(res);
138: }
139: }
|