001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.mail.action;
022:
023: import com.liferay.portal.kernel.util.Constants;
024: import com.liferay.portal.kernel.util.ParamUtil;
025: import com.liferay.portal.kernel.util.StringMaker;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portal.kernel.util.StringUtil;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.model.User;
030: import com.liferay.portal.struts.PortletAction;
031: import com.liferay.portal.theme.ThemeDisplay;
032: import com.liferay.portal.util.ContentTypeUtil;
033: import com.liferay.portal.util.DateFormats;
034: import com.liferay.portal.util.PortalUtil;
035: import com.liferay.portal.util.WebKeys;
036: import com.liferay.portlet.mail.RecipientException;
037: import com.liferay.portlet.mail.model.MailAttachment;
038: import com.liferay.portlet.mail.model.MailMessage;
039: import com.liferay.portlet.mail.model.RemoteMailAttachment;
040: import com.liferay.portlet.mail.util.MailUtil;
041: import com.liferay.portlet.mail.util.multiaccount.MailAccount;
042: import com.liferay.portlet.mail.util.multiaccount.MailAccounts;
043: import com.liferay.util.FileUtil;
044: import com.liferay.util.Html;
045: import com.liferay.util.mail.InternetAddressUtil;
046: import com.liferay.util.servlet.SessionErrors;
047: import com.liferay.util.servlet.UploadPortletRequest;
048:
049: import java.io.File;
050:
051: import java.text.DateFormat;
052:
053: import java.util.ArrayList;
054: import java.util.Enumeration;
055: import java.util.HashMap;
056: import java.util.Iterator;
057: import java.util.List;
058: import java.util.Map;
059:
060: import javax.mail.internet.InternetAddress;
061:
062: import javax.portlet.ActionRequest;
063: import javax.portlet.ActionResponse;
064: import javax.portlet.PortletConfig;
065: import javax.portlet.PortletPreferences;
066: import javax.portlet.PortletRequest;
067: import javax.portlet.RenderRequest;
068: import javax.portlet.RenderResponse;
069:
070: import javax.servlet.http.HttpServletRequest;
071:
072: import org.apache.struts.action.ActionForm;
073: import org.apache.struts.action.ActionForward;
074: import org.apache.struts.action.ActionMapping;
075:
076: /**
077: * <a href="EditMessageAction.java.html"><b><i>View Source</i></b></a>
078: *
079: * @author Ming-Gih Lam
080: * @author Alexander Chow
081: *
082: */
083: public class EditMessageAction extends PortletAction {
084:
085: public void processAction(ActionMapping mapping, ActionForm form,
086: PortletConfig config, ActionRequest req, ActionResponse res)
087: throws Exception {
088:
089: try {
090: completeMessage(req);
091:
092: sendRedirect(req, res);
093: } catch (Exception e) {
094: if (e instanceof RecipientException) {
095: SessionErrors.add(req, e.getClass().getName());
096: } else {
097: throw e;
098: }
099: }
100: }
101:
102: public ActionForward render(ActionMapping mapping, ActionForm form,
103: PortletConfig config, RenderRequest req, RenderResponse res)
104: throws Exception {
105:
106: HttpServletRequest httpReq = PortalUtil
107: .getHttpServletRequest(req);
108:
109: String cmd = ParamUtil.getString(req, Constants.CMD);
110:
111: PortletPreferences prefs = req.getPreferences();
112:
113: String folderId = ParamUtil.getString(req, "folderId");
114: long messageId = ParamUtil.getLong(req, "messageId");
115:
116: String signature = prefs
117: .getValue("signature", StringPool.BLANK);
118:
119: if (Validator.isNotNull(signature)) {
120: signature = "<br />--<br />" + signature;
121: }
122:
123: if (cmd.equals("forward") || cmd.startsWith("reply")) {
124: MailUtil.setFolder(httpReq, folderId);
125:
126: MailMessage mailMessage = MailUtil.getMessage(httpReq,
127: messageId);
128:
129: if (cmd.equals("forward")) {
130: req.setAttribute(WebKeys.MAIL_MESSAGE_SUBJECT, "Fw: "
131: + getSubject(mailMessage.getSubject(), "fw"));
132: req.setAttribute(WebKeys.MAIL_MESSAGE_ATTACHMENTS,
133: mailMessage.getRemoteAttachments());
134: } else {
135: String to = StringPool.BLANK;
136: String cc = StringPool.BLANK;
137:
138: if (cmd.equals("replyAll")) {
139: User user = PortalUtil.getUser(req);
140:
141: String emailAddress = user.getEmailAddress();
142:
143: to = InternetAddressUtil
144: .toString(InternetAddressUtil.removeEntry(
145: mailMessage.getTo(), emailAddress));
146:
147: cc = InternetAddressUtil
148: .toString(InternetAddressUtil.removeEntry(
149: mailMessage.getCc(), emailAddress));
150:
151: String replyTo = InternetAddressUtil
152: .toString(mailMessage.getReplyTo());
153:
154: if (Validator.isNull(replyTo)) {
155: InternetAddress from = (InternetAddress) mailMessage
156: .getFrom();
157:
158: replyTo = from.toUnicodeString();
159: }
160:
161: to = replyTo + StringPool.COMMA + StringPool.SPACE
162: + to;
163: } else {
164: to = InternetAddressUtil.toString(mailMessage
165: .getReplyTo());
166:
167: if (Validator.isNull(to)) {
168: InternetAddress from = (InternetAddress) mailMessage
169: .getFrom();
170:
171: to = from.toUnicodeString();
172: }
173: }
174:
175: String[] recipients = new String[] { Html.escape(to),
176: Html.escape(cc), StringPool.BLANK };
177:
178: req.setAttribute(WebKeys.MAIL_MESSAGE_ORIGINAL_ID,
179: String.valueOf(mailMessage.getMessageId()));
180: req.setAttribute(WebKeys.MAIL_MESSAGE_RECIPIENTS,
181: recipients);
182: req.setAttribute(WebKeys.MAIL_MESSAGE_IN_REPLY_TO,
183: String.valueOf(mailMessage.getInReplyTo()));
184: req.setAttribute(WebKeys.MAIL_MESSAGE_REFERENCES,
185: String.valueOf(mailMessage.getReferences()));
186: req.setAttribute(WebKeys.MAIL_MESSAGE_SUBJECT, "Re: "
187: + getSubject(mailMessage.getSubject(), "re"));
188: }
189:
190: req.setAttribute(WebKeys.MAIL_MESSAGE_BODY, signature
191: + getBody(req, mailMessage));
192: } else if (cmd.equals(Constants.EDIT)) {
193: MailUtil.setFolder(httpReq, folderId);
194:
195: MailMessage mailMessage = MailUtil.getMessage(httpReq,
196: messageId);
197:
198: String to = Html.escape(InternetAddressUtil
199: .toString(mailMessage.getTo()));
200: String cc = Html.escape(InternetAddressUtil
201: .toString(mailMessage.getCc()));
202: String bcc = Html.escape(InternetAddressUtil
203: .toString(mailMessage.getBcc()));
204:
205: String[] recipients = new String[] { to, cc, bcc };
206:
207: req.setAttribute(WebKeys.MAIL_MESSAGE_ORIGINAL_ID,
208: new String(_DRAFT_ID_PREFIX + messageId));
209: req.setAttribute(WebKeys.MAIL_MESSAGE_RECIPIENTS,
210: recipients);
211: req.setAttribute(WebKeys.MAIL_MESSAGE_SUBJECT, mailMessage
212: .getSubject());
213: req.setAttribute(WebKeys.MAIL_MESSAGE_BODY, mailMessage
214: .getBody());
215: req.setAttribute(WebKeys.MAIL_MESSAGE_ATTACHMENTS,
216: mailMessage.getRemoteAttachments());
217: } else if (cmd.equals(Constants.SEND)) {
218: String originalId = ParamUtil.getString(req, "originalId");
219:
220: String to = ParamUtil.getString(req, "to");
221: String cc = ParamUtil.getString(req, "cc");
222: String bcc = ParamUtil.getString(req, "bcc");
223:
224: String[] recipients = new String[] { to, cc, bcc };
225:
226: String subject = ParamUtil.getString(req, "subject");
227: String body = ParamUtil.getString(req, "body");
228:
229: req.setAttribute(WebKeys.MAIL_MESSAGE_ORIGINAL_ID,
230: originalId);
231: req.setAttribute(WebKeys.MAIL_MESSAGE_RECIPIENTS,
232: recipients);
233: req.setAttribute(WebKeys.MAIL_MESSAGE_SUBJECT, subject);
234: req.setAttribute(WebKeys.MAIL_MESSAGE_BODY, body);
235: req.setAttribute(WebKeys.MAIL_MESSAGE_ATTACHMENTS,
236: getRemoteAttachments(req));
237: } else {
238: String to = ParamUtil.getString(req, "to");
239:
240: String[] recipients = new String[] { to, StringPool.BLANK,
241: StringPool.BLANK };
242:
243: req.setAttribute(WebKeys.MAIL_MESSAGE_RECIPIENTS,
244: recipients);
245: req.setAttribute(WebKeys.MAIL_MESSAGE_BODY, signature);
246: }
247:
248: return mapping.findForward("portlet.mail.edit_message");
249: }
250:
251: protected void completeMessage(ActionRequest req) throws Exception {
252:
253: String cmd = ParamUtil.getString(req, Constants.CMD);
254:
255: User user = PortalUtil.getUser(req);
256:
257: String originalId = ParamUtil.getString(req, "originalId");
258:
259: boolean wasDraft = false;
260:
261: if (originalId.startsWith(_DRAFT_ID_PREFIX)) {
262: wasDraft = true;
263:
264: originalId = originalId
265: .substring(_DRAFT_ID_PREFIX.length());
266: }
267:
268: String to = ParamUtil.getString(req, "to");
269: String cc = ParamUtil.getString(req, "cc");
270: String bcc = ParamUtil.getString(req, "bcc");
271: String inReplyTo = ParamUtil.getString(req, "inReplyTo");
272: String references = ParamUtil.getString(req, "references");
273: String subject = ParamUtil.getString(req, "subject");
274: String body = ParamUtil.getString(req, "body");
275:
276: MailMessage mailMessage = new MailMessage();
277:
278: try {
279: MailAccount account = MailAccounts.getCurrentAccount(req);
280:
281: mailMessage.setFrom(new InternetAddress(account
282: .getEmailAddress(), user.getFullName()));
283: mailMessage.setTo(to);
284: mailMessage.setCc(cc);
285: mailMessage.setBcc(bcc);
286: mailMessage.setInReplyTo(inReplyTo);
287: mailMessage.setReferences(references);
288: } catch (Exception ex) {
289: throw new RecipientException(ex);
290: }
291:
292: mailMessage.setSubject(subject);
293: mailMessage.setBody(body);
294:
295: Iterator itr = getAttachments(req).entrySet().iterator();
296:
297: while (itr.hasNext()) {
298: Map.Entry entry = (Map.Entry) itr.next();
299:
300: String fileName = (String) entry.getKey();
301: byte[] attachment = (byte[]) entry.getValue();
302:
303: MailAttachment mailAttachment = new MailAttachment();
304:
305: mailAttachment.setFilename(fileName);
306: mailAttachment.setContent(attachment);
307: mailAttachment.setContentType(ContentTypeUtil
308: .getContentType(fileName));
309:
310: mailMessage.appendAttachment(mailAttachment);
311: }
312:
313: mailMessage.setRemoteAttachments(getRemoteAttachments(req));
314:
315: boolean send = cmd.equals(Constants.SEND);
316:
317: HttpServletRequest httpReq = PortalUtil
318: .getHttpServletRequest(req);
319:
320: MailUtil.completeMessage(httpReq, mailMessage, send,
321: originalId, wasDraft);
322: }
323:
324: protected Map getAttachments(ActionRequest req) throws Exception {
325: UploadPortletRequest uploadReq = PortalUtil
326: .getUploadPortletRequest(req);
327:
328: Map attachments = new HashMap();
329:
330: Enumeration enu = uploadReq.getParameterNames();
331:
332: while (enu.hasMoreElements()) {
333: String name = (String) enu.nextElement();
334:
335: if (name.startsWith("attachment")) {
336: File file = uploadReq.getFile(name);
337: String fileName = uploadReq.getFileName(name);
338: byte[] bytes = FileUtil.getBytes(file);
339:
340: if ((bytes != null) && (bytes.length > 0)) {
341: attachments.put(fileName, bytes);
342: }
343: }
344: }
345:
346: return attachments;
347: }
348:
349: protected String getBody(RenderRequest req, MailMessage mailMessage)
350: throws Exception {
351:
352: StringMaker sm = new StringMaker();
353:
354: InternetAddress from = (InternetAddress) mailMessage.getFrom();
355:
356: ThemeDisplay themeDisplay = (ThemeDisplay) req
357: .getAttribute(WebKeys.THEME_DISPLAY);
358:
359: DateFormat dateFormatDateTime = DateFormats.getDateTime(
360: themeDisplay.getLocale(), themeDisplay.getTimeZone());
361:
362: sm.append("<br /><br />");
363: sm.append("On "
364: + dateFormatDateTime.format(mailMessage.getSentDate()));
365: sm.append(StringPool.COMMA + StringPool.NBSP
366: + from.getPersonal());
367: sm
368: .append(" <<a href=\"mailto: " + from.getAddress()
369: + "\">");
370: sm.append(from.getAddress() + "</a>> wrote:<br />");
371: sm.append("<div style=\"");
372: sm.append("border-left: 1px solid rgb(204, 204, 204); ");
373: sm.append("margin: 0pt 0pt 0pt 1ex; ");
374: sm.append("padding-left: 1ex; \">");
375: sm.append(mailMessage.getBody());
376: sm.append("</div>");
377:
378: return sm.toString();
379: }
380:
381: protected List getRemoteAttachments(PortletRequest req)
382: throws Exception {
383:
384: List list = new ArrayList();
385:
386: String prefix = "remoteAttachment";
387:
388: Enumeration enu = req.getParameterNames();
389:
390: while (enu.hasMoreElements()) {
391: String name = (String) enu.nextElement();
392:
393: if (name.startsWith(prefix)) {
394: String fileName = name.substring(prefix.length());
395: String contentPath = ParamUtil.getString(req, name);
396:
397: RemoteMailAttachment remoteMailAttachment = new RemoteMailAttachment();
398:
399: remoteMailAttachment.setFilename(fileName);
400: remoteMailAttachment.setContentPath(contentPath);
401:
402: list.add(remoteMailAttachment);
403: }
404: }
405:
406: return list;
407: }
408:
409: protected String getSubject(String subject, String prefix)
410: throws Exception {
411:
412: if (Validator.isNotNull(subject)) {
413: while (StringUtil.startsWith(subject, prefix + ":")
414: || StringUtil.startsWith(subject, prefix + ">")) {
415:
416: subject = subject.substring(3).trim();
417: }
418: }
419:
420: return subject;
421: }
422:
423: private String _DRAFT_ID_PREFIX = "draft.";
424:
425: }
|