001: package org.claros.intouch.webmail.services;
002:
003: import java.io.BufferedInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.File;
006: import java.io.FileInputStream;
007: import java.io.IOException;
008: import java.io.ObjectOutputStream;
009: import java.io.PrintWriter;
010: import java.util.ArrayList;
011: import java.util.Date;
012: import java.util.HashMap;
013: import java.util.List;
014: import java.util.Locale;
015:
016: import javax.activation.DataHandler;
017: import javax.activation.DataSource;
018: import javax.mail.Address;
019: import javax.mail.internet.MimeBodyPart;
020: import javax.mail.internet.MimeMessage;
021: import javax.servlet.ServletException;
022: import javax.servlet.http.HttpServletRequest;
023: import javax.servlet.http.HttpServletResponse;
024:
025: import org.claros.commons.auth.models.AuthProfile;
026: import org.claros.commons.mail.models.ByteArrayDataSource;
027: import org.claros.commons.mail.models.ConnectionMetaHandler;
028: import org.claros.commons.mail.models.ConnectionProfile;
029: import org.claros.commons.mail.models.Email;
030: import org.claros.commons.mail.models.EmailHeader;
031: import org.claros.commons.mail.models.EmailPart;
032: import org.claros.commons.mail.protocols.Smtp;
033: import org.claros.commons.mail.utility.Utility;
034: import org.claros.commons.utility.MD5;
035: import org.claros.intouch.common.services.BaseService;
036: import org.claros.intouch.common.utility.Constants;
037: import org.claros.intouch.webmail.controllers.FolderController;
038: import org.claros.intouch.webmail.controllers.MailController;
039: import org.claros.intouch.webmail.factory.FolderControllerFactory;
040: import org.claros.intouch.webmail.factory.MailControllerFactory;
041: import org.claros.intouch.webmail.models.MsgDbObject;
042: import org.claros.intouch.webmail.models.FolderDbObject;
043:
044: public class SaveDraftService extends BaseService {
045:
046: /**
047: *
048: */
049: private static final long serialVersionUID = 8437322711523600816L;
050:
051: /**
052: * The doPost method of the servlet. <br>
053: *
054: * This method is called when a form has its tag value method equals to post.
055: *
056: * @param request the request send by the client to the server
057: * @param response the response send by the server to the client
058: * @throws ServletException if an error occurred
059: * @throws IOException if an error occurred
060: */
061: public void doPost(HttpServletRequest request,
062: HttpServletResponse response) throws ServletException,
063: IOException {
064:
065: response.setHeader("Expires", "-1");
066: response.setHeader("Pragma", "no-cache");
067: response.setHeader("Cache-control", "no-cache");
068: response.setHeader("Content-Type", "text/html; charset=utf-8");
069:
070: PrintWriter out = response.getWriter();
071:
072: try {
073: String charset = Constants.charset;
074: /*
075: String from = new String(request.getParameter("from").getBytes(charset), "utf-8");
076: String to = new String(request.getParameter("to").getBytes(charset), "utf-8");
077: String cc = new String(request.getParameter("cc").getBytes(charset), "utf-8");
078: String bcc = new String(request.getParameter("bcc").getBytes(charset), "utf-8");
079: String subject = new String(request.getParameter("subject").getBytes(charset), "utf-8");
080: String body= new String(request.getParameter("body").getBytes(charset), "utf-8");
081: */
082: String from = request.getParameter("from");
083: String to = request.getParameter("to");
084: String cc = request.getParameter("cc");
085: String bcc = request.getParameter("bcc");
086: String subject = request.getParameter("subject");
087: String body = request.getParameter("body");
088:
089: // learn the global charset setting.
090:
091: // learn user preferences from the DB.
092: AuthProfile auth = getAuthProfile(request);
093:
094: // now create a new email object.
095: Email email = new Email();
096: EmailHeader header = new EmailHeader();
097:
098: Address adrs[] = Utility.stringToAddressArray(from);
099: header.setFrom(adrs);
100:
101: header.setTo(Utility.stringToAddressArray(to));
102: if (cc != null && cc.trim() != "") {
103: header.setCc(Utility.stringToAddressArray(cc));
104: }
105: if (bcc != null && bcc.trim() != "") {
106: header.setBcc(Utility.stringToAddressArray(bcc));
107: }
108: header.setSubject(subject);
109: header.setDate(new Date());
110:
111: email.setBaseHeader(header);
112:
113: ArrayList parts = new ArrayList();
114: EmailPart bodyPart = new EmailPart();
115: bodyPart.setContentType("text/html; charset=" + charset);
116: bodyPart.setContent(body);
117: parts.add(0, bodyPart);
118:
119: // attach some files...
120: ArrayList attachments = (ArrayList) request.getSession()
121: .getAttribute("attachments");
122: if (attachments != null) {
123: List newLst = new ArrayList();
124: EmailPart tmp = null;
125: for (int i = 0; i < attachments.size(); i++) {
126: try {
127: tmp = (EmailPart) attachments.get(i);
128: String disp = tmp.getDisposition();
129: File f = new File(disp);
130: BufferedInputStream bis = new BufferedInputStream(
131: new FileInputStream(f));
132: byte data[] = new byte[(int) f.length()];
133: bis.read(data);
134:
135: MimeBodyPart bp = new MimeBodyPart();
136: DataSource ds = new ByteArrayDataSource(data,
137: tmp.getContentType(), tmp.getFilename());
138: bp.setDataHandler(new DataHandler(ds));
139: bp.setDisposition("attachment; filename=\""
140: + tmp.getFilename() + "\"");
141: tmp.setDisposition(bp.getDisposition());
142: bp.setFileName(tmp.getFilename());
143: tmp.setDataSource(ds);
144: tmp.setContent(bp.getContent());
145: newLst.add(tmp);
146:
147: bis.close();
148: } catch (Exception e) {
149: e.printStackTrace();
150: }
151: }
152: parts.addAll(newLst);
153: }
154: email.setParts(parts);
155:
156: // it is time to send the email object message
157: Smtp smtp = new Smtp(getConnectionProfile(request),
158: getAuthProfile(request));
159: HashMap sendRes = smtp.send(email, true);
160: MimeMessage msg = (MimeMessage) sendRes.get("msg");
161:
162: saveDraft(auth, msg, request);
163: out.print("ok");
164: } catch (Exception e) {
165: out.print("fail");
166: }
167: }
168:
169: /**
170: *
171: * @param auth
172: * @param msg
173: * @param request
174: * @throws Exception
175: */
176: private void saveDraft(AuthProfile auth, MimeMessage msg,
177: HttpServletRequest request) throws Exception {
178: ByteArrayOutputStream bos = new ByteArrayOutputStream();
179: msg.writeTo(bos);
180: byte bMsg[] = bos.toByteArray();
181:
182: // serialize the message byte array
183: ObjectOutputStream os = new ObjectOutputStream(bos);
184: os.writeObject(bMsg);
185:
186: // create an email db item
187: MsgDbObject item = new MsgDbObject();
188: item.setEmail(bMsg);
189: String md5Header = new String(MD5.getHashString(bMsg))
190: .toUpperCase(new Locale("en", "US"));
191:
192: ConnectionMetaHandler handler = getConnectionHandler(request);
193: ConnectionProfile profile = getConnectionProfile(request);
194:
195: FolderControllerFactory factory = new FolderControllerFactory(
196: auth, profile, handler);
197: FolderController foldCont = factory.getFolderController();
198: FolderDbObject fItem = foldCont.getDraftsFolder();
199:
200: item.setUniqueId(md5Header);
201: item.setFolderId(fItem.getId());
202: item.setUnread(new Boolean(false));
203: item.setUsername(auth.getUsername());
204: item.setMsgSize(new Long(bMsg.length));
205:
206: // save the email db item.
207: MailControllerFactory mailFact = new MailControllerFactory(
208: auth, profile, handler, fItem.getFolderName());
209: MailController mailCont = mailFact.getMailController();
210: mailCont.appendEmail(item);
211: }
212:
213: }
|