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.model;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024:
025: import java.util.ArrayList;
026: import java.util.Date;
027: import java.util.List;
028: import java.util.regex.Pattern;
029:
030: import javax.mail.Address;
031: import javax.mail.internet.AddressException;
032: import javax.mail.internet.InternetAddress;
033:
034: /**
035: * <a href="MailMessage.java.html"><b><i>View Source</i></b></a>
036: *
037: * @author Alexander Chow
038: *
039: */
040: public class MailMessage {
041:
042: public long getMessageId() {
043: return _messageId;
044: }
045:
046: public void setMessageId(long messageId) {
047: _messageId = messageId;
048: }
049:
050: public Address getFrom() {
051: return _from;
052: }
053:
054: public void setFrom(Address from) {
055: _from = from;
056: }
057:
058: public Address[] getTo() {
059: return _to;
060: }
061:
062: public void setTo(String to) throws AddressException {
063: _to = InternetAddress.parse(to);
064: }
065:
066: public void setTo(Address[] to) {
067: _to = to;
068: }
069:
070: public Address[] getCc() {
071: return _cc;
072: }
073:
074: public void setCc(Address[] cc) {
075: _cc = cc;
076: }
077:
078: public void setCc(String ccs) throws AddressException {
079: _cc = InternetAddress.parse(ccs);
080: }
081:
082: public Address[] getBcc() {
083: return _bcc;
084: }
085:
086: public void setBcc(Address[] bcc) {
087: _bcc = bcc;
088: }
089:
090: public void setBcc(String bccs) throws AddressException {
091: _bcc = InternetAddress.parse(bccs);
092: }
093:
094: public String getInReplyTo() {
095: return _inReplyTo;
096: }
097:
098: public void setInReplyTo(String inReplyTo) {
099: _inReplyTo = inReplyTo;
100: }
101:
102: public Address[] getReplyTo() {
103: return _replyTo;
104: }
105:
106: public void setReplyTo(String replyTos) throws AddressException {
107: _replyTo = InternetAddress.parse(replyTos);
108: }
109:
110: public void setReplyTo(Address[] replyTo) {
111: _replyTo = replyTo;
112: }
113:
114: public String getReferences() {
115: return _references;
116: }
117:
118: public void setReferences(String references) {
119: _references = references;
120: }
121:
122: public String getSubject() {
123: return GetterUtil.getString(_subject);
124: }
125:
126: public void setSubject(String subject) {
127: _subject = subject;
128: }
129:
130: public Date getSentDate() {
131: return _sentDate;
132: }
133:
134: public void setSentDate(Date sentDate) {
135: _sentDate = sentDate;
136: }
137:
138: public String getBody() {
139: return getBody(false, false);
140: }
141:
142: public String getBody(boolean replaceLinks, boolean popup) {
143: String body = _content.toString();
144:
145: if (replaceLinks) {
146: for (int i = 0; i < _LINK_REGEXP.length; i++) {
147: body = Pattern.compile(_LINK_REGEXP[i],
148: Pattern.CASE_INSENSITIVE).matcher(body)
149: .replaceAll(_LINK_REPLACEMENT[i]);
150: }
151:
152: String mailtoReplacement = "<a href=\"javascript: ";
153:
154: if (popup) {
155: mailtoReplacement += "opener.compose('$2')";
156: } else {
157: mailtoReplacement += "parent.compose('$2')";
158: }
159:
160: body = Pattern.compile(_MAILTO_REGEXP,
161: Pattern.CASE_INSENSITIVE).matcher(body).replaceAll(
162: mailtoReplacement);
163: }
164:
165: return body;
166: }
167:
168: public void setBody(String body) {
169: MailContent content = new MailContent(body);
170:
171: _content = content;
172: }
173:
174: public MailContent getMailContent() {
175: return _content;
176: }
177:
178: public void setMailContent(MailContent mc) {
179: _content = mc;
180: }
181:
182: public List getAttachments() {
183: return _attachments;
184: }
185:
186: public void appendAttachment(MailAttachment ma) {
187: _attachments.add(ma);
188: }
189:
190: public List getRemoteAttachments() {
191: return _remoteAttachments;
192: }
193:
194: public void setRemoteAttachments(List remoteAttachments) {
195: _remoteAttachments = remoteAttachments;
196: }
197:
198: public void appendRemoteAttachment(RemoteMailAttachment rma) {
199: _remoteAttachments.add(rma);
200: }
201:
202: public void purgeDirtyRemoteAttachments() {
203: for (int i = 0; i < _remoteAttachments.size(); i++) {
204: RemoteMailAttachment rma = (RemoteMailAttachment) _remoteAttachments
205: .get(i);
206:
207: if (rma.isDirty()) {
208: _remoteAttachments.remove(i);
209:
210: i--;
211: }
212: }
213: }
214:
215: private static final String[] _LINK_REGEXP = {
216: "([^]_a-z0-9-=\"'/])"
217: + "((https?|ftp|gopher|news|telnet)://|www\\.)"
218: + "([^\\s\\(\\)\\*\\^\\$!`\"'\\|\\[\\]\\{\\};<>\\.]*)"
219: + "((\\.[^\\s\\(\\)\\*\\^\\$!`\"'\\|\\[\\]\\{\\};<>\\.]+)*)",
220: "<a\\s+href=\"www\\." };
221:
222: private static String[] _LINK_REPLACEMENT = {
223: "$1<a href=\"$2$4$5\" target=\"_blank\">$2$4$5</a>",
224: "<a href=\"http://www." };
225:
226: private static String _MAILTO_REGEXP = "(<a\\s+href=\"mailto:\\s*)([\\w.-_]*@[\\w.-_]*)";
227:
228: private long _messageId;
229: private Address _from;
230: private Address[] _to;
231: private Address[] _cc;
232: private Address[] _bcc;
233: private String _inReplyTo;
234: private Address[] _replyTo;
235: private String _references;
236: private String _subject;
237: private Date _sentDate;
238: private MailContent _content;
239: private List _attachments = new ArrayList();
240: private List _remoteAttachments = new ArrayList();
241:
242: }
|