| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 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 | |
import net.sourceforge.pebble.web.security.SecurityTokenValidator; |
| 41 | |
import net.sourceforge.pebble.web.security.SecurityTokenValidatorImpl; |
| 42 | |
|
| 43 | |
import javax.mail.Session; |
| 44 | |
import java.text.SimpleDateFormat; |
| 45 | |
import java.util.Collection; |
| 46 | |
import java.util.HashMap; |
| 47 | |
import java.util.Iterator; |
| 48 | |
import java.util.Map; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | 0 | public abstract class AbstractEmailNotificationListener extends CommentListenerSupport { |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
private static final String EMAIL_ADDRESS_TOKEN = "EMAIL_ADDRESS"; |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
public void commentAdded(CommentEvent event) { |
| 69 | 0 | Comment comment = event.getComment(); |
| 70 | 0 | if (comment.isApproved()) { |
| 71 | 0 | sendNotification(comment); |
| 72 | 0 | } else if (comment.isPending()) { |
| 73 | 0 | sendApprovalRequest(comment); |
| 74 | |
} |
| 75 | 0 | } |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
public void commentApproved(CommentEvent event) { |
| 83 | 0 | Comment comment = event.getComment(); |
| 84 | 0 | sendNotification(comment); |
| 85 | 0 | } |
| 86 | |
|
| 87 | |
private void sendNotification(Comment comment) { |
| 88 | 0 | Blog blog = comment.getBlogEntry().getBlog(); |
| 89 | |
|
| 90 | 0 | ContentDecoratorContext context = new ContentDecoratorContext(); |
| 91 | 0 | context.setView(ContentDecoratorContext.DETAIL_VIEW); |
| 92 | 0 | context.setMedia(ContentDecoratorContext.EMAIL); |
| 93 | |
|
| 94 | 0 | blog.getContentDecoratorChain().decorate(context, comment); |
| 95 | |
|
| 96 | 0 | SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z"); |
| 97 | 0 | sdf.setTimeZone(blog.getTimeZone()); |
| 98 | |
|
| 99 | 0 | String subject = MailUtils.getCommentPrefix(blog) + " " + comment.getTitle(); |
| 100 | 0 | String author = StringUtils.transformHTML(comment.getAuthor()); |
| 101 | 0 | if (comment.getWebsite() != null) { |
| 102 | 0 | author = "<a href=\"" + comment.getWebsite() + "\">" + author + "</a>"; |
| 103 | |
} |
| 104 | |
|
| 105 | 0 | String message = "Comment from " + author + " on " + sdf.format(comment.getDate()); |
| 106 | 0 | message += " in response to " + comment.getBlogEntry().getTitle(); |
| 107 | 0 | message += "\n\n<br><br>"; |
| 108 | 0 | message += comment.getBody(); |
| 109 | 0 | message += "\n\n<br><br>"; |
| 110 | 0 | message += "<a href=\"" + comment.getPermalink() + "\">Permalink</a>"; |
| 111 | 0 | message += " | "; |
| 112 | 0 | message += "<a href=\"" + blog.getUrl() + "removeEmailAddress.action?entry=" + comment.getBlogEntry().getId() + "&email=" + EMAIL_ADDRESS_TOKEN + "\">Opt-out</a>"; |
| 113 | |
|
| 114 | 0 | Collection to = getEmailAddresses(comment); |
| 115 | 0 | Iterator it = comment.getBlogEntry().getComments().iterator(); |
| 116 | |
Comment blogComment; |
| 117 | 0 | while (it.hasNext()) { |
| 118 | 0 | blogComment = (Comment) it.next(); |
| 119 | 0 | if (blogComment.getEmail() != null && blogComment.getEmail().length() > 0) { |
| 120 | 0 | to.add(blogComment.getEmail()); |
| 121 | |
} |
| 122 | |
} |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
try { |
| 127 | 0 | Session session = MailUtils.createSession(); |
| 128 | 0 | Iterator emailAddresses = to.iterator(); |
| 129 | 0 | while (emailAddresses.hasNext()) { |
| 130 | 0 | String emailAddress = (String) emailAddresses.next(); |
| 131 | |
|
| 132 | |
|
| 133 | 0 | MailUtils.sendMail(session, blog, emailAddress, subject, |
| 134 | |
message.replaceAll(EMAIL_ADDRESS_TOKEN, emailAddress)); |
| 135 | 0 | } |
| 136 | 0 | } catch (Exception e) { |
| 137 | 0 | e.printStackTrace(); |
| 138 | 0 | } |
| 139 | 0 | } |
| 140 | |
|
| 141 | |
private void sendApprovalRequest(Comment comment) { |
| 142 | 0 | Blog blog = comment.getBlogEntry().getBlog(); |
| 143 | |
|
| 144 | 0 | SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z"); |
| 145 | 0 | sdf.setTimeZone(blog.getTimeZone()); |
| 146 | |
|
| 147 | 0 | String subject = MailUtils.getCommentPrefix(blog, comment.getState()) + " " + comment.getTitle(); |
| 148 | |
|
| 149 | 0 | String author = StringUtils.transformHTML(comment.getAuthor()); |
| 150 | 0 | if (comment.getWebsite() != null) { |
| 151 | 0 | author = "<a href=\"" + comment.getWebsite() + "\">" + author + "</a>"; |
| 152 | |
} |
| 153 | |
|
| 154 | 0 | SecurityTokenValidator validator = new SecurityTokenValidatorImpl(); |
| 155 | |
|
| 156 | 0 | String message = "Comment from " + author + " on " + sdf.format(comment.getDate()); |
| 157 | 0 | message += " in response to " + comment.getBlogEntry().getTitle(); |
| 158 | 0 | message += "\n\n<br><br>"; |
| 159 | 0 | message += comment.getBody(); |
| 160 | 0 | message += "\n\n<br><br>"; |
| 161 | 0 | message += "<a href=\"" + comment.getPermalink() + "\">Permalink</a>"; |
| 162 | 0 | message += " | "; |
| 163 | 0 | message += "<a href=\"" + blog.getUrl() + validator.generateSignedQueryString("manageResponses.secureaction", |
| 164 | |
createMap("response", comment.getGuid(), "submit", "Approve"), blog.getXsrfSigningSalt()) + "\">Approve</a>"; |
| 165 | 0 | message += " | "; |
| 166 | 0 | message += "<a href=\"" + blog.getUrl() + validator.generateSignedQueryString("manageResponses.secureaction", |
| 167 | |
createMap("response", comment.getGuid(), "submit", "Reject"), blog.getXsrfSigningSalt()) + "\">Reject</a>"; |
| 168 | 0 | message += " | "; |
| 169 | 0 | message += "<a href=\"" + blog.getUrl() + validator.generateSignedQueryString("manageResponses.secureaction", |
| 170 | |
createMap("response", comment.getGuid(), "submit", "Remove"), blog.getXsrfSigningSalt()) + "\">Remove</a>"; |
| 171 | |
|
| 172 | 0 | Collection to = getEmailAddresses(comment); |
| 173 | |
|
| 174 | |
try { |
| 175 | 0 | MailUtils.sendMail(MailUtils.createSession(), blog, to, subject, message); |
| 176 | 0 | } catch (Exception e) { |
| 177 | 0 | e.printStackTrace(); |
| 178 | 0 | } |
| 179 | 0 | } |
| 180 | |
|
| 181 | |
private Map<String, String[]> createMap(String... values) { |
| 182 | 0 | Map<String, String[]> map = new HashMap<String, String[]>(); |
| 183 | 0 | for (int i = 0; i < values.length - 1; i += 2) { |
| 184 | 0 | map.put(values[i], new String[]{values[i + 1]}); |
| 185 | |
} |
| 186 | 0 | return map; |
| 187 | |
} |
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
protected abstract Collection getEmailAddresses(Comment comment); |
| 196 | |
|
| 197 | |
} |