@@ -181,6 +181,29 @@ private String wrapMessageInQuotes(String message) {
181
181
return "\" " + message + "\" " ;
182
182
}
183
183
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
+
184
207
/**
185
208
* send an event via stream
186
209
*
@@ -191,9 +214,7 @@ public void streamEvent(String message) {
191
214
String currentMessage = "" ;
192
215
try {
193
216
194
- if (!(message .startsWith ("{" ) && message .endsWith ("}" ))
195
- && !(message .startsWith ("\" " ) && message .endsWith ("\" " )))
196
- message = wrapMessageInQuotes (message );
217
+ message = escapeMessageIfNeeded (message );
197
218
198
219
// could use a JSON Object , but the JSON is so trivial , just
199
220
// building it with a StringBuffer
0 commit comments