Search in sources :

Example 1 with SyslogReceivingChannelAdapterFactoryBean

use of org.springframework.integration.syslog.config.SyslogReceivingChannelAdapterFactoryBean in project spring-integration by spring-projects.

the class SyslogReceivingChannelAdapterTests method testAsMapFalse.

@Test
public void testAsMapFalse() throws Exception {
    SyslogReceivingChannelAdapterFactoryBean factory = new SyslogReceivingChannelAdapterFactoryBean(SyslogReceivingChannelAdapterFactoryBean.Protocol.udp);
    factory.setPort(0);
    PollableChannel outputChannel = new QueueChannel();
    factory.setOutputChannel(outputChannel);
    factory.setBeanFactory(mock(BeanFactory.class));
    factory.afterPropertiesSet();
    factory.start();
    UnicastReceivingChannelAdapter server = TestUtils.getPropertyValue(factory, "adapter.udpAdapter", UnicastReceivingChannelAdapter.class);
    TestingUtilities.waitListening(server, null);
    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", adapter.getPort()));
    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();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) InetSocketAddress(java.net.InetSocketAddress) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UnicastReceivingChannelAdapter(org.springframework.integration.ip.udp.UnicastReceivingChannelAdapter) SyslogReceivingChannelAdapterFactoryBean(org.springframework.integration.syslog.config.SyslogReceivingChannelAdapterFactoryBean) DefaultMessageConverter(org.springframework.integration.syslog.DefaultMessageConverter) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 2 with SyslogReceivingChannelAdapterFactoryBean

use of org.springframework.integration.syslog.config.SyslogReceivingChannelAdapterFactoryBean in project spring-integration by spring-projects.

the class SyslogReceivingChannelAdapterTests method testTcpRFC5424.

@Test
public void testTcpRFC5424() throws Exception {
    SyslogReceivingChannelAdapterFactoryBean factory = new SyslogReceivingChannelAdapterFactoryBean(SyslogReceivingChannelAdapterFactoryBean.Protocol.tcp);
    PollableChannel outputChannel = new QueueChannel();
    factory.setOutputChannel(outputChannel);
    ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
    final CountDownLatch latch = new CountDownLatch(2);
    doAnswer(invocation -> {
        latch.countDown();
        return null;
    }).when(publisher).publishEvent(any(ApplicationEvent.class));
    factory.setBeanFactory(mock(BeanFactory.class));
    AbstractServerConnectionFactory connectionFactory = new TcpNioServerConnectionFactory(0);
    connectionFactory.setDeserializer(new RFC6587SyslogDeserializer());
    connectionFactory.setApplicationEventPublisher(publisher);
    factory.setConnectionFactory(connectionFactory);
    factory.setConverter(new RFC5424MessageConverter());
    factory.afterPropertiesSet();
    factory.start();
    TestingUtilities.waitListening(connectionFactory, null);
    TcpSyslogReceivingChannelAdapter adapter = (TcpSyslogReceivingChannelAdapter) factory.getObject();
    Log logger = spy(TestUtils.getPropertyValue(adapter, "logger", Log.class));
    doReturn(true).when(logger).isDebugEnabled();
    final CountDownLatch sawLog = new CountDownLatch(1);
    doAnswer(invocation -> {
        if (((String) invocation.getArgument(0)).contains("Error on syslog socket")) {
            sawLog.countDown();
        }
        invocation.callRealMethod();
        return null;
    }).when(logger).debug(anyString());
    new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
    Thread.sleep(1000);
    byte[] buf = ("253 <14>1 2014-06-20T09:14:07+00:00 loggregator d0602076-b14a-4c55-852a-981e7afeed38 DEA - " + "[exampleSDID@32473 iut=\\\"3\\\" eventSource=\\\"Application\\\" eventID=\\\"1011\\\"]" + "[exampleSDID@32473 iut=\\\"3\\\" eventSource=\\\"Application\\\" eventID=\\\"1011\\\"] Removing instance").getBytes("UTF-8");
    Socket socket = SocketFactory.getDefault().createSocket("localhost", connectionFactory.getPort());
    socket.getOutputStream().write(buf);
    socket.close();
    assertTrue(sawLog.await(10, TimeUnit.SECONDS));
    @SuppressWarnings("unchecked") Message<Map<String, ?>> message = (Message<Map<String, ?>>) outputChannel.receive(10000);
    assertNotNull(message);
    assertEquals("loggregator", message.getPayload().get("syslog_HOST"));
    adapter.stop();
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) RFC5424MessageConverter(org.springframework.integration.syslog.RFC5424MessageConverter) Message(org.springframework.messaging.Message) TcpNioServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory) Log(org.apache.commons.logging.Log) ApplicationEvent(org.springframework.context.ApplicationEvent) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) SyslogReceivingChannelAdapterFactoryBean(org.springframework.integration.syslog.config.SyslogReceivingChannelAdapterFactoryBean) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) Map(java.util.Map) Socket(java.net.Socket) DatagramSocket(java.net.DatagramSocket) Test(org.junit.Test)

Example 3 with SyslogReceivingChannelAdapterFactoryBean

use of org.springframework.integration.syslog.config.SyslogReceivingChannelAdapterFactoryBean in project spring-integration by spring-projects.

the class SyslogReceivingChannelAdapterTests method testUdp.

@Test
public void testUdp() throws Exception {
    SyslogReceivingChannelAdapterFactoryBean factory = new SyslogReceivingChannelAdapterFactoryBean(SyslogReceivingChannelAdapterFactoryBean.Protocol.udp);
    PollableChannel outputChannel = new QueueChannel();
    factory.setPort(0);
    factory.setOutputChannel(outputChannel);
    factory.setBeanFactory(mock(BeanFactory.class));
    factory.afterPropertiesSet();
    factory.start();
    UnicastReceivingChannelAdapter server = TestUtils.getPropertyValue(factory, "adapter.udpAdapter", UnicastReceivingChannelAdapter.class);
    TestingUtilities.waitListening(server, null);
    UdpSyslogReceivingChannelAdapter adapter = (UdpSyslogReceivingChannelAdapter) factory.getObject();
    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", server.getPort()));
    DatagramSocket socket = new DatagramSocket();
    socket.send(packet);
    socket.close();
    Message<?> message = outputChannel.receive(10000);
    assertNotNull(message);
    assertEquals("WEBERN", message.getHeaders().get("syslog_HOST"));
    adapter.stop();
}
Also used : SyslogReceivingChannelAdapterFactoryBean(org.springframework.integration.syslog.config.SyslogReceivingChannelAdapterFactoryBean) QueueChannel(org.springframework.integration.channel.QueueChannel) DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(java.net.DatagramPacket) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) UnicastReceivingChannelAdapter(org.springframework.integration.ip.udp.UnicastReceivingChannelAdapter) Test(org.junit.Test)

Example 4 with SyslogReceivingChannelAdapterFactoryBean

use of org.springframework.integration.syslog.config.SyslogReceivingChannelAdapterFactoryBean in project spring-integration by spring-projects.

the class SyslogReceivingChannelAdapterTests method testUdpRFC5424.

@Test
public void testUdpRFC5424() throws Exception {
    SyslogReceivingChannelAdapterFactoryBean factory = new SyslogReceivingChannelAdapterFactoryBean(SyslogReceivingChannelAdapterFactoryBean.Protocol.udp);
    factory.setPort(0);
    PollableChannel outputChannel = new QueueChannel();
    factory.setOutputChannel(outputChannel);
    factory.setBeanFactory(mock(BeanFactory.class));
    factory.setConverter(new RFC5424MessageConverter());
    factory.afterPropertiesSet();
    factory.start();
    UnicastReceivingChannelAdapter server = TestUtils.getPropertyValue(factory, "adapter.udpAdapter", UnicastReceivingChannelAdapter.class);
    TestingUtilities.waitListening(server, null);
    UdpSyslogReceivingChannelAdapter adapter = (UdpSyslogReceivingChannelAdapter) factory.getObject();
    Thread.sleep(1000);
    byte[] buf = ("<14>1 2014-06-20T09:14:07+00:00 loggregator d0602076-b14a-4c55-852a-981e7afeed38 DEA - " + "[exampleSDID@32473 iut=\\\"3\\\" eventSource=\\\"Application\\\" eventID=\\\"1011\\\"]" + "[exampleSDID@32473 iut=\\\"3\\\" eventSource=\\\"Application\\\" eventID=\\\"1011\\\"] Removing instance").getBytes("UTF-8");
    DatagramPacket packet = new DatagramPacket(buf, buf.length, new InetSocketAddress("localhost", adapter.getPort()));
    DatagramSocket socket = new DatagramSocket();
    socket.send(packet);
    socket.close();
    @SuppressWarnings("unchecked") Message<Map<String, ?>> message = (Message<Map<String, ?>>) outputChannel.receive(10000);
    assertNotNull(message);
    assertEquals("loggregator", message.getPayload().get("syslog_HOST"));
    adapter.stop();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) RFC5424MessageConverter(org.springframework.integration.syslog.RFC5424MessageConverter) Message(org.springframework.messaging.Message) InetSocketAddress(java.net.InetSocketAddress) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UnicastReceivingChannelAdapter(org.springframework.integration.ip.udp.UnicastReceivingChannelAdapter) SyslogReceivingChannelAdapterFactoryBean(org.springframework.integration.syslog.config.SyslogReceivingChannelAdapterFactoryBean) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) Map(java.util.Map) Test(org.junit.Test)

Example 5 with SyslogReceivingChannelAdapterFactoryBean

use of org.springframework.integration.syslog.config.SyslogReceivingChannelAdapterFactoryBean in project spring-integration by spring-projects.

the class SyslogReceivingChannelAdapterTests method testTcp.

@Test
public void testTcp() throws Exception {
    SyslogReceivingChannelAdapterFactoryBean factory = new SyslogReceivingChannelAdapterFactoryBean(SyslogReceivingChannelAdapterFactoryBean.Protocol.tcp);
    factory.setPort(0);
    PollableChannel outputChannel = new QueueChannel();
    factory.setOutputChannel(outputChannel);
    ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
    final CountDownLatch latch = new CountDownLatch(2);
    doAnswer(invocation -> {
        latch.countDown();
        return null;
    }).when(publisher).publishEvent(any(ApplicationEvent.class));
    factory.setApplicationEventPublisher(publisher);
    factory.setBeanFactory(mock(BeanFactory.class));
    factory.afterPropertiesSet();
    factory.start();
    AbstractServerConnectionFactory server = TestUtils.getPropertyValue(factory, "adapter.connectionFactory", AbstractServerConnectionFactory.class);
    TestingUtilities.waitListening(server, null);
    TcpSyslogReceivingChannelAdapter adapter = (TcpSyslogReceivingChannelAdapter) factory.getObject();
    Log logger = spy(TestUtils.getPropertyValue(adapter, "logger", Log.class));
    doReturn(true).when(logger).isDebugEnabled();
    final CountDownLatch sawLog = new CountDownLatch(1);
    doAnswer(invocation -> {
        if (((String) invocation.getArgument(0)).contains("Error on syslog socket")) {
            sawLog.countDown();
        }
        invocation.callRealMethod();
        return null;
    }).when(logger).debug(anyString());
    new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
    Thread.sleep(1000);
    byte[] buf = "<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE\n".getBytes("UTF-8");
    Socket socket = SocketFactory.getDefault().createSocket("localhost", server.getPort());
    socket.getOutputStream().write(buf);
    socket.close();
    assertTrue(sawLog.await(10, TimeUnit.SECONDS));
    Message<?> message = outputChannel.receive(10000);
    assertNotNull(message);
    assertEquals("WEBERN", message.getHeaders().get("syslog_HOST"));
    adapter.stop();
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) Log(org.apache.commons.logging.Log) ApplicationEvent(org.springframework.context.ApplicationEvent) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) SyslogReceivingChannelAdapterFactoryBean(org.springframework.integration.syslog.config.SyslogReceivingChannelAdapterFactoryBean) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) Socket(java.net.Socket) DatagramSocket(java.net.DatagramSocket) Test(org.junit.Test)

Aggregations

DatagramSocket (java.net.DatagramSocket)5 Test (org.junit.Test)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 QueueChannel (org.springframework.integration.channel.QueueChannel)5 SyslogReceivingChannelAdapterFactoryBean (org.springframework.integration.syslog.config.SyslogReceivingChannelAdapterFactoryBean)5 PollableChannel (org.springframework.messaging.PollableChannel)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 DatagramPacket (java.net.DatagramPacket)3 InetSocketAddress (java.net.InetSocketAddress)3 UnicastReceivingChannelAdapter (org.springframework.integration.ip.udp.UnicastReceivingChannelAdapter)3 Socket (java.net.Socket)2 Map (java.util.Map)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Log (org.apache.commons.logging.Log)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 ApplicationEvent (org.springframework.context.ApplicationEvent)2 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)2 AbstractServerConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory)2 RFC5424MessageConverter (org.springframework.integration.syslog.RFC5424MessageConverter)2 Message (org.springframework.messaging.Message)2