INT-3368: SyslogToMapTrans: Make TAG Optional

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

INT-3368: PR comments
This commit is contained in:
Artem Bilan
2014-04-11 15:10:21 +03:00
committed by Gary Russell
parent ab8e193b01
commit b63085c52f
3 changed files with 62 additions and 27 deletions

View File

@@ -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<Object, M
public static final String TIMESTAMP = "TIMESTAMP";
/**
* @deprecated use {@link #TIMESTAMP}
*/
@Deprecated
public static final String TIMESAMP = TIMESTAMP;
public static final String HOST = "HOST";
public static final String TAG = "TAG";
@@ -58,10 +54,24 @@ public class SyslogToMapTransformer extends AbstractPayloadTransformer<Object, M
public static final String UNDECODED = "UNDECODED";
private final Pattern pattern = Pattern.compile("<([^>]+)>(.{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<String, ?> 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<String, ?> transform(byte[] payloadBytes) {
String payload;
try {
@@ -116,7 +126,9 @@ public class SyslogToMapTransformer extends AbstractPayloadTransformer<Object, M
map.put(TIMESTAMP, timestamp);
}
map.put(HOST, matcher.group(3));
map.put(TAG, matcher.group(4));
if (matcher.group(4) != null) {
map.put(TAG, matcher.group(4));
}
map.put(MESSAGE, matcher.group(5));
}
catch (Exception e) {
@@ -136,17 +148,4 @@ public class SyslogToMapTransformer extends AbstractPayloadTransformer<Object, M
return map;
}
@Override
protected Map<String, ?> 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;
}
}

View File

@@ -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<String, ?> 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<String, ?> 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);
}
}
}

1
spring-integration-jdbc/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
src/test/java/org/springframework/integration/jdbc/store/channel/DataSource-oracle-context-*