org.apache.camel.component.syslog.SyslogDataFormat Java Examples

The following examples show how to use org.apache.camel.component.syslog.SyslogDataFormat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CamelSourceSyslogITCase.java    From camel-kafka-connector with Apache License 2.0 6 votes vote down vote up
private void produceLogMessages(String protocol, String host, String port, String message) throws Exception {
    CamelContext camelContext = new DefaultCamelContext();

    try {
        camelContext.getRegistry().bind("encoder", new Rfc5425Encoder());
        camelContext.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:test").marshal(new SyslogDataFormat()).to("netty:" + protocol + ":" + host + ":" + port + "?sync=false&encoders=#encoder&useByteBuf=true");
            }
        });

        camelContext.start();
        camelContext.createProducerTemplate().sendBody("direct:test", message);
    } catch (Exception e) {
        LOG.error("Failed to send log messages {} to : {}", message, "netty:" + protocol + ":" + host + ":" + port);
        fail(e.getMessage());
    } finally {
        camelContext.stop();
    }
}
 
Example #2
Source File: SyslogService.java    From camel-kafka-connector with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeAll(ExtensionContext context) throws Exception {
    CAMEL_CONTEXT.getRegistry().bind("decoder", new Rfc5425FrameDecoder());
    CAMEL_CONTEXT.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("netty:" + protocol + ":" + host + ":" + port + "?sync=false&decoders=#decoder").unmarshal(new SyslogDataFormat()).to("seda:syslog");
        }
    });
    CAMEL_CONTEXT.start();
}
 
Example #3
Source File: SyslogDataFormatTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testSyslogMarshal() throws Exception {

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .marshal(new SyslogDataFormat());
        }
    });

    camelctx.start();
    try {
        Calendar calendar = Calendar.getInstance();
        calendar.set(2016, Calendar.SEPTEMBER, 26, 19, 30, 55);

        SyslogMessage message = new SyslogMessage();
        message.setHostname("camel-test-host");
        message.setLogMessage("Hello Kermit!");
        message.setTimestamp(calendar);

        ProducerTemplate template = camelctx.createProducerTemplate();
        String result = template.requestBody("direct:start", message, String.class);

        Assert.assertEquals(SYSLOG_RAW_MESSAGE, result);
    } finally {
        camelctx.close();
    }
}
 
Example #4
Source File: SyslogDataFormatTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testSyslogUnmarshal() throws Exception {
    int port = AvailablePortFinder.getNextAvailable();

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("netty:udp://localhost:" + port + "?sync=false&allowDefaultCodec=false")
            .unmarshal(new SyslogDataFormat())
            .to("mock:result");
        }
    });

    camelctx.start();
    try {
        MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);
        mockEndpoint.expectedMessageCount(1);

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.requestBody("netty:udp://127.0.0.1:" + port + "?sync=false&allowDefaultCodec=false&useByteBuf=true", SYSLOG_RAW_MESSAGE);

        mockEndpoint.assertIsSatisfied();

        Exchange exchange = mockEndpoint.getReceivedExchanges().get(0);
        SyslogMessage message = exchange.getIn().getBody(SyslogMessage.class);

        Assert.assertEquals("camel-test-host", message.getHostname());
        Assert.assertEquals("Hello Kermit!", message.getLogMessage());
    } finally {
        camelctx.close();
    }
}
 
Example #5
Source File: SyslogDataFormatAutoConfiguration.java    From camel-spring-boot with Apache License 2.0 4 votes vote down vote up
@Bean(name = "syslog-dataformat-factory")
@ConditionalOnMissingBean(SyslogDataFormat.class)
public DataFormatFactory configureSyslogDataFormatFactory() throws Exception {
    return new DataFormatFactory() {
        @Override
        public DataFormat newInstance() {
            SyslogDataFormat dataformat = new SyslogDataFormat();
            if (CamelContextAware.class
                    .isAssignableFrom(SyslogDataFormat.class)) {
                CamelContextAware contextAware = CamelContextAware.class
                        .cast(dataformat);
                if (contextAware != null) {
                    contextAware.setCamelContext(camelContext);
                }
            }
            try {
                Map<String, Object> parameters = new HashMap<>();
                IntrospectionSupport.getProperties(configuration,
                        parameters, null, false);
                CamelPropertiesHelper.setCamelProperties(camelContext,
                        dataformat, parameters, false);
            } catch (Exception e) {
                throw new RuntimeCamelException(e);
            }
            if (ObjectHelper.isNotEmpty(customizers)) {
                for (DataFormatCustomizer<SyslogDataFormat> customizer : customizers) {
                    boolean useCustomizer = (customizer instanceof HasId)
                            ? HierarchicalPropertiesEvaluator.evaluate(
                                    applicationContext.getEnvironment(),
                                    "camel.dataformat.customizer",
                                    "camel.dataformat.syslog.customizer",
                                    ((HasId) customizer).getId())
                            : HierarchicalPropertiesEvaluator.evaluate(
                                    applicationContext.getEnvironment(),
                                    "camel.dataformat.customizer",
                                    "camel.dataformat.syslog.customizer");
                    if (useCustomizer) {
                        LOGGER.debug(
                                "Configure dataformat {}, with customizer {}",
                                dataformat, customizer);
                        customizer.customize(dataformat);
                    }
                }
            }
            return dataformat;
        }
    };
}