Search in sources :

Example 1 with CreateStreamRuleRequest

use of org.graylog2.rest.resources.streams.rules.requests.CreateStreamRuleRequest in project graylog2-server by Graylog2.

the class StreamResource method create.

@POST
@Timed
@ApiOperation(value = "Create a stream")
@RequiresPermissions(RestPermissions.STREAMS_CREATE)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@AuditEvent(type = AuditEventTypes.STREAM_CREATE)
public Response create(@ApiParam(name = "JSON body", required = true) final CreateStreamRequest cr) throws ValidationException {
    // Create stream.
    final Stream stream = streamService.create(cr, getCurrentUser().getName());
    stream.setDisabled(true);
    if (!stream.getIndexSet().getConfig().isWritable()) {
        throw new BadRequestException("Assigned index set must be writable!");
    }
    final String id = streamService.save(stream);
    final List<CreateStreamRuleRequest> rules = firstNonNull(cr.rules(), Collections.<CreateStreamRuleRequest>emptyList());
    for (CreateStreamRuleRequest request : rules) {
        StreamRule streamRule = streamRuleService.create(id, request);
        streamRuleService.save(streamRule);
    }
    clusterEventBus.post(StreamsChangedEvent.create(stream.getId()));
    final Map<String, String> result = ImmutableMap.of("stream_id", id);
    final URI streamUri = getUriBuilderToSelf().path(StreamResource.class).path("{streamId}").build(id);
    return Response.created(streamUri).entity(result).build();
}
Also used : StreamRule(org.graylog2.plugin.streams.StreamRule) CreateStreamRuleRequest(org.graylog2.rest.resources.streams.rules.requests.CreateStreamRuleRequest) BadRequestException(javax.ws.rs.BadRequestException) Stream(org.graylog2.plugin.streams.Stream) URI(java.net.URI) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) 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) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Example 2 with CreateStreamRuleRequest

use of org.graylog2.rest.resources.streams.rules.requests.CreateStreamRuleRequest in project graylog2-server by Graylog2.

the class StreamRuleResource method create.

@POST
@Timed
@ApiOperation(value = "Create a stream rule")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@AuditEvent(type = AuditEventTypes.STREAM_RULE_CREATE)
public Response create(@ApiParam(name = "streamid", value = "The stream id this new rule belongs to.", required = true) @PathParam("streamid") String streamId, @ApiParam(name = "JSON body", required = true) @Valid @NotNull CreateStreamRuleRequest cr) throws NotFoundException, ValidationException {
    checkPermission(RestPermissions.STREAMS_EDIT, streamId);
    checkNotDefaultStream(streamId, "Cannot add stream rules to the default stream.");
    final Stream stream = streamService.load(streamId);
    final StreamRule streamRule = streamRuleService.create(streamId, cr);
    final String id = streamService.save(streamRule);
    clusterEventBus.post(StreamsChangedEvent.create(stream.getId()));
    final SingleStreamRuleSummaryResponse response = SingleStreamRuleSummaryResponse.create(id);
    final URI streamRuleUri = getUriBuilderToSelf().path(StreamRuleResource.class).path("{streamRuleId}").build(streamId, id);
    return Response.created(streamRuleUri).entity(response).build();
}
Also used : SingleStreamRuleSummaryResponse(org.graylog2.rest.resources.streams.responses.SingleStreamRuleSummaryResponse) StreamRule(org.graylog2.plugin.streams.StreamRule) Stream(org.graylog2.plugin.streams.Stream) URI(java.net.URI) 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)

Example 3 with CreateStreamRuleRequest

use of org.graylog2.rest.resources.streams.rules.requests.CreateStreamRuleRequest in project graylog2-server by Graylog2.

the class StreamRuleResource method update.

@PUT
@Path("/{streamRuleId}")
@Timed
@ApiOperation(value = "Update a stream rule")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream or stream rule not found."), @ApiResponse(code = 400, message = "Invalid JSON Body.") })
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@AuditEvent(type = AuditEventTypes.STREAM_RULE_UPDATE)
public SingleStreamRuleSummaryResponse update(@ApiParam(name = "streamid", value = "The stream id this rule belongs to.", required = true) @PathParam("streamid") String streamid, @ApiParam(name = "streamRuleId", value = "The stream rule id we are updating", required = true) @PathParam("streamRuleId") String streamRuleId, @ApiParam(name = "JSON body", required = true) @Valid @NotNull CreateStreamRuleRequest cr) throws NotFoundException, ValidationException {
    checkPermission(RestPermissions.STREAMS_EDIT, streamid);
    checkNotDefaultStream(streamid, "Cannot update stream rules on default stream.");
    final StreamRule streamRule;
    streamRule = streamRuleService.load(streamRuleId);
    if (!streamRule.getStreamId().equals(streamid)) {
        throw new NotFoundException("Couldn't update stream rule " + streamRuleId + "in stream " + streamid);
    }
    final StreamRuleType streamRuleType = StreamRuleType.fromInteger(cr.type());
    if (null == streamRuleType) {
        throw new BadRequestException("Unknown stream rule type " + cr.type());
    }
    streamRule.setField(cr.field());
    streamRule.setType(streamRuleType);
    streamRule.setInverted(cr.inverted());
    streamRule.setValue(cr.value());
    streamRule.setDescription(cr.description());
    streamRuleService.save(streamRule);
    clusterEventBus.post(StreamsChangedEvent.create(streamid));
    return SingleStreamRuleSummaryResponse.create(streamRule.getId());
}
Also used : StreamRule(org.graylog2.plugin.streams.StreamRule) StreamRuleType(org.graylog2.plugin.streams.StreamRuleType) NotFoundException(org.graylog2.database.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) 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) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 4 with CreateStreamRuleRequest

use of org.graylog2.rest.resources.streams.rules.requests.CreateStreamRuleRequest in project graylog2-server by Graylog2.

the class UsersResource method changeUser.

@PUT
@Path("{username}")
@ApiOperation("Modify user details.")
@ApiResponses({ @ApiResponse(code = 400, message = "Attempted to modify a read only user account (e.g. built-in or LDAP users)."), @ApiResponse(code = 400, message = "Missing or invalid user details.") })
@AuditEvent(type = AuditEventTypes.USER_UPDATE)
public void changeUser(@ApiParam(name = "username", value = "The name of the user to modify.", required = true) @PathParam("username") String username, @ApiParam(name = "JSON body", value = "Updated user information.", required = true) @Valid @NotNull ChangeUserRequest cr) throws ValidationException {
    checkPermission(USERS_EDIT, username);
    final User user = userService.load(username);
    if (user == null) {
        throw new NotFoundException("Couldn't find user " + username);
    }
    if (user.isReadOnly()) {
        throw new BadRequestException("Cannot modify readonly user " + username);
    }
    // we only allow setting a subset of the fields in CreateStreamRuleRequest
    if (cr.email() != null) {
        user.setEmail(cr.email());
    }
    if (cr.fullName() != null) {
        user.setFullName(cr.fullName());
    }
    final boolean permitted = isPermitted(USERS_PERMISSIONSEDIT, user.getName());
    if (permitted && cr.permissions() != null) {
        user.setPermissions(getEffectiveUserPermissions(user, cr.permissions()));
    }
    if (isPermitted(USERS_ROLESEDIT, user.getName())) {
        setUserRoles(cr.roles(), user);
    }
    final String timezone = cr.timezone();
    if (timezone == null) {
        user.setTimeZone((String) null);
    } else {
        try {
            if (timezone.isEmpty()) {
                user.setTimeZone((String) null);
            } else {
                final DateTimeZone tz = DateTimeZone.forID(timezone);
                user.setTimeZone(tz);
            }
        } catch (IllegalArgumentException e) {
            LOG.error("Invalid timezone '{}', ignoring it for user {}.", timezone, username);
        }
    }
    final Startpage startpage = cr.startpage();
    if (startpage != null) {
        user.setStartpage(startpage.type(), startpage.id());
    }
    if (isPermitted("*")) {
        final Long sessionTimeoutMs = cr.sessionTimeoutMs();
        if (sessionTimeoutMs != null && sessionTimeoutMs != 0) {
            user.setSessionTimeoutMs(sessionTimeoutMs);
        }
    }
    userService.save(user);
}
Also used : User(org.graylog2.plugin.database.users.User) Startpage(org.graylog2.rest.models.users.requests.Startpage) NotFoundException(javax.ws.rs.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) DateTimeZone(org.joda.time.DateTimeZone) Path(javax.ws.rs.Path) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)4 AuditEvent (org.graylog2.audit.jersey.AuditEvent)4 Timed (com.codahale.metrics.annotation.Timed)3 BadRequestException (javax.ws.rs.BadRequestException)3 Consumes (javax.ws.rs.Consumes)3 Produces (javax.ws.rs.Produces)3 StreamRule (org.graylog2.plugin.streams.StreamRule)3 ApiResponses (io.swagger.annotations.ApiResponses)2 URI (java.net.URI)2 POST (javax.ws.rs.POST)2 PUT (javax.ws.rs.PUT)2 Path (javax.ws.rs.Path)2 Stream (org.graylog2.plugin.streams.Stream)2 NotFoundException (javax.ws.rs.NotFoundException)1 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)1 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)1 NotFoundException (org.graylog2.database.NotFoundException)1 User (org.graylog2.plugin.database.users.User)1 StreamRuleType (org.graylog2.plugin.streams.StreamRuleType)1 Startpage (org.graylog2.rest.models.users.requests.Startpage)1