File Source: abstractemailnotificationlistener.java
/*
P/P * Method: net.sourceforge.pebble.event.comment.AbstractEmailNotificationListener__static_init
*/
1 /*
2 * Copyright (c) 2003-2006, Simon Brown
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * - Neither the name of Pebble nor the names of its contributors may
17 * be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32 package net.sourceforge.pebble.event.comment;
33
34 import net.sourceforge.pebble.domain.Blog;
35 import net.sourceforge.pebble.domain.Comment;
36 import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
37 import net.sourceforge.pebble.api.event.comment.CommentEvent;
38 import net.sourceforge.pebble.util.MailUtils;
39 import net.sourceforge.pebble.util.StringUtils;
40
41 import javax.mail.Session;
42 import java.text.SimpleDateFormat;
43 import java.util.Collection;
44 import java.util.Iterator;
45
46 /**
47 * Base class for listeners that send an e-mail notification when new
48 * comments are added.
49 *
50 * @author Simon Brown
51 */
/*
P/P * Method: void net.sourceforge.pebble.event.comment.AbstractEmailNotificationListener()
*/
52 public abstract class AbstractEmailNotificationListener extends CommentListenerSupport {
53
54 /** a token to be replaced when sending e-mails */
55 private static final String EMAIL_ADDRESS_TOKEN = "EMAIL_ADDRESS";
56
57 /**
58 * Called when a comment has been added.
59 *
60 * @param event a CommentEvent instance
61 */
62 public void commentAdded(CommentEvent event) {
/*
P/P * Method: void commentAdded(CommentEvent)
*
* Preconditions:
* event != null
* (soft) net.sourceforge.pebble.domain.State__static_init.new State(State__static_init#1).name != null
* (soft) net.sourceforge.pebble.domain.State__static_init.new State(State__static_init#3).name != null
* (soft) net/sourceforge/pebble/domain/BlogManager.instance != null
* (soft) init'ed(net/sourceforge/pebble/domain/BlogManager.instance.multiBlog)
*
* Presumptions:
* comment.blogEntry.blog.decoratorChain.decorators@64 != null
* comment.blogEntry.blog.decoratorChain@64 != null
* comment.blogEntry.blog.permalinkProvider@64 != null
* comment.blogEntry.blog.permalinkProvider@66 != null
* comment.blogEntry.blog.properties@64 != null
* ...
*/
63 Comment comment = event.getComment();
64 if (comment.isApproved()) {
65 sendNotification(comment);
66 } else if (comment.isPending()) {
67 sendApprovalRequest(comment);
68 }
69 }
70
71 /**
72 * Called when a comment has been approved.
73 *
74 * @param event a CommentEvent instance
75 */
76 public void commentApproved(CommentEvent event) {
/*
P/P * Method: void commentApproved(CommentEvent)
*
* Preconditions:
* event != null
* (soft) net/sourceforge/pebble/domain/BlogManager.instance != null
* (soft) init'ed(net/sourceforge/pebble/domain/BlogManager.instance.multiBlog)
*
* Presumptions:
* comment.blogEntry.blog.decoratorChain.decorators@77 != null
* comment.blogEntry.blog.decoratorChain@77 != null
* comment.blogEntry.blog.permalinkProvider@77 != null
* comment.blogEntry.blog.properties@77 != null
* comment.blogEntry.blog@77 != null
* ...
*/
77 Comment comment = event.getComment();
78 sendNotification(comment);
79 }
80
81 private void sendNotification(Comment comment) {
/*
P/P * Method: void sendNotification(Comment)
*
* Preconditions:
* comment != null
* comment.blogEntry != null
* comment.blogEntry.blog != null
* comment.blogEntry.blog.decoratorChain != null
* comment.blogEntry.blog.decoratorChain.decorators != null
* comment.blogEntry.blog.properties != null
* comment.blogEntry.comments != null
* init'ed(comment.blogEntry.id)
* init'ed(comment.blogEntry.title)
* init'ed(comment.author)
* ...
*
* Presumptions:
* java.util.Iterator:next(...)@112 != null
*
* Postconditions:
* init'ed(comment.author)
* init'ed(comment.blogEntry.permalink)
* possibly_updated(comment.blogEntry.user)
* init'ed(comment.body)
* possibly_updated(comment.email)
* init'ed(comment.title)
* init'ed(comment.website)
*
* Test Vectors:
* blogComment.email@112: Addr_Set{null}, Inverse{null}
* java.lang.String:length(...)@113: {0}, {1..232-1}
* java.util.Iterator:hasNext(...)@111: {1}, {0}
*/
82 Blog blog = comment.getBlogEntry().getBlog();
83
84 ContentDecoratorContext context = new ContentDecoratorContext();
85 context.setView(ContentDecoratorContext.DETAIL_VIEW);
86 context.setMedia(ContentDecoratorContext.EMAIL);
87
88 blog.getContentDecoratorChain().decorate(context, comment);
89
90 SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
91 sdf.setTimeZone(blog.getTimeZone());
92
93 String subject = "[Comment] " + comment.getTitle();
94 String author = StringUtils.transformHTML(comment.getAuthor());
95 if (comment.getWebsite() != null) {
96 author = "<a href=\"" + comment.getWebsite() + "\">" + author + "</a>";
97 }
98
99 String message = "Comment from " + author + " on " + sdf.format(comment.getDate());
100 message += " in response to " + comment.getBlogEntry().getTitle();
101 message += "\n\n<br><br>";
102 message += comment.getBody();
103 message += "\n\n<br><br>";
104 message += "<a href=\"" + comment.getPermalink() + "\">Permalink</a>";
105 message += " | ";
106 message += "<a href=\"" + blog.getUrl() + "removeEmailAddress.action?entry=" + comment.getBlogEntry().getId() + "&email=" + EMAIL_ADDRESS_TOKEN + "\">Opt-out</a>";
107
108 Collection to = getEmailAddresses(comment);
109 Iterator it = comment.getBlogEntry().getComments().iterator();
110 Comment blogComment;
111 while (it.hasNext()) {
112 blogComment = (Comment)it.next();
113 if (blogComment.getEmail() != null && blogComment.getEmail().length() > 0) {
114 to.add(blogComment.getEmail());
115 }
116 }
117
118 // now send personalized e-mails to the blog owner and everybody
119 // that left a comment specifying their e-mail address
120 try {
121 Session session = MailUtils.createSession();
122 Iterator emailAddresses = to.iterator();
123 while (emailAddresses.hasNext()) {
124 String emailAddress = (String)emailAddresses.next();
125
126 // customize the opt-out link and send the message
127 MailUtils.sendMail(session, blog, emailAddress, subject,
128 message.replaceAll(EMAIL_ADDRESS_TOKEN, emailAddress));
129 }
130 } catch (Exception e) {
131 e.printStackTrace();
132 }
133 }
134
135 private void sendApprovalRequest(Comment comment) {
/*
P/P * Method: void sendApprovalRequest(Comment)
*
* Preconditions:
* comment != null
* init'ed(comment.author)
* comment.blogEntry != null
* comment.blogEntry.blog != null
* comment.blogEntry.blog.properties != null
* init'ed(comment.blogEntry.title)
* init'ed(comment.body)
* comment.date != null
* comment.state != null
* init'ed(comment.state.name)
* ...
*
* Postconditions:
* init'ed(comment.blogEntry.permalink)
* possibly_updated(comment.blogEntry.user)
*
* Test Vectors:
* comment.website: Addr_Set{null}, Inverse{null}
*/
136 Blog blog = comment.getBlogEntry().getBlog();
137
138 SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
139 sdf.setTimeZone(blog.getTimeZone());
140
141 String subject = "[Comment-" + comment.getState().getName() + "] " + comment.getTitle();
142 String author = StringUtils.transformHTML(comment.getAuthor());
143 if (comment.getWebsite() != null) {
144 author = "<a href=\"" + comment.getWebsite() + "\">" + author + "</a>";
145 }
146
147 String message = "Comment from " + author + " on " + sdf.format(comment.getDate());
148 message += " in response to " + comment.getBlogEntry().getTitle();
149 message += "\n\n<br><br>";
150 message += comment.getBody();
151 message += "\n\n<br><br>";
152 message += "<a href=\"" + comment.getPermalink() + "\">Permalink</a>";
153 message += " | ";
154 message += "<a href=\"" + blog.getUrl() + "manageResponses.secureaction?response=" + comment.getGuid() + "&submit=Approve" + "\">Approve</a>";
155 message += " | ";
156 message += "<a href=\"" + blog.getUrl() + "manageResponses.secureaction?response=" + comment.getGuid() + "&submit=Reject" + "\">Reject</a>";
157 message += " | ";
158 message += "<a href=\"" + blog.getUrl() + "manageResponses.secureaction?response=" + comment.getGuid() + "&submit=Remove" + "\">Remove</a>";
159
160 Collection to = getEmailAddresses(comment);
161
162 try {
163 MailUtils.sendMail(MailUtils.createSession(), blog, to, subject, message);
164 } catch (Exception e) {
165 e.printStackTrace();
166 }
167 }
168
169 /**
170 * Returns the collection of recipients.
171 *
172 * @param comment the Comment from the event
173 * @return a Collection of e-mail addresses (Strings)
174 */
175 protected abstract Collection getEmailAddresses(Comment comment);
176
177 }
SofCheck Inspector Build Version : 2.22510
| abstractemailnotificationlistener.java |
2010-Jun-25 19:40:32 |
| abstractemailnotificationlistener.class |
2010-Jul-19 20:23:38 |
| abstractemailnotificationlistener.class |
2010-Jul-16 19:33:50 |
| abstractemailnotificationlistener.class |
2010-Jul-16 19:33:50 |
| abstractemailnotificationlistener.class |
2010-Jul-19 20:23:38 |