Search in sources :

Example 81 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 roleRepresentation) throws NotFoundException {
    checkPermission(RestPermissions.ROLES_EDIT, name);
    final Role role = roleService.load(name);
    if (role.isReadOnly()) {
        throw new BadRequestException("Cannot update read only role " + name);
    }
    role.setName(roleRepresentation.name());
    role.setPermissions(roleRepresentation.permissions());
    roleRepresentation.description().ifPresent(role::setDescription);
    try {
        roleService.save(role);
    } catch (ValidationException e) {
        throw new BadRequestException("Couldn't update role \"" + roleRepresentation.name() + "\"", e);
    }
    return RoleResponse.create(role.getName(), Optional.ofNullable(role.getDescription()), role.getPermissions(), roleRepresentation.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)

Example 82 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("Create a new role")
@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());
    roleResponse.description().ifPresent(role::setDescription);
    try {
        role = roleService.save(role);
    } catch (ValidationException e) {
        log.error("Invalid role creation request", e);
        throw new BadRequestException("Couldn't create role \"" + roleResponse.name() + "\"", e);
    }
    final URI uri = getUriBuilderToSelf().path(RolesResource.class).path("{rolename}").build(role.getName());
    return Response.created(uri).entity(RoleResponse.create(role.getName(), Optional.ofNullable(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 83 with BadRequestException

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

the class StreamAlarmCallbackResource method update.

@PUT
@Path("/{alarmCallbackId}")
@Timed
@ApiOperation(value = "Update an alarm callback")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@AuditEvent(type = AuditEventTypes.ALARM_CALLBACK_UPDATE)
public void update(@ApiParam(name = "streamid", value = "The stream id this alarm callback belongs to.", required = true) @PathParam("streamid") String streamid, @ApiParam(name = "alarmCallbackId", required = true) @PathParam("alarmCallbackId") String alarmCallbackId, @ApiParam(name = "JSON body", required = true) CreateAlarmCallbackRequest alarmCallbackRequest) throws NotFoundException {
    checkPermission(RestPermissions.STREAMS_EDIT, streamid);
    final AlarmCallbackConfiguration callbackConfiguration = alarmCallbackConfigurationService.load(alarmCallbackId);
    if (callbackConfiguration == null) {
        throw new NotFoundException("Unable to find alarm callback configuration " + alarmCallbackId);
    }
    final Map<String, Object> configuration = convertConfigurationValues(alarmCallbackRequest);
    final AlarmCallbackConfiguration updatedConfig = ((AlarmCallbackConfigurationImpl) callbackConfiguration).toBuilder().setTitle(alarmCallbackRequest.title()).setConfiguration(configuration).build();
    try {
        alarmCallbackFactory.create(updatedConfig).checkConfiguration();
        alarmCallbackConfigurationService.save(updatedConfig);
    } catch (ValidationException | AlarmCallbackConfigurationException | ConfigurationException e) {
        LOG.error("Invalid alarm callback configuration.", e);
        throw new BadRequestException(e.getMessage(), e);
    } catch (ClassNotFoundException e) {
        LOG.error("Invalid alarm callback type.", e);
        throw new BadRequestException("Invalid alarm callback type.", e);
    }
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) AlarmCallbackConfigurationException(org.graylog2.plugin.alarms.callbacks.AlarmCallbackConfigurationException) ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) NotFoundException(org.graylog2.database.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) AlarmCallbackConfigurationException(org.graylog2.plugin.alarms.callbacks.AlarmCallbackConfigurationException) 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)

Example 84 with BadRequestException

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

the class StreamAlarmCallbackResource method convertConfigurationValues.

private Map<String, Object> convertConfigurationValues(final CreateAlarmCallbackRequest alarmCallbackRequest) {
    final ConfigurationRequest requestedConfiguration;
    try {
        final AlarmCallback alarmCallback = alarmCallbackFactory.create(alarmCallbackRequest.type());
        requestedConfiguration = alarmCallback.getRequestedConfiguration();
    } catch (ClassNotFoundException e) {
        throw new BadRequestException("Unable to load alarm callback of type " + alarmCallbackRequest.type(), e);
    }
    // coerce the configuration to their correct types according to the alarmcallback's requested config
    final Map<String, Object> configuration;
    try {
        configuration = ConfigurationMapConverter.convertValues(alarmCallbackRequest.configuration(), requestedConfiguration);
    } catch (ValidationException e) {
        throw new BadRequestException("Invalid configuration map", e);
    }
    return configuration;
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) ConfigurationRequest(org.graylog2.plugin.configuration.ConfigurationRequest) BadRequestException(javax.ws.rs.BadRequestException) AlarmCallback(org.graylog2.plugin.alarms.callbacks.AlarmCallback)

Example 85 with BadRequestException

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

the class ContentPackResourceTest method notDeleteContentPack.

@Test
public void notDeleteContentPack() throws Exception {
    final ModelId id = ModelId.of("1");
    when(contentPackInstallations.size()).thenReturn(1);
    when(contentPackInstallationPersistenceService.findByContentPackId(id)).thenReturn(contentPackInstallations);
    boolean exceptionCalled = false;
    try {
        contentPackResource.deleteContentPack(id);
    } catch (BadRequestException e) {
        exceptionCalled = true;
    }
    assertThat(exceptionCalled).isEqualTo(true);
    verify(contentPackInstallationPersistenceService, times(1)).findByContentPackId(id);
    verify(contentPackPersistenceService, times(0)).deleteById(id);
    when(contentPackInstallations.size()).thenReturn(1);
    when(contentPackInstallationPersistenceService.findByContentPackIdAndRevision(id, 1)).thenReturn(contentPackInstallations);
    exceptionCalled = false;
    try {
        contentPackResource.deleteContentPack(id, 1);
    } catch (BadRequestException e) {
        exceptionCalled = true;
    }
    assertThat(exceptionCalled).isEqualTo(true);
    verify(contentPackInstallationPersistenceService, times(1)).findByContentPackIdAndRevision(id, 1);
    verify(contentPackPersistenceService, times(0)).deleteByIdAndRevision(id, 1);
}
Also used : BadRequestException(javax.ws.rs.BadRequestException) ModelId(org.graylog2.contentpacks.model.ModelId) Test(org.junit.Test)

Aggregations

BadRequestException (javax.ws.rs.BadRequestException)238 Path (javax.ws.rs.Path)92 ApiOperation (io.swagger.annotations.ApiOperation)80 POST (javax.ws.rs.POST)65 Consumes (javax.ws.rs.Consumes)61 Produces (javax.ws.rs.Produces)55 AuditEvent (org.graylog2.audit.jersey.AuditEvent)52 NotFoundException (javax.ws.rs.NotFoundException)42 Timed (com.codahale.metrics.annotation.Timed)40 PUT (javax.ws.rs.PUT)40 ApiResponses (io.swagger.annotations.ApiResponses)38 Test (org.junit.Test)34 GET (javax.ws.rs.GET)32 IOException (java.io.IOException)30 Response (javax.ws.rs.core.Response)27 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)27 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)26 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)26 URI (java.net.URI)24 HashMap (java.util.HashMap)22