Search in sources :

Example 81 with ValidationException

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

the class PersistedServiceImpl method save.

@Override
public <T extends Persisted> String save(T model) throws ValidationException {
    Map<String, List<ValidationResult>> errors = validate(model, model.getFields());
    if (!errors.isEmpty()) {
        throw new ValidationException(errors);
    }
    BasicDBObject doc = new BasicDBObject(model.getFields());
    // ID was created in constructor or taken from original doc already.
    doc.put("_id", new ObjectId(model.getId()));
    // Do field transformations
    fieldTransformations(doc);
    /*
         * We are running an upsert. This means that the existing
         * document will be updated if the ID already exists and
         * a new document will be created if it doesn't.
         */
    BasicDBObject q = new BasicDBObject("_id", new ObjectId(model.getId()));
    collection(model).update(q, doc, true, false);
    return model.getId();
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) ValidationException(org.graylog2.plugin.database.ValidationException) ObjectId(org.bson.types.ObjectId) ArrayList(java.util.ArrayList) List(java.util.List)

Example 82 with ValidationException

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

the class PersistedServiceImpl method embed.

protected <T extends Persisted> void embed(T model, String key, EmbeddedPersistable o) throws ValidationException {
    Map<String, List<ValidationResult>> errors = validate(model.getEmbeddedValidations(key), o.getPersistedFields());
    if (!errors.isEmpty()) {
        throw new ValidationException(errors);
    }
    Map<String, Object> fields = Maps.newHashMap(o.getPersistedFields());
    fieldTransformations(fields);
    BasicDBObject dbo = new BasicDBObject(fields);
    collection(model).update(new BasicDBObject("_id", new ObjectId(model.getId())), new BasicDBObject("$push", new BasicDBObject(key, dbo)));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) ValidationException(org.graylog2.plugin.database.ValidationException) ObjectId(org.bson.types.ObjectId) ArrayList(java.util.ArrayList) List(java.util.List) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 83 with ValidationException

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

the class MongoDbGrokPatternService method saveAll.

@Override
public List<GrokPattern> saveAll(Collection<GrokPattern> patterns, ImportStrategy importStrategy) throws ValidationException {
    final Map<String, GrokPattern> newPatternsByName;
    try {
        newPatternsByName = patterns.stream().collect(Collectors.toMap(GrokPattern::name, Function.identity()));
    } catch (IllegalStateException e) {
        throw new ValidationException("The supplied Grok patterns contain conflicting names: " + e.getLocalizedMessage());
    }
    final Map<String, GrokPattern> existingPatternsByName = loadAll().stream().collect(Collectors.toMap(GrokPattern::name, Function.identity()));
    if (importStrategy == ABORT_ON_CONFLICT) {
        final Sets.SetView<String> conflictingNames = Sets.intersection(newPatternsByName.keySet(), existingPatternsByName.keySet());
        if (!conflictingNames.isEmpty()) {
            final Iterable<String> limited = Iterables.limit(conflictingNames, MAX_DISPLAYED_CONFLICTS);
            throw new ValidationException("The following Grok patterns already exist: " + StringUtils.join(limited, ", ") + (conflictingNames.size() > MAX_DISPLAYED_CONFLICTS ? " (+ " + (conflictingNames.size() - MAX_DISPLAYED_CONFLICTS) + " more)" : "") + ".");
        }
    }
    try {
        if (!validateAll(patterns)) {
            throw new ValidationException("Invalid patterns");
        }
    } catch (GrokException | PatternSyntaxException e) {
        throw new ValidationException("Invalid patterns.\n" + e.getMessage());
    }
    if (importStrategy == DROP_ALL_EXISTING) {
        deleteAll();
    }
    final List<GrokPattern> savedPatterns = patterns.stream().map(newPattern -> {
        final GrokPattern existingPattern = existingPatternsByName.get(newPattern.name());
        if (existingPattern != null) {
            return newPattern.toBuilder().id(existingPattern.id()).build();
        } else {
            return newPattern;
        }
    }).map(dbCollection::save).map(WriteResult::getSavedObject).collect(Collectors.toList());
    clusterBus.post(GrokPatternsUpdatedEvent.create(newPatternsByName.keySet()));
    return savedPatterns;
}
Also used : Iterables(com.google.common.collect.Iterables) DROP_ALL_EXISTING(org.graylog2.grok.GrokPatternService.ImportStrategy.DROP_ALL_EXISTING) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) WriteResult(org.mongojack.WriteResult) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) Map(java.util.Map) NotFoundException(org.graylog2.database.NotFoundException) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) PatternSyntaxException(java.util.regex.PatternSyntaxException) Iterator(java.util.Iterator) GrokException(io.krakens.grok.api.exception.GrokException) DBCursor(org.mongojack.DBCursor) Collection(java.util.Collection) MongoJackObjectMapperProvider(org.graylog2.bindings.providers.MongoJackObjectMapperProvider) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) JacksonDBCollection(org.mongojack.JacksonDBCollection) Set(java.util.Set) DBQuery(org.mongojack.DBQuery) IndexOptions(com.mongodb.client.model.IndexOptions) ABORT_ON_CONFLICT(org.graylog2.grok.GrokPatternService.ImportStrategy.ABORT_ON_CONFLICT) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Indexes(com.mongodb.client.model.Indexes) List(java.util.List) ClusterEventBus(org.graylog2.events.ClusterEventBus) Grok(io.krakens.grok.api.Grok) ValidationException(org.graylog2.plugin.database.ValidationException) ObjectId(org.bson.types.ObjectId) Optional(java.util.Optional) GrokCompiler(io.krakens.grok.api.GrokCompiler) MongoConnection(org.graylog2.database.MongoConnection) ValidationException(org.graylog2.plugin.database.ValidationException) Sets(com.google.common.collect.Sets) GrokException(io.krakens.grok.api.exception.GrokException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 84 with ValidationException

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

the class InMemoryGrokPatternService method saveAll.

@Override
public List<GrokPattern> saveAll(Collection<GrokPattern> patterns, ImportStrategy importStrategy) throws ValidationException {
    if (importStrategy == ABORT_ON_CONFLICT) {
        for (GrokPattern pattern : loadAll()) {
            final boolean patternExists = patterns.stream().anyMatch(p -> p.name().equals(pattern.name()));
            if (patternExists) {
                throw new ValidationException("Grok pattern " + pattern.name() + " already exists");
            }
        }
    }
    try {
        if (!validateAll(patterns)) {
            throw new ValidationException("Patterns invalid.");
        }
    } catch (GrokException | PatternSyntaxException e) {
        throw new ValidationException("Invalid patterns.\n" + e.getMessage());
    }
    if (importStrategy == DROP_ALL_EXISTING) {
        deleteAll();
    }
    final List<GrokPattern> grokPatterns = patterns.stream().map(this::uncheckedSave).collect(Collectors.toList());
    final Set<String> patternNames = grokPatterns.stream().map(GrokPattern::name).collect(Collectors.toSet());
    if (!patternNames.isEmpty()) {
        clusterBus.post(GrokPatternsUpdatedEvent.create(patternNames));
    }
    return grokPatterns;
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) GrokException(io.krakens.grok.api.exception.GrokException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 85 with ValidationException

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

the class V20191121145100_FixDefaultGrokPatterns method migratePattern.

private void migratePattern(PatternToMigrate patternToMigrate) throws ValidationException {
    final Optional<GrokPattern> currentPattern = grokPatternService.loadByName(patternToMigrate.name());
    if (!currentPattern.isPresent()) {
        log.debug("Couldn't find default pattern '{}'. Skipping migration.", patternToMigrate.name());
        return;
    }
    final GrokPattern pattern = currentPattern.get();
    if (!patternToMigrate.migrateFrom().equals(pattern.pattern())) {
        log.info("Skipping migration of modified default Grok Pattern '{}'.", pattern.name());
    } else {
        log.info("Migrating default Grok Pattern '{}'.", pattern.name());
        final GrokPattern migratedPattern = pattern.toBuilder().pattern(patternToMigrate.migrateTo()).build();
        grokPatternService.update(migratedPattern);
    }
}
Also used : GrokPattern(org.graylog2.grok.GrokPattern)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)52 AuditEvent (org.graylog2.audit.jersey.AuditEvent)52 ValidationException (org.graylog2.plugin.database.ValidationException)52 Timed (com.codahale.metrics.annotation.Timed)39 Path (javax.ws.rs.Path)32 ApiResponses (io.swagger.annotations.ApiResponses)30 BadRequestException (javax.ws.rs.BadRequestException)28 PUT (javax.ws.rs.PUT)27 Consumes (javax.ws.rs.Consumes)25 POST (javax.ws.rs.POST)24 Produces (javax.ws.rs.Produces)24 User (org.graylog2.plugin.database.users.User)22 URI (java.net.URI)20 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)15 NotFoundException (org.graylog2.database.NotFoundException)15 Test (org.junit.Test)15 Stream (org.graylog2.plugin.streams.Stream)14 ObjectId (org.bson.types.ObjectId)13 GrokPattern (org.graylog2.grok.GrokPattern)13 MessageInput (org.graylog2.plugin.inputs.MessageInput)13