Search in sources :

Example 51 with Extractor

use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.

the class ExtractorTest method testWithMultipleValueOnlyResults.

@Test(expected = NullPointerException.class)
public void testWithMultipleValueOnlyResults() throws Exception {
    final TestExtractor extractor = new TestExtractor.Builder().callback(new Callable<Result[]>() {

        @Override
        public Result[] call() throws Exception {
            return new Result[] { new Result("1", -1, -1), new Result("2", -1, -1) };
        }
    }).build();
    final Message msg = createMessage("the hello");
    // TODO: Throwing a NPE with multiple value-only Result objects is a bug!
    extractor.runExtractor(msg);
}
Also used : Message(org.graylog2.plugin.Message) Callable(java.util.concurrent.Callable) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Example 52 with Extractor

use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.

the class ExtractorTest method testWithMultipleTargetValueResultsAndOneValueIsNull.

@Test
public void testWithMultipleTargetValueResultsAndOneValueIsNull() throws Exception {
    final TestExtractor extractor = new TestExtractor.Builder().callback(new Callable<Result[]>() {

        @Override
        public Result[] call() throws Exception {
            return new Result[] { new Result(1, "one", -1, -1), new Result(2, "two", -1, -1), new Result(null, "three", -1, -1) };
        }
    }).build();
    final Message msg = createMessage("the hello");
    extractor.runExtractor(msg);
    // If the extractor returns multiple results and one result value is null, all results will be ignored.
    // TODO: This is the current behaviour and it is weird. Will be fixed soon.
    assertThat(msg.hasField("one")).isFalse();
    assertThat(msg.hasField("two")).isFalse();
    assertThat(msg.hasField("three")).isFalse();
}
Also used : Message(org.graylog2.plugin.Message) Callable(java.util.concurrent.Callable) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Example 53 with Extractor

use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.

the class InputFacade method addExtractor.

private Extractor addExtractor(final Input input, final String title, final int order, final Extractor.CursorStrategy cursorStrategy, final Extractor.Type type, final String sourceField, final String targetField, final Map<String, Object> configuration, final List<Converter> converters, final Extractor.ConditionType conditionType, final String conditionValue, final String username) throws ValidationException, org.graylog2.ConfigurationException, ExtractorFactory.NoSuchExtractorException, Extractor.ReservedFieldException {
    final String extractorId = UUID.randomUUID().toString();
    final Extractor extractor = extractorFactory.factory(extractorId, title, order, cursorStrategy, type, sourceField, targetField, configuration, username, converters, conditionType, conditionValue);
    inputService.addExtractor(input, extractor);
    return extractor;
}
Also used : Extractor(org.graylog2.plugin.inputs.Extractor) GrokExtractor(org.graylog2.inputs.extractors.GrokExtractor) LookupTableExtractor(org.graylog2.inputs.extractors.LookupTableExtractor)

Example 54 with Extractor

use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.

the class InputFacade method createExtractors.

private List<Extractor> createExtractors(final Input input, final List<ExtractorEntity> extractorEntities, final String username, final Map<String, ValueReference> parameters) throws Extractor.ReservedFieldException, org.graylog2.ConfigurationException, ExtractorFactory.NoSuchExtractorException, ValidationException {
    final ImmutableList.Builder<Extractor> result = ImmutableList.builder();
    for (ExtractorEntity extractorEntity : extractorEntities) {
        final List<Converter> converters = createConverters(extractorEntity.converters(), parameters);
        final Extractor extractor = addExtractor(input, extractorEntity.title().asString(parameters), extractorEntity.order().asInteger(parameters), extractorEntity.cursorStrategy().asEnum(parameters, Extractor.CursorStrategy.class), extractorEntity.type().asEnum(parameters, Extractor.Type.class), extractorEntity.sourceField().asString(parameters), extractorEntity.targetField().asString(parameters), toValueMap(extractorEntity.configuration(), parameters), converters, extractorEntity.conditionType().asEnum(parameters, Extractor.ConditionType.class), extractorEntity.conditionValue().asString(parameters), username);
        result.add(extractor);
    }
    return result.build();
}
Also used : ModelType(org.graylog2.contentpacks.model.ModelType) ImmutableList(com.google.common.collect.ImmutableList) ExtractorEntity(org.graylog2.contentpacks.model.entities.ExtractorEntity) Converter(org.graylog2.plugin.inputs.Converter) Extractor(org.graylog2.plugin.inputs.Extractor) GrokExtractor(org.graylog2.inputs.extractors.GrokExtractor) LookupTableExtractor(org.graylog2.inputs.extractors.LookupTableExtractor)

Example 55 with Extractor

use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.

the class ExtractorFilter method loadExtractors.

private void loadExtractors(final String inputId) {
    LOG.debug("Re-loading extractors for input <{}>", inputId);
    try {
        final Input input = inputService.find(inputId);
        final List<Extractor> sortedExtractors = inputService.getExtractors(input).stream().sorted(Comparator.comparingLong(Extractor::getOrder)).collect(Collectors.toList());
        extractors.put(inputId, ImmutableList.copyOf(sortedExtractors));
    } catch (NotFoundException e) {
        LOG.warn("Unable to load input <{}>: {}", inputId, e.getMessage());
    }
}
Also used : Input(org.graylog2.inputs.Input) NotFoundException(org.graylog2.database.NotFoundException) Extractor(org.graylog2.plugin.inputs.Extractor)

Aggregations

Test (org.junit.Test)40 Message (org.graylog2.plugin.Message)39 Result (org.graylog2.plugin.inputs.Extractor.Result)29 Callable (java.util.concurrent.Callable)27 Extractor (org.graylog2.plugin.inputs.Extractor)18 Input (org.graylog2.inputs.Input)10 DateConverter (org.graylog2.inputs.converters.DateConverter)9 MessageInput (org.graylog2.plugin.inputs.MessageInput)9 Function (com.google.common.base.Function)8 NotFoundException (org.graylog2.database.NotFoundException)7 ValidationException (org.graylog2.plugin.database.ValidationException)7 Timed (com.codahale.metrics.annotation.Timed)6 ApiOperation (io.swagger.annotations.ApiOperation)6 ApiResponses (io.swagger.annotations.ApiResponses)6 Produces (javax.ws.rs.Produces)5 BadRequestException (javax.ws.rs.BadRequestException)4 Path (javax.ws.rs.Path)4 LookupTableExtractor (org.graylog2.inputs.extractors.LookupTableExtractor)4 Converter (org.graylog2.plugin.inputs.Converter)4 ImmutableList (com.google.common.collect.ImmutableList)3