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());
}
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();
}
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);
}
}
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;
}
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);
}
Aggregations