use of org.graylog2.rest.resources.tools.responses.GrokTesterResponse in project graylog2-server by Graylog2.
the class GrokTesterResource method doTestGrok.
private GrokTesterResponse doTestGrok(String string, String pattern, boolean namedCapturesOnly) throws GrokException {
final Set<GrokPattern> grokPatterns = grokPatternService.loadAll();
final GrokCompiler grokCompiler = GrokCompiler.newInstance();
for (GrokPattern grokPattern : grokPatterns) {
grokCompiler.register(grokPattern.name(), grokPattern.pattern());
}
final Grok grok;
try {
grok = grokCompiler.compile(pattern, namedCapturesOnly);
} catch (Exception e) {
return GrokTesterResponse.createError(pattern, string, e.getMessage());
}
final Match match = grok.match(string);
final Map<String, Object> matches = match.captureFlattened();
final GrokTesterResponse response;
if (matches.isEmpty()) {
response = GrokTesterResponse.createSuccess(false, Collections.<GrokTesterResponse.Match>emptyList(), pattern, string);
} else {
final List<GrokTesterResponse.Match> responseMatches = Lists.newArrayList();
for (final Map.Entry<String, Object> entry : matches.entrySet()) {
final Object value = entry.getValue();
if (value != null) {
responseMatches.add(GrokTesterResponse.Match.create(entry.getKey(), value.toString()));
}
}
response = GrokTesterResponse.createSuccess(true, responseMatches, pattern, string);
}
return response;
}
use of org.graylog2.rest.resources.tools.responses.GrokTesterResponse in project graylog2-server by Graylog2.
the class GrokTesterResourceTest method testGrokWithInvalidPattern.
@Test
public void testGrokWithInvalidPattern() {
final GrokTesterResponse response = resource.grokTest("%{NUMBER", "abc 1234", false);
assertThat(response.matched()).isFalse();
assertThat(response.pattern()).isEqualTo("%{NUMBER");
assertThat(response.string()).isEqualTo("abc 1234");
assertThat(response.errorMessage()).startsWith("Illegal repetition near index 0");
}
use of org.graylog2.rest.resources.tools.responses.GrokTesterResponse in project graylog2-server by Graylog2.
the class GrokTesterResourceTest method testGrokWithValidPatternAndMatch.
@Test
public void testGrokWithValidPatternAndMatch() {
final GrokTesterResponse response = resource.grokTest("%{NUMBER}", "abc 1234", false);
assertThat(response.matched()).isTrue();
assertThat(response.pattern()).isEqualTo("%{NUMBER}");
assertThat(response.string()).isEqualTo("abc 1234");
assertThat(response.matches()).containsOnly(GrokTesterResponse.Match.create("NUMBER", "1234"));
assertThat(response.errorMessage()).isNullOrEmpty();
}
use of org.graylog2.rest.resources.tools.responses.GrokTesterResponse in project graylog2-server by Graylog2.
the class GrokTesterResourceTest method testGrokWithMissingPattern.
@Test
public void testGrokWithMissingPattern() {
final GrokTesterResponse response = resource.grokTest("%{FOOBAR} %{NUMBER}", "abc 1234", false);
assertThat(response.matched()).isFalse();
assertThat(response.pattern()).isEqualTo("%{FOOBAR} %{NUMBER}");
assertThat(response.string()).isEqualTo("abc 1234");
assertThat(response.errorMessage()).isEqualTo("No definition for key 'FOOBAR' found, aborting");
}
use of org.graylog2.rest.resources.tools.responses.GrokTesterResponse in project graylog2-server by Graylog2.
the class GrokTesterResourceTest method testGrokWithEmptyPattern.
@Test
public void testGrokWithEmptyPattern() {
final GrokTesterResponse response = resource.grokTest("", "abc 1234", false);
assertThat(response.matched()).isFalse();
assertThat(response.pattern()).isEqualTo("");
assertThat(response.string()).isEqualTo("abc 1234");
assertThat(response.errorMessage()).isEqualTo("{pattern} should not be empty or null");
}
Aggregations