From b63085c52fbe5d84eea301f596e265834d64c037 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Fri, 11 Apr 2014 15:10:21 +0300 Subject: [PATCH] INT-3368: `SyslogToMapTrans`: Make `TAG` Optional JIRA: https://jira.spring.io/browse/INT-3368 INT-3368: PR comments --- .../transformer/SyslogToMapTransformer.java | 47 +++++++++---------- .../transformer/SysLogTransformerTests.java | 41 ++++++++++++++-- spring-integration-jdbc/.gitignore | 1 + 3 files changed, 62 insertions(+), 27 deletions(-) create mode 100644 spring-integration-jdbc/.gitignore diff --git a/spring-integration-core/src/main/java/org/springframework/integration/transformer/SyslogToMapTransformer.java b/spring-integration-core/src/main/java/org/springframework/integration/transformer/SyslogToMapTransformer.java index a09f1b33c7..719e415e6c 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/transformer/SyslogToMapTransformer.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/transformer/SyslogToMapTransformer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.transformer; import java.io.UnsupportedEncodingException; @@ -27,12 +28,13 @@ import java.util.regex.Pattern; import org.springframework.util.Assert; /** - * Transforms a packet in Syslog (RFC5424) format to a Map. + * Transforms a packet in Syslog (RFC3164) format to a Map. * If the packet cannot be decoded, the entire packet - * is returned as a String under the key UNDECODED. If the date field can be + * is returned as a String under the key {@code UNDECODED}. If the date field can be * parsed, it will be returned as a {@link Date} object; otherwise it is returned as a String. * * @author Gary Russell + * @author Artem Bilan * @since 2.2 * */ @@ -44,12 +46,6 @@ public class SyslogToMapTransformer extends AbstractPayloadTransformer]+)>(.{15}) ([^ ]+) ([^:]+): (.*)", Pattern.DOTALL); + private final Pattern pattern = Pattern.compile("<([^>]+)>(.{15}) ([^ ]+) (?:([^:]+): )?(.*)", Pattern.DOTALL); private final SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd HH:mm:ss"); + @Override + protected Map transformPayload(Object payload) throws Exception { + boolean isByteArray = payload instanceof byte[]; + boolean isString = payload instanceof String; + Assert.isTrue(isByteArray || isString, "payload must be String or byte[]"); + if (isByteArray) { + return this.transform((byte[]) payload); + } + else if (isString) { + return this.transform((String) payload); + } + return null; + } + private Map transform(byte[] payloadBytes) { String payload; try { @@ -116,7 +126,9 @@ public class SyslogToMapTransformer extends AbstractPayloadTransformer transformPayload(Object payload) throws Exception { - boolean isByteArray = payload instanceof byte[]; - boolean isString = payload instanceof String; - Assert.isTrue(isByteArray || isString, "payload must be String or byte[]"); - if (isByteArray) { - return this.transform((byte[]) payload); - } - else if (isString) { - return this.transform((String) payload); - } - return null; - } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/transformer/SysLogTransformerTests.java b/spring-integration-core/src/test/java/org/springframework/integration/transformer/SysLogTransformerTests.java index 732674ef8b..fa10271ca0 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/transformer/SysLogTransformerTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/transformer/SysLogTransformerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.transformer; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; import java.util.Date; import java.util.Map; @@ -84,4 +84,39 @@ public class SysLogTransformerTests { assertEquals(1, transformed.size()); assertEquals(syslog, transformed.get(SyslogToMapTransformer.UNDECODED)); } + + @Test + public void testWithoutTag() throws Exception { + SyslogToMapTransformer t = new SyslogToMapTransformer(); + Map transformed = t.transformPayload( + "<158>JUL 26 22:08:35 WEBERN TEST SYSLOG MESSAGE".getBytes()); + assertEquals(5, transformed.size()); + assertEquals(19, transformed.get(SyslogToMapTransformer.FACILITY)); + assertEquals(6, transformed.get(SyslogToMapTransformer.SEVERITY)); + Object date = transformed.get(SyslogToMapTransformer.TIMESTAMP); + assertTrue(date instanceof Date || date instanceof String); + assertEquals("WEBERN", transformed.get(SyslogToMapTransformer.HOST)); + assertFalse(transformed.containsKey(SyslogToMapTransformer.TAG)); + assertEquals("TEST SYSLOG MESSAGE", transformed.get(SyslogToMapTransformer.MESSAGE)); + + String[] fields = new String[] {SyslogToMapTransformer.FACILITY, + SyslogToMapTransformer.SEVERITY, SyslogToMapTransformer.TIMESTAMP, SyslogToMapTransformer.HOST, + SyslogToMapTransformer.MESSAGE}; + + Object[] values = new Object[] {19, 6, date, "WEBERN", "TEST SYSLOG MESSAGE"}; + // check iteration order + int n = 0; + for (Entry entry : transformed.entrySet()) { + assertEquals(fields[n++], entry.getKey()); + } + n = 0; + for (String key : transformed.keySet()) { + assertEquals(fields[n++], key); + } + n = 0; + for (Object value : transformed.values()) { + assertEquals(values[n++], value); + } + } + } diff --git a/spring-integration-jdbc/.gitignore b/spring-integration-jdbc/.gitignore new file mode 100644 index 0000000000..591e68460b --- /dev/null +++ b/spring-integration-jdbc/.gitignore @@ -0,0 +1 @@ +src/test/java/org/springframework/integration/jdbc/store/channel/DataSource-oracle-context-*