-
Notifications
You must be signed in to change notification settings - Fork 1k
/
FormatStringDecorator.java
128 lines (111 loc) · 5.36 KB
/
FormatStringDecorator.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see <http://www.gnu.org/licenses/>.
*/
package org.graylog2.decorators;
import com.floreysoft.jmte.Engine;
import com.floreysoft.jmte.template.Template;
import com.google.common.collect.ImmutableMap;
import com.google.inject.assistedinject.Assisted;
import org.graylog2.plugin.Message;
import org.graylog2.plugin.configuration.ConfigurationRequest;
import org.graylog2.plugin.configuration.fields.BooleanField;
import org.graylog2.plugin.configuration.fields.TextField;
import org.graylog2.plugin.decorators.SearchResponseDecorator;
import org.graylog2.rest.models.messages.responses.ResultMessageSummary;
import org.graylog2.rest.resources.search.responses.SearchResponse;
import javax.inject.Inject;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;
import static java.util.Objects.requireNonNull;
public class FormatStringDecorator implements SearchResponseDecorator {
private static final String CK_FORMAT_STRING = "format_string";
private static final String CK_REQUIRE_ALL_FIELDS = "require_all_fields";
private static final String CK_TARGET_FIELD = "target_field";
private final String targetField;
private final Template template;
private final boolean requireAllFields;
private final Set<String> usedVariables;
public interface Factory extends SearchResponseDecorator.Factory {
@Override
FormatStringDecorator create(Decorator decorator);
@Override
FormatStringDecorator.Config getConfig();
@Override
FormatStringDecorator.Descriptor getDescriptor();
}
public static class Config implements SearchResponseDecorator.Config {
@Override
public ConfigurationRequest getRequestedConfiguration() {
return new ConfigurationRequest() {
{
addField(new TextField(
CK_FORMAT_STRING,
"Format String",
"${source} - ${message}",
"The format string used to create the concatenated field."
));
addField(new BooleanField(
CK_REQUIRE_ALL_FIELDS,
"Require all fields",
false,
"Check this if all fields in the format string need to be present in order to apply this decorator."));
addField(new TextField(
CK_TARGET_FIELD,
"Target field",
"message",
"The message field that will be created with the formatted string."
));
}
};
}
}
public static class Descriptor extends SearchResponseDecorator.Descriptor {
public Descriptor() {
super("Format String", "http://docs.graylog.org/", "Format string");
}
}
@Inject
public FormatStringDecorator(@Assisted Decorator decorator, Engine templateEngine) {
final String formatString = (String) requireNonNull(decorator.config().get(CK_FORMAT_STRING),
CK_FORMAT_STRING + " cannot be null");
this.targetField = (String) requireNonNull(decorator.config().get(CK_TARGET_FIELD),
CK_TARGET_FIELD + " cannot be null");
requireAllFields = (boolean) requireNonNull(decorator.config().get(CK_REQUIRE_ALL_FIELDS),
CK_REQUIRE_ALL_FIELDS + " cannot be null");
template = requireNonNull(templateEngine, "templateEngine").getTemplate(formatString);
usedVariables = template.getUsedVariables();
}
@Override
public SearchResponse apply(SearchResponse searchResponse) {
final List<ResultMessageSummary> summaries = searchResponse.messages().stream()
.map(summary -> {
if (requireAllFields && !usedVariables.stream().allMatch(variable -> summary.message().containsKey(variable))) {
return summary;
}
final String formattedString = template.transform(summary.message(), Locale.ENGLISH);
if (formattedString == null) {
return summary;
}
final Message message = new Message(ImmutableMap.copyOf(summary.message()));
message.addField(targetField, formattedString);
return summary.toBuilder().message(message.getFields()).build();
})
.collect(Collectors.toList());
return searchResponse.toBuilder().messages(summaries).build();
}
}