Skip to content

Commit 3092752

Browse files
author
Damien Dallimore
committed
HEC tweaks
1 parent 88b6060 commit 3092752

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/com/splunk/logging/SplunkHECInput.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,29 @@ private String wrapMessageInQuotes(String message) {
181181
return "\"" + message + "\"";
182182
}
183183

184+
/**
185+
* from Tivo
186+
*
187+
* @param message
188+
* @return
189+
*/
190+
private String escapeMessageIfNeeded(String message) {
191+
String trimmedMessage = message.trim();
192+
if (trimmedMessage.startsWith("{") && trimmedMessage.endsWith("}")) {
193+
// this is *probably* JSON.
194+
return trimmedMessage;
195+
} else if (trimmedMessage.startsWith("\"")
196+
&& trimmedMessage.endsWith("\"")
197+
&& !message.substring(1, message.length() - 1).contains("\"")) {
198+
// this appears to be a quoted string with no internal quotes
199+
return trimmedMessage;
200+
} else {
201+
// don't know what this thing is, so need to escape all quotes, and
202+
// then wrap the result in quotes
203+
return "\"" + message.replace("\"", "\\\"") + "\"";
204+
}
205+
}
206+
184207
/**
185208
* send an event via stream
186209
*
@@ -191,9 +214,7 @@ public void streamEvent(String message) {
191214
String currentMessage = "";
192215
try {
193216

194-
if (!(message.startsWith("{") && message.endsWith("}"))
195-
&& !(message.startsWith("\"") && message.endsWith("\"")))
196-
message = wrapMessageInQuotes(message);
217+
message = escapeMessageIfNeeded(message);
197218

198219
// could use a JSON Object , but the JSON is so trivial , just
199220
// building it with a StringBuffer

0 commit comments

Comments
 (0)