INTEXT-6: Add AWS core and SES adapter
For reference see: https://jira.springsource.org/browse/INTEXT-6
This commit is contained in:
committed by
Gunnar Hillert
parent
b284307878
commit
639d4b27e0
3
.gitignore
vendored
3
.gitignore
vendored
@@ -17,4 +17,5 @@ lib
|
||||
logs
|
||||
nohup.out
|
||||
out
|
||||
target
|
||||
target
|
||||
spring-integration-aws/src/test/resources/awscredentials.properties
|
||||
@@ -10,6 +10,7 @@ The Spring Integration Extensions project provides extension modules for [Spring
|
||||
* [Atmosphere][] Support ([Websockets][])
|
||||
* [XQuery][] Support
|
||||
* [Splunk][] Support
|
||||
* [Amazon Web Services (AWS)][] Support
|
||||
|
||||
## Getting support
|
||||
|
||||
@@ -127,4 +128,5 @@ The Spring Integration Extensions Framework is released under version 2.0 of the
|
||||
[Atmosphere]: https://github.com/Atmosphere/atmosphere
|
||||
[Websockets]: http://www.html5rocks.com/en/tutorials/websockets/basics/
|
||||
[XQuery]: http://en.wikipedia.org/wiki/XQuery
|
||||
[Splunk]:http://www.splunk.com/
|
||||
[Splunk]:http://www.splunk.com/
|
||||
[Amazon Web Services (AWS)]: http://aws.amazon.com/
|
||||
141
spring-integration-aws/README.md
Normal file
141
spring-integration-aws/README.md
Normal file
@@ -0,0 +1,141 @@
|
||||
Spring Integration Extension for Amazon Web Services (AWS)
|
||||
==========================================================
|
||||
|
||||
|
||||
##Small Introduction to Amazon Web Services (AWS)
|
||||
|
||||
|
||||
Launched in 2006, Amazon Web Services (AWS) started providing key infrastructure for business as web services, which, also known as cloud computing. Using cloud computing businesses can adopt a new business model whereby they do not have to plan and invest in procuring their own IT infrastructure. They can use the infrastructure and services provided by the cloud service provider and pay as they use the services.
|
||||
Visit [http://aws.amazon.com/products/] for more details about various products offered by Amazon as a part their cloud computing services.
|
||||
|
||||
##Introduction to Spring Integration's extensions to AWS
|
||||
|
||||
This guide intends to explain in brief about various adapters available for the AWS and sample xml tag definition for each of them and code snippets wherever necessary,
|
||||
|
||||
The library aims to provide adapters for following AWS Services
|
||||
|
||||
* **Amazon Simple Email Service (SES)**
|
||||
* **Amazon Simple Storage Service (S3)** (Development complete, coming soon)
|
||||
* **Amazon Simple Queue Service (SQS)** (Development complete, coming soon)
|
||||
* **Amazon DynamoDB** (Analysis ongoing)
|
||||
* **Amazon SimpleDB** (Not initiated)
|
||||
* **Amazon SNS** (Not initiated)
|
||||
|
||||
Of the above libraries, SES and SNS have outbound adapters only. All other
|
||||
services have inbound and outbound adapters. The SQS inbound adapter is capable of receiving notifications sent out from SNS where the topic is an SQS Queue.
|
||||
For DymamoDB and SimpleDB, apart from inbound and outbound adapters we shall provide a *MessageStore* implementation too.
|
||||
|
||||
|
||||
###Executing the test cases.
|
||||
|
||||
All the test cases for the adapters are present in the *src/test/java* folder. On executing the build, maven's surefire plugin will execute all
|
||||
the tests. Please note that all the tests ending with **AWSTests.java* connect
|
||||
to the actual webservices and are excluded by default in the maven build.
|
||||
All other tests rely on mocking to test the functionality. You need to execute the **AWSTests.java*
|
||||
explicitly, manually to test the conectivity to AWS using your credentials.
|
||||
All these **AWSTests.java* tests look for the file *awscredentials.properties* in the classpath.
|
||||
To be on the safer side create one at location *src/test/resources* as this file
|
||||
*spring-integration-aws/src/test/resources/awscredentials.properties* is added to *.gitignore* file.
|
||||
This will prevent this file to be checked in accidently and revealing your credentials.
|
||||
This file needs to have two properties *accessKey* and *secretKey* holding the values of your access key and secret key respectively.
|
||||
> **Note: AWS Services are chargeable and we recommend not to execute the **AWSTests.java* as part of your regular builds.
|
||||
AWS does provide a free tier which is sufficient to perform your tests without being
|
||||
charged (not true for DynamoDB though), however keep a check on your account usage regularly.
|
||||
Get more information about AWS free tier at [http://aws.amazon.com/free/][]**
|
||||
|
||||
|
||||
|
||||
#Adapters
|
||||
|
||||
##Simple Email Service (SES)
|
||||
|
||||
|
||||
###Introduction
|
||||
|
||||
Amazon Simple Email Service (SES) is a web service for sending emails from the cloud. It supports two types of mails currently, the simple mail and raw email.
|
||||
Use simple mail if your application just needs to send out emails with some html formatting and without embedded images or attachments. Raw emails gives more flixibility to
|
||||
send complex emails with embedded images and attachments.
|
||||
For more details about Amazon SES and its pricing visit [http://aws.amazon.com/ses/]
|
||||
We have an outbound channel adapter for the Amazon SES Service for sending out simple and raw emails with complete namespace support for configuring the adapter.
|
||||
To prevent misuse of the service, Amazon has enforced some restrictions, for sandbox you need to verify email ids which would be used in *to, cc, bcc* and *from*. For more details on how to use SES and
|
||||
other details refer to the SES documentation at [http://aws.amazon.com/documentation/ses/][].
|
||||
|
||||
###Outbound Channel Adapter
|
||||
|
||||
Below xml snippet is a simple definition of the outbound channel adapter
|
||||
|
||||
|
||||
<integration:channel id="outboundAdapterChannel"/>
|
||||
<int-aws-ses:outbound-channel-adapter
|
||||
propertiesFile="classpath:awscredentials.properties"
|
||||
channel="outboundAdapterChannel"/>
|
||||
|
||||
|
||||
*propertiesFile* attribute contains the AWS access key and secret key. The
|
||||
name of the properties are *accessKey* and *secretKey* respectively.
|
||||
An alternative to the *propertiesFile* attribute is the *accessKey* and the
|
||||
*secretKey* attributes containing the values of access key and the secret key. The definition will look as below.
|
||||
|
||||
<integration:channel id="outboundAdapterChannel"/>
|
||||
<int-aws-ses:outbound-channel-adapter
|
||||
accessKey="{your access key}"
|
||||
secreKey="{your secret key}"
|
||||
channel="outboundAdapterChannel"/>
|
||||
|
||||
Both the approaches for providing the credentials are mutually exclusive to each other.
|
||||
|
||||
####Sending Mail Messages
|
||||
We shall now see a java code snippet to send a mail using the SNS adapter
|
||||
|
||||
|
||||
* **Simple Mail Message**
|
||||
|
||||
A Simple Mail Message does not support attachments and embedded contents. It supports basic html content to be sent as the mail body.
|
||||
|
||||
Map<String, Object> headers = new HashMap<String, Object>();
|
||||
headers.put("fromEmailId", "xyz@somemail.com");
|
||||
headers.put("htmlFormat", true);
|
||||
headers.put("subject", "Mail Sent from AWS SES Outbound adapter");
|
||||
headers.put("toEmailId", "abc@anothermail.com");
|
||||
|
||||
Message<String> msg =
|
||||
MessageBuilder.withPayload("<html><i>A Simple Mail Message sent from " +
|
||||
"Amazon SES Outbound adapter from Spring integration<i></html>")
|
||||
.copyHeaders(headers)
|
||||
.build();
|
||||
channel.send(msg);
|
||||
|
||||
The above piece off code is pretty simple.
|
||||
* We add four headers to the message for the *to email id*, *from email id*, *subject* and a *flag* to indicate whether the content is an html content or plain text content.
|
||||
* Of these headers the *from email id* and *subject* are mandatory. We can specify one or more of *to*, *cc* or *bcc* email addresses.
|
||||
* The message needs to have a payload of string which is either a plain
|
||||
text or html content. The content will be rendered a html only if the
|
||||
*htmlFormat *header is set appropriately.
|
||||
* The value of the *htmlFormat* header can be Boolean *true*, or *y*,*yes*
|
||||
or *true* as String. For String the value is case insensitive. Any other value will be considered as *false*.
|
||||
* The message is sent over the channel to which is the input channel of
|
||||
the outbound channel adapter.
|
||||
* See *o.s.i.aws.ses.AmazonSESMailHeaders* for all the possible header values supported.
|
||||
|
||||
* **Raw Mail Message**
|
||||
|
||||
Use raw mail when you need more flexibility to send mails, like setting mime types and email headers. As long as the content complies with the standard email format standard you can use this means for sending the mail to your recipients. In the below sample we use the spring's *o.s.mail.javamail.MimeMessageHelper* to construct the Mime message.
|
||||
|
||||
Session session = Session.getDefaultInstance(new Properties());
|
||||
MimeMessageHelper helper = new MimeMessageHelper(new MimeMessage(session),true);
|
||||
helper.setTo("abc@somemail.com");
|
||||
helper.setFrom("xyz@anothermail.com");
|
||||
helper.setText("A Sample Embedded image");
|
||||
helper.addAttachment(file.getName(),new File("<File path to the attachment>"));
|
||||
helper.setSubject("Name Pic");
|
||||
Message<MimeMessage> message =
|
||||
MessageBuilder.withPayload(helper.getMimeMessage()).build();
|
||||
channel.send(message);
|
||||
|
||||
The messages over this channel are consumed by the outbound SES adapter and the mail is sent out using SES.
|
||||
|
||||
|
||||
[http://aws.amazon.com/products/]: http://aws.amazon.com/products/
|
||||
[http://aws.amazon.com/ses/]: http://aws.amazon.com/ses/
|
||||
[http://aws.amazon.com/documentation/ses/]: http://aws.amazon.com/documentation/ses/
|
||||
[http://aws.amazon.com/free/]: http://aws.amazon.com/free/
|
||||
223
spring-integration-aws/pom.xml
Normal file
223
spring-integration-aws/pom.xml
Normal file
@@ -0,0 +1,223 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.springframework.integration</groupId>
|
||||
<artifactId>spring-integration-aws</artifactId>
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
<name>Spring Integration Amazon Web Services Support</name>
|
||||
<licenses>
|
||||
<license>
|
||||
<name>The Apache Software License, Version 2.0</name>
|
||||
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
|
||||
<distribution>repo</distribution>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
<prerequisites>
|
||||
<maven>2.2.1</maven>
|
||||
</prerequisites>
|
||||
|
||||
<properties>
|
||||
<spring.integration.version>2.2.0.RELEASE</spring.integration.version>
|
||||
<spring.test.version>3.1.3.RELEASE</spring.test.version>
|
||||
<aws.sdk.version>1.3.12</aws.sdk.version>
|
||||
<commons.codec.version>1.5</commons.codec.version>
|
||||
<commons.logging.version>1.1.1</commons.logging.version>
|
||||
<commons.io.version>2.0.1</commons.io.version>
|
||||
<java.mail.version>1.4.4</java.mail.version>
|
||||
<jackson.version>1.9.11</jackson.version>
|
||||
<apache.httpclient.version>4.1.1</apache.httpclient.version>
|
||||
<hamcrest.version>1.1</hamcrest.version>
|
||||
<junit.version>4.11</junit.version>
|
||||
<mockito.version>1.9.0</mockito.version>
|
||||
<project.build.sourceEncoding>UTF8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/java</directory>
|
||||
<includes>
|
||||
<include>**/*</include>
|
||||
</includes>
|
||||
<excludes>
|
||||
<exclude>**/*.java</exclude>
|
||||
</excludes>
|
||||
</resource>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<includes>
|
||||
<include>**/*</include>
|
||||
</includes>
|
||||
</resource>
|
||||
</resources>
|
||||
<testResources>
|
||||
<testResource>
|
||||
<directory>src/test/java</directory>
|
||||
<includes>
|
||||
<include>**/*</include>
|
||||
</includes>
|
||||
<excludes>
|
||||
<exclude>**/*.java</exclude>
|
||||
</excludes>
|
||||
</testResource>
|
||||
<testResource>
|
||||
<directory>src/test/resources</directory>
|
||||
<includes>
|
||||
<include>**/*</include>
|
||||
</includes>
|
||||
</testResource>
|
||||
</testResources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<includes>
|
||||
<include>**/*Tests.java</include>
|
||||
</includes>
|
||||
<excludes>
|
||||
<exclude>**/*Abstract*.java</exclude>
|
||||
<exclude>**/*AWSTests.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>springsource-libs-milestone</id>
|
||||
<name>Spring Framework Maven Milestone Repository</name>
|
||||
<url>https://repo.springsource.org/libs-milestone</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-java-sdk</artifactId>
|
||||
<version>${aws.sdk.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons.codec.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<version>${commons.logging.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons.io.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.mail</groupId>
|
||||
<artifactId>mail</artifactId>
|
||||
<version>${java.mail.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.integration</groupId>
|
||||
<artifactId>spring-integration-core</artifactId>
|
||||
<version>${spring.integration.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.integration</groupId>
|
||||
<artifactId>spring-integration-mail</artifactId>
|
||||
<version>${spring.integration.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.codehaus.jackson</groupId>
|
||||
<artifactId>jackson-core-asl</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.codehaus.jackson</groupId>
|
||||
<artifactId>jackson-mapper-asl</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${apache.httpclient.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpcore</artifactId>
|
||||
<version>${apache.httpclient.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.integration</groupId>
|
||||
<artifactId>spring-integration-test</artifactId>
|
||||
<version>${spring.integration.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-core</artifactId>
|
||||
<version>${hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-all</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.test.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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.aws.config.xml;
|
||||
|
||||
import org.springframework.integration.aws.ses.config.xml.AmazonSESOutboundAdapterParser;
|
||||
import org.springframework.integration.config.xml.AbstractIntegrationNamespaceHandler;
|
||||
|
||||
/**
|
||||
* The namepsace handler for "int-aws" namespace
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public class AWSNamespaceHandler extends
|
||||
AbstractIntegrationNamespaceHandler {
|
||||
|
||||
|
||||
public void init() {
|
||||
this.registerBeanDefinitionParser("ses-outbound-channel-adapter", new AmazonSESOutboundAdapterParser());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.aws.config.xml;
|
||||
|
||||
import static org.springframework.integration.aws.config.xml.AmazonWSParserUtils.getAmazonWSCredentials;
|
||||
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.config.xml.AbstractOutboundChannelAdapterParser;
|
||||
import org.springframework.integration.core.MessageHandler;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* The common adapter parser for all AWS Outbound channel adapters
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractAWSOutboundChannelAdapterParser extends
|
||||
AbstractOutboundChannelAdapterParser {
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.integration.config.xml.AbstractOutboundChannelAdapterParser#parseConsumer(org.w3c.dom.Element, org.springframework.beans.factory.xml.ParserContext)
|
||||
*/
|
||||
@Override
|
||||
protected final AbstractBeanDefinition parseConsumer(Element element,
|
||||
ParserContext parserContext) {
|
||||
String awsCredentialsGeneratedName = getAmazonWSCredentials(element,parserContext);
|
||||
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(getMessageHandlerImplementation());
|
||||
builder.addConstructorArgReference(awsCredentialsGeneratedName);
|
||||
processBeanDefinition(builder,awsCredentialsGeneratedName,element,parserContext);
|
||||
return builder.getBeanDefinition();
|
||||
}
|
||||
|
||||
protected abstract Class<? extends MessageHandler> getMessageHandlerImplementation();
|
||||
|
||||
/**
|
||||
* The subclasses can override this method to set additional attributes and perform some
|
||||
* additional operations on the {@link BeanDefinitionBuilder}
|
||||
*
|
||||
* @param builder
|
||||
* @param awsCredentialsGeneratedName
|
||||
* @param element
|
||||
* @param context
|
||||
*/
|
||||
protected void processBeanDefinition(BeanDefinitionBuilder builder,String awsCredentialsGeneratedName,
|
||||
Element element,ParserContext context) {
|
||||
//Default implementation does nothing
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* 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.aws.config.xml;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.aws.core.BasicAWSCredentials;
|
||||
import org.springframework.integration.aws.core.PropertiesAWSCredentials;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* The utility class for the namespace parsers
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public final class AmazonWSParserUtils {
|
||||
|
||||
public static final String ACCESS_KEY = "accessKey";
|
||||
public static final String SECRET_KEY = "secretKey";
|
||||
public static final String PROPERTIES_FILE = "propertiesFile";
|
||||
public static final String CREDENTIALS_REF = "credentials-ref";
|
||||
|
||||
private AmazonWSParserUtils() {
|
||||
throw new AssertionError("Cannot instantiate the utility class");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Registers the {@link AmazonWSCredentials} bean with the current ApplicationContext if
|
||||
* accessKey and secretKey is given, if the credentials-ref is given, the given value
|
||||
* is returned.
|
||||
*
|
||||
* @param element
|
||||
* @param parserContext
|
||||
* @return
|
||||
*/
|
||||
public static String getAmazonWSCredentials(Element element,ParserContext parserContext) {
|
||||
//TODO: Some mechanism to use the same instance with same ACCESS_KEY to be implemented
|
||||
String accessKey = element.getAttribute(ACCESS_KEY);
|
||||
String secretKey = element.getAttribute(SECRET_KEY);
|
||||
String propertiesFile = element.getAttribute(PROPERTIES_FILE);
|
||||
String credentialsRef = element.getAttribute(CREDENTIALS_REF);
|
||||
String awsCredentialsGeneratedName;
|
||||
|
||||
if(StringUtils.hasText(credentialsRef)) {
|
||||
if(StringUtils.hasText(propertiesFile)
|
||||
|| StringUtils.hasText(accessKey)
|
||||
|| StringUtils.hasText(secretKey)) {
|
||||
parserContext.getReaderContext().error("When " + CREDENTIALS_REF + " is specified, " +
|
||||
"do not specify the " + PROPERTIES_FILE + " attribute or the "
|
||||
+ SECRET_KEY + " and " + ACCESS_KEY + " attributes", element);
|
||||
}
|
||||
awsCredentialsGeneratedName = credentialsRef;
|
||||
}
|
||||
else {
|
||||
if(StringUtils.hasText(propertiesFile)) {
|
||||
if(StringUtils.hasText(accessKey) && StringUtils.hasText(secretKey)) {
|
||||
parserContext.getReaderContext().error("When " + ACCESS_KEY + " and " + SECRET_KEY +
|
||||
" are specified, do not specify the " + PROPERTIES_FILE + " attribute", element);
|
||||
}
|
||||
|
||||
BeanDefinitionBuilder builder =
|
||||
BeanDefinitionBuilder.genericBeanDefinition(PropertiesAWSCredentials.class);
|
||||
builder.addConstructorArgValue(propertiesFile);
|
||||
awsCredentialsGeneratedName = BeanDefinitionReaderUtils.registerWithGeneratedName(
|
||||
builder.getBeanDefinition(), parserContext.getRegistry());
|
||||
} else {
|
||||
BeanDefinitionBuilder builder
|
||||
= BeanDefinitionBuilder.genericBeanDefinition(BasicAWSCredentials.class);
|
||||
builder.addConstructorArgValue(accessKey);
|
||||
builder.addConstructorArgValue(secretKey);
|
||||
awsCredentialsGeneratedName = BeanDefinitionReaderUtils.registerWithGeneratedName(
|
||||
builder.getBeanDefinition(), parserContext.getRegistry());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return awsCredentialsGeneratedName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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.aws.core;
|
||||
|
||||
import com.amazonaws.AmazonWebServiceClient;
|
||||
|
||||
/**
|
||||
* The factory interface that would be used to get the implementation of the appropriate
|
||||
* instance of {@link AmazonWebServiceClient}
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public interface AWSClientFactory<T extends AmazonWebServiceClient> {
|
||||
|
||||
/**
|
||||
* Returns the instance of the {@link AmazonWebServiceClient} with the apropriate endpoint value
|
||||
* set based on the provided url value
|
||||
*
|
||||
* @param url The url of the service
|
||||
* @return The appropriate {@link AmazonWebServiceClient} for the provided endpoint URL
|
||||
*/
|
||||
T getClient(String url);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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.aws.core;
|
||||
|
||||
/**
|
||||
* The common interfaces for all implementations of Amazon WS Credentials
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public interface AWSCredentials {
|
||||
|
||||
/**
|
||||
* Get the Access key to the Amazon WS account
|
||||
* @return
|
||||
*/
|
||||
public String getAccessKey();
|
||||
|
||||
/**
|
||||
* Get the Secret key to the Amazon WS account
|
||||
* @return
|
||||
*/
|
||||
public String getSecretKey();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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.aws.core;
|
||||
|
||||
/**
|
||||
* The Base class for all other AWS operation exceptions
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public class AWSOperationException extends RuntimeException {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 3391888045993691634L;
|
||||
|
||||
private final String accessKey;
|
||||
|
||||
public AWSOperationException(String accessKey) {
|
||||
super();
|
||||
this.accessKey = accessKey;
|
||||
}
|
||||
|
||||
public AWSOperationException(String accessKey,String message) {
|
||||
super(message);
|
||||
this.accessKey = accessKey;
|
||||
}
|
||||
|
||||
public AWSOperationException(String accessKey,String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.accessKey = accessKey;
|
||||
}
|
||||
|
||||
public AWSOperationException(String accessKey,Throwable cause) {
|
||||
super(cause);
|
||||
this.accessKey = accessKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the access key for the user who encountered the exception
|
||||
* @return
|
||||
*/
|
||||
public String getAccessKey() {
|
||||
return accessKey;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* 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.aws.core;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.amazonaws.AmazonWebServiceClient;
|
||||
|
||||
|
||||
/**
|
||||
* The abstract factory class that will be used by all the client operations to acquire
|
||||
* the appropriate implementation of the {@link AmazonWebServiceClient} based on the URL
|
||||
* passed to the <i>getClient</i> method
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractAWSClientFactory<T extends AmazonWebServiceClient> implements AWSClientFactory<T> {
|
||||
|
||||
/**
|
||||
* A map storing the {@link AmazonWebServiceClient} endpoint as the key and the SQS Client as the
|
||||
* value. Since setting the endpoint is not a thread safe operation once the client is instantiated,
|
||||
* we maintain a map of endpoint and the {@link AmazonWebServiceClient} instantiated the first time a request
|
||||
* for the designated endpoint is received
|
||||
*/
|
||||
private final ConcurrentHashMap<String, T> clientMap = new ConcurrentHashMap<String, T>();
|
||||
|
||||
/**
|
||||
* The String constant for HTTP
|
||||
*/
|
||||
protected final static String HTTP = "http://";
|
||||
|
||||
/**
|
||||
* The String constant for HTTPS
|
||||
*/
|
||||
protected final static String HTTPS = "https://";
|
||||
|
||||
/**
|
||||
* The String constant for SMTP
|
||||
*/
|
||||
protected final String SMTP = "smtp://";
|
||||
|
||||
/**
|
||||
* The default protocol to be used in case none is provided
|
||||
*/
|
||||
protected final static String DEFAULT_PROTOCOL = HTTPS;
|
||||
|
||||
|
||||
/**
|
||||
* Returns the cached implementation of the {@link AmazonWebServiceClient} based on the URL provided.
|
||||
* the client instance is acquired using the abstract <i>getClientImplementation</i> method.
|
||||
* The instance is added to the client map with the endpoint string as the key and the
|
||||
* {@link AmazonWebServiceClient} as the value.
|
||||
*
|
||||
* @param url the URL for which the client is requested.
|
||||
* @return the implementation of the {@link AmazonWebServiceClient} to be used for the provided url
|
||||
*/
|
||||
public final T getClient(String url) {
|
||||
String endpoint = getEndpointFromURL(url);
|
||||
if(!clientMap.containsKey(endpoint)) {
|
||||
T client = getClientImplementation();
|
||||
client.setEndpoint(endpoint);
|
||||
T existingClient = clientMap.putIfAbsent(endpoint, client);
|
||||
if(existingClient != null) {
|
||||
//in rare scenarios where a new implementation was created after
|
||||
//checking for the existence of the endpoint in the client map
|
||||
client = existingClient;
|
||||
}
|
||||
return client;
|
||||
} else
|
||||
return clientMap.get(endpoint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a copy of the client map
|
||||
* @return the copy of the clientMap
|
||||
*/
|
||||
public final Map<String, T> getClientMap() {
|
||||
return new HashMap<String, T>(clientMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the complete cache
|
||||
*/
|
||||
public final void clear() {
|
||||
clientMap.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the endpoint from the URL provided
|
||||
*
|
||||
* @param url
|
||||
* @return
|
||||
*/
|
||||
private String getEndpointFromURL(String stringUrl) {
|
||||
Assert.notNull(stringUrl,"Provided String URL is null");
|
||||
String endpoint;
|
||||
try {
|
||||
if(!(stringUrl.startsWith(HTTP)
|
||||
|| stringUrl.startsWith(HTTPS)
|
||||
|| stringUrl.startsWith(SMTP))) {
|
||||
stringUrl = DEFAULT_PROTOCOL + stringUrl;
|
||||
}
|
||||
URL url = new URL(stringUrl);
|
||||
String host = url.getHost();
|
||||
String protocol = url.getProtocol();
|
||||
if(StringUtils.hasText(protocol)) {
|
||||
endpoint = protocol + "://" + host;
|
||||
}
|
||||
else {
|
||||
endpoint = host;
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
throw new AWSOperationException(null, "The URL \"" + stringUrl + "\" is malformed",e);
|
||||
}
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* The subclass needs to implement this method and return an appropriate implementation
|
||||
* @return
|
||||
*/
|
||||
protected abstract T getClientImplementation();
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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.aws.core;
|
||||
|
||||
/**
|
||||
* The basic implementation class holding the Access key and the secret
|
||||
* key for the AWS account .
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public class BasicAWSCredentials implements AWSCredentials {
|
||||
|
||||
/**.
|
||||
* Hold the Access key for the AWS account
|
||||
*/
|
||||
private String accessKey;
|
||||
|
||||
/**.
|
||||
* Hold the Secret key for the
|
||||
*/
|
||||
private String secretKey;
|
||||
|
||||
|
||||
/**.
|
||||
* Default constructor
|
||||
*/
|
||||
public BasicAWSCredentials() {
|
||||
|
||||
}
|
||||
|
||||
/**.
|
||||
* The constructor accepting the access and secret key
|
||||
* @param accessKey
|
||||
* @param secretKey
|
||||
*/
|
||||
public BasicAWSCredentials(String accessKey, String secretKey) {
|
||||
super();
|
||||
this.accessKey = accessKey;
|
||||
this.secretKey = secretKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Access key to the Amazon WS account
|
||||
* @return
|
||||
*/
|
||||
public String getAccessKey() {
|
||||
return accessKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Access key to the Amazon WS account
|
||||
* @param accessKey
|
||||
*/
|
||||
public void setAccessKey(String accessKey) {
|
||||
this.accessKey = accessKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Secret key to the Amazon WS account
|
||||
* @return
|
||||
*/
|
||||
public String getSecretKey() {
|
||||
return secretKey;
|
||||
}
|
||||
|
||||
/**.
|
||||
* Set the Secret key to the Amazon WS account
|
||||
* @param secretKey
|
||||
*/
|
||||
public void setSecretKey(String secretKey) {
|
||||
this.secretKey = secretKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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.aws.core;
|
||||
|
||||
/**
|
||||
* Thrown when AWS Credentials provided by the user are incomplete or invalid
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public class InvalidAWSCredentialsException extends RuntimeException {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public InvalidAWSCredentialsException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public InvalidAWSCredentialsException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public InvalidAWSCredentialsException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public InvalidAWSCredentialsException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
/*
|
||||
* 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.aws.core;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Load the AWS credentials from the .properties file
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public class PropertiesAWSCredentials extends BasicAWSCredentials implements InitializingBean {
|
||||
|
||||
|
||||
public static final String DEFAULT_AWS_ACCESS_KEY_PROPERTY = "accessKey";
|
||||
public static final String DEFAULT_AWS_SECRET_KEY_PROPERTY = "secretKey";
|
||||
|
||||
private String accessKeyProperty = DEFAULT_AWS_ACCESS_KEY_PROPERTY;
|
||||
private String secretKeyProperty = DEFAULT_AWS_SECRET_KEY_PROPERTY;
|
||||
private String propertyFileName;
|
||||
|
||||
/**.
|
||||
* Constructor accepting the properties file
|
||||
* @param propertyFileName
|
||||
*/
|
||||
public PropertiesAWSCredentials(String propertyFileName) {
|
||||
super();
|
||||
this.propertyFileName = propertyFileName;
|
||||
}
|
||||
|
||||
/**.
|
||||
* Gets the property name which holds the
|
||||
* @return
|
||||
*/
|
||||
public String getAccessKeyProperty() {
|
||||
return accessKeyProperty;
|
||||
}
|
||||
|
||||
/**.
|
||||
* Sets the name of the property that will be used as the key in the properties
|
||||
* file to hold the AWS access key
|
||||
* @param accessKeyProperty
|
||||
*/
|
||||
public void setAccessKeyProperty(String accessKeyProperty) {
|
||||
this.accessKeyProperty = accessKeyProperty;
|
||||
}
|
||||
|
||||
/**.
|
||||
* Gets the property name which holds the
|
||||
* @return
|
||||
*/
|
||||
public String getSecretKeyProperty() {
|
||||
return secretKeyProperty;
|
||||
}
|
||||
|
||||
/**.
|
||||
* Sets the name of the property that will be used as the key in the properties
|
||||
* file to hold the AWS secret key
|
||||
* @param accessKeyProperty
|
||||
*/
|
||||
public void setSecretKeyProperty(String secretKeyProperty) {
|
||||
this.secretKeyProperty = secretKeyProperty;
|
||||
}
|
||||
|
||||
/**.
|
||||
* Get the name of the property file that will hold the AWS credentials
|
||||
* @return
|
||||
*/
|
||||
public String getPropertyFileName() {
|
||||
return propertyFileName;
|
||||
}
|
||||
|
||||
/**.
|
||||
* Sets the name of the file that will hold the AW credentials
|
||||
* @param propertyFileName
|
||||
*/
|
||||
public void setPropertyFileName(String propertyFileName) {
|
||||
this.propertyFileName = propertyFileName;
|
||||
}
|
||||
|
||||
/**.
|
||||
* Load the properties file and the keys
|
||||
*/
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
if (!StringUtils.hasText(propertyFileName))
|
||||
throw new InvalidAWSCredentialsException("Mandatory property propertyFileName expected");
|
||||
|
||||
if(!StringUtils.hasText(accessKeyProperty))
|
||||
throw new InvalidAWSCredentialsException("accessKeyValue has to be non empty and non null");
|
||||
|
||||
if(!StringUtils.hasText(secretKeyProperty))
|
||||
throw new InvalidAWSCredentialsException("secretKeyValue has to be non empty and non null");
|
||||
|
||||
loadProperties();
|
||||
|
||||
}
|
||||
|
||||
/**.
|
||||
* The private method that loads the properties from the .properties file and sets the access keys
|
||||
*/
|
||||
private void loadProperties() {
|
||||
Resource resource;
|
||||
if(propertyFileName.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)) {
|
||||
resource = new ClassPathResource(propertyFileName.substring(ResourceUtils.CLASSPATH_URL_PREFIX.length()), ClassUtils.getDefaultClassLoader());
|
||||
} else {
|
||||
resource = new ClassPathResource(propertyFileName, ClassUtils.getDefaultClassLoader());
|
||||
}
|
||||
if(!resource.exists())
|
||||
throw new InvalidAWSCredentialsException("Unable to find resource \"" + propertyFileName + "\" in classpath");
|
||||
|
||||
Properties props = new Properties();
|
||||
try {
|
||||
props.load(resource.getInputStream());
|
||||
} catch (IOException e) {
|
||||
throw new InvalidAWSCredentialsException("Unable to load properties from \"" + propertyFileName + "\" in classpath");
|
||||
}
|
||||
|
||||
setAccessKey((String)props.get(accessKeyProperty));
|
||||
setSecretKey((String)props.get(secretKeyProperty));
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.aws.ses;
|
||||
|
||||
import org.springframework.integration.aws.core.AWSCredentials;
|
||||
import org.springframework.integration.aws.ses.core.DefaultAmazonSESMailSender;
|
||||
import org.springframework.integration.mail.MailSendingMessageHandler;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
|
||||
/**
|
||||
* The Message handler for the SES Mail. This will be used to send email
|
||||
* using Amazon SES
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public class AmazonSESMessageHandler extends MailSendingMessageHandler {
|
||||
|
||||
/**
|
||||
* The Default constructor that extends from the {@link MailSendingMessageHandler} and passes
|
||||
* it an instance of {@link DefaultAmazonSESMailSender}
|
||||
* @param credentials
|
||||
*/
|
||||
public AmazonSESMessageHandler(AWSCredentials credentials) {
|
||||
super(new DefaultAmazonSESMailSender(credentials));
|
||||
}
|
||||
|
||||
/**
|
||||
* The constructor that accepts the {@link JavaMailSender} instance, used for
|
||||
* unit tests only
|
||||
* @param mailSender
|
||||
*/
|
||||
AmazonSESMessageHandler(JavaMailSender mailSender) {
|
||||
super(mailSender);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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.aws.ses.config.xml;
|
||||
|
||||
|
||||
import org.springframework.integration.aws.config.xml.AbstractAWSOutboundChannelAdapterParser;
|
||||
import org.springframework.integration.aws.ses.AmazonSESMessageHandler;
|
||||
import org.springframework.integration.core.MessageHandler;
|
||||
|
||||
/**
|
||||
* parse the <ses-outbound-channel-adapter/> of the "int-aws" namespace
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public class AmazonSESOutboundAdapterParser extends
|
||||
AbstractAWSOutboundChannelAdapterParser {
|
||||
|
||||
|
||||
@Override
|
||||
public Class<? extends MessageHandler> getMessageHandlerImplementation() {
|
||||
return AmazonSESMessageHandler.class;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* 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.aws.ses.core;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.integration.aws.core.AWSOperationException;
|
||||
|
||||
/**
|
||||
* This exception will be thrown upon failure in sending a mail from Amazon SES
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public class AmazonSESMailSendException extends AWSOperationException {
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = -3035267174544370619L;
|
||||
|
||||
private final Map<Object, Exception> failedMessages;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param accessKey
|
||||
* @param message
|
||||
* @param cause
|
||||
* @param failedMessages
|
||||
*/
|
||||
public AmazonSESMailSendException(String accessKey, String message,
|
||||
Throwable cause,Map<Object, Exception> failedMessages) {
|
||||
super(accessKey, message, cause);
|
||||
this.failedMessages = failedMessages;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param accessKey
|
||||
* @param message
|
||||
* @param failedMessages
|
||||
*/
|
||||
public AmazonSESMailSendException(String accessKey,
|
||||
String message,Map<Object, Exception> failedMessages) {
|
||||
super(accessKey, message);
|
||||
this.failedMessages = failedMessages;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param accessKey
|
||||
* @param cause
|
||||
* @param failedMessages
|
||||
*/
|
||||
public AmazonSESMailSendException(String accessKey,
|
||||
Throwable cause,Map<Object, Exception> failedMessages) {
|
||||
super(accessKey, cause);
|
||||
this.failedMessages = failedMessages;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param accessKey
|
||||
* @param failedMessages
|
||||
*/
|
||||
public AmazonSESMailSendException(String accessKey,Map<Object, Exception> failedMessages) {
|
||||
super(accessKey);
|
||||
this.failedMessages = failedMessages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the map of failed messages where the failed message is the key and the exception
|
||||
* while sending it is the value
|
||||
* @return
|
||||
*/
|
||||
public Map<Object, Exception> getFailedMessages() {
|
||||
return failedMessages;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* 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.aws.ses.core;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.mail.internet.MimeMessage;
|
||||
|
||||
import org.springframework.integration.aws.core.AWSCredentials;
|
||||
import org.springframework.integration.aws.core.AWSOperationException;
|
||||
import org.springframework.mail.MailException;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
|
||||
import com.amazonaws.auth.BasicAWSCredentials;
|
||||
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient;
|
||||
import com.amazonaws.services.simpleemail.model.RawMessage;
|
||||
import com.amazonaws.services.simpleemail.model.SendRawEmailRequest;
|
||||
|
||||
/**
|
||||
* The implementation class for sending mail using the Amazon SES service
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public class DefaultAmazonSESMailSender extends JavaMailSenderImpl {
|
||||
|
||||
/**.
|
||||
* The API class used for sending email using Amazon SES
|
||||
*/
|
||||
private final AmazonSimpleEmailServiceClient emailService;
|
||||
|
||||
private final AWSCredentials credentials;
|
||||
|
||||
public DefaultAmazonSESMailSender(AWSCredentials credentials) {
|
||||
if(credentials == null)
|
||||
throw new AWSOperationException(null, "Credentials cannot be null, provide a non null valid set of credentials");
|
||||
this.credentials = credentials;
|
||||
|
||||
emailService = new AmazonSimpleEmailServiceClient(
|
||||
new BasicAWSCredentials(credentials.getAccessKey(),
|
||||
credentials.getSecretKey()));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The Actual implementation of the sending of the mail message using the AWS SES
|
||||
* SDK
|
||||
*/
|
||||
@Override
|
||||
protected void doSend(MimeMessage[] mimeMessages, Object[] originalMessages)
|
||||
throws MailException {
|
||||
Map<Object, Exception> failedMessageMap = new HashMap<Object, Exception>();
|
||||
if(mimeMessages != null && mimeMessages.length > 0) {
|
||||
int index = 0;
|
||||
for(MimeMessage mailMessage:mimeMessages) {
|
||||
try {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
mailMessage.writeTo(baos);
|
||||
RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(baos.toByteArray()));
|
||||
SendRawEmailRequest rawMail = new SendRawEmailRequest(rawMessage);
|
||||
emailService.sendRawEmail(rawMail);
|
||||
index++;
|
||||
} catch (Exception e) {
|
||||
failedMessageMap.put(originalMessages != null? originalMessages[index]:mailMessage, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!failedMessageMap.isEmpty()) {
|
||||
throw new AmazonSESMailSendException(credentials.getAccessKey(),
|
||||
"Exception while sending one or more messages, see failedMessages for more details",
|
||||
failedMessageMap);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPort(int port) {
|
||||
throw new UnsupportedOperationException("AWS SES Implementation of mail " +
|
||||
"sender does not allow setting the port number");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setHost(String host) {
|
||||
throw new UnsupportedOperationException("AWS SES Implementation of mail " +
|
||||
"sender does not allow setting the host name. It is already configured with an endpoint");
|
||||
}
|
||||
|
||||
//Should we disallow setSession?
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
http\://www.springframework.org/schema/integration/aws=org.springframework.integration.aws.config.xml.AWSNamespaceHandler
|
||||
@@ -0,0 +1,2 @@
|
||||
http\://www.springframework.org/schema/integration/aws/spring-integration-aws-1.0.xsd=org/springframework/integration/aws/config/xml/spring-integration-aws-1.0.xsd
|
||||
http\://www.springframework.org/schema/integration/aws/spring-integration-aws.xsd=org/springframework/integration/aws/config/xml/spring-integration-aws-1.0.xsd
|
||||
@@ -0,0 +1,3 @@
|
||||
http\://www.springframework.org/schema/integration/aws@name=Integration AWS Namespace
|
||||
http\://www.springframework.org/schema/integration/aws@prefix=int-aws
|
||||
http\://www.springframework.org/schema/integration/aws@icon=org/springframework/integration/aws/config/xml/spring-integration-aws.gif
|
||||
@@ -0,0 +1,107 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsd:schema xmlns="http://www.springframework.org/schema/integration/aws"
|
||||
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:tool="http://www.springframework.org/schema/tool"
|
||||
xmlns:integration="http://www.springframework.org/schema/integration"
|
||||
targetNamespace="http://www.springframework.org/schema/integration/aws"
|
||||
elementFormDefault="qualified"
|
||||
attributeFormDefault="unqualified">
|
||||
|
||||
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
|
||||
<xsd:import namespace="http://www.springframework.org/schema/tool"/>
|
||||
<xsd:import namespace="http://www.springframework.org/schema/integration"
|
||||
schemaLocation="http://www.springframework.org/schema/integration/spring-integration-2.2.xsd"/>
|
||||
|
||||
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
Defines the configuration elements for Spring Integration's AWS Adapters.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
|
||||
<xsd:attributeGroup name="awsAdaptersCommonAttributes">
|
||||
<xsd:attribute name="accessKey" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Specify the access key to be used to connect to the AWS service.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="secretKey" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Specify the secret key corresponding to the access key to be used to
|
||||
authenticate the user and connect to the AWS service.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="propertiesFile" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Specifies the properties file used that will be used to hold the access key
|
||||
and the secret key of the AWS. This file expects two properties accessKey and
|
||||
secretKey for Access key and Secret Key respectively. This attribute is mutually
|
||||
exclusive to the accessKey, secretKey and credentials-ref attribute.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="credentials-ref" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation kind="ref">
|
||||
<tool:expected-type type="org.springframework.integration.aws.core.AWSCredentials"/>
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
<xsd:documentation>
|
||||
If none of the default mechanisms work, you may provide a custom implementation
|
||||
of org.springframework.integration.aws.core.AWSCredentials. Various use cases are
|
||||
when you might want to read the credentials from a database or any other secure store
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
</xsd:attributeGroup>
|
||||
|
||||
<xsd:element name="ses-outbound-channel-adapter">
|
||||
<xsd:complexType>
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines an outbound mail-sending Channel Adapter for sending mails using Amazon SES.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:attribute name="id" type="xsd:string"/>
|
||||
<xsd:attribute name="channel" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation kind="ref">
|
||||
<tool:expected-type type="org.springframework.integration.core.MessageChannel"/>
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
<xsd:documentation>
|
||||
Identifies channel attached to this adapter. This is the channel over which
|
||||
the SES outbound adapter will receive messages from. The received message
|
||||
will then be converted to the email to be sent.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attributeGroup ref="awsAdaptersCommonAttributes"/>
|
||||
<xsd:attribute name="auto-startup" type="xsd:string" default="true">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Lifecycle attribute signaling if this component should be started during
|
||||
Application Context startup. Default is 'true'
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
|
||||
<xsd:attribute name="order">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Specifies the order for invocation when this endpoint is connected as a
|
||||
subscriber to a SubscribableChannel.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 539 B |
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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.aws.common;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.aws.core.AbstractAWSClientFactory;
|
||||
|
||||
import com.amazonaws.auth.AWSCredentials;
|
||||
import com.amazonaws.services.sqs.AmazonSQSClient;
|
||||
|
||||
/**
|
||||
* The test class for {@link AbstractAmazonWSClientFactory}
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public class AWSClientFactoryTest {
|
||||
|
||||
AbstractAWSClientFactory<AmazonSQSClient> factory = new AbstractAWSClientFactory<AmazonSQSClient>() {
|
||||
|
||||
@Override
|
||||
protected AmazonSQSClient getClientImplementation() {
|
||||
return new AmazonSQSClient((AWSCredentials)null);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Test
|
||||
public void getRegionSpecificEndpoints() {
|
||||
//TODO: Use Spring integration test to inspect and assert the variables within
|
||||
AmazonSQSClient usEastClient = factory.getClient("https://queue.amazonaws.com/123456789012/MyTestQueue");
|
||||
Assert.assertNotNull(usEastClient);
|
||||
Assert.assertEquals(factory.getClientMap().size(),1);
|
||||
AmazonSQSClient usEastClient1 = factory.getClient("https://queue.amazonaws.com/123456789012/MyTestQueue1");
|
||||
Assert.assertNotNull(usEastClient1);
|
||||
Assert.assertEquals(usEastClient,usEastClient1);
|
||||
Assert.assertEquals(factory.getClientMap().size(),1);
|
||||
AmazonSQSClient apacClient1 = factory.getClient("https://ap-southeast-1.queue.amazonaws.com/123456789012/MyApacTestQueue");
|
||||
Assert.assertNotNull(apacClient1);
|
||||
Assert.assertEquals(factory.getClientMap().size(),2);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.aws.common;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Class extended by some of the test cases which are not spring based to get access to the properties
|
||||
* and other common functionality
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public abstract class BaseTestCase {
|
||||
|
||||
protected static String propsLocation = "testprops.properties";
|
||||
protected static Properties props;
|
||||
static {
|
||||
props = new Properties();
|
||||
try {
|
||||
props.load(ClassLoader.getSystemClassLoader().getResourceAsStream(propsLocation));
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
protected static String getProperty(String key) {
|
||||
if(props != null)
|
||||
return props.getProperty(key);
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* 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.aws.config.xml;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.aws.core.AWSCredentials;
|
||||
import org.springframework.integration.core.MessageHandler;
|
||||
import org.springframework.integration.endpoint.AbstractEndpoint;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
|
||||
/**
|
||||
* The Abstract test class for the AWS outbound adapter parsers
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractAWSOutboundChannelAdapterParserTests<T extends MessageHandler> {
|
||||
|
||||
protected static ClassPathXmlApplicationContext ctx;
|
||||
private static boolean isInitialized;
|
||||
|
||||
/**
|
||||
* The bean name expected to be present in the context which would be used when the
|
||||
* element is defined with the propertiesFile attribute
|
||||
*/
|
||||
private final String CREDENTIALS_TEST_ONE = "credentialsTestOne";
|
||||
|
||||
/**
|
||||
* The bean name expected to be present in the context which would be used when the
|
||||
* element is defined with the accessKey and the secretKey definition
|
||||
*/
|
||||
private final String CREDENTIALS_TEST_TWO = "credentialsTestTwo";
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
if(!isInitialized) {
|
||||
String contextLocation = getConfigFilePath();
|
||||
Assert.assertNotNull("Non null path value expected", contextLocation);
|
||||
ctx = new ClassPathXmlApplicationContext(contextLocation);
|
||||
Assert.assertTrue("Bean with id " + CREDENTIALS_TEST_ONE
|
||||
+ " expected to be present in the context for credentials test",
|
||||
ctx.containsBean(CREDENTIALS_TEST_ONE));
|
||||
Assert.assertTrue("Bean with id " + CREDENTIALS_TEST_TWO
|
||||
+ " expected to be present in the context for credentials test",
|
||||
ctx.containsBean(CREDENTIALS_TEST_TWO));
|
||||
isInitialized = true;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void destroy() {
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Message handler implementation for the given bean id
|
||||
* @param beanId
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected T getMessageHandlerForBeanDefinition(String beanId) {
|
||||
AbstractEndpoint endpoint = (AbstractEndpoint)ctx.getBean(beanId);
|
||||
return (T)TestUtils.getPropertyValue(endpoint, "handler");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the config file path that would be used to create the {@link ApplicationContext} instance
|
||||
* sub class should implement this method to return a value of the config to be used to create the
|
||||
* context
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected abstract String getConfigFilePath();
|
||||
|
||||
/**
|
||||
* The subclass should return the {@link AmazonWSCredentials} instance that is being used
|
||||
* to configure the adapters
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected abstract AWSCredentials getCredentials();
|
||||
|
||||
/**
|
||||
* The test class for the AWS Credentials test that used property files
|
||||
*/
|
||||
@Test
|
||||
public final void awsCredentialsTestWithPropFiles() {
|
||||
MessageHandler handler = getMessageHandlerForBeanDefinition(CREDENTIALS_TEST_ONE);
|
||||
AWSCredentials credentials = getCredentials();
|
||||
String accessKey = credentials.getAccessKey();
|
||||
String secretKey = credentials.getSecretKey();
|
||||
AWSCredentials configuredCredentials =
|
||||
TestUtils.getPropertyValue(handler, "credentials",AWSCredentials.class);
|
||||
Assert.assertEquals(accessKey, configuredCredentials.getAccessKey());
|
||||
Assert.assertEquals(secretKey, configuredCredentials.getSecretKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* The test class for the AWS Credentials test that used accessKey and secretKey elements
|
||||
*/
|
||||
@Test
|
||||
public final void awsCredentialsTestWithoutPropFiles() {
|
||||
MessageHandler handler = getMessageHandlerForBeanDefinition(CREDENTIALS_TEST_TWO);
|
||||
AWSCredentials credentials = getCredentials();
|
||||
String accessKey = credentials.getAccessKey();
|
||||
String secretKey = credentials.getSecretKey();
|
||||
AWSCredentials configuredCredentials =
|
||||
TestUtils.getPropertyValue(handler, "credentials",AWSCredentials.class);
|
||||
Assert.assertEquals(accessKey, configuredCredentials.getAccessKey());
|
||||
Assert.assertEquals(secretKey, configuredCredentials.getSecretKey());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* 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.aws.core;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
|
||||
import com.amazonaws.AmazonWebServiceClient;
|
||||
|
||||
/**
|
||||
* The abstract test class for Amazon WebService client factory tests
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractAWSClientFactoryTests<T extends AmazonWebServiceClient> {
|
||||
|
||||
protected AbstractAWSClientFactory<T> factory;
|
||||
|
||||
@Before
|
||||
public final void setup() {
|
||||
factory = getFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* The subclass is responsible for returning the appropriate factory implementation
|
||||
* @return
|
||||
*/
|
||||
protected abstract AbstractAWSClientFactory<T> getFactory();
|
||||
|
||||
/**
|
||||
* Gets the endpoint for the service in US-EAST-1 region
|
||||
* @return
|
||||
*/
|
||||
protected abstract String getUSEast1Endpoint();
|
||||
|
||||
|
||||
/**
|
||||
* Gets the endpoint for the service in US-EAST-1 region
|
||||
* @return
|
||||
*/
|
||||
protected abstract String getEUWest1Endpoint();
|
||||
|
||||
/**
|
||||
* Gets the Suffix for the endpoint URL which is service specific
|
||||
* @return
|
||||
*/
|
||||
protected abstract String getSuffix();
|
||||
|
||||
/**
|
||||
* The test case for giving a US east endpoint without protocol
|
||||
*/
|
||||
@Test
|
||||
public void withUSEast1EndpointWithoutProtocol() {
|
||||
String usEast1 = getUSEast1Endpoint();
|
||||
factory.clear();
|
||||
T client = factory.getClient(usEast1 + getSuffix());
|
||||
Map<String, T> map = factory.getClientMap();
|
||||
Assert.assertNotNull(map);
|
||||
Assert.assertEquals(1, map.size());
|
||||
Assert.assertTrue("Expected one key with value https://" + usEast1, map.containsKey("https://" + usEast1));
|
||||
URI endpoint = TestUtils.getPropertyValue(client, "endpoint", URI.class);
|
||||
Assert.assertNotNull(endpoint);
|
||||
Assert.assertEquals(usEast1,endpoint.getHost());
|
||||
Assert.assertEquals("https",endpoint.getScheme());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the factory by providing an endpoint in US east with protocol as http
|
||||
*/
|
||||
@Test
|
||||
public void withUSEast1EndpointWithProtocol() {
|
||||
String usEast1 = getUSEast1Endpoint();
|
||||
factory.clear();
|
||||
T client = factory.getClient("http://" + usEast1 + getSuffix());
|
||||
Map<String, T> map = factory.getClientMap();
|
||||
Assert.assertNotNull(map);
|
||||
Assert.assertEquals(1, map.size());
|
||||
Assert.assertTrue("Expected one key with value http://" + usEast1, map.containsKey("http://" + usEast1));
|
||||
URI endpoint = TestUtils.getPropertyValue(client, "endpoint", URI.class);
|
||||
Assert.assertNotNull(endpoint);
|
||||
Assert.assertEquals(usEast1,endpoint.getHost());
|
||||
Assert.assertEquals("http",endpoint.getScheme());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Calls the getClient multiple times to get the same client instance on each invocation
|
||||
*/
|
||||
@Test
|
||||
public void withMultipleCallsToSameEndpoint() {
|
||||
String usEast1 = getUSEast1Endpoint();
|
||||
factory.clear();
|
||||
T client = factory.getClient("https://" + usEast1 + getSuffix());
|
||||
Map<String, T> map = factory.getClientMap();
|
||||
Assert.assertNotNull(map);
|
||||
Assert.assertEquals(1, map.size());
|
||||
Assert.assertTrue("Expected one key with value http://" + usEast1, map.containsKey("https://" + usEast1));
|
||||
//default to https
|
||||
T client1 = factory.getClient(usEast1 + getSuffix());
|
||||
map = factory.getClientMap();
|
||||
Assert.assertEquals(1, map.size());
|
||||
Assert.assertTrue("Expected one key with value http://" + usEast1, map.containsKey("https://" + usEast1));
|
||||
Assert.assertTrue("Expecting to get the same instance of the client, but was not", client == client1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*Calls to different endpoints on the same client, expected to return a client with
|
||||
*appropriate endpoint URI set
|
||||
*/
|
||||
@Test
|
||||
public void withMultipleCallsToDifferentEndpoints() {
|
||||
String usEast1 = getUSEast1Endpoint();
|
||||
String euWest1 = getEUWest1Endpoint();
|
||||
factory.clear();
|
||||
T client = factory.getClient(usEast1 + getSuffix());
|
||||
Map<String, T> map = factory.getClientMap();
|
||||
Assert.assertNotNull(map);
|
||||
Assert.assertEquals(1, map.size());
|
||||
Assert.assertTrue("Expected one key with value https://" + usEast1, map.containsKey("https://" + usEast1));
|
||||
URI endpoint = TestUtils.getPropertyValue(client, "endpoint", URI.class);
|
||||
Assert.assertNotNull(endpoint);
|
||||
Assert.assertEquals(usEast1,endpoint.getHost());
|
||||
Assert.assertEquals("https",endpoint.getScheme());
|
||||
client = factory.getClient(euWest1 + getSuffix());
|
||||
map = factory.getClientMap();
|
||||
Assert.assertNotNull(map);
|
||||
Assert.assertEquals(2, map.size());
|
||||
Assert.assertTrue("Expected one key with value https://" + euWest1, map.containsKey("https://" + euWest1));
|
||||
endpoint = TestUtils.getPropertyValue(client, "endpoint", URI.class);
|
||||
Assert.assertNotNull(endpoint);
|
||||
Assert.assertEquals(euWest1,endpoint.getHost());
|
||||
Assert.assertEquals("https",endpoint.getScheme());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
/*
|
||||
* 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.aws.ses;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.springframework.integration.mail.MailHeaders.BCC;
|
||||
import static org.springframework.integration.mail.MailHeaders.CC;
|
||||
import static org.springframework.integration.mail.MailHeaders.FROM;
|
||||
import static org.springframework.integration.mail.MailHeaders.SUBJECT;
|
||||
import static org.springframework.integration.mail.MailHeaders.TO;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.mail.internet.MimeMessage;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageHandlingException;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
|
||||
|
||||
/**
|
||||
* The test class for {@link AmazonSESMessageHandler}
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public class AmazonSESMessageHandlerTests {
|
||||
|
||||
private static final List<SimpleMailMessage> messages = new ArrayList<SimpleMailMessage>();
|
||||
private static final List<MimeMessage> mimeMessages = new ArrayList<MimeMessage>();
|
||||
private static AmazonSESMessageHandler handler;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupAmazonSESMailSender() {
|
||||
JavaMailSender sender = mock(JavaMailSender.class);
|
||||
doAnswer(new Answer<Void>() {
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
Object[] args = invocation.getArguments();
|
||||
if(args != null) {
|
||||
messages.add((SimpleMailMessage)args[0]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}).when(sender).send(any(SimpleMailMessage.class));
|
||||
|
||||
doAnswer(new Answer<Void>() {
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
Object[] args = invocation.getArguments();
|
||||
if(args != null) {
|
||||
messages.addAll(Arrays.asList((SimpleMailMessage[])args));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}).when(sender).send(any(SimpleMailMessage[].class));
|
||||
|
||||
doAnswer(new Answer<Void>() {
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
Object[] args = invocation.getArguments();
|
||||
if(args != null) {
|
||||
mimeMessages.addAll(Arrays.asList((MimeMessage[])args));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}).when(sender).send(any(MimeMessage[].class));
|
||||
|
||||
doAnswer(new Answer<Void>() {
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
Object[] args = invocation.getArguments();
|
||||
if(args != null) {
|
||||
mimeMessages.add((MimeMessage)args[0]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}).when(sender).send(any(MimeMessage.class));
|
||||
handler = new AmazonSESMessageHandler(sender);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
messages.clear();
|
||||
mimeMessages.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
*Test case for sending a message with subject line of unexpected type
|
||||
*{@link AmazonSESSimpleMailMessage}
|
||||
*
|
||||
*/
|
||||
@Test(expected=MessageHandlingException.class)
|
||||
public void withIncorrectSubjectClass() {
|
||||
Message<?> testMessage = MessageBuilder
|
||||
.withPayload("Test")
|
||||
.setHeader(SUBJECT, Arrays.asList("Some Header"))
|
||||
.build();
|
||||
handler.handleMessage(testMessage);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test case where the message text used of incorrect type
|
||||
*
|
||||
*/
|
||||
@Test(expected=MessageHandlingException.class)
|
||||
public void withIncorrectMessageTextClass() {
|
||||
Message<?> testMessage = MessageBuilder
|
||||
.withPayload(Arrays.asList("Content"))
|
||||
.build();
|
||||
handler.handleMessage(testMessage);
|
||||
}
|
||||
|
||||
//Test case to check if from id exists or not cant work now as MailSendingMessageHandler
|
||||
//doesn't check if from is present or not
|
||||
|
||||
/**
|
||||
* With all the details
|
||||
*/
|
||||
@Test
|
||||
public void withAllTheDetails() {
|
||||
clear();
|
||||
Message<?> testMessage = MessageBuilder
|
||||
.withPayload("Test Content")
|
||||
.setHeader(TO, "to@to.com")
|
||||
.setHeader(BCC, "bcc@bcc.com")
|
||||
.setHeader(CC, "cc@cc.com")
|
||||
.setHeader(SUBJECT, "Test Subject")
|
||||
.setHeader(FROM, "from@from.com")
|
||||
.build();
|
||||
handler.handleMessage(testMessage);
|
||||
SimpleMailMessage message = messages.get(0);
|
||||
String[] to = message.getTo();
|
||||
Assert.assertNotNull(to);
|
||||
Assert.assertEquals(1,to.length);
|
||||
Assert.assertEquals("to@to.com", to[0]);
|
||||
|
||||
String[] cc = message.getCc();
|
||||
Assert.assertNotNull(cc);
|
||||
Assert.assertEquals(1,cc.length);
|
||||
Assert.assertEquals("cc@cc.com", cc[0]);
|
||||
|
||||
String[] bcc = message.getBcc();
|
||||
Assert.assertNotNull(bcc);
|
||||
Assert.assertEquals(1,bcc.length);
|
||||
Assert.assertEquals("bcc@bcc.com", bcc[0]);
|
||||
|
||||
Assert.assertEquals("from@from.com",message.getFrom());
|
||||
Assert.assertEquals("Test Subject",message.getSubject());
|
||||
Assert.assertEquals("Test Content", message.getText());
|
||||
}
|
||||
|
||||
//Might need to uncomment this test later when we start allowing null in TO email id of
|
||||
//spring-int-mail
|
||||
// /**
|
||||
// *
|
||||
// */
|
||||
// @Test
|
||||
// public void withAllMultipleToBccAndCC() {
|
||||
// clear();
|
||||
// Message<?> testMessage = MessageBuilder
|
||||
// .withPayload("Test Content")
|
||||
// .setHeader(TO,
|
||||
// Arrays.asList("to1@to.com","to2@to.com"))
|
||||
// .setHeader(BCC,
|
||||
// Arrays.asList("bcc1@bcc.com","bcc2@bcc.com"))
|
||||
// .setHeader(CC,
|
||||
// Arrays.asList("cc1@cc.com","cc2@cc.com"))
|
||||
// .setHeader(SUBJECT, "Test Subject")
|
||||
// .setHeader(FROM, "from@from.com")
|
||||
// .build();
|
||||
// handler.handleMessage(testMessage);
|
||||
// SimpleMailMessage message = messages.get(0);
|
||||
// String[] to = message.getTo();
|
||||
// Assert.assertNotNull(to);
|
||||
// Assert.assertEquals(2,to.length);
|
||||
// Assert.assertEquals("to1@to.com", to[0]);
|
||||
// Assert.assertEquals("to2@to.com", to[1]);
|
||||
//
|
||||
// String[] cc = message.getCc();
|
||||
// Assert.assertNotNull(cc);
|
||||
// Assert.assertEquals(2,cc.length);
|
||||
// Assert.assertEquals("cc1@cc.com", cc[0]);
|
||||
// Assert.assertEquals("cc2@cc.com", cc[1]);
|
||||
//
|
||||
// String[] bcc = message.getBcc();
|
||||
// Assert.assertEquals(2,bcc.length);
|
||||
// Assert.assertEquals("bcc1@bcc.com", bcc[0]);
|
||||
// Assert.assertEquals("bcc2@bcc.com", bcc[1]);
|
||||
//
|
||||
// Assert.assertEquals("from@from.com",message.getFrom());
|
||||
//
|
||||
// Assert.assertEquals("Test Subject",message.getSubject());
|
||||
// Assert.assertEquals("Test Content", message.getText());
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* 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.aws.ses.config.xml;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.aws.core.AWSCredentials;
|
||||
import org.springframework.integration.core.MessageHandler;
|
||||
import org.springframework.integration.endpoint.EventDrivenConsumer;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
|
||||
/**
|
||||
* The test class for the AmazonSESOutboundAdapterParser
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public class AmazonSESOutboundAdapterParserTests {
|
||||
|
||||
|
||||
/**
|
||||
* Tests the creation of context with a valid definition by specifying the credentials
|
||||
* in a properties file
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void propFileValidOutboundAdapter() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:ses-propfile-valid-test.xml");
|
||||
EventDrivenConsumer consumer = ctx.getBean("validDefinition",EventDrivenConsumer.class);
|
||||
assertValidDefinition(consumer);
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the creation of context with a valid definition by specifying the credentials
|
||||
* as individual attributes of the xml
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void propsValidOutboundAdapter() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:ses-props-valid-test.xml");
|
||||
EventDrivenConsumer consumer = ctx.getBean("validDefinition",EventDrivenConsumer.class);
|
||||
assertValidDefinition(consumer);
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the creation of context with a valid definition by specifying the credentials
|
||||
* as individual attributes of the xml
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void refValidOutboundAdapter() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:ses-cred-ref-valid-test.xml");
|
||||
EventDrivenConsumer consumer = ctx.getBean("validDefinition",EventDrivenConsumer.class);
|
||||
assertValidDefinition(consumer);
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
private void assertValidDefinition(EventDrivenConsumer consumer) {
|
||||
Assert.assertNotNull("Expected a non null EventDrivenConsumer", consumer);
|
||||
MessageHandler handler = TestUtils.getPropertyValue(consumer, "handler", MessageHandler.class);
|
||||
Assert.assertNotNull("Expected a non null messagehandler", handler);
|
||||
AWSCredentials credentials = TestUtils.getPropertyValue(handler, "mailSender.credentials", AWSCredentials.class);
|
||||
Assert.assertNotNull("Expected a non null instance of credentials", credentials);
|
||||
Assert.assertEquals("dummy", credentials.getAccessKey());
|
||||
Assert.assertEquals("dummy", credentials.getSecretKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* A Test case that tests by specifying both the aws credentials properties file
|
||||
* and the individual properties to set the credentials
|
||||
*/
|
||||
@Test(expected=BeanDefinitionParsingException.class)
|
||||
public void invalidDefinitionWithBothPropsAndPropFile() {
|
||||
new ClassPathXmlApplicationContext("classpath:ses-both-awscred-props-property.xml");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* 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.aws.ses.core;
|
||||
|
||||
import javax.mail.Session;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.aws.core.PropertiesAWSCredentials;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
|
||||
/**
|
||||
*
|
||||
* The test class for the {@link DefaultAmazonSESMailSender} class.
|
||||
*
|
||||
* NOTE: You will have to modify the to and from email's yourselves
|
||||
* as you can send to verified emails only as part of the free tier
|
||||
* in which you may be running the test.
|
||||
* To run this test you need to have your AWSAccess key and Secret key in the
|
||||
* file awscredentials.properties in the classpath. This file is not present in the
|
||||
* repository and you need to add one yourselves to src/test/resources folder and have
|
||||
* two properties accessKey and secretKey in it containing the access and the secret key
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public class DefaultAmazonSESMailSenderAWSTests {
|
||||
|
||||
private static final String TO_EMAIL_ID = "amolnayak311@gmail.com";
|
||||
private static DefaultAmazonSESMailSender sender;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static final void setupSender() throws Exception {
|
||||
PropertiesAWSCredentials credentials =
|
||||
new PropertiesAWSCredentials("classpath:awscredentials.properties");
|
||||
credentials.afterPropertiesSet();
|
||||
sender = new DefaultAmazonSESMailSender(credentials);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a mail using {@link AmazonSESSimpleMailMessage}
|
||||
*/
|
||||
@Test
|
||||
public void sendSimpleMailMessage() {
|
||||
SimpleMailMessage message = new SimpleMailMessage();
|
||||
message.setFrom(TO_EMAIL_ID);
|
||||
message.setText("Some Test body content");
|
||||
message.setSubject("Test subject message");
|
||||
message.setTo(new String[]{TO_EMAIL_ID});
|
||||
sender.send(message);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Send a mail using {@link AmazonSESSimpleMailMessage}
|
||||
*/
|
||||
@Test
|
||||
public void sendSimpleMailMessageArray() {
|
||||
SimpleMailMessage[] messages = new SimpleMailMessage[2];
|
||||
SimpleMailMessage message = new SimpleMailMessage();
|
||||
message.setFrom(TO_EMAIL_ID);
|
||||
message.setText("Some Test body content one");
|
||||
message.setSubject("Test subject message one");
|
||||
message.setTo(new String[]{TO_EMAIL_ID});
|
||||
messages[0] = message;
|
||||
message = new SimpleMailMessage();
|
||||
message.setFrom(TO_EMAIL_ID);
|
||||
message.setText("Some Test body content two");
|
||||
message.setSubject("Test subject message two");
|
||||
message.setTo(new String[]{TO_EMAIL_ID});
|
||||
messages[1] = message;
|
||||
sender.send(messages);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The test case for sending a {@link MimeMessage}
|
||||
*/
|
||||
@Test
|
||||
public void sendMimeMessage() throws Exception {
|
||||
Session session = sender.getSession();
|
||||
MimeMessageHelper helper = new MimeMessageHelper(new MimeMessage(session));
|
||||
helper.setText("Some HTML Text", true);
|
||||
helper.setTo(TO_EMAIL_ID);
|
||||
helper.setFrom(TO_EMAIL_ID);
|
||||
helper.setSubject("Some HTML Message's Subject Line");
|
||||
MimeMessage message = helper.getMimeMessage();
|
||||
sender.send(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* The test case for sending a {@link MimeMessage}
|
||||
*/
|
||||
@Test
|
||||
public void sendMimeMessageArray() throws Exception {
|
||||
Session session = sender.getSession();
|
||||
MimeMessage[] messages = new MimeMessage[2];
|
||||
MimeMessageHelper helper = new MimeMessageHelper(new MimeMessage(session));
|
||||
helper.setText("Some HTML Text One", true);
|
||||
helper.setTo(TO_EMAIL_ID);
|
||||
helper.setFrom(TO_EMAIL_ID);
|
||||
helper.setSubject("Some HTML Message's Subject Line One");
|
||||
MimeMessage message = helper.getMimeMessage();
|
||||
messages[0] = message;
|
||||
helper = new MimeMessageHelper(new MimeMessage(session));
|
||||
helper.setText("Some HTML Text Two", true);
|
||||
helper.setTo(TO_EMAIL_ID);
|
||||
helper.setFrom(TO_EMAIL_ID);
|
||||
helper.setSubject("Some HTML Message's Subject Line Two");
|
||||
message = helper.getMimeMessage();
|
||||
messages[1] = message;
|
||||
sender.send(messages);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
log4j.rootCategory=INFO, stdout
|
||||
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
|
||||
|
||||
log4j.category.org.springframework=WARN
|
||||
log4j.category.org.springframework.integration.aws=DEBUG
|
||||
@@ -0,0 +1,18 @@
|
||||
<?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:integration="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-aws="http://www.springframework.org/schema/integration/aws"
|
||||
xsi:schemaLocation=
|
||||
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/aws http://www.springframework.org/schema/integration/aws/spring-integration-aws.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
|
||||
|
||||
<integration:channel id="outboundAdapterChannel"/>
|
||||
|
||||
<int-aws:ses-outbound-channel-adapter id="validDefinition"
|
||||
propertiesFile="classpath:testawscredentials.properties"
|
||||
accessKey="propDummy"
|
||||
secretKey="propDummy"
|
||||
channel="outboundAdapterChannel"/>
|
||||
</beans>
|
||||
@@ -0,0 +1,21 @@
|
||||
<?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:integration="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-aws="http://www.springframework.org/schema/integration/aws"
|
||||
xsi:schemaLocation=
|
||||
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/aws http://www.springframework.org/schema/integration/aws/spring-integration-aws.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
|
||||
|
||||
<integration:channel id="outboundAdapterChannel"/>
|
||||
|
||||
<bean id="credentials" class="org.springframework.integration.aws.core.BasicAWSCredentials">
|
||||
<constructor-arg name="accessKey" value="dummy"/>
|
||||
<constructor-arg name="secretKey" value="dummy"/>
|
||||
</bean>
|
||||
|
||||
<int-aws:ses-outbound-channel-adapter id="validDefinition"
|
||||
credentials-ref="credentials"
|
||||
channel="outboundAdapterChannel"/>
|
||||
</beans>
|
||||
@@ -0,0 +1,15 @@
|
||||
<?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:integration="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-aws="http://www.springframework.org/schema/integration/aws"
|
||||
xsi:schemaLocation=
|
||||
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/aws http://www.springframework.org/schema/integration/aws/spring-integration-aws.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
|
||||
|
||||
<integration:channel id="outboundAdapterChannel"/>
|
||||
|
||||
<int-aws:ses-outbound-channel-adapter id="validDefinition"
|
||||
propertiesFile="classpath:testawscredentials.properties" channel="outboundAdapterChannel"/>
|
||||
</beans>
|
||||
@@ -0,0 +1,17 @@
|
||||
<?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:integration="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-aws="http://www.springframework.org/schema/integration/aws"
|
||||
xsi:schemaLocation=
|
||||
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/aws http://www.springframework.org/schema/integration/aws/spring-integration-aws.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
|
||||
|
||||
<integration:channel id="outboundAdapterChannel"/>
|
||||
|
||||
<int-aws:ses-outbound-channel-adapter id="validDefinition"
|
||||
accessKey="dummy"
|
||||
secretKey="dummy"
|
||||
channel="outboundAdapterChannel"/>
|
||||
</beans>
|
||||
@@ -0,0 +1,2 @@
|
||||
accessKey=dummy
|
||||
secretKey=dummy
|
||||
Reference in New Issue
Block a user