diff --git a/components/camel-splunk/src/main/java/org/apache/camel/component/splunk/event/SplunkEvent.java b/components/camel-splunk/src/main/java/org/apache/camel/component/splunk/event/SplunkEvent.java index 8bfcc6d8db045..63c5cd1ccc221 100644 --- a/components/camel-splunk/src/main/java/org/apache/camel/component/splunk/event/SplunkEvent.java +++ b/components/camel-splunk/src/main/java/org/apache/camel/component/splunk/event/SplunkEvent.java @@ -18,7 +18,7 @@ import java.io.Serializable; import java.util.Date; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; import org.joda.time.format.DateTimeFormat; @@ -218,7 +218,7 @@ public class SplunkEvent implements Serializable { /** * Contents of the event message */ - private StringBuffer eventMessage; + private Map event; /** * A Constructor to load data from a Map @@ -226,17 +226,14 @@ public class SplunkEvent implements Serializable { * @param data the map */ public SplunkEvent(Map data) { - this.eventMessage = new StringBuffer(); - for (String key : data.keySet()) { - this.addPair(key, data.get(key)); - } + this.event = data; } /** * A Copy constructor */ public SplunkEvent(SplunkEvent splunkEvent) { - this.eventMessage = splunkEvent.eventMessage; + this.event = splunkEvent.getEventData(); this.quoteValues = splunkEvent.quoteValues; this.useInternalDate = splunkEvent.useInternalDate; } @@ -250,8 +247,7 @@ public SplunkEvent(SplunkEvent splunkEvent) { * @param quoteValues whether or not to put quotes around values */ public SplunkEvent(String eventName, String eventID, boolean useInternalDate, boolean quoteValues) { - - this.eventMessage = new StringBuffer(); + this.event = new LinkedHashMap(); this.quoteValues = quoteValues; this.useInternalDate = useInternalDate; @@ -276,33 +272,11 @@ public SplunkEvent(String eventName, String eventID) { * Default constructor */ public SplunkEvent() { - this.eventMessage = new StringBuffer(); + this.event = new LinkedHashMap(); } public Map getEventData() { - Map eventData = new HashMap(); - String eventEntries = eventMessage.toString(); - - String[] entries = eventEntries.split(PAIRDELIM); - - String quote = new String(new char[] {QUOTE}); - - for (String entry : entries) { - String[] pair = entry.split(KVDELIM); - - if (pair.length != 2) { - throw new UnsupportedOperationException(String.format("invalid event data [%s]", entry)); - } - - String key = pair[0].replaceAll(quote, ""); - String value = pair[1].replaceAll(quote, ""); - if ("null".equals(value)) { - value = null; - } - - eventData.put(key, value); - } - return eventData; + return event; } /** @@ -398,11 +372,7 @@ private void addThrowableObject(Throwable throwable, int stackTraceDepth) { * Add a key value pair */ public void addPair(String key, String value) { - if (quoteValues) { - this.eventMessage.append(key).append(KVDELIM).append(QUOTE).append(value).append(QUOTE).append(PAIRDELIM); - } else { - this.eventMessage.append(key).append(KVDELIM).append(value).append(PAIRDELIM); - } + this.event.put(key, value); } /** @@ -410,16 +380,19 @@ public void addPair(String key, String value) { */ @Override public String toString() { - String event = ""; - + StringBuilder event = new StringBuilder(); if (useInternalDate) { - StringBuilder clonedMessage = new StringBuilder(); - clonedMessage.append(DATE_FORMATTER.print(new Date().getTime())).append(PAIRDELIM).append(this.eventMessage); - event = clonedMessage.toString(); - } else { - event = eventMessage.toString(); + event.append(DATE_FORMATTER.print(new Date().getTime())).append(PAIRDELIM); + } + for (String key : this.event.keySet()) { + event.append(key); + event.append(KVDELIM); + if (quoteValues) { + event.append(QUOTE).append(this.event.get(key)).append(QUOTE).append(PAIRDELIM); + } else { + event.append(this.event.get(key)).append(PAIRDELIM); + } } - // trim off trailing pair delim char(s) String result = event.substring(0, event.length() - PAIRDELIM.length()) + LINEBREAK; return result; diff --git a/components/camel-splunk/src/test/java/org/apache/camel/component/splunk/SplunkEventTest.java b/components/camel-splunk/src/test/java/org/apache/camel/component/splunk/SplunkEventTest.java new file mode 100644 index 0000000000000..248078d8db515 --- /dev/null +++ b/components/camel-splunk/src/test/java/org/apache/camel/component/splunk/SplunkEventTest.java @@ -0,0 +1,52 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.splunk; + +import java.util.Date; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.camel.component.splunk.event.SplunkEvent; +import org.junit.Assert; +import org.junit.Test; + +public class SplunkEventTest extends Assert { + + @Test + public void testEventDataWithQuotedValues() { + Date now = new Date(); + SplunkEvent event = new SplunkEvent("testevent", "123", false, true); + event.addPair("key1", "value1"); + event.addPair("key2", "value2 with whitespace"); + event.addPair(SplunkEvent.COMMON_DVC_TIME, now); + assertEquals("Values should be quoted", "name=\"testevent\" event_id=\"123\" key1=\"value1\" key2=\"value2 with whitespace\" dvc_time=\"" + now.toString() + "\"\n", + event.toString()); + assertEquals(5, event.getEventData().size()); + assertTrue(event.getEventData().get("key2").equals("value2 with whitespace")); + } + + @Test + public void testEventDataFromMap() { + String rawString = "2013-10-26 15:16:38:011+0200 name=\"twitter-message\" from_user=\"MyNameIsZack_98\" in_reply_to=\"null\" start_time=\"Sat Oct 26 15:16:21 CEST 2013\" " + + "event_id=\"394090123278974976\" text=\"RT @RGIII: Just something about music that it can vibe with your soul\" retweet_count=\"1393\""; + Map eventData = new LinkedHashMap(); + eventData.put("_subsecond", ".011"); + eventData.put("_raw", rawString); + SplunkEvent splunkEvent = new SplunkEvent(eventData); + assertTrue(splunkEvent.toString().contains("_subsecond=\".011\" _raw=\"" + rawString + "\"\n")); + } +}