use of org.graylog2.plugin.database.ValidationException 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();
}
use of org.graylog2.plugin.database.ValidationException 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());
}
use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.
the class StreamAlertConditionResource method update.
@PUT
@Timed
@Path("{conditionId}")
@ApiOperation(value = "Modify an alert condition")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream not found."), @ApiResponse(code = 400, message = "Invalid ObjectId.") })
@AuditEvent(type = AuditEventTypes.ALERT_CONDITION_UPDATE)
public void update(@ApiParam(name = "streamId", value = "The stream id the alert condition belongs to.", required = true) @PathParam("streamId") String streamid, @ApiParam(name = "conditionId", value = "The alert condition id.", required = true) @PathParam("conditionId") String conditionid, @ApiParam(name = "JSON body", required = true) @Valid @NotNull CreateConditionRequest ccr) throws NotFoundException, ValidationException {
checkPermission(RestPermissions.STREAMS_EDIT, streamid);
final Stream stream = streamService.load(streamid);
AlertCondition alertCondition = streamService.getAlertCondition(stream, conditionid);
try {
final AlertCondition updatedCondition = alertService.updateFromRequest(alertCondition, convertConfigurationInRequest(ccr));
streamService.updateAlertCondition(stream, updatedCondition);
} catch (ConfigurationException e) {
throw new BadRequestException("Invalid alert condition parameters", e);
}
}
use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.
the class StreamAlertConditionResource method convertConfigurationInRequest.
private CreateConditionRequest convertConfigurationInRequest(final CreateConditionRequest request) {
final AlertCondition.Factory factory = alertConditionMap.get(request.type());
if (factory == null) {
throw new BadRequestException("Unable to load alert condition of type " + request.type());
}
final ConfigurationRequest requestedConfiguration = factory.config().getRequestedConfiguration();
// coerce the configuration to their correct types according to the condition's requested config
final Map<String, Object> parameters;
try {
parameters = ConfigurationMapConverter.convertValues(request.parameters(), requestedConfiguration);
} catch (ValidationException e) {
throw new BadRequestException("Invalid alert condition parameters", e);
}
return request.toBuilder().setParameters(parameters).build();
}
use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.
the class DashboardWidgetsResource method updateDescription.
@Deprecated
@PUT
@Timed
@ApiOperation(value = "Update description of a widget")
@Path("/{widgetId}/description")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Dashboard not found."), @ApiResponse(code = 404, message = "Widget not found.") })
@Produces(MediaType.APPLICATION_JSON)
@AuditEvent(type = AuditEventTypes.DASHBOARD_WIDGET_UPDATE)
public void updateDescription(@ApiParam(name = "dashboardId", required = true) @PathParam("dashboardId") String dashboardId, @ApiParam(name = "widgetId", required = true) @PathParam("widgetId") String widgetId, @ApiParam(name = "JSON body", required = true) @Valid UpdateWidgetRequest uwr) throws ValidationException, NotFoundException {
checkPermission(RestPermissions.DASHBOARDS_EDIT, dashboardId);
final Dashboard dashboard = dashboardService.load(dashboardId);
final DashboardWidget widget = dashboard.getWidget(widgetId);
if (widget == null) {
final String msg = "Widget " + widgetId + " on dashboard " + dashboardId + " not found.";
LOG.error(msg);
throw new javax.ws.rs.NotFoundException(msg);
}
dashboardService.updateWidgetDescription(dashboard, widget, uwr.description());
LOG.info("Updated description of widget <" + widgetId + "> on dashboard <" + dashboardId + ">. Reason: REST request.");
}
Aggregations