camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject git commit: Made SplunkEvent store field kvp's in Map instead of String
Date Sat, 02 Nov 2013 14:07:44 GMT
Updated Branches:
  refs/heads/master 6c881d53b -> f8e9ead3d


Made SplunkEvent store field kvp's in Map instead of String

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/f8e9ead3
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/f8e9ead3
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/f8e9ead3

Branch: refs/heads/master
Commit: f8e9ead3d7596c3533755b8a1a4070a3989b04b7
Parents: 6c881d5
Author: Preben Asmussen <preben.asmussen@gmail.com>
Authored: Sat Nov 2 12:28:16 2013 +0100
Committer: Preben Asmussen <preben.asmussen@gmail.com>
Committed: Sat Nov 2 12:28:16 2013 +0100

----------------------------------------------------------------------
 .../component/splunk/event/SplunkEvent.java     | 65 ++++++--------------
 .../camel/component/splunk/SplunkEventTest.java | 52 ++++++++++++++++
 2 files changed, 71 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/f8e9ead3/components/camel-splunk/src/main/java/org/apache/camel/component/splunk/event/SplunkEvent.java
----------------------------------------------------------------------
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 8bfcc6d..63c5cd1 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 @@ package org.apache.camel.component.splunk.event;
 
 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<String, String> 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<String, String> 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 class SplunkEvent implements Serializable {
      * @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<String, String>();
         this.quoteValues = quoteValues;
         this.useInternalDate = useInternalDate;
 
@@ -276,33 +272,11 @@ public class SplunkEvent implements Serializable {
      * Default constructor
      */
     public SplunkEvent() {
-        this.eventMessage = new StringBuffer();
+        this.event = new LinkedHashMap<String, String>();
     }
 
     public Map<String, String> getEventData() {
-        Map<String, String> eventData = new HashMap<String, String>();
-        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 @@ public class SplunkEvent implements Serializable {
      * 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 class SplunkEvent implements Serializable {
      */
     @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;

http://git-wip-us.apache.org/repos/asf/camel/blob/f8e9ead3/components/camel-splunk/src/test/java/org/apache/camel/component/splunk/SplunkEventTest.java
----------------------------------------------------------------------
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 0000000..248078d
--- /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<String, String> eventData = new LinkedHashMap<String, String>();
+        eventData.put("_subsecond", ".011");
+        eventData.put("_raw", rawString);
+        SplunkEvent splunkEvent = new SplunkEvent(eventData);
+        assertTrue(splunkEvent.toString().contains("_subsecond=\".011\" _raw=\"" + rawString
+ "\"\n"));
+    }
+}


Mime
View raw message