Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoberto E. Escobar2014-06-24 23:42:38 +0000
committerRoberto E. Escobar2014-08-28 23:58:56 +0000
commit868f517e191584dab637571e243b8eda2be1181f (patch)
tree23ffc238a81973e6565546a64d3d3a7bcb14ef08 /plugins/org.eclipse.osee.mail
parent543c55775e48e8f476abbbfe436d45778d4ad85c (diff)
downloadorg.eclipse.osee-868f517e191584dab637571e243b8eda2be1181f.tar.gz
org.eclipse.osee-868f517e191584dab637571e243b8eda2be1181f.tar.xz
org.eclipse.osee-868f517e191584dab637571e243b8eda2be1181f.zip
refactor: Simplify Mail Service API
Diffstat (limited to 'plugins/org.eclipse.osee.mail')
-rw-r--r--plugins/org.eclipse.osee.mail/META-INF/MANIFEST.MF7
-rw-r--r--plugins/org.eclipse.osee.mail/OSGI-INF/mail.jaxrs.application.xml2
-rw-r--r--plugins/org.eclipse.osee.mail/OSGI-INF/mail.service.xml7
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailEventUtil.java126
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailMessage.java100
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailService.java25
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailUtils.java137
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MultiPartDataSource.java74
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/SendMailStatus.java133
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/StringDataSource.java84
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/UrlDataSource.java55
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailConfiguration.java (renamed from plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailConfiguration.java)38
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailConstants.java (renamed from plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailConstants.java)26
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailMessageFactory.java82
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailServiceImpl.java167
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailTransportListener.java49
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/SendMailCallable.java102
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/StatusTransportListener.java62
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/resources/MailApplication.java2
-rw-r--r--plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/resources/MailResource.java75
20 files changed, 303 insertions, 1050 deletions
diff --git a/plugins/org.eclipse.osee.mail/META-INF/MANIFEST.MF b/plugins/org.eclipse.osee.mail/META-INF/MANIFEST.MF
index ce406e7112a..ab6ee3f8f83 100644
--- a/plugins/org.eclipse.osee.mail/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.osee.mail/META-INF/MANIFEST.MF
@@ -11,10 +11,9 @@ Import-Package: javax.activation,
javax.mail.internet;version="1.4.1",
javax.ws.rs;version="2.0.0",
javax.ws.rs.core;version="2.0.0",
- javax.xml.bind.annotation,
- org.eclipse.osee.event,
+ org.eclipse.osee.executor.admin,
org.eclipse.osee.framework.jdk.core.util,
- org.eclipse.osee.framework.jdk.core.util.windows,
- org.eclipse.osee.logger
+ org.eclipse.osee.logger,
+ org.eclipse.osee.mail.api
Service-Component: OSGI-INF/*.xml
Export-Package: org.eclipse.osee.mail
diff --git a/plugins/org.eclipse.osee.mail/OSGI-INF/mail.jaxrs.application.xml b/plugins/org.eclipse.osee.mail/OSGI-INF/mail.jaxrs.application.xml
index 09171a75f74..0ff3c509d49 100644
--- a/plugins/org.eclipse.osee.mail/OSGI-INF/mail.jaxrs.application.xml
+++ b/plugins/org.eclipse.osee.mail/OSGI-INF/mail.jaxrs.application.xml
@@ -4,5 +4,5 @@
<service>
<provide interface="javax.ws.rs.core.Application"/>
</service>
- <reference bind="setMailService" cardinality="1..1" interface="org.eclipse.osee.mail.MailService" name="MailService" policy="static"/>
+ <reference bind="setMailService" cardinality="1..1" interface="org.eclipse.osee.mail.api.MailService" name="MailService" policy="static"/>
</scr:component>
diff --git a/plugins/org.eclipse.osee.mail/OSGI-INF/mail.service.xml b/plugins/org.eclipse.osee.mail/OSGI-INF/mail.service.xml
index c86d68ada24..585e37bc2d7 100644
--- a/plugins/org.eclipse.osee.mail/OSGI-INF/mail.service.xml
+++ b/plugins/org.eclipse.osee.mail/OSGI-INF/mail.service.xml
@@ -2,11 +2,8 @@
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="start" configuration-policy="require" deactivate="stop" modified="update">
<implementation class="org.eclipse.osee.mail.internal.MailServiceImpl"/>
<service>
- <provide interface="org.eclipse.osee.mail.MailService"/>
+ <provide interface="org.eclipse.osee.mail.api.MailService"/>
</service>
- <reference interface="org.eclipse.osee.event.EventService" name="EventService"
- cardinality="1..1"
- policy="static"
- bind="setEventService" />
<reference bind="setLogger" cardinality="1..1" interface="org.eclipse.osee.logger.Log" name="Log" policy="static"/>
+ <reference bind="setExecutorAdmin" cardinality="1..1" interface="org.eclipse.osee.executor.admin.ExecutorAdmin" name="ExecutorAdmin" policy="static"/>
</scr:component>
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailEventUtil.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailEventUtil.java
deleted file mode 100644
index c92209507d8..00000000000
--- a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailEventUtil.java
+++ /dev/null
@@ -1,126 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2004, 2007 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-package org.eclipse.osee.mail;
-
-import java.util.Collection;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
-import javax.mail.Address;
-import javax.mail.Message;
-import javax.mail.event.TransportEvent;
-import org.eclipse.osee.mail.SendMailStatus.MailStatus;
-
-/**
- * @author Roberto E. Escobar
- */
-public final class MailEventUtil {
-
- private MailEventUtil() {
- // Utility
- }
-
- public static void loadStatus(SendMailStatus status, TransportEvent event) {
- MailStatus eventStatus = toStatus(event);
- status.add(eventStatus);
- }
-
- public static MailStatus toStatus(TransportEvent event) {
- MailStatus status = new MailStatus();
- status.setVerified(true);
- Message message = event.getMessage();
- String uuid;
- try {
- String[] header = message.getHeader(MailConstants.MAIL_UUID_HEADER);
- uuid = header.length == 0 ? "-1" : header[0];
- } catch (Exception ex) {
- uuid = ex.getMessage();
- }
- status.setUuid(uuid);
- try {
- Date sentDate = message.getSentDate();
- status.setDateSent(sentDate);
- } catch (Exception ex) {
- // Do nothing
- }
- String subject;
- try {
- subject = message.getSubject();
- } catch (Exception ex) {
- subject = ex.getMessage();
- }
- status.setSubject(subject);
-
- try {
- addAddress(status.getFromAddress(), message.getFrom());
- } catch (Exception ex) {
- //
- }
- addAddress(status.getInvalidAddress(), event.getInvalidAddresses());
- addAddress(status.getSentAddress(), event.getValidSentAddresses());
- addAddress(status.getUnsentAddress(), event.getValidUnsentAddresses());
- return status;
- }
-
- private static void addAddress(Collection<String> data, Address... addresses) {
- if (addresses != null) {
- for (Address address : addresses) {
- data.add(address.toString());
- }
- }
- }
-
- public static Map<String, String> createTransportEventData(TransportEvent event) {
- Map<String, String> data = new HashMap<String, String>();
- Message message = event.getMessage();
-
- String uuid;
- try {
- String[] header = message.getHeader(MailConstants.MAIL_UUID_HEADER);
- uuid = header.length == 0 ? "-1" : header[0];
- } catch (Exception ex) {
- uuid = ex.getMessage();
- }
- data.put(MailConstants.MAIL_UUID, uuid);
- try {
- Date sentDate = message.getSentDate();
- data.put(MailConstants.MAIL_DATE_SENT, String.valueOf(sentDate.getTime()));
- } catch (Exception ex) {
- // Do nothing
- }
- String subject;
- try {
- subject = message.getSubject();
- } catch (Exception ex) {
- subject = ex.getMessage();
- }
- data.put(MailConstants.MAIL_SUBJECT, subject);
-
- try {
- addAddress(data, MailConstants.MAIL_FROM_ADDRESS, message.getFrom());
- } catch (Exception ex) {
- data.put(MailConstants.MAIL_FROM_ADDRESS, ex.getMessage());
- }
- addAddress(data, MailConstants.MAIL_INVALID_ADDRESS, event.getInvalidAddresses());
- addAddress(data, MailConstants.MAIL_VALID_SENT_ADDRESS, event.getValidSentAddresses());
- addAddress(data, MailConstants.MAIL_VALID_UNSENT_ADDRESS, event.getValidUnsentAddresses());
- return data;
- }
-
- private static void addAddress(Map<String, String> data, String baseKey, Address... addresses) {
- int size = addresses != null ? addresses.length : 0;
- data.put(baseKey + ".count", String.valueOf(size));
- for (int index = 0; index < size; index++) {
- Address address = addresses[index];
- data.put(baseKey + "." + index, address.toString());
- }
- }
-}
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailMessage.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailMessage.java
deleted file mode 100644
index 49f273f9ac4..00000000000
--- a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailMessage.java
+++ /dev/null
@@ -1,100 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2004, 2007 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-package org.eclipse.osee.mail;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.LinkedHashSet;
-import java.util.List;
-import javax.activation.DataHandler;
-import javax.activation.DataSource;
-import javax.xml.bind.annotation.XmlElement;
-import javax.xml.bind.annotation.XmlRootElement;
-import javax.xml.bind.annotation.XmlTransient;
-
-/**
- * @author Roberto E. Escobar
- */
-@XmlRootElement
-public class MailMessage {
-
- private String id;
- private String subject;
- private String from;
-
- @XmlElement
- private final Collection<String> replyToAddress = new LinkedHashSet<String>();
-
- @XmlElement
- private final Collection<String> recepientAddresses = new LinkedHashSet<String>();
-
- @XmlTransient
- private final List<DataHandler> attachments = new ArrayList<DataHandler>();
-
- public void setId(String id) {
- this.id = id;
- }
-
- public String getId() {
- return id;
- }
-
- public String getSubject() {
- return subject;
- }
-
- public void setSubject(String subject) {
- this.subject = subject;
- }
-
- public String getFrom() {
- return from;
- }
-
- public void setFrom(String from) {
- this.from = from;
- }
-
- public Collection<String> getReplyTo() {
- return replyToAddress;
- }
-
- public void setReplyTo(Collection<String> replyTo) {
- this.replyToAddress.clear();
- this.replyToAddress.addAll(replyTo);
- }
-
- public Collection<String> getRecipients() {
- return recepientAddresses;
- }
-
- public void setRecipients(Collection<String> recipientAddress) {
- this.recepientAddresses.clear();
- this.recepientAddresses.addAll(recipientAddress);
- }
-
- @XmlElement
- public Collection<DataHandler> getAttachments() {
- return attachments;
- }
-
- public void addAttachment(DataSource... dataSource) {
- for (DataSource source : dataSource) {
- attachments.add(new DataHandler(source));
- }
- }
-
- public void addAttachment(DataHandler... dataHandler) {
- for (DataHandler handler : dataHandler) {
- attachments.add(handler);
- }
- }
-}
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailService.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailService.java
deleted file mode 100644
index 82fb83fea73..00000000000
--- a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailService.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2004, 2007 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-package org.eclipse.osee.mail;
-
-import java.util.List;
-import java.util.concurrent.Callable;
-
-/**
- * @author Roberto E. Escobar
- */
-public interface MailService {
-
- List<Callable<SendMailStatus>> createSendCalls(MailMessage... emails);
-
- MailMessage createSystemTestMessage();
-
-}
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailUtils.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailUtils.java
deleted file mode 100644
index 1362bc375b4..00000000000
--- a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailUtils.java
+++ /dev/null
@@ -1,137 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2004, 2007 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-package org.eclipse.osee.mail;
-
-import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
-import java.net.URL;
-import java.net.URLEncoder;
-import java.util.Date;
-import javax.activation.CommandMap;
-import javax.activation.DataSource;
-import javax.activation.MailcapCommandMap;
-import javax.mail.internet.AddressException;
-import javax.mail.internet.InternetAddress;
-import javax.mail.internet.MimeBodyPart;
-import javax.mail.internet.MimeMultipart;
-import org.eclipse.osee.framework.jdk.core.util.Lib;
-import org.eclipse.osee.framework.jdk.core.util.windows.OutlookCalendarEvent;
-
-/**
- * @author Roberto E. Escobar
- */
-public final class MailUtils {
-
- private static final String OUTLOOK_CALENDAR_EXTENSION = ".vcs";
-
- private MailUtils() {
- // Utility Class
- }
-
- public static boolean isValidEmail(String email) {
- boolean result = false;
- try {
- InternetAddress[] addresses = InternetAddress.parse(email, true);
- if (addresses != null && addresses.length > 0) {
- result = true;
- }
- } catch (AddressException ex) {
- // Do Nothing
- }
- return result;
- }
-
- public static MailcapCommandMap getMailcapCommandMap() {
- MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
- mc.addMailcap("text/*;; x-java-content-handler=com.sun.mail.handlers.text_plain");
- mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
- mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
- mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
- mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
- mc.addMailcap("multipart/mixed;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
- mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
- mc.addMailcap("image/jpeg;; x-java-content-handler=com.sun.mail.handlers.image_jpeg");
- mc.addMailcap("image/gif;; x-java-content-handler=com.sun.mail.handlers.image_gif");
- return mc;
- }
-
- public static StringDataSource createFromString(String name, String message, Object... args) {
- String data;
- if (args.length > 0) {
- data = String.format(message, args);
- } else {
- data = message;
- }
- StringDataSource dataSource = new StringDataSource(name, data);
- dataSource.setCharset("UTF-8");
- dataSource.setContentType("text/plain");
- return dataSource;
- }
-
- public static DataSource createFromHtml(final String name, String htmlData) throws Exception {
- String plainText = stripHtmlTags(htmlData);
- return createAlternativeDataSource(name, htmlData, plainText);
- }
-
- public static DataSource createAlternativeDataSource(String name, String htmlText, String plainText) throws Exception {
- final MimeMultipart content = new MimeMultipart("alternative");
-
- MimeBodyPart html = new MimeBodyPart();
- html.setContent(htmlText, "text/html");
-
- MimeBodyPart text = new MimeBodyPart();
- text.setText(plainText);
-
- content.addBodyPart(html);
- content.addBodyPart(text);
-
- return new MultiPartDataSource(name, content);
- }
-
- public static UrlDataSource createFromUrl(String name, URL url, String contentType) {
- return new UrlDataSource(name, url, contentType);
- }
-
- public static StringDataSource createOutlookEvent(String eventName, String location, Date date, String startTime, String endTime) throws UnsupportedEncodingException {
- OutlookCalendarEvent calendarEvent = new OutlookCalendarEvent(location, eventName, date, startTime, endTime);
- String fileName = toFileName(eventName, OUTLOOK_CALENDAR_EXTENSION);
-
- StringDataSource dataSource = new StringDataSource(fileName, calendarEvent.getEvent()) {
- @Override
- public OutputStream getOutputStream() {
- throw new UnsupportedOperationException("OutputStream is not available for this source");
- }
- };
- dataSource.setCharset("UTF-8");
- dataSource.setContentType("text/plain");
- return dataSource;
- }
-
- private static String toFileName(String value, String extension) throws UnsupportedEncodingException {
- String fileName = value;
- if (fileName.endsWith(OUTLOOK_CALENDAR_EXTENSION)) {
- fileName = Lib.removeExtension(fileName);
- }
- String validName = URLEncoder.encode(fileName, "UTF-8");
- StringBuilder builder = new StringBuilder();
- builder.append(validName);
- builder.append(OUTLOOK_CALENDAR_EXTENSION);
- return builder.toString();
- }
-
- private static String stripHtmlTags(String html) {
- String plainText = html.replaceAll("<(.|\n)*?>", "");
- plainText = plainText.replaceAll("\t", " ");
- plainText = plainText.replaceAll("\r\n", "");
- plainText = plainText.replaceAll(" +", " ");
- return plainText;
- }
-}
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MultiPartDataSource.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MultiPartDataSource.java
deleted file mode 100644
index b5a8c09b38f..00000000000
--- a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MultiPartDataSource.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2004, 2007 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-package org.eclipse.osee.mail;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import javax.mail.BodyPart;
-import javax.mail.MessagingException;
-import javax.mail.MultipartDataSource;
-import javax.mail.internet.MimeMultipart;
-
-/**
- * @author Roberto E. Escobar
- */
-public class MultiPartDataSource implements MultipartDataSource {
- private final String name;
- private final MimeMultipart content;
-
- public MultiPartDataSource(String name, MimeMultipart content) {
- this.name = name;
- this.content = content;
- }
-
- @Override
- public String getContentType() {
- return content.getContentType();
- }
-
- @Override
- public InputStream getInputStream() throws IOException {
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- try {
- content.writeTo(os);
- } catch (MessagingException ex) {
- throw new IOException(ex);
- }
- return new ByteArrayInputStream(os.toByteArray());
- }
-
- @Override
- public String getName() {
- return name;
- }
-
- @Override
- public OutputStream getOutputStream() {
- throw new UnsupportedOperationException("OutputStream is not available for this source");
- }
-
- @Override
- public int getCount() {
- try {
- return content.getCount();
- } catch (MessagingException ex) {
- return 0;
- }
- }
-
- @Override
- public BodyPart getBodyPart(int index) throws MessagingException {
- return content.getBodyPart(index);
- }
-}; \ No newline at end of file
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/SendMailStatus.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/SendMailStatus.java
deleted file mode 100644
index 3810b8757a6..00000000000
--- a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/SendMailStatus.java
+++ /dev/null
@@ -1,133 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2004, 2007 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-package org.eclipse.osee.mail;
-
-import java.util.Collection;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.Set;
-import javax.xml.bind.annotation.XmlElement;
-import javax.xml.bind.annotation.XmlRootElement;
-
-/**
- * @author Roberto E. Escobar
- */
-@XmlRootElement
-public class SendMailStatus {
-
- private final Set<MailStatus> entries = new HashSet<MailStatus>();
-
- public void add(MailStatus newStatus) {
- entries.add(newStatus);
- }
-
- @XmlElement
- public Collection<MailStatus> getEntries() {
- return entries;
- }
-
- @XmlRootElement
- public static class MailStatus {
-
- private String uuid;
- private String subject;
- private Date dateSent;
- private boolean verified;
-
- private final Set<String> fromAddress = new HashSet<String>();
- private final Set<String> invalidAddress = new HashSet<String>();
- private final Set<String> sentAddress = new HashSet<String>();
- private final Set<String> unsentAddress = new HashSet<String>();
-
- public void setUuid(String uuid) {
- this.uuid = uuid;
- }
-
- public void setDateSent(Date dateSent) {
- this.dateSent = dateSent;
- }
-
- public void setSubject(String subject) {
- this.subject = subject;
- }
-
- public String getUuid() {
- return uuid;
- }
-
- public Date getDateSent() {
- return dateSent;
- }
-
- public String getSubject() {
- return subject;
- }
-
- public boolean isVerified() {
- return verified;
- }
-
- public void setVerified(boolean verified) {
- this.verified = verified;
- }
-
- @XmlElement
- public Set<String> getFromAddress() {
- return fromAddress;
- }
-
- @XmlElement
- public Set<String> getInvalidAddress() {
- return invalidAddress;
- }
-
- @XmlElement
- public Set<String> getSentAddress() {
- return sentAddress;
- }
-
- @XmlElement
- public Set<String> getUnsentAddress() {
- return unsentAddress;
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- MailStatus other = (MailStatus) obj;
- if (uuid == null) {
- if (other.uuid != null) {
- return false;
- }
- } else if (!uuid.equals(other.uuid)) {
- return false;
- }
- return true;
- }
- }
-
-}
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/StringDataSource.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/StringDataSource.java
deleted file mode 100644
index 8770fb8d3bb..00000000000
--- a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/StringDataSource.java
+++ /dev/null
@@ -1,84 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2004, 2007 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-package org.eclipse.osee.mail;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-
-/**
- * @author Roberto E. Escobar
- */
-public class StringDataSource implements javax.activation.DataSource {
-
- private final String name;
- private String data;
- private String charset;
- private String contentType;
- private ByteArrayOutputStream outputStream;
-
- public StringDataSource(String name, String data) {
- super();
- this.name = name;
- this.data = data;
- }
-
- @Override
- public String getName() {
- return name;
- }
-
- public void setCharset(String charset) {
- this.charset = charset;
- }
-
- public void setContentType(String contentType) {
- this.contentType = contentType.toLowerCase();
- }
-
- @Override
- public InputStream getInputStream() throws IOException {
- if (data == null && outputStream == null) {
- throw new IOException("No data");
- }
- if (outputStream != null) {
- String encodedOut = outputStream.toString(charset);
- if (data == null) {
- data = encodedOut;
- } else {
- data = data.concat(encodedOut);
- }
- outputStream = null;
- }
- return new ByteArrayInputStream(data.getBytes(charset));
- }
-
- @Override
- public OutputStream getOutputStream() {
- if (outputStream == null) {
- outputStream = new ByteArrayOutputStream();
- }
- return outputStream;
- }
-
- @Override
- public String getContentType() {
- String toReturn;
- if (contentType != null && contentType.indexOf("charset") > 0 && contentType.startsWith("text/")) {
- toReturn = contentType;
- } else {
- toReturn = String.format("%s; charset=%s", contentType != null ? contentType : "text/plain", charset);
- }
- return toReturn;
- }
-} \ No newline at end of file
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/UrlDataSource.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/UrlDataSource.java
deleted file mode 100644
index 3ac2552c17f..00000000000
--- a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/UrlDataSource.java
+++ /dev/null
@@ -1,55 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2004, 2007 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-package org.eclipse.osee.mail;
-
-import java.io.BufferedInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.net.URL;
-import javax.activation.DataSource;
-
-/**
- * @author Roberto E. Escobar
- */
-public class UrlDataSource implements DataSource {
-
- private final String name;
- private final URL url;
- private final String contentType;
-
- public UrlDataSource(String name, URL url, String contentType) {
- super();
- this.name = name;
- this.url = url;
- this.contentType = contentType;
- }
-
- @Override
- public String getContentType() {
- return contentType;
- }
-
- @Override
- public InputStream getInputStream() throws IOException {
- return new BufferedInputStream(url.openStream());
- }
-
- @Override
- public String getName() {
- return name;
- }
-
- @Override
- public OutputStream getOutputStream() {
- throw new UnsupportedOperationException();
- }
-}
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailConfiguration.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailConfiguration.java
index d58533d2399..1271c6f274f 100644
--- a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailConfiguration.java
+++ b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailConfiguration.java
@@ -8,25 +8,25 @@
* Contributors:
* Boeing - initial API and implementation
*******************************************************************************/
-package org.eclipse.osee.mail;
-
-import static org.eclipse.osee.mail.MailConstants.DEFAULT_MAIL_SERVER_ADMIN_EMAIL;
-import static org.eclipse.osee.mail.MailConstants.DEFAULT_MAIL_SERVER_HOST;
-import static org.eclipse.osee.mail.MailConstants.DEFAULT_MAIL_SERVER_PASSWORD;
-import static org.eclipse.osee.mail.MailConstants.DEFAULT_MAIL_SERVER_PORT;
-import static org.eclipse.osee.mail.MailConstants.DEFAULT_MAIL_SERVER_STATUS_WAIT_TIME_MILLIS;
-import static org.eclipse.osee.mail.MailConstants.DEFAULT_MAIL_SERVER_TEST_EMAIL_BODY;
-import static org.eclipse.osee.mail.MailConstants.DEFAULT_MAIL_SERVER_TEST_EMAIL_SUBJECT;
-import static org.eclipse.osee.mail.MailConstants.DEFAULT_MAIL_SERVER_TRANSPORT;
-import static org.eclipse.osee.mail.MailConstants.MAIL_SERVER_ADMIN_EMAIL;
-import static org.eclipse.osee.mail.MailConstants.MAIL_SERVER_HOST;
-import static org.eclipse.osee.mail.MailConstants.MAIL_SERVER_PASSWORD;
-import static org.eclipse.osee.mail.MailConstants.MAIL_SERVER_PORT;
-import static org.eclipse.osee.mail.MailConstants.MAIL_SERVER_STATUS_WAIT_TIME_MILLIS;
-import static org.eclipse.osee.mail.MailConstants.MAIL_SERVER_TEST_EMAIL_BODY;
-import static org.eclipse.osee.mail.MailConstants.MAIL_SERVER_TEST_EMAIL_SUBJECT;
-import static org.eclipse.osee.mail.MailConstants.MAIL_SERVER_TRANSPORT;
-import static org.eclipse.osee.mail.MailConstants.MAIL_SERVER_USERNAME;
+package org.eclipse.osee.mail.internal;
+
+import static org.eclipse.osee.mail.internal.MailConstants.DEFAULT_MAIL_SERVER_ADMIN_EMAIL;
+import static org.eclipse.osee.mail.internal.MailConstants.DEFAULT_MAIL_SERVER_HOST;
+import static org.eclipse.osee.mail.internal.MailConstants.DEFAULT_MAIL_SERVER_PASSWORD;
+import static org.eclipse.osee.mail.internal.MailConstants.DEFAULT_MAIL_SERVER_PORT;
+import static org.eclipse.osee.mail.internal.MailConstants.DEFAULT_MAIL_SERVER_STATUS_WAIT_TIME_MILLIS;
+import static org.eclipse.osee.mail.internal.MailConstants.DEFAULT_MAIL_SERVER_TEST_EMAIL_BODY;
+import static org.eclipse.osee.mail.internal.MailConstants.DEFAULT_MAIL_SERVER_TEST_EMAIL_SUBJECT;
+import static org.eclipse.osee.mail.internal.MailConstants.DEFAULT_MAIL_SERVER_TRANSPORT;
+import static org.eclipse.osee.mail.internal.MailConstants.MAIL_SERVER_ADMIN_EMAIL;
+import static org.eclipse.osee.mail.internal.MailConstants.MAIL_SERVER_HOST;
+import static org.eclipse.osee.mail.internal.MailConstants.MAIL_SERVER_PASSWORD;
+import static org.eclipse.osee.mail.internal.MailConstants.MAIL_SERVER_PORT;
+import static org.eclipse.osee.mail.internal.MailConstants.MAIL_SERVER_STATUS_WAIT_TIME_MILLIS;
+import static org.eclipse.osee.mail.internal.MailConstants.MAIL_SERVER_TEST_EMAIL_BODY;
+import static org.eclipse.osee.mail.internal.MailConstants.MAIL_SERVER_TEST_EMAIL_SUBJECT;
+import static org.eclipse.osee.mail.internal.MailConstants.MAIL_SERVER_TRANSPORT;
+import static org.eclipse.osee.mail.internal.MailConstants.MAIL_SERVER_USERNAME;
import java.util.Map;
import org.eclipse.osee.framework.jdk.core.util.Strings;
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailConstants.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailConstants.java
index 80820a5ad0d..2b3f591e51c 100644
--- a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailConstants.java
+++ b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailConstants.java
@@ -8,7 +8,7 @@
* Contributors:
* Boeing - initial API and implementation
*******************************************************************************/
-package org.eclipse.osee.mail;
+package org.eclipse.osee.mail.internal;
/**
* @author Roberto E. Escobar
@@ -19,30 +19,6 @@ public final class MailConstants {
// Constants
}
- private static final String PREFIX = "org/eclipse/osee/mail/event/";
-
- public static final String MAIL_MESSAGE_DELIVERED = PREFIX + "MESSAGE_DELIVERED";
-
- public static final String MAIL_MESSAGE_NOT_DELIVERED = PREFIX + "MESSAGE_NOT_DELIVERED";
-
- public static final String MAIL_MESSAGE_PARTIALLY_DELIVERED = PREFIX + "MESSAGE_PARTIALLY_DELIVERED";
-
- public static final String MAIL_INVALID_ADDRESS = "mail.invalid.address";
-
- public static final String MAIL_VALID_SENT_ADDRESS = "mail.valid.sent.address";
-
- public static final String MAIL_VALID_UNSENT_ADDRESS = "mail.valid.unsent.address";
-
- public static final String MAIL_FROM_ADDRESS = "mail.from.address";
-
- public static final String MAIL_DATE_SENT = "mail.date.sent";
-
- public static final String MAIL_SUBJECT = "mail.subject";
-
- public static final String MAIL_UUID = "mail.uuid";
-
- public static final String MAIL_UUID_HEADER = "OSEE-MAIL-UUID";
-
public static final String NAMESPACE = "mail.server";
private static String qualify(String value) {
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailMessageFactory.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailMessageFactory.java
index 7e7662309e7..0cbba3bc48d 100644
--- a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailMessageFactory.java
+++ b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailMessageFactory.java
@@ -14,16 +14,15 @@ import java.io.PrintStream;
import java.util.Collection;
import java.util.Date;
import java.util.Properties;
-import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import javax.activation.DataHandler;
-import javax.activation.DataSource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
+import javax.mail.event.TransportEvent;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
@@ -31,15 +30,16 @@ import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.logger.Log;
-import org.eclipse.osee.mail.MailConstants;
-import org.eclipse.osee.mail.MailMessage;
-import org.eclipse.osee.mail.MailUtils;
+import org.eclipse.osee.mail.api.MailMessage;
+import org.eclipse.osee.mail.api.MailStatus;
/**
* @author Roberto E. Escobar
*/
public class MailMessageFactory {
+ public static final String MAIL_UUID_HEADER = "OSEE-MAIL-UUID";
+
private final Log logger;
private final AtomicInteger testCounter;
@@ -84,14 +84,14 @@ public class MailMessageFactory {
return transport;
}
- public MimeMessage createMimeMessage(Session session, MailMessage email) throws AddressException, MessagingException {
+ public MimeMessage createMimeMessage(Session session, MailMessage email, Date sendDate) throws AddressException, MessagingException {
MimeMessage message = new MimeMessage(session);
message.setFrom(toAddress(email.getFrom()));
message.setSubject(email.getSubject(), "UTF-8");
message.setReplyTo(toAddress(email.getReplyTo()));
message.setRecipients(Message.RecipientType.TO, toAddress(email.getRecipients()));
- message.addHeader(MailConstants.MAIL_UUID_HEADER, email.getId());
- message.setSentDate(new Date());
+ message.addHeader(MAIL_UUID_HEADER, email.getId());
+ message.setSentDate(sendDate);
Multipart multiPart = new MimeMultipart();
for (DataHandler handler : email.getAttachments()) {
@@ -104,25 +104,55 @@ public class MailMessageFactory {
return message;
}
- public MailMessage createTestMessage(String adminEmail, String subject, String plainTextBody) {
- MailMessage message = new MailMessage();
- message.setId(UUID.randomUUID().toString());
- message.setFrom(adminEmail);
- message.getRecipients().add(adminEmail);
-
+ public MailMessage createTestMessage(String adminEmail, String subject, String text) {
String subjectWithCounter = String.format("%s #%s", subject, testCounter.incrementAndGet());
- message.setSubject(subjectWithCounter);
+ String html = String.format("<html><body><big>%s</big><p>%s</p></body></html>", subjectWithCounter, text);
+
+ MailMessage message = MailMessage.newBuilder() //
+ .from(adminEmail) //
+ .recipient(adminEmail) //
+ .replyTo(adminEmail) //
+ .subject(subjectWithCounter) //
+ .addHtml(html) //
+ .build();
+ return message;
+ }
- String htmlData =
- String.format("<html><body><big>%s</big><p>%s</p></body></html>", subjectWithCounter, plainTextBody);
- DataSource source;
+ public MailStatus createMailStatus(TransportEvent event) {
+ MailStatus status = new MailStatus();
+ status.setVerified(true);
+ Message message = event.getMessage();
+ String uuid;
try {
- source = MailUtils.createAlternativeDataSource("test.email", htmlData, plainTextBody);
+ String[] header = message.getHeader(MAIL_UUID_HEADER);
+ uuid = header.length == 0 ? "-1" : header[0];
} catch (Exception ex) {
- source = MailUtils.createFromString("test.email", plainTextBody);
+ uuid = ex.getMessage();
}
- message.addAttachment(source);
- return message;
+ status.setUuid(uuid);
+ try {
+ Date sentDate = message.getSentDate();
+ status.setDateSent(sentDate);
+ } catch (Exception ex) {
+ // Do nothing
+ }
+ String subject;
+ try {
+ subject = message.getSubject();
+ } catch (Exception ex) {
+ subject = ex.getMessage();
+ }
+ status.setSubject(subject);
+
+ try {
+ addAddress(status.getFromAddress(), message.getFrom());
+ } catch (Exception ex) {
+ //
+ }
+ addAddress(status.getInvalidAddress(), event.getInvalidAddresses());
+ addAddress(status.getSentAddress(), event.getValidSentAddresses());
+ addAddress(status.getUnsentAddress(), event.getValidUnsentAddresses());
+ return status;
}
private Address[] toAddress(Collection<String> rawAddresses) throws AddressException {
@@ -138,4 +168,12 @@ public class MailMessageFactory {
return new InternetAddress(rawAddress);
}
+ private static void addAddress(Collection<String> data, Address... addresses) {
+ if (addresses != null) {
+ for (Address address : addresses) {
+ data.add(address.toString());
+ }
+ }
+ }
+
}
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailServiceImpl.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailServiceImpl.java
index e2ffcf9c6fe..9433d1f53a1 100644
--- a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailServiceImpl.java
+++ b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailServiceImpl.java
@@ -11,49 +11,54 @@
package org.eclipse.osee.mail.internal;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
+import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
-import javax.mail.event.TransportListener;
-import org.eclipse.osee.event.EventService;
+import javax.activation.CommandMap;
+import javax.activation.MailcapCommandMap;
+import org.eclipse.osee.executor.admin.ExecutionCallback;
+import org.eclipse.osee.executor.admin.ExecutorAdmin;
+import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.logger.Log;
-import org.eclipse.osee.mail.MailConfiguration;
-import org.eclipse.osee.mail.MailMessage;
-import org.eclipse.osee.mail.MailService;
-import org.eclipse.osee.mail.SendMailStatus;
+import org.eclipse.osee.mail.api.MailCallback;
+import org.eclipse.osee.mail.api.MailMessage;
+import org.eclipse.osee.mail.api.MailService;
+import org.eclipse.osee.mail.api.MailStatus;
/**
* @author Roberto E. Escobar
*/
public class MailServiceImpl implements MailService {
- private EventService eventService;
+ private static final MailCallback NO_OP_CALLBACK = null;
+
+ private ExecutorAdmin executorAdmin;
private Log logger;
private MailMessageFactory factory;
- private TransportListener[] listeners;
private volatile MailConfiguration config;
public void setLogger(Log logger) {
this.logger = logger;
}
- public void setEventService(EventService eventService) {
- this.eventService = eventService;
+ public void setExecutorAdmin(ExecutorAdmin executorAdmin) {
+ this.executorAdmin = executorAdmin;
}
public void start(Map<String, Object> props) {
logger.trace("Starting [%s]...", getClass().getSimpleName());
- update(props);
+ initJavaxMailRuntime();
AtomicInteger testCounter = new AtomicInteger();
factory = new MailMessageFactory(logger, testCounter);
- listeners = new TransportListener[] {new MailTransportListener(eventService)};
+ update(props);
}
public void stop(Map<String, Object> props) {
logger.trace("Stopping [%s]...", getClass().getSimpleName());
- listeners = null;
factory = null;
config = null;
}
@@ -63,23 +68,133 @@ public class MailServiceImpl implements MailService {
config = MailConfiguration.newConfig(props);
}
+ private void initJavaxMailRuntime() {
+ MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
+ mc.addMailcap("text/*;; x-java-content-handler=com.sun.mail.handlers.text_plain");
+ mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
+ mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
+ mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
+ mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
+ mc.addMailcap("multipart/mixed;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
+ mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
+ mc.addMailcap("image/jpeg;; x-java-content-handler=com.sun.mail.handlers.image_jpeg");
+ mc.addMailcap("image/gif;; x-java-content-handler=com.sun.mail.handlers.image_gif");
+ CommandMap.setDefaultCommandMap(mc);
+ }
+
+ private MailMessage newTestMailMessage() {
+ String adminEmail = config.getAdminEmail();
+ String subject = config.getTestEmailSubject();
+ String body = config.getTestEmailBody();
+ return factory.createTestMessage(adminEmail, subject, body);
+ }
+
@Override
- public List<Callable<SendMailStatus>> createSendCalls(MailMessage... emails) {
- final MailConfiguration configCopy = config.copy();
- long waitForStatus = configCopy.getStatusWaitTime();
- List<Callable<SendMailStatus>> callables = new ArrayList<Callable<SendMailStatus>>();
- for (MailMessage mail : emails) {
- callables.add(new SendMailCallable(configCopy, factory, mail, waitForStatus, listeners));
+ public MailStatus sendTestMessage() {
+ MailMessage message = newTestMailMessage();
+ List<MailStatus> results = sendMessages(message);
+ return results.isEmpty() ? null : results.iterator().next();
+ }
+
+ @Override
+ public Future<MailStatus> sendAsyncTestMessage() {
+ return sendAsyncTestMessage(NO_OP_CALLBACK);
+ }
+
+ @Override
+ public Future<MailStatus> sendAsyncTestMessage(MailCallback callback) {
+ MailMessage message = newTestMailMessage();
+ List<Future<MailStatus>> results = sendAsyncMessages(callback, message);
+ return results.isEmpty() ? null : results.iterator().next();
+ }
+
+ @Override
+ public List<MailStatus> sendMessages(MailMessage... email) {
+ return sendMessages(Arrays.asList(email));
+ }
+
+ @Override
+ public List<MailStatus> sendMessages(Iterable<MailMessage> emails) {
+ List<Future<MailStatus>> futures = sendAsyncMessages(emails);
+ List<MailStatus> toReturn = new ArrayList<MailStatus>();
+ for (Future<MailStatus> future : futures) {
+ toReturn.add(executeAndGetStatus(future));
}
- return callables;
+ return toReturn;
}
@Override
- public MailMessage createSystemTestMessage() {
- final MailConfiguration configCopy = config.copy();
- String adminEmail = configCopy.getAdminEmail();
- String subject = configCopy.getTestEmailSubject();
- String body = configCopy.getTestEmailBody();
- return factory.createTestMessage(adminEmail, subject, body);
+ public List<Future<MailStatus>> sendAsyncMessages(MailMessage... email) {
+ return sendAsyncMessages(NO_OP_CALLBACK, email);
}
+
+ @Override
+ public List<Future<MailStatus>> sendAsyncMessages(Iterable<MailMessage> emails) {
+ return sendAsyncMessages(NO_OP_CALLBACK, emails);
+ }
+
+ @Override
+ public List<Future<MailStatus>> sendAsyncMessages(MailCallback callback, MailMessage... email) {
+ return sendAsyncMessages(callback, Arrays.asList(email));
+ }
+
+ private MailStatus executeAndGetStatus(Future<MailStatus> future) {
+ MailStatus toReturn;
+ try {
+ toReturn = future.get();
+ } catch (Exception ex) {
+ toReturn = new MailStatus();
+ toReturn.setSubject(Lib.exceptionToString(ex));
+ }
+ return toReturn;
+ }
+
+ @Override
+ public List<Future<MailStatus>> sendAsyncMessages(MailCallback mailCallback, Iterable<MailMessage> emails) {
+ List<Future<MailStatus>> futures = new ArrayList<Future<MailStatus>>();
+ for (MailMessage mail : emails) {
+ String uuid = mail.getId();
+
+ Callable<MailStatus> callable = newSendCallable(mail);
+ ExecutionCallback<MailStatus> callback = null;
+ if (mailCallback != null) {
+ callback = newExecutionCallback(uuid, mailCallback);
+ }
+ try {
+ Future<MailStatus> future = executorAdmin.schedule(callable, callback);
+ futures.add(future);
+ } catch (Exception ex) {
+ if (mailCallback != null) {
+ mailCallback.onFailure(uuid, ex);
+ }
+ logger.error(ex, "Error sending email [%s] ", uuid);
+ }
+ }
+ return futures;
+ }
+
+ private Callable<MailStatus> newSendCallable(MailMessage mail) {
+ return new SendMailCallable(config, factory, mail);
+ }
+
+ private static ExecutionCallback<MailStatus> newExecutionCallback(final String mailId, final MailCallback mailCallback) {
+ return new ExecutionCallback<MailStatus>() {
+
+ @Override
+ public void onCancelled() {
+ mailCallback.onCancelled(mailId);
+ }
+
+ @Override
+ public void onSuccess(MailStatus status) {
+ mailCallback.onSuccess(mailId, status);
+ }
+
+ @Override
+ public void onFailure(Throwable throwable) {
+ mailCallback.onFailure(mailId, throwable);
+ }
+ };
+ }
+
}
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailTransportListener.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailTransportListener.java
deleted file mode 100644
index 08f6f220d16..00000000000
--- a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailTransportListener.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2004, 2007 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-package org.eclipse.osee.mail.internal;
-
-import java.util.Map;
-import javax.mail.event.TransportEvent;
-import javax.mail.event.TransportListener;
-import org.eclipse.osee.event.EventService;
-import org.eclipse.osee.mail.MailConstants;
-import org.eclipse.osee.mail.MailEventUtil;
-
-/**
- * @author Roberto E. Escobar
- */
-public final class MailTransportListener implements TransportListener {
-
- private final EventService eventService;
-
- public MailTransportListener(EventService eventService) {
- this.eventService = eventService;
- }
-
- @Override
- public void messageDelivered(TransportEvent event) {
- Map<String, String> data = MailEventUtil.createTransportEventData(event);
- eventService.postEvent(MailConstants.MAIL_MESSAGE_DELIVERED, data);
- }
-
- @Override
- public void messageNotDelivered(TransportEvent event) {
- Map<String, String> data = MailEventUtil.createTransportEventData(event);
- eventService.postEvent(MailConstants.MAIL_MESSAGE_NOT_DELIVERED, data);
- }
-
- @Override
- public void messagePartiallyDelivered(TransportEvent event) {
- Map<String, String> data = MailEventUtil.createTransportEventData(event);
- eventService.postEvent(MailConstants.MAIL_MESSAGE_PARTIALLY_DELIVERED, data);
- }
-
-} \ No newline at end of file
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/SendMailCallable.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/SendMailCallable.java
index dbb6e5b4bd2..94c52856aad 100644
--- a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/SendMailCallable.java
+++ b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/SendMailCallable.java
@@ -10,62 +10,49 @@
*******************************************************************************/
package org.eclipse.osee.mail.internal;
+import java.util.Date;
import java.util.concurrent.Callable;
-import javax.activation.CommandMap;
-import javax.activation.MailcapCommandMap;
+import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
+import javax.mail.event.TransportEvent;
import javax.mail.event.TransportListener;
import javax.mail.internet.MimeMessage;
-import org.eclipse.osee.mail.MailConfiguration;
-import org.eclipse.osee.mail.MailMessage;
-import org.eclipse.osee.mail.MailUtils;
-import org.eclipse.osee.mail.SendMailStatus;
-import org.eclipse.osee.mail.SendMailStatus.MailStatus;
+import org.eclipse.osee.mail.api.MailMessage;
+import org.eclipse.osee.mail.api.MailStatus;
/**
* @author Roberto E. Escobar
*/
-public class SendMailCallable implements Callable<SendMailStatus> {
+public class SendMailCallable implements Callable<MailStatus> {
private final MailConfiguration config;
private final MailMessage email;
private final MailMessageFactory factory;
- private final TransportListener[] listeners;
- private final long waitForStatus;
- public SendMailCallable(MailConfiguration config, MailMessageFactory factory, MailMessage email, long waitForStatus, TransportListener... listeners) {
+ public SendMailCallable(MailConfiguration config, MailMessageFactory factory, MailMessage email) {
this.config = config;
this.factory = factory;
this.email = email;
- this.listeners = listeners;
- this.waitForStatus = waitForStatus;
}
@Override
- public SendMailStatus call() throws Exception {
- MailcapCommandMap mc = MailUtils.getMailcapCommandMap();
- CommandMap.setDefaultCommandMap(mc);
-
+ public MailStatus call() throws Exception {
String transportProtocol = config.getTransport();
String host = config.getHost();
int port = config.getPort();
boolean requiresAuthentication = config.isAuthenticationRequired();
String username = config.getUserName();
String password = config.getPassword();
+ long waitForStatus = config.getStatusWaitTime();
- final Session session = factory.createSession(transportProtocol, host, port, requiresAuthentication);
- final MimeMessage message = factory.createMimeMessage(session, email);
- final Transport transport = factory.createTransport(session, username, password);
- final SendMailStatus status = new SendMailStatus();
+ Date sendDate = new Date();
+ Session session = factory.createSession(transportProtocol, host, port, requiresAuthentication);
+ MimeMessage message = factory.createMimeMessage(session, email, sendDate);
+ Transport transport = factory.createTransport(session, username, password);
- StatusTransportListener statusListener = new StatusTransportListener(status);
+ StatusListener statusListener = new StatusListener();
transport.addTransportListener(statusListener);
- if (listeners != null) {
- for (TransportListener listener : listeners) {
- transport.addTransportListener(listener);
- }
- }
try {
message.saveChanges();
transport.sendMessage(message, message.getAllRecipients());
@@ -73,22 +60,59 @@ public class SendMailCallable implements Callable<SendMailStatus> {
statusListener.wait(waitForStatus);
}
} finally {
- if (listeners != null) {
- for (TransportListener listener : listeners) {
- transport.removeTransportListener(listener);
+ try {
+ transport.removeTransportListener(statusListener);
+ } finally {
+ try {
+ transport.close();
+ } catch (MessagingException ex) {
+ // Do nothing;
}
}
- transport.removeTransportListener(statusListener);
- transport.close();
}
- if (waitForStatus <= 0 || !statusListener.wasUpdateReceived()) {
- MailStatus mStatus = new MailStatus();
- mStatus.setDateSent(message.getSentDate());
- mStatus.setSubject(message.getSubject());
- mStatus.setUuid(message.getMessageID());
- mStatus.setVerified(false);
- status.add(mStatus);
+ MailStatus status = statusListener.getMailStatus();
+ if (waitForStatus <= 0 || status == null) {
+ status = new MailStatus();
+ status.setDateSent(message.getSentDate());
+ status.setSubject(message.getSubject());
+ status.setUuid(message.getMessageID());
+ status.setVerified(false);
}
return status;
}
+
+ private class StatusListener implements TransportListener {
+
+ private MailStatus status;
+
+ public MailStatus getMailStatus() {
+ return status;
+ }
+
+ @Override
+ public void messageDelivered(TransportEvent event) {
+ handleEvent(event);
+ }
+
+ @Override
+ public void messageNotDelivered(TransportEvent event) {
+ handleEvent(event);
+ }
+
+ @Override
+ public void messagePartiallyDelivered(TransportEvent event) {
+ handleEvent(event);
+ }
+
+ private void handleEvent(TransportEvent event) {
+ synchronized (this) {
+ try {
+ status = factory.createMailStatus(event);
+ } finally {
+ notify();
+ }
+ }
+ }
+ }
+
}
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/StatusTransportListener.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/StatusTransportListener.java
deleted file mode 100644
index 810dbc131df..00000000000
--- a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/StatusTransportListener.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2004, 2007 Boeing.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Boeing - initial API and implementation
- *******************************************************************************/
-package org.eclipse.osee.mail.internal;
-
-import javax.mail.event.TransportEvent;
-import javax.mail.event.TransportListener;
-import org.eclipse.osee.mail.MailEventUtil;
-import org.eclipse.osee.mail.SendMailStatus;
-
-/**
- * @author Roberto E. Escobar
- */
-public class StatusTransportListener implements TransportListener {
-
- private final SendMailStatus status;
- private volatile boolean wasUpdateReceived;
-
- public StatusTransportListener(SendMailStatus status) {
- this.status = status;
- this.wasUpdateReceived = false;
- }
-
- @Override
- public void messageDelivered(TransportEvent event) {
- handleEvent(event);
- }
-
- @Override
- public void messageNotDelivered(TransportEvent event) {
- handleEvent(event);
- }
-
- @Override
- public void messagePartiallyDelivered(TransportEvent event) {
- handleEvent(event);
- }
-
- private void handleEvent(TransportEvent event) {
- synchronized (this) {
- try {
- MailEventUtil.loadStatus(status, event);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- wasUpdateReceived = true;
- notify();
- }
- }
-
- public synchronized boolean wasUpdateReceived() {
- return wasUpdateReceived;
- }
-
-} \ No newline at end of file
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/resources/MailApplication.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/resources/MailApplication.java
index 3f5e4938f68..cf226af5e2f 100644
--- a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/resources/MailApplication.java
+++ b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/resources/MailApplication.java
@@ -14,7 +14,7 @@ import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
-import org.eclipse.osee.mail.MailService;
+import org.eclipse.osee.mail.api.MailService;
/**
* @author Roberto E. Escobar
diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/resources/MailResource.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/resources/MailResource.java
index a6e7282d94b..32da8e49029 100644
--- a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/resources/MailResource.java
+++ b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/resources/MailResource.java
@@ -10,31 +10,16 @@
*******************************************************************************/
package org.eclipse.osee.mail.internal.resources;
-import java.util.ArrayList;
-import java.util.Collection;
import java.util.List;
-import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-import java.util.concurrent.TimeUnit;
-import javax.activation.DataHandler;
-import javax.ws.rs.Consumes;
-import javax.ws.rs.GET;
-import javax.ws.rs.POST;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.core.MediaType;
-import org.eclipse.osee.mail.MailMessage;
-import org.eclipse.osee.mail.MailService;
-import org.eclipse.osee.mail.SendMailStatus;
+import org.eclipse.osee.mail.api.MailEndpoint;
+import org.eclipse.osee.mail.api.MailMessage;
+import org.eclipse.osee.mail.api.MailService;
+import org.eclipse.osee.mail.api.MailStatus;
/**
* @author Roberto E. Escobar
*/
-@Path("send")
-public class MailResource {
+public class MailResource implements MailEndpoint {
private final MailService mailService;
@@ -43,50 +28,14 @@ public class MailResource {
this.mailService = mailService;
}
- @GET
- @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
- public MailMessage getTestMailMessage() {
- return mailService.createSystemTestMessage();
+ @Override
+ public MailStatus sendTestMail() {
+ return mailService.sendTestMessage();
}
- @POST
- @Path("test")
- @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
- public SendMailStatus sendTestMail() throws Exception {
- MailMessage message = mailService.createSystemTestMessage();
-
- List<SendMailStatus> results = sendMail(message);
- return results.iterator().next();
- }
-
- @POST
- @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
- @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
- public List<SendMailStatus> sendXmlMail(MailMessage mailMessage) throws Exception {
- Collection<? extends DataHandler> handlers = mailMessage.getAttachments();
- for (DataHandler handler : handlers) {
- System.out.println(handler.getName() + " " + handler.getContentType());
- }
- return sendMail(mailMessage);
- }
-
- private List<SendMailStatus> sendMail(MailMessage... messages) throws InterruptedException, ExecutionException {
- List<Callable<SendMailStatus>> calls = mailService.createSendCalls(messages);
- List<Future<SendMailStatus>> futures = new ArrayList<Future<SendMailStatus>>();
-
- if (messages.length > 0) {
- ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
- for (Callable<SendMailStatus> task : calls) {
- Future<SendMailStatus> future = executor.submit(task);
- futures.add(future);
- }
- executor.shutdown();
- executor.awaitTermination(100, TimeUnit.MINUTES);
- }
- List<SendMailStatus> results = new ArrayList<SendMailStatus>();
- for (Future<SendMailStatus> future : futures) {
- results.add(future.get());
- }
- return results;
+ @Override
+ public MailStatus[] sendMail(MailMessage mailMessage) {
+ List<MailStatus> results = mailService.sendMessages(mailMessage);
+ return results.toArray(new MailStatus[0]);
}
}

Back to the top