001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/mailarchive/tags/sakai_2-4-1/mailarchive-impl/impl/src/java/org/sakaiproject/mailarchive/impl/SiteEmailNotificationMail.java $
003: * $Id: SiteEmailNotificationMail.java 13809 2006-08-17 02:48:33Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.mailarchive.impl;
021:
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.ResourceBundle;
026:
027: import org.sakaiproject.component.cover.ServerConfigurationService;
028: import org.sakaiproject.entity.api.Reference;
029: import org.sakaiproject.entity.api.ResourceProperties;
030: import org.sakaiproject.entity.cover.EntityManager;
031: import org.sakaiproject.event.api.Event;
032: import org.sakaiproject.event.api.NotificationAction;
033: import org.sakaiproject.mailarchive.api.MailArchiveMessage;
034: import org.sakaiproject.mailarchive.api.MailArchiveMessageHeader;
035: import org.sakaiproject.mailarchive.api.MailArchiveService;
036: import org.sakaiproject.site.api.Site;
037: import org.sakaiproject.site.cover.SiteService;
038: import org.sakaiproject.util.FormattedText;
039: import org.sakaiproject.util.SiteEmailNotification;
040:
041: /**
042: * <p>
043: * SiteEmailNotificationMail fills the notification message and headers with details from the email message that triggered the notification event.
044: * </p>
045: */
046: public class SiteEmailNotificationMail extends SiteEmailNotification {
047: // borrow from announcement's notification class
048: private static ResourceBundle rb = ResourceBundle
049: .getBundle("siteemaanc");
050:
051: /**
052: * Construct.
053: */
054: public SiteEmailNotificationMail() {
055: }
056:
057: /**
058: * Construct.
059: */
060: public SiteEmailNotificationMail(String siteId) {
061: super (siteId);
062: }
063:
064: /**
065: * @inheritDoc
066: */
067: protected String getResourceAbility() {
068: return MailArchiveService.SECURE_MAIL_READ;
069: }
070:
071: /**
072: * @inheritDoc
073: */
074: public NotificationAction getClone() {
075: SiteEmailNotificationMail clone = new SiteEmailNotificationMail();
076: clone.set(this );
077:
078: return clone;
079: }
080:
081: /**
082: * @inheritDoc
083: */
084: protected String getMessage(Event event) {
085: StringBuffer buf = new StringBuffer();
086:
087: // get the message
088: Reference ref = EntityManager.newReference(event.getResource());
089: MailArchiveMessage msg = (MailArchiveMessage) ref.getEntity();
090: MailArchiveMessageHeader hdr = (MailArchiveMessageHeader) msg
091: .getMailArchiveHeader();
092:
093: // use either the configured site, or if not configured, the site (context) of the resource
094: String siteId = (getSite() != null) ? getSite() : ref
095: .getContext();
096:
097: // get a site title
098: String title = siteId;
099: try {
100: Site site = SiteService.getSite(siteId);
101: title = site.getTitle();
102: } catch (Exception ignore) {
103: }
104:
105: // use the message's body
106: // %%% JANDERSE convert to plaintext - email is currently sent plaintext only,
107: // so text formatting that may be present in the message should be removed.
108: buf.append(FormattedText.convertFormattedTextToPlaintext(msg
109: .getBody()));
110:
111: // add any attachments
112: List attachments = hdr.getAttachments();
113: if (attachments.size() > 0) {
114: buf.append("\n" + "Attachments:\n");
115: for (Iterator iAttachments = attachments.iterator(); iAttachments
116: .hasNext();) {
117: Reference attachment = (Reference) iAttachments.next();
118: String attachmentTitle = attachment.getProperties()
119: .getPropertyFormatted(
120: ResourceProperties.PROP_DISPLAY_NAME);
121: buf.append("\n" + attachmentTitle);
122: buf.append("\n" + attachment.getUrl() + "\n");
123: }
124: }
125:
126: return buf.toString();
127: }
128:
129: /**
130: * @inheritDoc
131: */
132: protected List getHeaders(Event event) {
133: // send most of the headers from the original message, removing some
134: Reference ref = EntityManager.newReference(event.getResource());
135: MailArchiveMessage msg = (MailArchiveMessage) ref.getEntity();
136: MailArchiveMessageHeader hdr = (MailArchiveMessageHeader) msg
137: .getMailArchiveHeader();
138: List headers = hdr.getMailHeaders();
139:
140: List filteredHeaders = new ArrayList();
141: String innerContentType = null;
142: String outerContentType = null;
143: String contentType = null;
144:
145: for (int i = 0; i < headers.size(); i++) {
146: String headerStr = (String) headers.get(i);
147:
148: if (headerStr.startsWith("Return-Path")
149: || headerStr.startsWith("Mime-Version")
150: || headerStr
151: .startsWith("Content-Transfer-Encoding"))
152: continue;
153:
154: if (headerStr
155: .startsWith(MailArchiveService.HEADER_INNER_CONTENT_TYPE
156: + ": "))
157: innerContentType = headerStr;
158: if (headerStr
159: .startsWith(MailArchiveService.HEADER_OUTER_CONTENT_TYPE
160: + ": "))
161: outerContentType = headerStr;
162:
163: if (!headerStr.startsWith("Content-Type: ")) {
164: filteredHeaders.add(headerStr);
165: } else {
166: contentType = headerStr;
167: }
168: }
169:
170: if (innerContentType != null) {
171: // use the content type of the inner email message body
172: filteredHeaders.add(innerContentType.replaceAll(
173: MailArchiveService.HEADER_INNER_CONTENT_TYPE,
174: "Content-Type"));
175: } else if (outerContentType != null) {
176: // use the content type from the outer message (content type as set in the email originally)
177: filteredHeaders.add(outerContentType.replaceAll(
178: MailArchiveService.HEADER_OUTER_CONTENT_TYPE,
179: "Content-Type"));
180: } else if (contentType != null) {
181: // Oh well, use the plain old Content-Type header
182: filteredHeaders.add(contentType);
183: }
184:
185: return filteredHeaders;
186: }
187:
188: /**
189: * @inheritDoc
190: */
191: protected String getTag(String newline, String title) {
192: // tag the message
193: String rv = newline
194: + rb.getString("separator")
195: + newline
196: + rb.getString("this")
197: + " "
198: + ServerConfigurationService.getString("ui.service",
199: "Sakai") + " ("
200: + ServerConfigurationService.getPortalUrl() + ") "
201: + rb.getString("forthe") + " " + title + " "
202: + rb.getString("site") + newline
203: + rb.getString("youcan") + newline;
204: return rv;
205: }
206: }
|