Search in sources :

Example 56 with ValidationResult

use of org.graylog2.plugin.rest.ValidationResult in project graylog2-server by Graylog2.

the class EventNotificationsResource method test.

@POST
@Timed
@Path("/test")
@RequiresPermissions(RestPermissions.EVENT_NOTIFICATIONS_CREATE)
@ApiOperation(value = "Send a test alert for a given event notification")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Event notification is invalid."), @ApiResponse(code = 500, message = "Error while testing event notification") })
@NoAuditEvent("only used to test event notifications")
public Response test(@ApiParam(name = "JSON Body") NotificationDto dto) {
    checkPermission(RestPermissions.EVENT_NOTIFICATIONS_CREATE);
    final ValidationResult validationResult = dto.validate();
    if (validationResult.failed()) {
        return Response.status(Response.Status.BAD_REQUEST).entity(validationResult).build();
    }
    resourceHandler.test(dto, getSubject().getPrincipal().toString());
    return Response.ok().build();
}
Also used : ValidationResult(org.graylog2.plugin.rest.ValidationResult) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) 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 57 with ValidationResult

use of org.graylog2.plugin.rest.ValidationResult in project graylog2-server by Graylog2.

the class EventNotificationsResource method update.

@PUT
@Path("/{notificationId}")
@ApiOperation("Update existing notification")
@AuditEvent(type = EventsAuditEventTypes.EVENT_NOTIFICATION_UPDATE)
public Response update(@ApiParam(name = "notificationId") @PathParam("notificationId") @NotBlank String notificationId, @ApiParam(name = "JSON Body") NotificationDto dto) {
    checkPermission(RestPermissions.EVENT_NOTIFICATIONS_EDIT, notificationId);
    dbNotificationService.get(notificationId).orElseThrow(() -> new NotFoundException("Notification " + notificationId + " doesn't exist"));
    if (!notificationId.equals(dto.id())) {
        throw new BadRequestException("Notification IDs don't match");
    }
    final ValidationResult validationResult = dto.validate();
    if (validationResult.failed()) {
        return Response.status(Response.Status.BAD_REQUEST).entity(validationResult).build();
    }
    return Response.ok().entity(resourceHandler.update(dto)).build();
}
Also used : NotFoundException(javax.ws.rs.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) ValidationResult(org.graylog2.plugin.rest.ValidationResult) Path(javax.ws.rs.Path) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) PUT(javax.ws.rs.PUT)

Example 58 with ValidationResult

use of org.graylog2.plugin.rest.ValidationResult in project graylog2-server by Graylog2.

the class EventDefinitionsResource method create.

@POST
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Create new event definition")
@AuditEvent(type = EventsAuditEventTypes.EVENT_DEFINITION_CREATE)
@RequiresPermissions(RestPermissions.EVENT_DEFINITIONS_CREATE)
public Response create(@ApiParam("schedule") @QueryParam("schedule") @DefaultValue("true") boolean schedule, @ApiParam(name = "JSON Body") EventDefinitionDto dto, @Context UserContext userContext) {
    checkEventDefinitionPermissions(dto, "create");
    final ValidationResult result = dto.validate();
    if (result.failed()) {
        return Response.status(Response.Status.BAD_REQUEST).entity(result).build();
    }
    final EventDefinitionDto entity = schedule ? eventDefinitionHandler.create(dto, Optional.of(userContext.getUser())) : eventDefinitionHandler.createWithoutSchedule(dto, Optional.of(userContext.getUser()));
    return Response.ok().entity(entity).build();
}
Also used : EventDefinitionDto(org.graylog.events.processor.EventDefinitionDto) ValidationResult(org.graylog2.plugin.rest.ValidationResult) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Aggregations

ValidationResult (org.graylog2.plugin.rest.ValidationResult)43 Test (org.junit.Test)24 ApiOperation (io.swagger.annotations.ApiOperation)16 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)16 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)14 Path (javax.ws.rs.Path)12 AuditEvent (org.graylog2.audit.jersey.AuditEvent)12 POST (javax.ws.rs.POST)11 List (java.util.List)10 Subject (org.apache.shiro.subject.Subject)8 GRN (org.graylog.grn.GRN)8 User (org.graylog2.plugin.database.users.User)8 ArrayList (java.util.ArrayList)7 Produces (javax.ws.rs.Produces)7 ValidationException (org.graylog2.plugin.database.ValidationException)6 DisplayName (org.junit.jupiter.api.DisplayName)6 Test (org.junit.jupiter.api.Test)6 BasicDBObject (com.mongodb.BasicDBObject)5 Collectors (java.util.stream.Collectors)5 NotFoundException (javax.ws.rs.NotFoundException)5