Search in sources :

Example 21 with Output

use of org.graylog2.plugin.streams.Output in project graylog2-server by Graylog2.

the class OutputResource method create.

@POST
@Timed
@ApiOperation(value = "Create an output")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid output specification in input.", response = OutputSummary.class) })
@AuditEvent(type = AuditEventTypes.MESSAGE_OUTPUT_CREATE)
public Response create(@ApiParam(name = "JSON body", required = true) CreateOutputRequest csor) throws ValidationException {
    checkPermission(RestPermissions.OUTPUTS_CREATE);
    final AvailableOutputSummary outputSummary = messageOutputFactory.getAvailableOutputs().get(csor.type());
    if (outputSummary == null) {
        throw new ValidationException("type", "Invalid output type");
    }
    // Make sure the config values will be stored with the correct type.
    final CreateOutputRequest createOutputRequest = CreateOutputRequest.create(csor.title(), csor.type(), ConfigurationMapConverter.convertValues(csor.configuration(), outputSummary.requestedConfiguration()), csor.streams());
    final Output output = outputService.create(createOutputRequest, getCurrentUser().getName());
    final URI outputUri = getUriBuilderToSelf().path(OutputResource.class).path("{outputId}").build(output.getId());
    return Response.created(outputUri).entity(OutputSummary.create(output.getId(), output.getTitle(), output.getType(), output.getCreatorUserId(), new DateTime(output.getCreatedAt()), new HashMap<>(output.getConfiguration()), output.getContentPack())).build();
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) AvailableOutputSummary(org.graylog2.rest.resources.streams.outputs.AvailableOutputSummary) Output(org.graylog2.plugin.streams.Output) URI(java.net.URI) DateTime(org.joda.time.DateTime) CreateOutputRequest(org.graylog2.rest.models.streams.outputs.requests.CreateOutputRequest) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) ApiResponses(io.swagger.annotations.ApiResponses)

Example 22 with Output

use of org.graylog2.plugin.streams.Output in project graylog2-server by Graylog2.

the class OutputResource method update.

@PUT
@Path("/{outputId}")
@Timed
@ApiOperation(value = "Update output")
@RequiresPermissions(RestPermissions.OUTPUTS_EDIT)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses(value = { @ApiResponse(code = 404, message = "No such output on this node.") })
@AuditEvent(type = AuditEventTypes.MESSAGE_OUTPUT_UPDATE)
public Output update(@ApiParam(name = "outputId", value = "The id of the output that should be deleted", required = true) @PathParam("outputId") String outputId, @ApiParam(name = "JSON body", required = true) Map<String, Object> deltas) throws ValidationException, NotFoundException {
    checkPermission(RestPermissions.OUTPUTS_EDIT, outputId);
    final Output oldOutput = outputService.load(outputId);
    final AvailableOutputSummary outputSummary = messageOutputFactory.getAvailableOutputs().get(oldOutput.getType());
    if (outputSummary == null) {
        throw new ValidationException("type", "Invalid output type");
    }
    deltas.remove("streams");
    if (deltas.containsKey("configuration")) {
        @SuppressWarnings("unchecked") final Map<String, Object> configuration = (Map<String, Object>) deltas.get("configuration");
        deltas.put("configuration", ConfigurationMapConverter.convertValues(configuration, outputSummary.requestedConfiguration()));
    }
    final Output output = this.outputService.update(outputId, deltas);
    this.outputRegistry.removeOutput(oldOutput);
    return output;
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) Output(org.graylog2.plugin.streams.Output) AvailableOutputSummary(org.graylog2.rest.resources.streams.outputs.AvailableOutputSummary) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 23 with Output

use of org.graylog2.plugin.streams.Output in project graylog2-server by Graylog2.

the class OutputResource method get.

@GET
@Path("/{outputId}")
@Timed
@ApiOperation(value = "Get specific output")
@RequiresPermissions(RestPermissions.OUTPUTS_CREATE)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses(value = { @ApiResponse(code = 404, message = "No such output on this node.") })
public OutputSummary get(@ApiParam(name = "outputId", value = "The id of the output we want.", required = true) @PathParam("outputId") String outputId) throws NotFoundException {
    checkPermission(RestPermissions.OUTPUTS_READ, outputId);
    final Output output = outputService.load(outputId);
    return OutputSummary.create(output.getId(), output.getTitle(), output.getType(), output.getCreatorUserId(), new DateTime(output.getCreatedAt()), output.getConfiguration(), output.getContentPack());
}
Also used : Output(org.graylog2.plugin.streams.Output) DateTime(org.joda.time.DateTime) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 24 with Output

use of org.graylog2.plugin.streams.Output in project graylog2-server by Graylog2.

the class ExtractorTest method testConvertersWithExceptions.

@Test
public void testConvertersWithExceptions() throws Exception {
    final Converter converter1 = new TestConverter.Builder().callback(new Function<Object, Object>() {

        @Nullable
        @Override
        public Object apply(Object input) {
            throw new NullPointerException("EEK");
        }
    }).build();
    final Converter converter2 = new TestConverter.Builder().callback(new Function<Object, Object>() {

        @Nullable
        @Override
        public Object apply(Object input) {
            return input + "2";
        }
    }).build();
    final Converter converter3 = new TestConverter.Builder().callback(new Function<Object, Object>() {

        @Nullable
        @Override
        public Object apply(Object input) {
            throw new NullPointerException("EEK");
        }
    }).build();
    final TestExtractor extractor = new TestExtractor.Builder().converters(Lists.newArrayList(converter1, converter2, converter3)).callback(new Callable<Result[]>() {

        @Override
        public Result[] call() throws Exception {
            return new Result[] { new Result("converter", -1, -1) };
        }
    }).build();
    final Message msg = createMessage("message");
    extractor.runExtractor(msg);
    // The two exceptions should have been recorded.
    assertThat(extractor.getConverterExceptionCount()).isEqualTo(2);
    // It ignores all converters which throw an exception but executes the ones that don't.
    // TODO: Is this really the expected behaviour? The converters are executed in order and basically depend on the output of the previous. This might not work for all converters.
    assertThat(msg.getField("target")).isEqualTo("converter2");
}
Also used : Function(com.google.common.base.Function) Message(org.graylog2.plugin.Message) Callable(java.util.concurrent.Callable) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Aggregations

Output (org.graylog2.plugin.streams.Output)15 Stream (org.graylog2.plugin.streams.Stream)11 ApiOperation (io.swagger.annotations.ApiOperation)10 Timed (com.codahale.metrics.annotation.Timed)9 Produces (javax.ws.rs.Produces)9 MessageOutput (org.graylog2.plugin.outputs.MessageOutput)9 ApiResponses (io.swagger.annotations.ApiResponses)8 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)8 AuditEvent (org.graylog2.audit.jersey.AuditEvent)7 Path (javax.ws.rs.Path)6 Test (org.junit.Test)6 Consumes (javax.ws.rs.Consumes)4 DateTime (org.joda.time.DateTime)4 HashSet (java.util.HashSet)3 GET (javax.ws.rs.GET)3 POST (javax.ws.rs.POST)3 ObjectId (org.bson.types.ObjectId)3 ValidationException (org.graylog2.plugin.database.ValidationException)3 StreamRule (org.graylog2.plugin.streams.StreamRule)3 AvailableOutputSummary (org.graylog2.rest.resources.streams.outputs.AvailableOutputSummary)3