Search in sources :

Example 81 with NotFoundException

use of org.graylog2.database.NotFoundException in project graylog2-server by Graylog2.

the class AlertResource method get.

@GET
@Timed
@Path("{alertId}")
@ApiOperation(value = "Get an alert by ID.")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Alert not found."), @ApiResponse(code = 400, message = "Invalid ObjectId.") })
public AlertSummary get(@ApiParam(name = "alertId", value = "The alert ID to retrieve.", required = true) @PathParam("alertId") String alertId) throws NotFoundException {
    final Alert alert = alertService.load(alertId, "");
    checkPermission(STREAMS_READ, alert.getStreamId());
    return AlertSummary.create(alert.getId(), alert.getConditionId(), alert.getStreamId(), alert.getDescription(), alert.getConditionParameters(), alert.getTriggeredAt(), alert.getResolvedAt(), alert.isInterval());
}
Also used : Alert(org.graylog2.alerts.Alert) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 82 with NotFoundException

use of org.graylog2.database.NotFoundException in project graylog2-server by Graylog2.

the class MessageResource method parse.

@POST
@Path("/parse")
@Timed
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Parse a raw message")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Specified codec does not exist."), @ApiResponse(code = 400, message = "Could not decode message.") })
@NoAuditEvent("only used to parse a test message")
public ResultMessage parse(@ApiParam(name = "JSON body", required = true) MessageParseRequest request) {
    Codec codec;
    try {
        final Configuration configuration = new Configuration(request.configuration());
        codec = codecFactory.create(request.codec(), configuration);
    } catch (IllegalArgumentException e) {
        throw new NotFoundException(e);
    }
    final ResolvableInetSocketAddress remoteAddress = ResolvableInetSocketAddress.wrap(new InetSocketAddress(request.remoteAddress(), 1234));
    final RawMessage rawMessage = new RawMessage(0, new UUID(), Tools.nowUTC(), remoteAddress, request.message().getBytes(StandardCharsets.UTF_8));
    final Message message = decodeMessage(codec, remoteAddress, rawMessage);
    return ResultMessage.createFromMessage(message);
}
Also used : Codec(org.graylog2.plugin.inputs.codecs.Codec) ResolvableInetSocketAddress(org.graylog2.plugin.ResolvableInetSocketAddress) Configuration(org.graylog2.plugin.configuration.Configuration) ResultMessage(org.graylog2.indexer.results.ResultMessage) RawMessage(org.graylog2.plugin.journal.RawMessage) Message(org.graylog2.plugin.Message) ResolvableInetSocketAddress(org.graylog2.plugin.ResolvableInetSocketAddress) InetSocketAddress(java.net.InetSocketAddress) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) NotFoundException(javax.ws.rs.NotFoundException) DocumentNotFoundException(org.graylog2.indexer.messages.DocumentNotFoundException) RawMessage(org.graylog2.plugin.journal.RawMessage) UUID(com.eaio.uuid.UUID) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Example 83 with NotFoundException

use of org.graylog2.database.NotFoundException in project graylog2-server by Graylog2.

the class RolesResource method addMember.

@PUT
@Path("{rolename}/members/{username}")
@ApiOperation("Add a user to a role")
@AuditEvent(type = AuditEventTypes.ROLE_MEMBERSHIP_UPDATE)
public Response addMember(@ApiParam(name = "rolename") @PathParam("rolename") String rolename, @ApiParam(name = "username") @PathParam("username") String username, @ApiParam(name = "JSON Body", value = "Placeholder because PUT requests should have a body. Set to '{}', the content will be ignored.", defaultValue = "{}") String body) throws NotFoundException {
    checkPermission(RestPermissions.ROLES_EDIT, username);
    final User user = userService.load(username);
    if (user == null) {
        throw new NotFoundException("User " + username + " has not been found.");
    }
    // verify that the role exists
    final Role role = roleService.load(rolename);
    final HashSet<String> roles = Sets.newHashSet(user.getRoleIds());
    roles.add(role.getId());
    user.setRoleIds(roles);
    try {
        userService.save(user);
    } catch (ValidationException e) {
        throw new BadRequestException("Validation failed", e);
    }
    return status(Response.Status.NO_CONTENT).build();
}
Also used : Role(org.graylog2.shared.users.Role) User(org.graylog2.plugin.database.users.User) ValidationException(org.graylog2.plugin.database.ValidationException) NotFoundException(org.graylog2.database.NotFoundException) 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 84 with NotFoundException

use of org.graylog2.database.NotFoundException in project graylog2-server by Graylog2.

the class RolesResource method removeMember.

@DELETE
@Path("{rolename}/members/{username}")
@ApiOperation("Remove a user from a role")
@AuditEvent(type = AuditEventTypes.ROLE_MEMBERSHIP_DELETE)
public Response removeMember(@ApiParam(name = "rolename") @PathParam("rolename") String rolename, @ApiParam(name = "username") @PathParam("username") String username) throws NotFoundException {
    checkPermission(RestPermissions.ROLES_EDIT, username);
    final User user = userService.load(username);
    if (user == null) {
        throw new NotFoundException("User " + username + " has not been found.");
    }
    // verify that the role exists
    final Role role = roleService.load(rolename);
    final HashSet<String> roles = Sets.newHashSet(user.getRoleIds());
    roles.remove(role.getId());
    user.setRoleIds(roles);
    try {
        userService.save(user);
    } catch (ValidationException e) {
        throw new BadRequestException("Validation failed", e);
    }
    return status(Response.Status.NO_CONTENT).build();
}
Also used : Role(org.graylog2.shared.users.Role) User(org.graylog2.plugin.database.users.User) ValidationException(org.graylog2.plugin.database.ValidationException) NotFoundException(org.graylog2.database.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Example 85 with NotFoundException

use of org.graylog2.database.NotFoundException in project graylog2-server by Graylog2.

the class RolesResource method getMembers.

@GET
@Path("{rolename}/members")
@RequiresPermissions({ RestPermissions.USERS_LIST, RestPermissions.ROLES_READ })
@ApiOperation(value = "Retrieve the role's members")
public RoleMembershipResponse getMembers(@ApiParam(name = "rolename", required = true) @PathParam("rolename") String name) throws NotFoundException {
    final Role role = roleService.load(name);
    final Collection<User> users = userService.loadAllForRole(role);
    Set<UserSummary> userSummaries = Sets.newHashSetWithExpectedSize(users.size());
    for (User user : users) {
        final Set<String> roleNames = userService.getRoleNames(user);
        userSummaries.add(UserSummary.create(user.getId(), user.getName(), user.getEmail(), user.getFullName(), isPermitted(RestPermissions.USERS_PERMISSIONSEDIT, user.getName()) ? userService.getPermissionsForUser(user) : Collections.<String>emptyList(), user.getPreferences(), firstNonNull(user.getTimeZone(), DateTimeZone.UTC).getID(), user.getSessionTimeoutMs(), user.isReadOnly(), user.isExternalUser(), user.getStartpage(), roleNames, // there is no session information available in this call, so we set it to null
        false, null, null));
    }
    return RoleMembershipResponse.create(role.getName(), userSummaries);
}
Also used : Role(org.graylog2.shared.users.Role) User(org.graylog2.plugin.database.users.User) UserSummary(org.graylog2.rest.models.users.responses.UserSummary) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)91 Timed (com.codahale.metrics.annotation.Timed)77 Path (javax.ws.rs.Path)75 ApiResponses (io.swagger.annotations.ApiResponses)66 AuditEvent (org.graylog2.audit.jersey.AuditEvent)60 Produces (javax.ws.rs.Produces)44 NotFoundException (org.graylog2.database.NotFoundException)32 GET (javax.ws.rs.GET)30 PUT (javax.ws.rs.PUT)28 BadRequestException (javax.ws.rs.BadRequestException)27 Stream (org.graylog2.plugin.streams.Stream)27 NotFoundException (javax.ws.rs.NotFoundException)26 Consumes (javax.ws.rs.Consumes)21 DELETE (javax.ws.rs.DELETE)21 POST (javax.ws.rs.POST)19 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)15 MessageInput (org.graylog2.plugin.inputs.MessageInput)15 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)11 Input (org.graylog2.inputs.Input)11 ValidationException (org.graylog2.plugin.database.ValidationException)11