Search in sources :

Example 6 with Persisted

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

the class PersistedImplTest method testConstructorWithFieldsOnly.

@Test
public void testConstructorWithFieldsOnly() throws Exception {
    Map<String, Object> fields = Maps.newHashMap();
    Persisted persisted = new PersistedImplSUT(fields);
    assertNotNull(persisted);
    assertNotNull(persisted.getId());
    assertFalse(persisted.getId().isEmpty());
}
Also used : Persisted(org.graylog2.plugin.database.Persisted) Test(org.junit.Test)

Example 7 with Persisted

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

the class PersistedImplTest method testConstructorWithFieldsAndId.

@Test
public void testConstructorWithFieldsAndId() throws Exception {
    Map<String, Object> fields = Maps.newHashMap();
    ObjectId id = new ObjectId();
    Persisted persisted = new PersistedImplSUT(id, fields);
    assertNotNull(persisted);
    assertNotNull(persisted.getId());
    assertFalse(persisted.getId().isEmpty());
    assertEquals(id.toString(), persisted.getId());
}
Also used : ObjectId(org.bson.types.ObjectId) Persisted(org.graylog2.plugin.database.Persisted) Test(org.junit.Test)

Example 8 with Persisted

use of org.graylog2.plugin.database.Persisted 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 9 with Persisted

use of org.graylog2.plugin.database.Persisted 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 10 with Persisted

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

the class InputServiceImpl method getStaticFields.

@Override
public List<Map.Entry<String, String>> getStaticFields(Input input) {
    if (input.getFields().get(InputImpl.EMBEDDED_STATIC_FIELDS) == null) {
        return Collections.emptyList();
    }
    final ImmutableList.Builder<Map.Entry<String, String>> listBuilder = ImmutableList.builder();
    final BasicDBList mSF = (BasicDBList) input.getFields().get(InputImpl.EMBEDDED_STATIC_FIELDS);
    for (final Object element : mSF) {
        final DBObject ex = (BasicDBObject) element;
        try {
            final Map.Entry<String, String> staticField = Maps.immutableEntry((String) ex.get(InputImpl.FIELD_STATIC_FIELD_KEY), (String) ex.get(InputImpl.FIELD_STATIC_FIELD_VALUE));
            listBuilder.add(staticField);
        } catch (Exception e) {
            LOG.error("Cannot build static field from persisted data. Skipping.", e);
        }
    }
    return listBuilder.build();
}
Also used : BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) ImmutableList(com.google.common.collect.ImmutableList) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) NoSuchInputTypeException(org.graylog2.shared.inputs.NoSuchInputTypeException) NotFoundException(org.graylog2.database.NotFoundException) ValidationException(org.graylog2.plugin.database.ValidationException)

Aggregations

ValidationException (org.graylog2.plugin.database.ValidationException)6 BasicDBObject (com.mongodb.BasicDBObject)5 NoSuchInputTypeException (org.graylog2.shared.inputs.NoSuchInputTypeException)5 DBObject (com.mongodb.DBObject)4 ObjectId (org.bson.types.ObjectId)4 NotFoundException (org.graylog2.database.NotFoundException)4 ImmutableList (com.google.common.collect.ImmutableList)3 BasicDBList (com.mongodb.BasicDBList)3 Persisted (org.graylog2.plugin.database.Persisted)3 Test (org.junit.Test)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Extractor (org.graylog2.plugin.inputs.Extractor)2 MessageInput (org.graylog2.plugin.inputs.MessageInput)2 ApiOperation (io.swagger.annotations.ApiOperation)1 POST (javax.ws.rs.POST)1 Query (org.graylog.plugins.views.search.Query)1