From 67606fa2dbb303d5ac001f8bd523815b0efe0397 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Sat, 10 Dec 2011 11:45:57 -0500 Subject: [PATCH] INTSAMPLES-37 Add ConversionService to TCP Sample Show alternative configuration for converting from tcp byte arrays to Strings. --- basic/tcp-client-server/readme.txt | 4 + .../ByteArrayToStringConverter.java | 57 +++++++++++++++ ...tcpClientServerDemo-conversion-context.xml | 73 +++++++++++++++++++ .../TcpClientServerDemoTest.java | 13 +++- 4 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 basic/tcp-client-server/src/main/java/org/springframework/integration/samples/tcpclientserver/ByteArrayToStringConverter.java create mode 100644 basic/tcp-client-server/src/main/resources/META-INF/spring/integration/tcpClientServerDemo-conversion-context.xml diff --git a/basic/tcp-client-server/readme.txt b/basic/tcp-client-server/readme.txt index 3567dd93..653b1f2c 100644 --- a/basic/tcp-client-server/readme.txt +++ b/basic/tcp-client-server/readme.txt @@ -7,6 +7,10 @@ and the result is returned to the client that invoked the original SimpleGateway To run sample simply execute a test case in org.springframework.integration.samples.tcpclientservice package. +Note that the test case includes an alternative configuration that uses the in-built conversion service +and the channel dataType attribute, instead of explicit transformers, to convert from byte arrays to Strings. + +Simply change the @ContextConfiguration to switch between the two techniques. In addition, a simple telnet server is provided; see TelnetServer in src/main/java. Run this class as a java application and then use telnet to connect to the service ('telnet localhost 11111'). diff --git a/basic/tcp-client-server/src/main/java/org/springframework/integration/samples/tcpclientserver/ByteArrayToStringConverter.java b/basic/tcp-client-server/src/main/java/org/springframework/integration/samples/tcpclientserver/ByteArrayToStringConverter.java new file mode 100644 index 00000000..096b1d7c --- /dev/null +++ b/basic/tcp-client-server/src/main/java/org/springframework/integration/samples/tcpclientserver/ByteArrayToStringConverter.java @@ -0,0 +1,57 @@ +/* + * 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.tcpclientserver; + +import java.io.UnsupportedEncodingException; + +import org.springframework.core.convert.converter.Converter; + +/** + * Simple byte array to String converter; allowing the character set + * to be specified. + * + * @author Gary Russell + * @since 2.1 + * + */ +public class ByteArrayToStringConverter implements Converter { + + private String charSet = "UTF-8"; + + public String convert(byte[] bytes) { + try { + return new String(bytes, this.charSet); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + return new String(bytes); + } + } + + /** + * @return the charSet + */ + public String getCharSet() { + return charSet; + } + + /** + * @param charSet the charSet to set + */ + public void setCharSet(String charSet) { + this.charSet = charSet; + } + +} diff --git a/basic/tcp-client-server/src/main/resources/META-INF/spring/integration/tcpClientServerDemo-conversion-context.xml b/basic/tcp-client-server/src/main/resources/META-INF/spring/integration/tcpClientServerDemo-conversion-context.xml new file mode 100644 index 00000000..fd5d0cda --- /dev/null +++ b/basic/tcp-client-server/src/main/resources/META-INF/spring/integration/tcpClientServerDemo-conversion-context.xml @@ -0,0 +1,73 @@ + + + + + This version demonstrates the use of a conversion service and channel 'dataType' + instead of explicit transformers to convert from byte array to String. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java b/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java index 705a0c97..50c830b4 100644 --- a/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java +++ b/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java @@ -30,24 +30,31 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; * to the inbound gateway. In turn the inbound gateway sends the message to an * echo service and the echoed response comes back over tcp and is returned to * the test case for verification. - * + * + * The alternate configuration shows how the conversion service can be used + * instead of explicit transformers to convert the byte array payloads to + * Strings. + * * @author Gary Russell * */ +// This one uses transformers @ContextConfiguration("/META-INF/spring/integration/tcpClientServerDemo-context.xml") +// This one uses the conversion service +//@ContextConfiguration("/META-INF/spring/integration/tcpClientServerDemo-conversion-context.xml") @RunWith(SpringJUnit4ClassRunner.class) public class TcpClientServerDemoTest { @Autowired SimpleGateway gw; - + @Test public void testHappyDay() { String result = gw.send("Hello world!"); System.out.println(result); assertEquals("echo:Hello world!", result); } - + @Test public void testZeroLength() { String result = gw.send("");