Category Archives: Spring Integration

Doing HTTPS communication with http:outbound-gateway

In Spring Integration when using http:outbound-gateway if you have to do https communication, you need to do the following trick,

<int-http:outbound-gateway id="my.outbound.gateway"
request-channel="request-channel" reply-channel="reply-channel"
url="https://localhost:8443/myservice/myService"
http-method="POST"
expected-response-type="java.lang.String">
</int-http:outbound-gateway>

Now you need to pass the keystore and trust store of the client and server as follows,

<bean id="trustStore">
<property name="targetObject" value="#{@systemProperties}" />
<property name="targetMethod" value="putAll" />
<property name="arguments">
<props>
<prop key="javax.net.ssl.trustStore"><jks key location></prop>
<prop key="javax.net.ssl.keyStorePassword">password</prop>
</props>
</property>
</bean>

<bean id="keystore">
<property name="targetObject" value="#{@systemProperties}" />
<property name="targetMethod" value="putAll" />
<property name="arguments">
<props>
<prop key="javax.net.ssl.keyStore"><jks key location></prop>
<prop key="javax.net.ssl.keyStorePassword">password</prop>
</props>
</property>
</bean>

There is also another way as mentioned in this article @ http://forum.springsource.org/showthread.php?115198-HTTPS-in-http-outbound-gateway

I hope this blog helped you.

Advertisements

Spring Integration FakeFtpServer example

For people in hurry, get the latest code and the steps in GitHub.

To run the junit test, run “mvn test” and understand the test flow.

Introduction: FakeFtpServer

In this Spring Integration FakeFtpServer example, I will demonstrate using Spring FakeFtpServer to JUnit test a Spring Integration flow. This is a interesting topic, there are few articles like Unit testing file transfers, which gives some insight on this topic.

In this blog, we will test a Spring Integration flow which checks for a list of files, apply a splitter to separate each file and start downloading them into local location, once the download is complete, it will delete the files on the FTP server. In my next blog, I will show how to do JUnit testing of Spring Integration flow with SFTP Server.

Spring Integration flow

Spring Integration FakeFtpServer example

Spring Integration FakeFtpServer example

In order to use FakeFtpServer we need to have Maven dependency as below,


<dependency>
<groupId>org.mockftpserver</groupId>
<artifactId>MockFtpServer</artifactId>
<version>2.3</version>
<scope>test</scope>
</dependency>

The first step to this is to create a FakeFtpServer before every test runs as below,


@Before
public void setUp() throws Exception {
fakeFtpServer = new FakeFtpServer();
fakeFtpServer.setServerControlPort(9999); // use any free port
FileSystem fileSystem = new UnixFakeFileSystem();
fileSystem.add(new FileEntry(FILE, CONTENTS));
fakeFtpServer.setFileSystem(fileSystem);
UserAccount userAccount = new UserAccount("user", "password", HOME_DIR);
fakeFtpServer.addUserAccount(userAccount);
fakeFtpServer.start();
}

@After
public void tearDown() throws Exception {
fakeFtpServer.stop();
}

Finally run the JUnit test case as below,

    @Autowired
private FileDownloadUtil downloadUtil;
@Test
public void testFtpDownload() throws Exception {
File file = new File("src/test/resources/output");
delete(file);
FTPClient client = new FTPClient();
client.connect("localhost", 9999);
client.login("user", "password");
String files[] = client.listNames("/dir");
client.help();
logger.debug("Before delete" + files[0]);
assertEquals(1, files.length);
downloadUtil.downloadFilesFromRemoteDirectory();
logger.debug("After delete");
files = client.listNames("/dir");
client.help();
assertEquals(0, files.length);
assertEquals(1, file.list().length);
}

I hope this blog helped.

Spring Integration Mock SftpServer example

In continuation of my earlier blog Spring Integration FakeFtpServer example in this example I will show how to test Spring Integration flow using Mock SftpServer. There are few good writeup on the net including the Stackoverflow writeup, Using Apache Mina as a Mock/In Memory SFTP Server for Unit Testing.The code for this blog is @ Spring Integration flow to test Ftp/Sftp server.

To run the junit test, run “mvn test” and understand the test flow.

Again talking of the same spring integration flow as mentioned in my earlier blog, I will write test for sftp server,

Spring Integration Mock SftpServer example

Spring Integration Mock SftpServer example

The maven dependency for this is as below,


<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>0.5.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.49</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>

The startup and teardown of the junit is as below,


@Before
public void beforeTestSetup() throws Exception {
sshd = SshServer.setUpDefaultServer();
sshd.setPort(22999);

sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));
sshd.setPasswordAuthenticator(new PasswordAuthenticator() {

public boolean authenticate(String username, String password, ServerSession session) {
// TODO Auto-generated method stub
return true;
}
});

CommandFactory myCommandFactory = new CommandFactory() {

public Command createCommand(String command) {
System.out.println("Command: " + command);
return null;
}
};
sshd.setCommandFactory(new ScpCommandFactory(myCommandFactory));

List<NamedFactory<command>> namedFactoryList = new ArrayList<NamedFactory<command>>(); namedFactoryList.add(new SftpSubsystem.Factory()); sshd.setSubsystemFactories(namedFactoryList); sshd.start(); }

@After

public void teardown() throws Exception { sshd.stop(); }

The Junit test is as below,

public void testPutAndGetFile() throws Exception {
JSch jsch = new JSch();

Hashtable config = new Hashtable();
config.put("StrictHostKeyChecking", "no");
JSch.setConfig(config);

Session session = jsch.getSession("remote-username", "localhost", 22999);
session.setPassword("remote-password");

session.connect();

Channel channel = session.openChannel("sftp");
channel.connect();

ChannelSftp sftpChannel = (ChannelSftp) channel;

final String testFileContents = "some file contents";

String uploadedFileName = "uploadFile";
sftpChannel.put(new ByteArrayInputStream(testFileContents.getBytes()), uploadedFileName);

String downloadedFileName = "downLoadFile";
sftpChannel.get(uploadedFileName, downloadedFileName);

File downloadedFile = new File(downloadedFileName);
assertTrue(downloadedFile.exists());

String fileData = getFileContents(downloadedFile);

assertEquals(testFileContents, fileData);

if (sftpChannel.isConnected()) {
sftpChannel.exit();
logger.debug("Disconnected channel");
}

if (session.isConnected()) {
session.disconnect();
logger.debug("Disconnected session");
}

}

I hope this blog helped you.

Managing Raw SOAP message within Spring Integration – Part 2

In my earlier blog, Managing Raw SOAP message within Spring Integration, I demonstrated how to get the raw SOAP message from int-http:inbound-gateway and process the SOAP message.

There are few scenarios, where in the Spring Integration flow is invoked by an event. Where Spring Integration flow, should get a payload from source Webservice and just transfer it to target Webservice, acting as a Proxy, retaining all the properties of the source and target systems.

In this blog I will demonstrate how you can create a Proxy and how int-ws-outbound-gateway can call the Proxy service when calling Source Webservice. If you notice int-ws-outbound-gateway carefully, we can configure the security keystore and other properties. These properties will be retained, when the payload is passed to the Proxy. Once the request payload is in the Proxy, it can be sent to the source Webservice and once we get the response, we can bind it to another channel, which can be processed further or sent to the target Webservice.

Managing Raw SOAP message within Spring Integration

Managing Raw SOAP message within Spring Integration


<int:channel id="ws.outbound" />

 <int-ws:outbound-gateway id="ws.gateway"
 request-channel="ws.inbound" reply-channel="ws.outbound"
 ignore-empty-responses="true" message-sender="common.message.sender.ws"
 uri="http://localhost:8080/http-proxy-for-ws/receiveGateway" />

 <int-http:inbound-gateway id="http.proxy.gateway"
 request-channel="http.proxy.inbound" name="/receiveGateway"
 supported-methods="POST" />

 <int:channel id="http.proxy.inbound" />

 <int:service-activator input-channel="http.proxy.inbound"
 id="httpProxyServiceActivator" ref="httpProxyServiceActivatorBean"
 method="sendXml" />

 <int:channel id="http.proxy.outbound" />

 <bean id="httpProxyServiceActivatorBean">
 <property name="uri"
 value="http://www.webservicex.net/CurrencyConvertor.asmx" />
 <property name="soapAction" value="http://www.webserviceX.NET/ConversionRate" />
 <property name="wsOutboundChannel" ref="http.proxy.outbound" />
 </bean>

 <int:service-activator input-channel="http.proxy.outbound"
 id="finalProcessorServiceActivator" ref="finalProcessorServiceActivatorBean"
 method="sendXml" />

 <bean id="finalProcessorServiceActivatorBean" />
 

Managing Raw SOAP message within Spring Integration

In Spring Integration if we use int-ws:outbound-gateway, once a message is associated to the request channel, the Spring Integration container add the SOAP header information and sends the SOAP payload to the target system to be processed. Most of the time, this is what is needed, but sometimes, we just need the integration layer to act as a proxy. The SOAP payload coming from the source system is just handed to the target system retaining all the properties of source system like security, enrichment and others.

In this case the trick is instead of using int-ws:outbound-gateway, we should use int-http:inbound-gateway and int-http:outbound-gateway. This way you can fool the target system to accept this as a SOAP message. We can also make it aware what soapAction we need to execute is a below,


<int-http:inbound-gateway id="webservice.inbound.gateway"
request-channel="http.outbound.enrich.in" reply-channel="ws.outbound"
name="/receiveGateway" supported-methods="POST" />

<int:header-enricher input-channel="http.outbound.enrich.in"
output-channel="ws.inbound">
<int:header name="Content-Type" value="text/xml;charset=UTF-8" />
<int:header name="SoapAction"
value="http://www.webserviceX.NET/ConversionRate" />
</int:header-enricher>

<int:channel id="ws.outbound" />

<int-http:outbound-gateway id="webservice.outbound.gateway"
request-channel="ws.inbound" reply-channel="ws.outbound"
url="http://www.webservicex.net/CurrencyConvertor.asmx"
expected-response-type="java.lang.String" />

The Spring Integration flow is as below,

Managing Raw SOAP message within Spring Integration

Managing Raw SOAP message within Spring Integration

I hope this blog helped you.

Claimcheck pattern with vFabric RabbitMQ and Gemfire

For people in hurry refer the steps to run this sample.

In continuation to my earlier blog on Claimcheck Pattern in this blog I use AMQP backed message channel to simplify implement Claimcheck Pattern. There is also another good blog on Spring Integration – Payload Storage via Claim-check, which talks about Claimcheck Pattern

The Enterprise Integration Pattern flow is as below,

Claimcheck pattern with vFabric RabbitMQ and Gemfire

Claimcheck pattern with vFabric RabbitMQ and Gemfire

The use case in this blog is, there is a message that gets posted on to a Queue, the message checks into a vFabric Gemfire messageStore, and a GUID is extracted and posted on to a vFabric RabbitMQ Exchange. The subscribers listening to that Exchange and will get the GUID and checks out of the messageStore and calls a Service activator.

As per the Test First development, the JUnit test case looks as below,

@Test
public void testIntegration() {
String request = streamToString(getClass().getResourceAsStream(
"/data/payload.xml"));
Message<String> message = MessageBuilder.withPayload(request)
.build();
channel.send(message);
String outMessage = outChannel.receive(1000).getPayload().toString();
assertEquals(outMessage, request);
}

In the test code, since there is no transformation, we just compare the input data to the data coming out of the target channel.

The test configuration looks as below,

<int:channel id="p2p-pollable-channel" />
<int:publish-subscribe-channel id="pub-sub-channel" />
<int:channel id="checkout-channel3" >
<int:queue />
</int:channel>
<int:claim-check-out message-store="simpleMessageStore"
input-channel="pub-sub-channel" output-channel="checkout-channel3"
remove-message="false" />

In the main configuration the vFabric RabbitMQ configuration looks as below,

<!-- A reference to the org.springframework.amqp.rabbit.connection.ConnectionFactory -->
<rabbit:connection-factory id="connectionFactory" />
<!-- Creates a org.springframework.amqp.rabbit.core.RabbitAdmin to manage
exchanges, queues and bindings -->
<rabbit:admin connection-factory="connectionFactory" />
<int-amqp:channel id="p2p-pollable-channel" connection-factory="connectionFactory" />
<int-amqp:publish-subscribe-channel id="pub-sub-channel" connection-factory="connectionFactory" />

In the main configuration the vFabric Gemfire messageStore configuration looks as below,

<bean id="simpleMessageStore"
class="org.springframework.integration.gemfire.store.GemfireMessageStore">
<constructor-arg ref="myCache" />
</bean>
<gfe:cache id="myCache" />

Finally the integration flow demonstrate the claim checkin and claim checkout as below,

<int:claim-check-in message-store="simpleMessageStore"
input-channel="p2p-pollable-channel" output-channel="pub-sub-channel" />

<int:claim-check-out message-store="simpleMessageStore"
input-channel="pub-sub-channel" output-channel="checkout-channel1"
remove-message="false" />

<int:claim-check-out message-store="simpleMessageStore"
input-channel="pub-sub-channel" output-channel="checkout-channel2"
remove-message="false" />

If you notice carefully, “p2p-pollable-channel” and “pub-sub-channel” are physical Queues and Exchanges in vFabric RabbitMQ as per the last blog on Amqp Backed Spring Integration Using vFabric Rabbitmq

Control Bus Pattern with Spring Integration and JMS

For people in hurry, refer the steps and the demo.

Introduction

Control Bus Pattern is a Enterprise Integration Pattern is used to control distributed systems in Spring Integration. In this blog, I will show you how a Control Bus can control your application or a component to start or stop listening to JMS message. In this example, we are using JMS queue to start and stop the jms inbound-channel-adapter, we can also do this with jdbc inbound-channel-adapter and control this thru an external application. The other way to do the same is by using MBean as in this example.

In this use case, there is a Spring Integration flow. This Spring Integration flow can be controlled by sending start / stop message to inbound-channel-adapter from a ActiveMQ JMS queue.

Details Control Bus with Spring Integration

Control Bus Spring Integration JMS

Control Bus Spring Integration JMS

To start implementing this use case, we write the JUnit test 1st. If you notice once the inboundAdapter is started the message is received from the adapterOutchannel. Once the inboundAdapter is stopped no message is received. This is demonstrated as below,

@Test
public void demoControlBus() {
assertNull(adapterOutputChanel.receive(1000));
controlChannel.send(new GenericMessage<String>("@inboundAdapter.start()"));
assertNotNull(adapterOutputChanel.receive(1000));
controlChannel.send(new GenericMessage<String>("@inboundAdapter.stop()"));
assertNull(adapterOutputChanel.receive(1000));
}

The test configuration looks as below,

<int:inbound-channel-adapter id="inboundAdapter"
channel="controlbus-managed-p2p-pollable-channel" expression="'Hello'"
auto-startup="false">
<int:poller fixed-rate="6000" />
</int:inbound-channel-adapter>

If you run the “mvn test” the tests work. In the main configuration, we will be configuring actual queues and jms inbound-channel-adapter as below,

<int-jms:inbound-channel-adapter id="inboundAdapter"
channel="controlbus-managed-p2p-pollable-channel"
jms-template="jmsTemplate">
<int:poller fixed-rate="6000" />
</int-jms:inbound-channel-adapter>

<int-jms:inbound-channel-adapter id="controlBusAdapter"
channel="control-channel"
jms-template="controlBusJmsTemplate">
<int:poller fixed-rate="6000" />
</int-jms:inbound-channel-adapter>

Now when you start the component as “Run on Server” in STS IDE and post a message on
MyQueue, you can see the subscribers received the messages on the console. You can issue “@inboundAdapter.stop()” on the ControlBusQueue, it will stop the inbound-channel-adapter, it will also throw java.lang.InterruptedException, it looks like a false alarm. To test if the inbound-channel-adapter is stopped, post a message on to MyQueue, the component will not process the message. Now issue “@inboundAdapter.start()” on the ControlBusQueue, it will process the earlier message and start listening for new messages.

Conclusion

If you notice in this blog, we can control the component to listen to message using Control Bus.  The other way to do the same is by using MBean as in this example.

I hope this blog helped you.