Search in sources :

Example 1 with RegexReplaceExtractor

use of org.graylog2.inputs.extractors.RegexReplaceExtractor in project graylog2-server by Graylog2.

the class RegexReplaceExtractorTest method testReplacementWithReplaceAll.

@Test
public void testReplacementWithReplaceAll() throws Exception {
    final Message message = new Message("Foobar 123 Foobaz 456", "source", Tools.nowUTC());
    final RegexReplaceExtractor extractor = new RegexReplaceExtractor(metricRegistry, "id", "title", 0L, Extractor.CursorStrategy.COPY, "message", "message", ImmutableMap.<String, Object>of("regex", "(\\w+) (\\d+)", "replacement", "$2/$1", "replace_all", true), "user", Collections.<Converter>emptyList(), Extractor.ConditionType.NONE, null);
    extractor.runExtractor(message);
    assertThat(message.getMessage()).isEqualTo("123/Foobar 456/Foobaz");
}
Also used : Message(org.graylog2.plugin.Message) Test(org.junit.Test)

Example 2 with RegexReplaceExtractor

use of org.graylog2.inputs.extractors.RegexReplaceExtractor in project graylog2-server by Graylog2.

the class RegexReplaceExtractorTest method testReplacementWithNoMatchAndDefaultReplacement.

@Test
public void testReplacementWithNoMatchAndDefaultReplacement() throws Exception {
    final Message message = new Message("Test", "source", Tools.nowUTC());
    final RegexReplaceExtractor extractor = new RegexReplaceExtractor(metricRegistry, "id", "title", 0L, Extractor.CursorStrategy.COPY, "message", "message", ImmutableMap.<String, Object>of("regex", "NO-MATCH"), "user", Collections.<Converter>emptyList(), Extractor.ConditionType.NONE, null);
    extractor.runExtractor(message);
    assertThat(message.getMessage()).isEqualTo("Test");
}
Also used : Message(org.graylog2.plugin.Message) Test(org.junit.Test)

Example 3 with RegexReplaceExtractor

use of org.graylog2.inputs.extractors.RegexReplaceExtractor in project graylog2-server by Graylog2.

the class RegexReplaceExtractorTest method testReplacementWithOnePlaceholder.

@Test
public void testReplacementWithOnePlaceholder() throws Exception {
    final Message message = new Message("Test Foobar", "source", Tools.nowUTC());
    final RegexReplaceExtractor extractor = new RegexReplaceExtractor(metricRegistry, "id", "title", 0L, Extractor.CursorStrategy.COPY, "message", "message", ImmutableMap.<String, Object>of("regex", "Test (\\w+)"), "user", Collections.<Converter>emptyList(), Extractor.ConditionType.NONE, null);
    extractor.runExtractor(message);
    assertThat(message.getMessage()).isEqualTo("Foobar");
}
Also used : Message(org.graylog2.plugin.Message) Test(org.junit.Test)

Example 4 with RegexReplaceExtractor

use of org.graylog2.inputs.extractors.RegexReplaceExtractor in project graylog2-server by Graylog2.

the class RegexReplaceTesterResource method testRegexReplaceExtractor.

private RegexReplaceTesterResponse testRegexReplaceExtractor(String example, String regex, String replacement, boolean replaceAll) {
    final Map<String, Object> config = ImmutableMap.<String, Object>of("regex", regex, "replacement", replacement, "replace_all", replaceAll);
    final RegexReplaceExtractor extractor;
    try {
        extractor = new RegexReplaceExtractor(new MetricRegistry(), "test", "Test", 0L, Extractor.CursorStrategy.COPY, "test", "test", config, getCurrentUser().getName(), Collections.<Converter>emptyList(), Extractor.ConditionType.NONE, "");
    } catch (Extractor.ReservedFieldException e) {
        throw new BadRequestException("Trying to overwrite a reserved message field", e);
    } catch (ConfigurationException e) {
        throw new BadRequestException("Invalid extractor configuration", e);
    }
    final Extractor.Result result = extractor.runExtractor(example);
    final RegexReplaceTesterResponse.Match match = result == null ? null : RegexReplaceTesterResponse.Match.create(String.valueOf(result.getValue()), result.getBeginIndex(), result.getEndIndex());
    return RegexReplaceTesterResponse.create(result != null, match, regex, replacement, replaceAll, example);
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) RegexReplaceExtractor(org.graylog2.inputs.extractors.RegexReplaceExtractor) RegexReplaceTesterResponse(org.graylog2.rest.models.tools.responses.RegexReplaceTesterResponse) ConfigurationException(org.graylog2.ConfigurationException) Converter(org.graylog2.plugin.inputs.Converter) BadRequestException(javax.ws.rs.BadRequestException) Extractor(org.graylog2.plugin.inputs.Extractor) RegexReplaceExtractor(org.graylog2.inputs.extractors.RegexReplaceExtractor)

Example 5 with RegexReplaceExtractor

use of org.graylog2.inputs.extractors.RegexReplaceExtractor in project graylog2-server by Graylog2.

the class RegexReplaceExtractorTest method testReplacementWithoutReplaceAll.

@Test
public void testReplacementWithoutReplaceAll() throws Exception {
    final Message message = new Message("Foobar 123 Foobaz 456", "source", Tools.nowUTC());
    final RegexReplaceExtractor extractor = new RegexReplaceExtractor(metricRegistry, "id", "title", 0L, Extractor.CursorStrategy.COPY, "message", "message", ImmutableMap.<String, Object>of("regex", "(\\w+) (\\d+)", "replacement", "$2/$1", "replace_all", false), "user", Collections.<Converter>emptyList(), Extractor.ConditionType.NONE, null);
    extractor.runExtractor(message);
    assertThat(message.getMessage()).isEqualTo("123/Foobar Foobaz 456");
}
Also used : Message(org.graylog2.plugin.Message) Test(org.junit.Test)

Aggregations

Message (org.graylog2.plugin.Message)6 Test (org.junit.Test)6 MetricRegistry (com.codahale.metrics.MetricRegistry)1 BadRequestException (javax.ws.rs.BadRequestException)1 ConfigurationException (org.graylog2.ConfigurationException)1 RegexReplaceExtractor (org.graylog2.inputs.extractors.RegexReplaceExtractor)1 Converter (org.graylog2.plugin.inputs.Converter)1 Extractor (org.graylog2.plugin.inputs.Extractor)1 RegexReplaceTesterResponse (org.graylog2.rest.models.tools.responses.RegexReplaceTesterResponse)1