Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -218,25 +218,22 @@ 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
*
* @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;
}
Expand All @@ -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<String, String>();
this.quoteValues = quoteValues;
this.useInternalDate = useInternalDate;

Expand All @@ -276,33 +272,11 @@ public SplunkEvent(String eventName, String eventID) {
* 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;
}

/**
Expand Down Expand Up @@ -398,28 +372,27 @@ 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);
}

/**
* return the completed event message
*/
@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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}