INT-3369: Syslog Converter asMap Option

JIRA: https://jira.spring.io/browse/INT-3369

INT-3369: add option asMap in DefaultMessageConverter
* add test

GH-1250
This commit is contained in:
David Liu
2014-08-08 10:06:59 +08:00
committed by Gary Russell
parent 9766c8b4e7
commit e3644d8792
4 changed files with 60 additions and 6 deletions

View File

@@ -31,11 +31,11 @@ import org.springframework.integration.transformer.SyslogToMapTransformer;
import org.springframework.messaging.Message;
/**
* Default {@link MessageConverter}; delegates to a {@link SyslogToMapTransformer}
* to convert the payload to a map of values and also provides some of the map
* contents as message headers.
* See @link {@link SyslogHeaders} for the headers that are mapped.
* Default {@link MessageConverter}; delegates to a {@link SyslogToMapTransformer} to
* convert the payload to a map of values and also provides some of the map contents as
* message headers. See @link {@link SyslogHeaders} for the headers that are mapped.
* @author Gary Russell
* @author David Liu
* @since 3.0
*
*/
@@ -50,6 +50,15 @@ public class DefaultMessageConverter implements MessageConverter, BeanFactoryAwa
private volatile MessageBuilderFactory messageBuilderFactory = new DefaultMessageBuilderFactory();
private volatile boolean asMap = true;
/**
* Set false will leave the payload as the original complete syslog.
* @param asMap
*/
public void setAsMap(boolean asMap) {
this.asMap = asMap;
}
@Override
public final void setBeanFactory(BeanFactory beanFactory) {
@@ -71,7 +80,7 @@ public class DefaultMessageConverter implements MessageConverter, BeanFactoryAwa
out.put(SyslogHeaders.PREFIX + entry.getKey(), entry.getValue());
}
}
return this.messageBuilderFactory.withPayload(map)
return this.messageBuilderFactory.withPayload(this.asMap ? map : message.getPayload())
.copyHeaders(out)
.build();
}

View File

@@ -44,6 +44,7 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.syslog.DefaultMessageConverter;
import org.springframework.integration.syslog.config.SyslogReceivingChannelAdapterFactoryBean;
import org.springframework.integration.test.util.SocketUtils;
import org.springframework.integration.test.util.TestUtils;
@@ -52,6 +53,7 @@ import org.springframework.messaging.PollableChannel;
/**
* @author Gary Russell
* @author David Liu
* @since 3.0
*
*/
@@ -132,4 +134,33 @@ public class SyslogReceivingChannelAdapterTests {
assertTrue(latch.await(10, TimeUnit.SECONDS));
}
@Test
public void testAsMapFalse() throws Exception {
SyslogReceivingChannelAdapterFactoryBean factory = new SyslogReceivingChannelAdapterFactoryBean(
SyslogReceivingChannelAdapterFactoryBean.Protocol.udp);
int port = SocketUtils.findAvailableUdpSocket(1514);
factory.setPort(port);
PollableChannel outputChannel = new QueueChannel();
factory.setOutputChannel(outputChannel);
factory.setBeanFactory(mock(BeanFactory.class));
factory.afterPropertiesSet();
factory.start();
UdpSyslogReceivingChannelAdapter adapter = (UdpSyslogReceivingChannelAdapter) factory.getObject();
DefaultMessageConverter defaultMessageConverter = new DefaultMessageConverter();
defaultMessageConverter.setAsMap(false);
adapter.setConverter(defaultMessageConverter);
Thread.sleep(1000);
byte[] buf = "<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE".getBytes("UTF-8");
DatagramPacket packet = new DatagramPacket(buf, buf.length, new InetSocketAddress("localhost", port));
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();
Message<?> message = outputChannel.receive(10000);
assertNotNull(message);
assertEquals("WEBERN", message.getHeaders().get("syslog_HOST"));
assertEquals("<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE",
new String((byte[]) message.getPayload(), "UTF-8"));
adapter.stop();
}
}

View File

@@ -28,6 +28,13 @@
being the <code>Map</code> of Syslog fields. In addition, all fields except the message
are also made available as headers in the message, prefixed with <code>syslog_</code>.
</para>
<para>
Since <emphasis>version 4.1</emphasis>,
the <classname>DefaultMessageConverter</classname> has a property <code>asMap</code>
(default <code>true</code>); when it is <code>false</code>, the converter will leave
the message payload as the original complete syslog message, in a <code>byte[]</code>,
while still setting the headers.
</para>
<section id="syslog-inbound-examplers">
<title>Example Configuration</title>
<programlisting language="xml"><![CDATA[<int-syslog:inbound-channel-adapter id="syslogIn" port="1514" />]]></programlisting>

View File

@@ -169,6 +169,13 @@
for the JSON transformers. See <xref linkend="transformer"/> for more information.
</para>
</section>
<section id="4.1-syslog">
<title>Syslog Adapter</title>
<para>
The default syslog message converter now has an option to retain the original message in
the payload, while still setting the headers.
See <xref linkend="syslog-inbound-adapter"/> for more information.
</para>
</section>
</section>
</chapter>