diff --git a/basic/enricher/README.md b/basic/enricher/README.md new file mode 100644 index 00000000..e03919b3 --- /dev/null +++ b/basic/enricher/README.md @@ -0,0 +1,36 @@ +Spring Integration - Enricher Sample +================================ + +# Overview + +This sample demonstrates how the Enricher components can be used. + +# Getting Started + +You can run the sample application by either + +* running the "Main" class from within STS (Right-click on Main class --> Run As --> Java Application) +* or from the command line execute: + - mvn package + - mvn exec:java + +This example illustrates the usage of the Content Enricher. + +Once the application has started, lease execute the various Content Enricher examples by + +* entering 1 + Enter +* entering 2 + Enter +* entering 3 + Enter + +3 different message flows are triggered. For use-cases 1+2 a **User** object containing only the **username** is passed in. For use-case 3 a Map with the **username** key is passed in and enriched with the **User** object using the **user** key: + +* 1: In the *Enricher*, pass the full **User** object to the **request channel**. +* 2: In the *Enricher*, pass only the **username** to the **request channel** by using the **request-payload-expression** attribute. +* 3: In the *Enricher*, pass only the username to the **request channel**, executing the same Service Activator as in **2**. + +# Resources + +For help please take a look at the Spring Integration documentation: + +http://www.springsource.org/spring-integration + diff --git a/basic/enricher/pom.xml b/basic/enricher/pom.xml new file mode 100644 index 00000000..9c109744 --- /dev/null +++ b/basic/enricher/pom.xml @@ -0,0 +1,123 @@ + + 4.0.0 + + org.springframework.integration.samples + enricher + 1.0-SNAPSHOT + jar + + enricher-sample + http://www.springsource.org/spring-integration + + + UTF-8 + 2.1.0.BUILD-SNAPSHOT + 1.6.1 + 4.7 + + + + + repository.springframework.maven.release + Spring Framework Maven Release Repository + http://maven.springframework.org/release + + + repository.springframework.maven.milestone + Spring Framework Maven Milestone Repository + http://maven.springframework.org/milestone + + + + + + + maven-eclipse-plugin + 2.8 + + + org.springframework.ide.eclipse.core.springnature + + + org.springframework.ide.eclipse.core.springbuilder + + true + true + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + -Xlint:all + true + true + + + + org.codehaus.mojo + exec-maven-plugin + 1.2 + + org.springframework.integration.samples.enricher.Main + + + + + + + + + + + junit + junit + ${junit.version} + test + + + + + + org.springframework.integration + spring-integration-core + ${spring.integration.version} + + + + + + ch.qos.logback + logback-classic + 0.9.28 + + + org.slf4j + slf4j-api + ${slf4j.version} + + + org.slf4j + log4j-over-slf4j + ${slf4j.version} + + + org.slf4j + jcl-over-slf4j + ${slf4j.version} + + + + + + com.h2database + h2 + 1.3.160 + + + + diff --git a/basic/enricher/src/main/java/org/springframework/integration/samples/enricher/Main.java b/basic/enricher/src/main/java/org/springframework/integration/samples/enricher/Main.java new file mode 100644 index 00000000..92f67e0c --- /dev/null +++ b/basic/enricher/src/main/java/org/springframework/integration/samples/enricher/Main.java @@ -0,0 +1,142 @@ +/* + * Copyright 2002-2010 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.enricher; + +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.integration.samples.enricher.service.UserService; + + +/** + * Starts the Spring Context and will initialize the Spring Integration routes. + * + * @author Gunnar Hillert + * @version 1.0 + * + */ +public final class Main { + + private static final Logger LOGGER = LoggerFactory.getLogger(Main.class); + + private static final String LINE_SEPARATOR = "\n=========================================================================="; + private static final String EMPTY_LINE = "\n "; + + private Main() { } + + /** + * Load the Spring Integration Application Context + * + * @param args - command line arguments + */ + public static void main(final String... args) { + + LOGGER.info(LINE_SEPARATOR + + EMPTY_LINE + + "\n Welcome to Spring Integration! " + + EMPTY_LINE + + "\n For more information please visit: " + + "\n http://www.springsource.org/spring-integration " + + EMPTY_LINE + + LINE_SEPARATOR ); + + final AbstractApplicationContext context = + new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/*-context.xml"); + + context.registerShutdownHook(); + + final Scanner scanner = new Scanner(System.in); + + final UserService service = context.getBean(UserService.class); + + LOGGER.info(LINE_SEPARATOR + + EMPTY_LINE + + "\n Please press 'q + Enter' to quit the application. " + + EMPTY_LINE + + LINE_SEPARATOR + + EMPTY_LINE + + "\n This example illustrates the usage of the Content Enricher. " + + EMPTY_LINE + + "\n Usage: Please enter 1 or 2 or 3 + Enter " + + EMPTY_LINE + + "\n 3 different message flows are triggered. For sample 1+2 a " + + "\n user object containing only the username is passed in. " + + "\n For sample 3 a Map with the 'username' key is passed in and enriched " + + "\n with the user object using the 'user' key. " + + EMPTY_LINE + + "\n 1: In the Enricher, pass the full User object to the request channel. " + + "\n 2: In the Enricher, pass only the username to the request channel. " + + "\n 3: In the Enricher, pass only the username to the request channel. " + + EMPTY_LINE + + LINE_SEPARATOR); + + while (!scanner.hasNext("q")) { + + final String input = scanner.nextLine(); + + User user = new User("foo", null, null); + + if ("1".equals(input)) { + + final User fullUser = service.findUser(user); + printUserInformation(fullUser); + + } else if ("2".equals(input)) { + + final User fullUser = service.findUserByUsername(user); + printUserInformation(fullUser); + + } else if ("3".equals(input)) { + + final Map userData = new HashMap(); + userData.put("username", "foo_map"); + + final Map enrichedUserData = service.findUserWithUsernameInMap(userData); + + final User fullUser = (User) enrichedUserData.get("user"); + + printUserInformation(fullUser); + + } else { + LOGGER.info("\n\n Please enter '1' or '2' :\n\n"); + } + + } + + LOGGER.info("\n\nExiting application...bye."); + + System.exit(0); + + } + + private static void printUserInformation(User user) { + + if (user != null) { + LOGGER.info("\n\n User found - Username: '{}', Email: '{}', Password: '{}'.\n\n", + new Object[] {user.getUsername(), user.getEmail(), user.getPassword()}); + + } else { + LOGGER.info("\n\n No User found for username: 'foo'.\n\n"); + } + + } + +} diff --git a/basic/enricher/src/main/java/org/springframework/integration/samples/enricher/User.java b/basic/enricher/src/main/java/org/springframework/integration/samples/enricher/User.java new file mode 100644 index 00000000..eb6defa9 --- /dev/null +++ b/basic/enricher/src/main/java/org/springframework/integration/samples/enricher/User.java @@ -0,0 +1,63 @@ +/* + * Copyright 2002-2011 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.enricher; + +public class User { + private String username; + private String password; + private String email; + + public User(String username, String password, String email) { + super(); + this.username = username; + this.password = password; + this.email = email; + } + + public String getUsername() { + return this.username; + } + + public String getPassword() { + return this.password; + } + + public String getEmail() { + return this.email; + } + + public void setUsername(String username) { + this.username = username; + } + + public void setPassword(String password) { + this.password = password; + } + + public void setEmail(String email) { + this.email = email; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("User [username=") + .append(this.username) + .append(", password=") + .append(this.password) + .append(", email=") + .append(this.email).append("]"); + return builder.toString(); + } + +} diff --git a/basic/enricher/src/main/java/org/springframework/integration/samples/enricher/service/UserService.java b/basic/enricher/src/main/java/org/springframework/integration/samples/enricher/service/UserService.java new file mode 100644 index 00000000..3b2cdcac --- /dev/null +++ b/basic/enricher/src/main/java/org/springframework/integration/samples/enricher/service/UserService.java @@ -0,0 +1,46 @@ +/* + * Copyright 2002-2011 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.enricher.service; + +import java.util.Map; + +import org.springframework.integration.samples.enricher.User; + +/** + * Provides user services. + */ +public interface UserService { + + /** + * Retrieves a user based on the provided user. User object is routed to the + * "findUserEnricherChannel" channel. + */ + User findUser(User user); + + /** + * Retrieves a user based on the provided user. User object is routed to the + * "findUserByUsernameEnricherChannel" channel. + */ + User findUserByUsername(User user); + + /** + * Retrieves a user based on the provided username that is provided as a Map + * entry using the mapkey 'username'. Map object is routed to the + * "findUserWithMapChannel" channel. + */ + Map findUserWithUsernameInMap(Map userdata); + +} diff --git a/basic/enricher/src/main/java/org/springframework/integration/samples/enricher/service/impl/SystemService.java b/basic/enricher/src/main/java/org/springframework/integration/samples/enricher/service/impl/SystemService.java new file mode 100644 index 00000000..a6a41625 --- /dev/null +++ b/basic/enricher/src/main/java/org/springframework/integration/samples/enricher/service/impl/SystemService.java @@ -0,0 +1,55 @@ +/* + * Copyright 2002-2011 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.enricher.service.impl; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.integration.samples.enricher.User; + +/** + * Simple Service class for retrieving user information. + * + * @author Gunnar Hillert + * + */ +public class SystemService { + + private static final Logger LOGGER = LoggerFactory.getLogger(SystemService.class); + + /** Default Constructor. */ + public SystemService() { + super(); + } + + public User findUser(User user) { + + LOGGER.info("Calling method 'findUser' with parameter {}", user); + + final User fullUser = new User(user.getUsername(), + "secret", + user.getUsername() + "@springintegration.org"); + return fullUser; + } + + public User findUserByUsername(String username) { + + LOGGER.info("Calling method 'findUserByUsername' with parameter: {}", username); + + return new User(username, "secret", username + "@springintegration.org"); + + } + +} diff --git a/basic/enricher/src/main/resources/META-INF/spring/integration/spring-integration-context.xml b/basic/enricher/src/main/resources/META-INF/spring/integration/spring-integration-context.xml new file mode 100644 index 00000000..b9b57f57 --- /dev/null +++ b/basic/enricher/src/main/resources/META-INF/spring/integration/spring-integration-context.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/basic/enricher/src/main/resources/logback.xml b/basic/enricher/src/main/resources/logback.xml new file mode 100644 index 00000000..eee6b966 --- /dev/null +++ b/basic/enricher/src/main/resources/logback.xml @@ -0,0 +1,24 @@ + + + + + + + %d %5p | %t | %-55logger{55} | %m %n + + + + + + + + + + + + + + + + + diff --git a/basic/enricher/src/test/java/org/springframework/integration/samples/enricher/service/UserServiceTest.java b/basic/enricher/src/test/java/org/springframework/integration/samples/enricher/service/UserServiceTest.java new file mode 100644 index 00000000..ac16ba85 --- /dev/null +++ b/basic/enricher/src/test/java/org/springframework/integration/samples/enricher/service/UserServiceTest.java @@ -0,0 +1,77 @@ +/* + * Copyright 2002-2010 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.enricher.service; + +import static junit.framework.Assert.*; + +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.integration.samples.enricher.User; +import org.springframework.integration.samples.enricher.service.UserService; + + +/** + * Verify that the Spring Integration Application Context starts successfully. + */ +public class UserServiceTest { + + @Test + public void testStartupOfSpringInegrationContext() throws Exception{ + final ApplicationContext context + = new ClassPathXmlApplicationContext("/META-INF/spring/integration/spring-integration-context.xml", + UserServiceTest.class); + Thread.sleep(2000); + } + + @Test + public void testExecuteFindUser() { + + final ApplicationContext context + = new ClassPathXmlApplicationContext("/META-INF/spring/integration/spring-integration-context.xml", + UserServiceTest.class); + + final UserService service = context.getBean(UserService.class); + + User user = new User("foo", null, null); + final User fullUser = service.findUser(user); + + assertEquals("foo", fullUser.getUsername()); + assertEquals("foo@springintegration.org", fullUser.getEmail()); + assertEquals("secret", fullUser.getPassword()); + + } + + @Test + public void testExecuteFindUserByUsername() { + + final ApplicationContext context + = new ClassPathXmlApplicationContext("/META-INF/spring/integration/spring-integration-context.xml", + UserServiceTest.class); + + final UserService service = context.getBean(UserService.class); + + User user = new User("foo", null, null); + final User fullUser = service.findUserByUsername(user); + + assertEquals("foo", fullUser.getUsername()); + assertEquals("foo@springintegration.org", fullUser.getEmail()); + assertEquals("secret", fullUser.getPassword()); + + } + +} diff --git a/basic/pom.xml b/basic/pom.xml index 6a0492e0..c183f4c5 100644 --- a/basic/pom.xml +++ b/basic/pom.xml @@ -11,6 +11,7 @@ amqp + enricher feed file ftp