001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.plugin.registration;
031:
032: import org.apache.commons.mail.Email;
033: import org.apache.commons.mail.EmailException;
034: import org.blojsom.blog.Blog;
035: import org.blojsom.blog.User;
036: import org.blojsom.plugin.notification.AbstractVelocityEmailNotification;
037: import org.blojsom.plugin.notification.AbstractNotification;
038:
039: import javax.mail.Address;
040: import javax.mail.MessagingException;
041: import java.net.URL;
042: import java.util.*;
043:
044: /**
045: * RegistrationNotification
046: *
047: * @author Eric Broyles
048: * @version $Id: RegistrationNotification.java,v 1.2 2007/01/17 02:35:05 czarneckid Exp $
049: */
050: public class RegistrationNotification extends
051: AbstractVelocityEmailNotification {
052:
053: private Email email;
054:
055: /**
056: * Create a new registration notification
057: *
058: * @param email E-mail message
059: * @param emailTemplate URL for template
060: * @param user {@link User}
061: * @param blog {@link Blog}
062: * @throws MessagingException If there is an error setting the e-mail content
063: */
064: public RegistrationNotification(Email email, URL emailTemplate,
065: User user, Blog blog) throws MessagingException {
066: super (email, emailTemplate);
067: setEmail(email);
068: if (!"".equals(user.getUserName()))
069: put("name", user.getUserName());
070: else
071: put("name", user.getUserLogin());
072: put("subject", email.getSubject());
073: put("blogName", blog.getBlogName());
074: put("username", user.getUserLogin());
075: put("password", user.getUserPassword());
076: put("blogUrl", blog.getBlogURL());
077: put("privacyUrl", blog.getProperty("privacy-policy-url",
078: (new StringBuffer()).append(blog.getBlogURL()).append(
079: "/privacy.html").toString(), false));
080: put("blogOwner", blog.getBlogOwner());
081: put("activationUrl", (new StringBuffer()).append(
082: blog.getBlogURL()).append(
083: "?plugins=registration&action=activate&username=")
084: .append(user.getUserLogin()).toString());
085: try {
086: email.setContent(getMessage(), "text/html");
087: } catch (Exception e) {
088: throw new MessagingException(
089: "Failed to set the content of the message. ", e);
090: }
091: }
092:
093: /**
094: * @see AbstractNotification#getBlindCarbonCopyRecipients()
095: */
096: protected List getBlindCarbonCopyRecipients() {
097: try {
098: return convertElementsToString(getEmail()
099: .getMimeMessage()
100: .getRecipients(
101: javax.mail.internet.MimeMessage.RecipientType.BCC));
102: } catch (MessagingException e) {
103: e.printStackTrace();
104: }
105: return Collections.EMPTY_LIST;
106: }
107:
108: /**
109: * @see AbstractNotification#getBlindCarbonCopyRecipients()
110: */
111: protected List getCarbonCopyRecipients() {
112: try {
113: return convertElementsToString(getEmail()
114: .getMimeMessage()
115: .getRecipients(
116: javax.mail.internet.MimeMessage.RecipientType.CC));
117: } catch (MessagingException e) {
118: e.printStackTrace();
119: }
120: return Collections.EMPTY_LIST;
121: }
122:
123: /**
124: * @see AbstractNotification#getRecipients()
125: */
126: protected List getRecipients() {
127: try {
128: return convertElementsToString(getEmail()
129: .getMimeMessage()
130: .getRecipients(
131: javax.mail.internet.MimeMessage.RecipientType.TO));
132: } catch (MessagingException e) {
133: e.printStackTrace();
134: }
135: return Collections.EMPTY_LIST;
136: }
137:
138: /**
139: * @see AbstractNotification#getSender()
140: */
141: protected String getSender() {
142: try {
143: return getEmail().getFromAddress().getAddress();
144: } catch (NullPointerException e) {
145: e.printStackTrace();
146: }
147: return null;
148: }
149:
150: /**
151: * @see AbstractNotification#getSubject()
152: */
153: protected String getSubject() {
154: return getEmail().getSubject();
155: }
156:
157: /**
158: * Retrieve the e-mail message
159: *
160: * @return E-mail message
161: */
162: public Email getEmail() {
163: return email;
164: }
165:
166: /**
167: * Set the e-mail message
168: *
169: * @param email E-mail message
170: */
171: public void setEmail(Email email) {
172: this .email = email;
173: }
174:
175: /**
176: * Send the e-mail
177: *
178: * @throws EmailException If there is an error sending the e-mail
179: */
180: public void send() throws EmailException {
181: getEmail().send();
182: }
183:
184: /**
185: * Convert a list of addresses into the list
186: *
187: * @param addresses Array of addresses
188: * @return List of addresses
189: */
190: protected List convertElementsToString(Address addresses[]) {
191: if (addresses != null) {
192: List addressList = Arrays.asList(addresses);
193: List stringList = new ArrayList(addressList.size());
194: Address address;
195: for (Iterator iter = addressList.iterator(); iter.hasNext(); stringList
196: .add(address.toString()))
197: address = (Address) iter.next();
198:
199: return stringList;
200: } else {
201: return Collections.EMPTY_LIST;
202: }
203: }
204: }
|