INTEXT-6 - Add AWS SES Samples

* Add SES JavaMailSender Sample
* Add SES Spring Integration Sample
* Refactor DefaultAmazonSESMailSender to implement JavaMailSender and delegate to JavaMailSenderImpl rather than extending it
* Cleanup
This commit is contained in:
Gunnar Hillert
2013-01-17 17:14:54 -05:00
parent 639d4b27e0
commit 897fc0e9b4
34 changed files with 929 additions and 311 deletions

View File

@@ -0,0 +1,4 @@
AWS SES Spring Integration Sample
=========================

View File

@@ -0,0 +1,84 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.integration.samples</groupId>
<artifactId>mail-ses-integration</artifactId>
<name>AWS SES Mail Demo</name>
<version>1.0.0.BUILD-SNAPSHOT</version>
<prerequisites>
<maven>2.2.1</maven>
</prerequisites>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.integration.version>2.2.0.RELEASE</spring.integration.version>
<log4j.version>1.2.17</log4j.version>
<junit.version>4.10</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mail</artifactId>
<version>${spring.integration.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-test</artifactId>
<version>${spring.integration.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-aws</artifactId>
<version>0.5.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- test-scoped dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.subethamail</groupId>
<artifactId>subethasmtp-wiser</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.springframework.integration.samples.mailses.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>repo.springsource.org.milestone</id>
<name>SpringSource Maven Milestone Repository</name>
<url>https://repo.springsource.org/libs-milestone</url>
</repository>
</repositories>
</project>

View File

@@ -0,0 +1,24 @@
package org.springframework.integration.samples.mailses;
import org.springframework.integration.annotation.Header;
import org.springframework.integration.annotation.Payload;
import org.springframework.integration.mail.MailHeaders;
public interface EmailService {
void send(
@Header(MailHeaders.FROM)
String fromEmail,
@Header(MailHeaders.TO)
String toEmail,
@Header(MailHeaders.SUBJECT)
String subject,
@Payload
String body);
}

View File

@@ -0,0 +1,101 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.mailses;
import java.util.Scanner;
import org.apache.log4j.Logger;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
/**
* Starts the Spring Context and will initialize the Spring Integration routes.
*
* @author Gunnar Hillert
* @since 1.0
*
*/
public final class Main {
private static final Logger LOGGER = Logger.getLogger(Main.class);
private static final String HORIZONTAL_LINE = "\n=========================================================";
private Main() {
}
/**
* Load the Spring Integration Application Context
*
* @param args - command line arguments
*/
public static void main(final String... args) {
final Scanner scanner = new Scanner(System.in);
LOGGER.info(HORIZONTAL_LINE + "\n"
+ "\n Welcome to Spring Integration! "
+ "\n"
+ "\n For more information please visit: "
+ "\n http://www.springsource.org/spring-integration "
+ "\n" + HORIZONTAL_LINE);
final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
final ConfigurableEnvironment environment = context.getEnvironment();
final String fromEmailAddress;
final String toEmailAddress;
final String subject;
final String body;
System.out.print("\nFrom which email address would you like to send a message?: ");
fromEmailAddress = scanner.nextLine();
System.out.print("To which email address would you like to send a message?: ");
toEmailAddress = scanner.nextLine();
System.out.print("What is the subject line?: ");
subject = scanner.nextLine();
System.out.print("What is the body of the message?: ");
body = scanner.nextLine();
if (!environment.containsProperty("accessKey")) {
System.out.print("Please enter your access key: ");
final String accessKey = scanner.nextLine();
environment.getSystemProperties().put("accessKey", accessKey);
}
if (!environment.containsProperty("secretKey")) {
System.out.print("Please enter your secret key: ");
final String secretKey = scanner.nextLine();
environment.getSystemProperties().put("secretKey", secretKey);
}
context.load("classpath:META-INF/spring/integration/*-context.xml");
context.registerShutdownHook();
context.refresh();
final EmailService emailService = context.getBean(EmailService.class);
emailService.send(fromEmailAddress, toEmailAddress, subject, body);
System.out.println(String.format("The email to '%s' was sent successfully.", toEmailAddress));
System.exit(0);
}
}

View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-file="http://www.springframework.org/schema/integration/file"
xmlns:int-mail="http://www.springframework.org/schema/integration/mail"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:int-aws="http://www.springframework.org/schema/integration/aws"
xsi:schemaLocation="http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/integration/aws http://www.springframework.org/schema/integration/aws/spring-integration-aws.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder />
<bean id="tcpIpUtils" class="org.springframework.integration.test.util.SocketUtils" />
<bean id="serverPort" class="java.lang.Integer">
<constructor-arg value="#{tcpIpUtils.findAvailableServerSocket(12000)}"/>
</bean>
<int:gateway id="emailService" service-interface="org.springframework.integration.samples.mailses.EmailService">
<int:method name="send" request-channel="inputChannel" request-timeout="5000"/>
</int:gateway>
<int:channel id="inputChannel"/>
<int-aws:ses-outbound-channel-adapter id="sesOutbound" channel="inputChannel" accessKey="${accessKey}" secretKey="${secretKey}"/>
</beans>

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss.SSS} %-5p [%t][%c] %m%n" />
</layout>
</appender>
<!-- Loggers -->
<logger name="org.springframework.integration.mail">
<level value="debug" />
</logger>
<logger name="org.springframework.integration.samples">
<level value="debug" />
</logger>
<!-- Root Logger -->
<root>
<priority value="warn" />
<appender-ref ref="console" />
</root>
</log4j:configuration>

View File

@@ -0,0 +1,5 @@
AWS SES MailSender sample
=========================
This sample uses a Spring-provided *JavaMailSender* to send emails. This application demonstrates that by only replacing the *JavaMailSender* XML bean declaration with the *DefaultAmazonSESMailSender*, existing applications can send emails using Amazon SES without changing application code. The *DefaultAmazonSESMailSender* is provided by the *Spring Integration Extensions AWS Module*.

84
samples/mail-ses/pom.xml Normal file
View File

@@ -0,0 +1,84 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.integration.samples</groupId>
<artifactId>mail-ses</artifactId>
<name>AWS SES Mail Demo</name>
<version>1.0.0.BUILD-SNAPSHOT</version>
<prerequisites>
<maven>2.2.1</maven>
</prerequisites>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.integration.version>2.2.0.RELEASE</spring.integration.version>
<log4j.version>1.2.17</log4j.version>
<junit.version>4.10</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mail</artifactId>
<version>${spring.integration.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-test</artifactId>
<version>${spring.integration.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-aws</artifactId>
<version>0.5.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- test-scoped dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.subethamail</groupId>
<artifactId>subethasmtp-wiser</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.springframework.integration.samples.mailses.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>repo.springsource.org.milestone</id>
<name>SpringSource Maven Milestone Repository</name>
<url>https://repo.springsource.org/libs-milestone</url>
</repository>
</repositories>
</project>

View File

@@ -0,0 +1,165 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.mailses;
import java.util.List;
import java.util.Scanner;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.subethamail.wiser.Wiser;
import org.subethamail.wiser.WiserMessage;
/**
* Starts the Spring Context and will initialize the Spring Integration routes.
*
* @author Gunnar Hillert
* @since 2.2
*
*/
public final class Main {
private static final Logger LOGGER = Logger.getLogger(Main.class);
private static final String HORIZONTAL_LINE = "\n=========================================================";
private Main() {
}
/**
* Load the Spring Integration Application Context
*
* @param args - command line arguments
*/
public static void main(final String... args) {
final Scanner scanner = new Scanner(System.in);
LOGGER.info(HORIZONTAL_LINE + "\n"
+ "\n Welcome to Spring Integration! "
+ "\n"
+ "\n For more information please visit: "
+ "\n http://www.springsource.org/spring-integration "
+ "\n" + HORIZONTAL_LINE);
System.out.println("Please enter a choice and press <enter>: ");
System.out.println("\t1. Use Embedded SMTP Server (Wiser)");
System.out.println("\t2. Use Amazon SES");
System.out.println("\tq. Quit the application");
System.out.print("Enter your choice: ");
final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
final ConfigurableEnvironment environment = context.getEnvironment();
boolean usingWiser = false;
String toEmailAddress;
while (true) {
final String input = scanner.nextLine();
if("1".equals(input.trim())) {
environment.setActiveProfiles("default");
usingWiser = true;
System.out.print("\nTo which email address would you like to send a message?: ");
toEmailAddress = scanner.nextLine();
break;
} else if("2".equals(input.trim())) {
environment.setActiveProfiles("aws");
if (!environment.containsProperty("accessKey")) {
System.out.print("\nPlease enter your access key: ");
final String accessKey = scanner.nextLine();
environment.getSystemProperties().put("accessKey", accessKey);
}
if (!environment.containsProperty("secretKey")) {
System.out.print("\nPlease enter your secret key: ");
final String secretKey = scanner.nextLine();
environment.getSystemProperties().put("secretKey", secretKey);
}
System.out.print("\nTo which email address would you like to send a message?: ");
toEmailAddress = scanner.nextLine();
break;
} else if("q".equals(input.trim())) {
System.out.println("Exiting application...bye.");
System.exit(0);
} else {
System.out.println("Invalid choice\n\n");
System.out.print("Enter you choice: ");
}
}
context.load("classpath:META-INF/spring/integration/*-context.xml");
context.registerShutdownHook();
context.refresh();
final JavaMailSender ms = context.getBean(JavaMailSender.class);
final String toEmailAddressToUse = toEmailAddress;
final MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
mimeMessage.setRecipient(Message.RecipientType.TO,
new InternetAddress(toEmailAddressToUse));
mimeMessage.setFrom(new InternetAddress(toEmailAddressToUse));
mimeMessage.setSubject("Testing Email - Subject");
mimeMessage.setText("Hello World");
}
};
try {
ms.send(preparator);
} catch (MailException e) {
throw new IllegalStateException(e);
}
System.out.println(String.format("The email to '%s' was sent successfully.", toEmailAddress));
if (usingWiser) {
Wiser wiser = context.getBean(Wiser.class);
List<WiserMessage> messages = wiser.getMessages();
final String from;
final String subject;
try {
from = messages.get(0).getMimeMessage().getFrom()[0].toString();
subject = messages.get(0).getMimeMessage().getSubject();
} catch (MessagingException e) {
throw new IllegalStateException(e);
}
System.out.println(String.format("Wiser received an email from '%s' with subject '%s'", from, subject));
}
System.exit(0);
}
}

View File

@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-file="http://www.springframework.org/schema/integration/file"
xmlns:int-mail="http://www.springframework.org/schema/integration/mail"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder />
<bean id="tcpIpUtils" class="org.springframework.integration.test.util.SocketUtils" />
<bean id="serverPort" class="java.lang.Integer">
<constructor-arg value="#{tcpIpUtils.findAvailableServerSocket(12000)}"/>
</bean>
<beans profile="default">
<bean id="wiser" class="org.subethamail.wiser.Wiser" init-method="start">
<property name="port" ref="serverPort"/>
</bean>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="port" ref="serverPort"/>
</bean>
</beans>
<beans profile="aws">
<bean id="mailSender" class="org.springframework.integration.aws.ses.core.DefaultAmazonSESMailSender">
<constructor-arg name="credentials">
<bean class="org.springframework.integration.aws.core.BasicAWSCredentials">
<property name="accessKey" value="${accessKey}"/>
<property name="secretKey" value="${secretKey}"/>
</bean>
</constructor-arg>
</bean>
</beans>
</beans>

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss.SSS} %-5p [%t][%c] %m%n" />
</layout>
</appender>
<!-- Loggers -->
<logger name="org.springframework.integration.mail">
<level value="debug" />
</logger>
<logger name="org.springframework.integration.samples">
<level value="debug" />
</logger>
<!-- Root Logger -->
<root>
<priority value="warn" />
<appender-ref ref="console" />
</root>
</log4j:configuration>