diff options
author | rescobar | 2011-09-20 19:52:26 +0000 |
---|---|---|
committer | Ryan D. Brooks | 2011-09-20 19:52:26 +0000 |
commit | 7035596c9237da7042db9bee5ca00d2774759665 (patch) | |
tree | c7debb69d98778e34aa3c7a57a7304a50e693e87 /plugins/org.eclipse.osee.mail | |
parent | 17f56c2b7decae6ed9713c3df459003c9208b7c6 (diff) | |
download | org.eclipse.osee-7035596c9237da7042db9bee5ca00d2774759665.tar.gz org.eclipse.osee-7035596c9237da7042db9bee5ca00d2774759665.tar.xz org.eclipse.osee-7035596c9237da7042db9bee5ca00d2774759665.zip |
feature[ats_Q9NLC]: Remove org.eclipse.osee.mail.admin bundle
Diffstat (limited to 'plugins/org.eclipse.osee.mail')
9 files changed, 428 insertions, 4 deletions
diff --git a/plugins/org.eclipse.osee.mail/.project b/plugins/org.eclipse.osee.mail/.project index ebfd15fd1cd..9afcd2ff090 100644 --- a/plugins/org.eclipse.osee.mail/.project +++ b/plugins/org.eclipse.osee.mail/.project @@ -20,6 +20,11 @@ <arguments> </arguments> </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ds.core.builder</name> + <arguments> + </arguments> + </buildCommand> </buildSpec> <natures> <nature>org.eclipse.pde.PluginNature</nature> diff --git a/plugins/org.eclipse.osee.mail/META-INF/MANIFEST.MF b/plugins/org.eclipse.osee.mail/META-INF/MANIFEST.MF index b0f27ed4b79..c86d8cab496 100644 --- a/plugins/org.eclipse.osee.mail/META-INF/MANIFEST.MF +++ b/plugins/org.eclipse.osee.mail/META-INF/MANIFEST.MF @@ -11,6 +11,7 @@ Import-Package: javax.activation, javax.mail.internet;version="1.4.1", javax.xml.bind.annotation, org.eclipse.osee.framework.jdk.core.util, - org.eclipse.osee.framework.jdk.core.util.windows, - org.osgi.framework + org.eclipse.osee.framework.jdk.core.util.windows +Service-Component: OSGI-INF/*.xml Export-Package: org.eclipse.osee.mail +Require-Bundle: org.eclipse.osee.event diff --git a/plugins/org.eclipse.osee.mail/OSGI-INF/mail.service.xml b/plugins/org.eclipse.osee.mail/OSGI-INF/mail.service.xml new file mode 100644 index 00000000000..f0698c3fd31 --- /dev/null +++ b/plugins/org.eclipse.osee.mail/OSGI-INF/mail.service.xml @@ -0,0 +1,14 @@ +<?xml version="1.0" encoding="UTF-8"?> +<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="start" deactivate="stop" enabled="true" name="org.eclipse.osee.mail.internal.MailServiceImpl"> + <implementation class="org.eclipse.osee.mail.internal.MailServiceImpl"/> + + <reference interface="org.eclipse.osee.event.EventService" name="EventService" + cardinality="1..1" + policy="static" + bind="setEventService" /> + + <service> + <provide interface="org.eclipse.osee.mail.MailService"/> + </service> + +</scr:component> diff --git a/plugins/org.eclipse.osee.mail/build.properties b/plugins/org.eclipse.osee.mail/build.properties index fc412f6afa1..74d5509ca51 100644 --- a/plugins/org.eclipse.osee.mail/build.properties +++ b/plugins/org.eclipse.osee.mail/build.properties @@ -1,6 +1,8 @@ source.. = src/ output.. = bin/ bin.includes = META-INF/,\ - . + .,\ + OSGI-INF/ additional.bundles = javax.mail.glassfish,\ - org.eclipse.osee.framework.jdk.core + org.eclipse.osee.framework.jdk.core,\ + org.eclipse.osee.event 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 new file mode 100644 index 00000000000..8832232753b --- /dev/null +++ b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailMessageFactory.java @@ -0,0 +1,104 @@ +/******************************************************************************* + * 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.Collection; +import java.util.Date; +import java.util.Properties; +import javax.activation.DataHandler; +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.internet.AddressException; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeBodyPart; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; +import org.eclipse.osee.mail.MailConstants; +import org.eclipse.osee.mail.MailMessage; +import org.eclipse.osee.mail.MailServiceConfig; + +/** + * @author Roberto E. Escobar + */ +public class MailMessageFactory { + + private final MailServiceConfig config; + + public MailMessageFactory(MailServiceConfig config) { + this.config = config; + } + + public Session createSession() { + final Properties props = System.getProperties(); + + String transport = config.getTransport(); + props.put("mail.transport.protocol", transport); + props.put("mail." + transport + ".host", config.getHost()); + props.put("mail." + transport + ".port", String.valueOf(config.getPort())); + props.put("mail." + transport + ".auth", String.valueOf(config.isAuthenticationRequired()).toLowerCase()); + props.put("mail.debug", config.isDebug()); + + Session session = Session.getDefaultInstance(props); + session.setDebug(config.isDebug()); + session.setDebugOut(System.out); + return session; + } + + public Transport createTransport(Session session) throws MessagingException { + Transport transport = session.getTransport(); + if (!transport.isConnected()) { + if (config.isAuthenticationRequired()) { + transport.connect(config.getUserName(), config.getPassword()); + } else { + transport.connect(); + } + } + return transport; + } + + public MimeMessage createMimeMessage(Session session, MailMessage email) 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()); + + Multipart multiPart = new MimeMultipart(); + for (DataHandler handler : email.getAttachments()) { + MimeBodyPart part = new MimeBodyPart(); + part.setDataHandler(handler); + multiPart.addBodyPart(part); + } + message.setContent(multiPart); + message.saveChanges(); + return message; + } + + private Address[] toAddress(Collection<String> rawAddresses) throws AddressException { + InternetAddress[] toReturn = new InternetAddress[rawAddresses.size()]; + int index = 0; + for (String rawAddress : rawAddresses) { + toReturn[index++] = toAddress(rawAddress); + } + return toReturn; + } + + private InternetAddress toAddress(String rawAddress) throws AddressException { + return new InternetAddress(rawAddress); + } + +} 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 new file mode 100644 index 00000000000..32c9295cdd1 --- /dev/null +++ b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailServiceImpl.java @@ -0,0 +1,99 @@ +/******************************************************************************* + * 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.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.Callable; +import java.util.concurrent.TimeUnit; +import javax.activation.DataSource; +import javax.mail.event.TransportListener; +import org.eclipse.osee.event.EventService; +import org.eclipse.osee.mail.MailConstants; +import org.eclipse.osee.mail.MailMessage; +import org.eclipse.osee.mail.MailService; +import org.eclipse.osee.mail.MailServiceConfig; +import org.eclipse.osee.mail.MailUtils; +import org.eclipse.osee.mail.SendMailStatus; + +/** + * @author Roberto E. Escobar + */ +public class MailServiceImpl implements MailService { + + private MailServiceConfig config; + private MailMessageFactory factory; + private EventService eventService; + private TransportListener[] listeners; + + public void setEventService(EventService eventService) { + this.eventService = eventService; + } + + public EventService getEventService() { + return eventService; + } + + @Override + public MailServiceConfig getConfiguration() { + return config; + } + + @Override + public void setConfiguration(MailServiceConfig config) { + this.config.setTo(config); + } + + public synchronized void start(Map<String, ?> props) { + config = new MailServiceConfig(); + factory = new MailMessageFactory(config); + getEventService().postEvent(MailConstants.REGISTRATION_EVENT, props); + + listeners = new TransportListener[] {new MailTransportListener(eventService)}; + } + + public synchronized void stop(Map<String, ?> props) { + getEventService().postEvent(MailConstants.DEREGISTRATION_EVENT, props); + listeners = null; + } + + @Override + public List<Callable<SendMailStatus>> createSendCalls(long waitForStatus, TimeUnit timeUnit, MailMessage... emails) { + List<Callable<SendMailStatus>> callables = new ArrayList<Callable<SendMailStatus>>(); + for (MailMessage mail : emails) { + callables.add(new SendMailCallable(factory, mail, waitForStatus, timeUnit, listeners)); + } + return callables; + } + + @Override + public MailMessage createSystemTestMessage(int testNumber) { + MailServiceConfig config = getConfiguration(); + MailMessage message = new MailMessage(); + message.setId(UUID.randomUUID().toString()); + message.setFrom(config.getSystemAdminEmailAddress()); + message.setSubject(String.format("Test email #%s", testNumber)); + message.getRecipients().add(config.getSystemAdminEmailAddress()); + + String plainText = String.format("This is test email %s sent from org.eclipse.osee.mail.admin", testNumber); + String htmlData = String.format("<html><body><h4>%s</h4></body></html>", plainText); + DataSource source; + try { + source = MailUtils.createAlternativeDataSource("TestEmail", htmlData, plainText); + } catch (Exception ex) { + source = MailUtils.createFromString("TestEmail", plainText); + } + message.addAttachment(source); + return message; + } +} 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 new file mode 100644 index 00000000000..08f6f220d16 --- /dev/null +++ b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/MailTransportListener.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * 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 new file mode 100644 index 00000000000..bf46158f6c5 --- /dev/null +++ b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/SendMailCallable.java @@ -0,0 +1,88 @@ +/******************************************************************************* + * 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.concurrent.Callable; +import java.util.concurrent.TimeUnit; +import javax.activation.CommandMap; +import javax.activation.MailcapCommandMap; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.event.TransportListener; +import javax.mail.internet.MimeMessage; +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; + +/** + * @author Roberto E. Escobar + */ +public class SendMailCallable implements Callable<SendMailStatus> { + + private final MailMessage email; + private final MailMessageFactory factory; + private final TransportListener[] listeners; + private final long waitForStatus; + private final TimeUnit timeUnit; + + public SendMailCallable(MailMessageFactory factory, MailMessage email, long waitForStatus, TimeUnit timeUnit, TransportListener... listeners) { + this.factory = factory; + this.email = email; + this.listeners = listeners; + this.waitForStatus = waitForStatus; + this.timeUnit = timeUnit; + } + + @Override + public SendMailStatus call() throws Exception { + MailcapCommandMap mc = MailUtils.getMailcapCommandMap(); + CommandMap.setDefaultCommandMap(mc); + + final Session session = factory.createSession(); + final MimeMessage message = factory.createMimeMessage(session, email); + final Transport transport = factory.createTransport(session); + final SendMailStatus status = new SendMailStatus(); + + StatusTransportListener statusListener = new StatusTransportListener(status); + transport.addTransportListener(statusListener); + if (listeners != null) { + for (TransportListener listener : listeners) { + transport.addTransportListener(listener); + } + } + try { + message.saveChanges(); + transport.sendMessage(message, message.getAllRecipients()); + synchronized (statusListener) { + long waitMillis = timeUnit.toMillis(waitForStatus); + statusListener.wait(waitMillis); + } + } finally { + if (listeners != null) { + for (TransportListener listener : listeners) { + transport.removeTransportListener(listener); + } + } + 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); + } + return status; + } +} 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 new file mode 100644 index 00000000000..810dbc131df --- /dev/null +++ b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/internal/StatusTransportListener.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * 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 |