Search in sources :

Example 11 with BadRequestException

use of javax.ws.rs.BadRequestException 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);
    }
}
Also used : ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) BadRequestException(javax.ws.rs.BadRequestException) Stream(org.graylog2.plugin.streams.Stream) Path(javax.ws.rs.Path) 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 12 with BadRequestException

use of javax.ws.rs.BadRequestException 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();
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) ConfigurationRequest(org.graylog2.plugin.configuration.ConfigurationRequest) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) BadRequestException(javax.ws.rs.BadRequestException)

Example 13 with BadRequestException

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

the class StreamAlertResource method removeReceiver.

@DELETE
@Timed
@Path("receivers")
@ApiOperation(value = "Remove an alert receiver")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream not found."), @ApiResponse(code = 400, message = "Invalid ObjectId."), @ApiResponse(code = 400, message = "Stream has no email alarm callbacks.") })
@AuditEvent(type = AuditEventTypes.ALERT_RECEIVER_DELETE)
@Deprecated
public void removeReceiver(@ApiParam(name = "streamId", value = "The stream id this new alert condition belongs to.", required = true) @PathParam("streamId") String streamId, @ApiParam(name = "entity", value = "Name/ID of user or email address to remove from alert receivers.", required = true) @QueryParam("entity") String entity, @ApiParam(name = "type", value = "Type: users or emails", required = true) @QueryParam("type") String type) throws NotFoundException {
    checkPermission(RestPermissions.STREAMS_EDIT, streamId);
    if (!"users".equals(type) && !"emails".equals(type)) {
        final String msg = "No such type: [" + type + "]";
        LOG.warn(msg);
        throw new BadRequestException(msg);
    }
    final Stream stream = streamService.load(streamId);
    streamService.removeAlertReceiver(stream, type, entity);
}
Also used : BadRequestException(javax.ws.rs.BadRequestException) Stream(org.graylog2.plugin.streams.Stream) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent) ApiResponses(io.swagger.annotations.ApiResponses)

Example 14 with BadRequestException

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

the class StreamAlertResource method sendDummyAlert.

@POST
@Timed
@Path("sendDummyAlert")
@ApiOperation(value = "Send a test mail for a given stream")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream not found."), @ApiResponse(code = 400, message = "Invalid ObjectId."), @ApiResponse(code = 400, message = "Stream has no alarm callbacks") })
@NoAuditEvent("only used to test alert emails")
public void sendDummyAlert(@ApiParam(name = "streamId", value = "The stream id the test alert should be sent for.", required = true) @PathParam("streamId") String streamId) throws TransportConfigurationException, EmailException, NotFoundException {
    checkPermission(RestPermissions.STREAMS_EDIT, streamId);
    final Stream stream = streamService.load(streamId);
    final DummyAlertCondition dummyAlertCondition = new DummyAlertCondition(stream, null, Tools.nowUTC(), getSubject().getPrincipal().toString(), Collections.emptyMap(), "Test Alert");
    try {
        AbstractAlertCondition.CheckResult checkResult = dummyAlertCondition.runCheck();
        List<AlarmCallbackConfiguration> callConfigurations = alarmCallbackConfigurationService.getForStream(stream);
        if (callConfigurations.size() == 0) {
            final String message = "Stream has no alarm callbacks, cannot send test alert.";
            LOG.warn(message);
            throw new BadRequestException(message);
        }
        for (AlarmCallbackConfiguration configuration : callConfigurations) {
            AlarmCallback alarmCallback = alarmCallbackFactory.create(configuration);
            alarmCallback.call(stream, checkResult);
        }
    } catch (AlarmCallbackException | ClassNotFoundException | AlarmCallbackConfigurationException e) {
        throw new InternalServerErrorException(e.getMessage(), e);
    }
}
Also used : AlarmCallbackConfigurationException(org.graylog2.plugin.alarms.callbacks.AlarmCallbackConfigurationException) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Stream(org.graylog2.plugin.streams.Stream) AlarmCallback(org.graylog2.plugin.alarms.callbacks.AlarmCallback) AbstractAlertCondition(org.graylog2.alerts.AbstractAlertCondition) DummyAlertCondition(org.graylog2.alerts.types.DummyAlertCondition) AlarmCallbackException(org.graylog2.plugin.alarms.callbacks.AlarmCallbackException) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Example 15 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)

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