Permalink
Browse files

Merge pull request #47 from ghillert/INTSAMPLES-70

* ghillert-INTSAMPLES-70:
  INTSAMPLES-70 - Sample showing how to process E-mail attachments For reference see: https://jira.springsource.org/browse/INTSAMPLES-70
  • Loading branch information...
2 parents e81cf14 + dba742b commit 291cdb81db9a982340681020173268e3ec722f61 Gunnar Hillert committed Oct 4, 2012
@@ -0,0 +1,41 @@
+Spring Integration - Processing Email Attachments Sample
+========================================================
+
+# Overview
+
+This example demonstrates how emails including their attachments can be processed using Spring Integration. This sample uses the following Spring Integration components:
+
+* Mail Inbound Channel Adapter
+* Chain
+* Transformer
+* Splitter
+* File Outbound Channel Adapter
+
+# Getting Started
+
+In order to use this sample you must have access to a mail-server. You can either use an external server (e.g. GMail) or you can also easily setup your own mail server using Apache James 3.0 (http://james.apache.org/). You can find instructions for setting up a basic instance at:
+
+* http://james.apache.org/server/3/quick-start.html
+* http://hillert.blogspot.com/2011/05/testing-email-notifications-with-apache.html
+
+In **src/main/resources/META-INF/spring/integration/spring-integration-context.xml** change the following to reflect the settings for your mail server.
+
+ store-uri="imap://test:test@localhost:143/INBOX"
+
+Lastly, before you run the example, please make sure that your email inbox contains some messages.
+
+You can run this sample by either.
+
+* running the "Main" class from within STS (Right-click on Main class --> Run As --> Java Application)
+* or from the command line:
+ - mvn package
+ - mvn exec:java
+
+Once started, the configured mail server will be polled for new email messages every 5 seconds.
+
+--------------------------------------------------------------------------------
+
+For help please take a look at the Spring Integration documentation:
+
+http://www.springsource.org/spring-integration
+
@@ -0,0 +1,103 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.springframework.integration.samples</groupId>
+ <artifactId>mail-attachments</artifactId>
+ <name>Samples (Intermediate) - Mail Attachment Demo</name>
+ <version>2.2.0.BUILD-SNAPSHOT</version>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <spring.integration.version>2.2.0.RC1</spring.integration.version>
+ <log4j.version>1.2.17</log4j.version>
+ <junit.version>4.10</junit.version>
+ </properties>
+
+ <prerequisites>
+ <maven>2.2.1</maven>
+ </prerequisites>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.springframework.integration</groupId>
+ <artifactId>spring-integration-mail</artifactId>
+ <version>${spring.integration.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.integration</groupId>
+ <artifactId>spring-integration-file</artifactId>
+ <version>${spring.integration.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>javax.activation</groupId>
+ <artifactId>activation</artifactId>
+ <version>1.1.1</version>
+ </dependency>
+ <dependency>
+ <groupId>javax.mail</groupId>
+ <artifactId>mail</artifactId>
+ <version>1.4.5</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>2.4</version>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>${junit.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-test</artifactId>
+ <version>3.1.1.RELEASE</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.subethamail</groupId>
+ <artifactId>subethasmtp-wiser</artifactId>
+ <version>1.2</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <version>${log4j.version}</version>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.5.1</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ <compilerArgument>-Xlint:all</compilerArgument>
+ <showWarnings>true</showWarnings>
+ <showDeprecation>true</showDeprecation>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>exec-maven-plugin</artifactId>
+ <version>1.2.1</version>
+ <configuration>
+ <mainClass>org.springframework.integration.samples.mailattachments.Main</mainClass>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <repositories>
+ <repository>
+ <id>repo.springsource.org.milestone</id>
+ <name>Spring Framework Maven Milestone Repository</name>
+ <url>https://repo.springsource.org/libs-milestone</url>
+ </repository>
+ </repositories>
+</project>
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2002-2012 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.integration.samples.mailattachments;
+
+import java.util.Scanner;
+
+import org.apache.log4j.Logger;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * Starts the Spring Context and will initialize the Spring Integration routes.
+ *
+ * @author Gunnar Hillert
+ * @since 2.2
+ *
+ */
+public final class Main {
+
+ private static final Logger LOGGER = Logger.getLogger(Main.class);
+
+ private static final String HORIZONTAL_LINE =
+ "\n=========================================================";
+
+ private Main() { }
+
+ /**
+ * Load the Spring Integration Application Context
+ *
+ * @param args - command line arguments
+ */
+ public static void main(final String... args) {
+
+ LOGGER.info(HORIZONTAL_LINE
+ + "\n"
+ + "\n Welcome to Spring Integration! "
+ + "\n"
+ + "\n For more information please visit: "
+ + "\n http://www.springsource.org/spring-integration "
+ + "\n"
+ + HORIZONTAL_LINE );
+
+ final AbstractApplicationContext context =
+ new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/*-context.xml");
+
+ context.registerShutdownHook();
+
+ final Scanner scanner = new Scanner(System.in);
+
+ LOGGER.info(HORIZONTAL_LINE
+ + "\n"
+ + "\n Please press 'q + Enter' to quit the application. "
+ + "\n"
+ + HORIZONTAL_LINE );
+
+ while (true) {
+
+ final String input = scanner.nextLine();
+
+ if("q".equals(input.trim())) {
+ break;
+ }
+
+ }
+
+ LOGGER.info("Exiting application...bye.");
+
+ System.exit(0);
+
+ }
+}
@@ -0,0 +1,129 @@
+/*
+ * Copyright 2002-2012 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.integration.samples.mailattachments.support;
+
+import java.io.File;
+
+import org.springframework.util.Assert;
+
+/**
+ * Represents a part of the original email messsage. EmailFragments could be either
+ * Email messages themselves or also attachments. The sample will use {@link EmailFragment}s
+ * to ultimately write the various pieces that constitute an email message out to
+ * the file system.
+ *
+ * @author Gunnar Hillert
+ * @since 2.2
+ *
+ */
+public class EmailFragment {
+
+ private Object data;
+ private String filename;
+ private File directory;
+
+ /**
+ * Constructor.
+ *
+ * @param directory Must not be null
+ * @param filename Must not be null
+ * @param data Must not be null
+ */
+ public EmailFragment(File directory, String filename, Object data) {
+ super();
+
+ Assert.notNull(directory);
+ Assert.hasText(filename);
+ Assert.notNull(data);
+
+ this.directory = directory;
+ this.filename = filename;
+ this.data = data;
+ }
+
+ /**
+ * The data to save to the file system, e.g. text messages/attachments, binary
+ * file attachments etc.
+ */
+ public Object getData() {
+ return data;
+ }
+
+ /**
+ * The file name to create for the respective {@link EmailFragment}.
+ */
+ public String getFilename() {
+ return filename;
+ }
+
+ /**
+ * The directory where to store the {@link #getData()} using the specified
+ * {@link #getFilename()}.
+ *
+ */
+ public File getDirectory() {
+ return this.directory;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result
+ + ((directory == null) ? 0 : directory.hashCode());
+ result = prime * result
+ + ((filename == null) ? 0 : filename.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ EmailFragment other = (EmailFragment) obj;
+ if (directory == null) {
+ if (other.directory != null) {
+ return false;
+ }
+ }
+ else if (!directory.equals(other.directory)) {
+ return false;
+ }
+ if (filename == null) {
+ if (other.filename != null) {
+ return false;
+ }
+ }
+ else if (!filename.equals(other.filename)) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ return "EmailFragment [filename=" + filename + ", directory="
+ + directory + "]";
+ }
+
+}
Oops, something went wrong.

0 comments on commit 291cdb8

Please sign in to comment.