Search in sources :

Example 21 with NotFoundException

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

the class IndexSetsResource method setDefault.

@PUT
@Path("{id}/default")
@Timed
@ApiOperation(value = "Set default index set")
@AuditEvent(type = AuditEventTypes.INDEX_SET_UPDATE)
@ApiResponses(value = { @ApiResponse(code = 403, message = "Unauthorized") })
public IndexSetSummary setDefault(@ApiParam(name = "id", required = true) @PathParam("id") String id) {
    checkPermission(RestPermissions.INDEXSETS_EDIT, id);
    final IndexSetConfig indexSet = indexSetService.get(id).orElseThrow(() -> new NotFoundException("Index set <" + id + "> does not exist"));
    if (!indexSet.isWritable()) {
        throw new ClientErrorException("Default index set must be writable.", Response.Status.CONFLICT);
    }
    clusterConfigService.write(DefaultIndexSetConfig.create(indexSet.id()));
    final IndexSetConfig defaultIndexSet = indexSetService.getDefault();
    return IndexSetSummary.fromIndexSetConfig(indexSet, indexSet.equals(defaultIndexSet));
}
Also used : IndexSetConfig(org.graylog2.indexer.indexset.IndexSetConfig) DefaultIndexSetConfig(org.graylog2.indexer.indexset.DefaultIndexSetConfig) NotFoundException(javax.ws.rs.NotFoundException) ClientErrorException(javax.ws.rs.ClientErrorException) 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 22 with NotFoundException

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

the class LoggersResource method messages.

@GET
@Timed
@ApiOperation(value = "Get recent internal log messages")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Memory appender is disabled."), @ApiResponse(code = 500, message = "Memory appender is broken.") })
@Path("/messages/recent")
@Produces(MediaType.APPLICATION_JSON)
public LogMessagesSummary messages(@ApiParam(name = "limit", value = "How many log messages should be returned", defaultValue = "500", allowableValues = "range[0, infinity]") @QueryParam("limit") @DefaultValue("500") @Min(0L) int limit, @ApiParam(name = "level", value = "Which log level (or higher) should the messages have", defaultValue = "ALL", allowableValues = "[OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL]") @QueryParam("level") @DefaultValue("ALL") @NotEmpty String level) {
    final Appender appender = getAppender(MEMORY_APPENDER_NAME);
    if (appender == null) {
        throw new NotFoundException("Memory appender is disabled. Please refer to the example log4j.xml file.");
    }
    if (!(appender instanceof MemoryAppender)) {
        throw new InternalServerErrorException("Memory appender is not an instance of MemoryAppender. Please refer to the example log4j.xml file.");
    }
    final Level logLevel = Level.toLevel(level, Level.ALL);
    final MemoryAppender memoryAppender = (MemoryAppender) appender;
    final List<InternalLogMessage> messages = new ArrayList<>(limit);
    for (LogEvent event : memoryAppender.getLogMessages(limit)) {
        final Level eventLevel = event.getLevel();
        if (!eventLevel.isMoreSpecificThan(logLevel)) {
            continue;
        }
        final ThrowableProxy thrownProxy = event.getThrownProxy();
        final String throwable;
        if (thrownProxy == null) {
            throwable = null;
        } else {
            throwable = thrownProxy.getExtendedStackTraceAsString();
        }
        final Marker marker = event.getMarker();
        messages.add(InternalLogMessage.create(event.getMessage().getFormattedMessage(), event.getLoggerName(), eventLevel.toString(), marker == null ? null : marker.toString(), new DateTime(event.getTimeMillis(), DateTimeZone.UTC), throwable, event.getThreadName(), event.getContextData().toMap()));
    }
    return LogMessagesSummary.create(messages);
}
Also used : Appender(org.apache.logging.log4j.core.Appender) MemoryAppender(org.graylog2.log4j.MemoryAppender) MemoryAppender(org.graylog2.log4j.MemoryAppender) LogEvent(org.apache.logging.log4j.core.LogEvent) ArrayList(java.util.ArrayList) NotFoundException(javax.ws.rs.NotFoundException) Marker(org.apache.logging.log4j.Marker) ThrowableProxy(org.apache.logging.log4j.core.impl.ThrowableProxy) DateTime(org.joda.time.DateTime) InternalLogMessage(org.graylog2.rest.models.system.loggers.responses.InternalLogMessage) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Level(org.apache.logging.log4j.Level) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 23 with NotFoundException

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

the class RetentionStrategyResource method getRetentionStrategyDescription.

private RetentionStrategyDescription getRetentionStrategyDescription(@ApiParam(name = "strategy", value = "The name of the retention strategy", required = true) @PathParam("strategy") @NotEmpty String strategyName) {
    final Provider<RetentionStrategy> provider = retentionStrategies.get(strategyName);
    if (provider == null) {
        throw new NotFoundException("Couldn't find retention strategy for given type " + strategyName);
    }
    final RetentionStrategy retentionStrategy = provider.get();
    final RetentionStrategyConfig defaultConfig = retentionStrategy.defaultConfiguration();
    final SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
    try {
        objectMapper.acceptJsonFormatVisitor(objectMapper.constructType(retentionStrategy.configurationClass()), visitor);
    } catch (JsonMappingException e) {
        throw new InternalServerErrorException("Couldn't generate JSON schema for retention strategy " + strategyName, e);
    }
    return RetentionStrategyDescription.create(strategyName, defaultConfig, visitor.finalSchema());
}
Also used : NoopRetentionStrategyConfig(org.graylog2.indexer.retention.strategies.NoopRetentionStrategyConfig) RetentionStrategyConfig(org.graylog2.plugin.indexer.retention.RetentionStrategyConfig) RetentionStrategy(org.graylog2.plugin.indexer.retention.RetentionStrategy) NoopRetentionStrategy(org.graylog2.indexer.retention.strategies.NoopRetentionStrategy) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) NotFoundException(javax.ws.rs.NotFoundException) SchemaFactoryWrapper(com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper) InternalServerErrorException(javax.ws.rs.InternalServerErrorException)

Example 24 with NotFoundException

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

the class RotationStrategyResource method config.

@PUT
@Path("config")
@Consumes(MediaType.APPLICATION_JSON)
@Timed
@ApiOperation(value = "Configuration of the current rotation strategy", notes = "This resource stores the configuration of the currently used rotation strategy.")
@AuditEvent(type = AuditEventTypes.ES_INDEX_ROTATION_STRATEGY_UPDATE)
public RotationStrategySummary config(@ApiParam(value = "The description of the rotation strategy and its configuration", required = true) @Valid @NotNull RotationStrategySummary rotationStrategySummary) {
    if (!rotationStrategies.containsKey(rotationStrategySummary.strategy())) {
        throw new NotFoundException("Couldn't find rotation strategy for given type " + rotationStrategySummary.strategy());
    }
    final IndexManagementConfig oldConfig = clusterConfigService.get(IndexManagementConfig.class);
    if (oldConfig == null) {
        throw new InternalServerErrorException("Couldn't retrieve index management configuration");
    }
    final IndexManagementConfig indexManagementConfig = IndexManagementConfig.create(rotationStrategySummary.strategy(), oldConfig.retentionStrategy());
    clusterConfigService.write(rotationStrategySummary.config());
    clusterConfigService.write(indexManagementConfig);
    return rotationStrategySummary;
}
Also used : NotFoundException(javax.ws.rs.NotFoundException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) IndexManagementConfig(org.graylog2.indexer.management.IndexManagementConfig) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT)

Example 25 with NotFoundException

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

the class RotationStrategyResource method getRotationStrategyDescription.

private RotationStrategyDescription getRotationStrategyDescription(String strategyName) {
    final Provider<RotationStrategy> provider = rotationStrategies.get(strategyName);
    if (provider == null) {
        throw new NotFoundException("Couldn't find rotation strategy for given type " + strategyName);
    }
    final RotationStrategy rotationStrategy = provider.get();
    final RotationStrategyConfig defaultConfig = rotationStrategy.defaultConfiguration();
    final SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
    try {
        objectMapper.acceptJsonFormatVisitor(objectMapper.constructType(rotationStrategy.configurationClass()), visitor);
    } catch (JsonMappingException e) {
        throw new InternalServerErrorException("Couldn't generate JSON schema for rotation strategy " + strategyName, e);
    }
    return RotationStrategyDescription.create(strategyName, defaultConfig, visitor.finalSchema());
}
Also used : RotationStrategy(org.graylog2.plugin.indexer.rotation.RotationStrategy) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) RotationStrategyConfig(org.graylog2.plugin.indexer.rotation.RotationStrategyConfig) NotFoundException(javax.ws.rs.NotFoundException) SchemaFactoryWrapper(com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper) InternalServerErrorException(javax.ws.rs.InternalServerErrorException)

Aggregations

NotFoundException (javax.ws.rs.NotFoundException)68 Path (javax.ws.rs.Path)46 Timed (com.codahale.metrics.annotation.Timed)45 ApiOperation (io.swagger.annotations.ApiOperation)27 ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)25 GET (javax.ws.rs.GET)22 ApiResponses (io.swagger.annotations.ApiResponses)20 DELETE (javax.ws.rs.DELETE)20 Produces (javax.ws.rs.Produces)18 AuditEvent (org.graylog2.audit.jersey.AuditEvent)16 HashMap (java.util.HashMap)15 PUT (javax.ws.rs.PUT)15 Group (keywhiz.api.model.Group)14 SanitizedSecret (keywhiz.api.model.SanitizedSecret)14 Event (keywhiz.log.Event)14 Consumes (javax.ws.rs.Consumes)12 Client (keywhiz.api.model.Client)11 POST (javax.ws.rs.POST)10 BadRequestException (javax.ws.rs.BadRequestException)9 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)9