-
Notifications
You must be signed in to change notification settings - Fork 175
/
MessageFormat.java
87 lines (77 loc) · 2.44 KB
/
MessageFormat.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.splunk.logging;
import org.json.simple.JSONValue;
/**
*
* Represents format of a message. It is used to apply necessary parsing and formatting to the message, if required for
* the message format.
* <p>
* If format is not set, then it is assumed to be "text"
* </p>
* <p>
* Note: Access modifier is intentionally left default which allows this enum to be accessed only within the package.
* </p>
*
*/
enum MessageFormat {
TEXT("text"),
JSON("json");
private final String format;
MessageFormat(final String format) {
this.format = format;
}
/**
* Parses the message on the basis of message format.
*
*
* @param message the message string
*
* @return parsed message object based on format
*/
Object parse(final String message) {
// if message is null or blank then return without parsing
if (message == null || message.trim().length() == 0) {
return message;
}
switch (this) {
case JSON:
return parseJsonEventMessage(message);
case TEXT:
default:
// Treat message type as text if format is not defined or defined as text
return message;
}
}
/**
* Parses the message JSON string into JSON object. If parsing fails then the input message is returned as is.
*
* @param message the message string
* @return the parsed message JSON object or input message if parsing fails
*/
private Object parseJsonEventMessage(final String message) {
final Object jsonObject = JSONValue.parse(message);
if (jsonObject == null) {
// If JSON parsing failed then it is likely a text message or a malformed JSON message.
// Return input message string in such an event.
return message;
} else {
return jsonObject;
}
}
/**
* Gets MessageFormat instance from format string.
*
* @param format the message format
* @return the MessageFormat enum
*/
static MessageFormat fromFormat(String format) {
if (format != null && format.trim().length() > 0) {
format = format.toLowerCase();
for (final MessageFormat formatEnum : values()) {
if (formatEnum.format.equals(format)) {
return formatEnum;
}
}
}
return TEXT;
}
}