Search in sources :

Example 6 with BadRequestException

use of javax.ws.rs.BadRequestException in project keywhiz by square.

the class GroupsResource method createGroup.

/**
   * Create Group
   *
   * @excludeParams user
   * @param request the JSON client request used to formulate the Group
   *
   * @description Creates a Group with the name from a valid group request.
   * Used by Keywhiz CLI and the web ui.
   * @responseMessage 200 Successfully created Group
   * @responseMessage 400 Group with given name already exists
   */
@Timed
@ExceptionMetered
@POST
@Consumes(APPLICATION_JSON)
public Response createGroup(@Auth User user, @Valid CreateGroupRequest request) {
    logger.info("User '{}' creating group.", user);
    if (groupDAO.getGroup(request.name).isPresent()) {
        throw new BadRequestException("Group already exists.");
    }
    long groupId = groupDAO.createGroup(request.name, user.getName(), nullToEmpty(request.description), request.metadata);
    URI uri = UriBuilder.fromResource(GroupsResource.class).build(groupId);
    Response response = Response.created(uri).entity(groupDetailResponseFromId(groupId)).build();
    if (response.getStatus() == HttpStatus.SC_CREATED) {
        Map<String, String> extraInfo = new HashMap<>();
        if (request.description != null) {
            extraInfo.put("description", request.description);
        }
        if (request.metadata != null) {
            extraInfo.put("metadata", request.metadata.toString());
        }
        auditLog.recordEvent(new Event(Instant.now(), EventTag.GROUP_CREATE, user.getName(), request.name, extraInfo));
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) GroupDetailResponse(keywhiz.api.GroupDetailResponse) HashMap(java.util.HashMap) BadRequestException(javax.ws.rs.BadRequestException) Event(keywhiz.log.Event) URI(java.net.URI) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Example 7 with BadRequestException

use of javax.ws.rs.BadRequestException 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 8 with BadRequestException

use of javax.ws.rs.BadRequestException in project graylog2-server by Graylog2.

the class RolesResource method delete.

@DELETE
@Path("{rolename}")
@ApiOperation(value = "Remove the named role and dissociate any users from it")
@AuditEvent(type = AuditEventTypes.ROLE_DELETE)
public void delete(@ApiParam(name = "rolename", required = true) @PathParam("rolename") String name) throws NotFoundException {
    checkPermission(RestPermissions.ROLES_DELETE, name);
    final Role role = roleService.load(name);
    if (role.isReadOnly()) {
        throw new BadRequestException("Cannot delete read only system role " + name);
    }
    userService.dissociateAllUsersFromRole(role);
    if (roleService.delete(name) == 0) {
        throw new NotFoundException("Couldn't find role " + name);
    }
}
Also used : Role(org.graylog2.shared.users.Role) BadRequestException(javax.ws.rs.BadRequestException) NotFoundException(org.graylog2.database.NotFoundException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Example 9 with BadRequestException

use of javax.ws.rs.BadRequestException in project graylog2-server by Graylog2.

the class RolesResource method create.

@POST
@RequiresPermissions(RestPermissions.ROLES_CREATE)
@ApiOperation(value = "Create a new role", notes = "")
@AuditEvent(type = AuditEventTypes.ROLE_CREATE)
public Response create(@ApiParam(name = "JSON body", value = "The new role to create", required = true) @Valid @NotNull RoleResponse roleResponse) {
    if (roleService.exists(roleResponse.name())) {
        throw new BadRequestException("Role " + roleResponse.name() + " already exists.");
    }
    Role role = new RoleImpl();
    role.setName(roleResponse.name());
    role.setPermissions(roleResponse.permissions());
    role.setDescription(roleResponse.description().orNull());
    try {
        role = roleService.save(role);
    } catch (ValidationException e) {
        log.error("Invalid role creation request.");
        throw new BadRequestException(e);
    }
    final URI uri = getUriBuilderToSelf().path(RolesResource.class).path("{rolename}").build(role.getName());
    return Response.created(uri).entity(RoleResponse.create(role.getName(), Optional.fromNullable(role.getDescription()), role.getPermissions(), role.isReadOnly())).build();
}
Also used : Role(org.graylog2.shared.users.Role) ValidationException(org.graylog2.plugin.database.ValidationException) RoleImpl(org.graylog2.users.RoleImpl) BadRequestException(javax.ws.rs.BadRequestException) URI(java.net.URI) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Example 10 with BadRequestException

use of javax.ws.rs.BadRequestException in project graylog2-server by Graylog2.

the class RolesResource method update.

@PUT
@Path("{rolename}")
@ApiOperation("Update an existing role")
@AuditEvent(type = AuditEventTypes.ROLE_UPDATE)
public RoleResponse update(@ApiParam(name = "rolename", required = true) @PathParam("rolename") String name, @ApiParam(name = "JSON Body", value = "The new representation of the role", required = true) RoleResponse role) throws NotFoundException {
    final Role roleToUpdate = roleService.load(name);
    if (roleToUpdate.isReadOnly()) {
        throw new BadRequestException("Cannot update read only role " + name);
    }
    roleToUpdate.setName(role.name());
    roleToUpdate.setDescription(role.description().orNull());
    roleToUpdate.setPermissions(role.permissions());
    try {
        roleService.save(roleToUpdate);
    } catch (ValidationException e) {
        throw new BadRequestException(e);
    }
    return RoleResponse.create(roleToUpdate.getName(), Optional.fromNullable(roleToUpdate.getDescription()), roleToUpdate.getPermissions(), role.readOnly());
}
Also used : Role(org.graylog2.shared.users.Role) ValidationException(org.graylog2.plugin.database.ValidationException) BadRequestException(javax.ws.rs.BadRequestException) Path(javax.ws.rs.Path) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT)

Aggregations

BadRequestException (javax.ws.rs.BadRequestException)58 ApiOperation (io.swagger.annotations.ApiOperation)34 AuditEvent (org.graylog2.audit.jersey.AuditEvent)31 Timed (com.codahale.metrics.annotation.Timed)26 Path (javax.ws.rs.Path)26 ApiResponses (io.swagger.annotations.ApiResponses)22 POST (javax.ws.rs.POST)20 Produces (javax.ws.rs.Produces)20 Consumes (javax.ws.rs.Consumes)18 URI (java.net.URI)13 PUT (javax.ws.rs.PUT)13 ValidationException (org.graylog2.plugin.database.ValidationException)11 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)9 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)8 NotFoundException (org.graylog2.database.NotFoundException)8 Stream (org.graylog2.plugin.streams.Stream)8 DELETE (javax.ws.rs.DELETE)6 NotFoundException (javax.ws.rs.NotFoundException)6 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)5 ConfigurationException (org.graylog2.plugin.configuration.ConfigurationException)5