Search in sources :

Example 1 with UpdateStreamRequest

use of org.graylog2.rest.models.streams.requests.UpdateStreamRequest in project graylog2-server by Graylog2.

the class StreamResource method update.

@PUT
@Timed
@Path("/{streamId}")
@ApiOperation(value = "Update a stream")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream not found."), @ApiResponse(code = 400, message = "Invalid ObjectId.") })
@AuditEvent(type = AuditEventTypes.STREAM_UPDATE)
public StreamResponse update(@ApiParam(name = "streamId", required = true) @PathParam("streamId") String streamId, @ApiParam(name = "JSON body", required = true) @Valid @NotNull UpdateStreamRequest cr) throws NotFoundException, ValidationException {
    checkPermission(RestPermissions.STREAMS_EDIT, streamId);
    checkNotEditableStream(streamId, "The stream cannot be edited.");
    final Stream stream = streamService.load(streamId);
    if (!Strings.isNullOrEmpty(cr.title())) {
        stream.setTitle(cr.title());
    }
    if (!Strings.isNullOrEmpty(cr.description())) {
        stream.setDescription(cr.description());
    }
    if (cr.matchingType() != null) {
        try {
            stream.setMatchingType(Stream.MatchingType.valueOf(cr.matchingType()));
        } catch (IllegalArgumentException e) {
            throw new BadRequestException("Invalid matching type '" + cr.matchingType() + "' specified. Should be one of: " + Arrays.toString(Stream.MatchingType.values()));
        }
    }
    final Boolean removeMatchesFromDefaultStream = cr.removeMatchesFromDefaultStream();
    if (removeMatchesFromDefaultStream != null) {
        stream.setRemoveMatchesFromDefaultStream(removeMatchesFromDefaultStream);
    }
    // id if it's null/empty in the update request.
    if (!Strings.isNullOrEmpty(cr.indexSetId())) {
        stream.setIndexSetId(cr.indexSetId());
    }
    final Optional<IndexSet> indexSet = indexSetRegistry.get(stream.getIndexSetId());
    if (!indexSet.isPresent()) {
        throw new BadRequestException("Index set with ID <" + stream.getIndexSetId() + "> does not exist!");
    } else if (!indexSet.get().getConfig().isWritable()) {
        throw new BadRequestException("Assigned index set must be writable!");
    } else if (!indexSet.get().getConfig().isRegularIndex()) {
        throw new BadRequestException("Assigned index set is not usable");
    }
    streamService.save(stream);
    return streamToResponse(stream);
}
Also used : BadRequestException(javax.ws.rs.BadRequestException) Stream(org.graylog2.plugin.streams.Stream) IndexSet(org.graylog2.indexer.IndexSet) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

Timed (com.codahale.metrics.annotation.Timed)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 BadRequestException (javax.ws.rs.BadRequestException)1 Consumes (javax.ws.rs.Consumes)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 AuditEvent (org.graylog2.audit.jersey.AuditEvent)1 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)1 IndexSet (org.graylog2.indexer.IndexSet)1 Stream (org.graylog2.plugin.streams.Stream)1