Search in sources :

Example 1 with Persisted

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

the class InputFacade method decode.

private NativeEntity<InputWithExtractors> decode(EntityV1 entity, Map<String, ValueReference> parameters, String username) {
    final InputEntity inputEntity = objectMapper.convertValue(entity.data(), InputEntity.class);
    final Map<String, ValueReference> staticFields = inputEntity.staticFields();
    final MessageInput messageInput;
    try {
        messageInput = createMessageInput(inputEntity.title().asString(parameters), inputEntity.type().asString(parameters), inputEntity.global().asBoolean(parameters), toValueMap(inputEntity.configuration(), parameters), username);
    } catch (Exception e) {
        throw new RuntimeException("Couldn't create input", e);
    }
    final Input input;
    try {
        input = inputService.find(messageInput.getPersistId());
    } catch (NotFoundException e) {
        throw new RuntimeException("Couldn't find persisted input", e);
    }
    try {
        addStaticFields(input, messageInput, staticFields, parameters);
    } catch (ValidationException e) {
        throw new RuntimeException("Couldn't add static fields to input", e);
    }
    final List<Extractor> extractors;
    try {
        extractors = createExtractors(input, inputEntity.extractors(), username, parameters);
    } catch (Exception e) {
        throw new RuntimeException("Couldn't create extractors", e);
    }
    return NativeEntity.create(entity.id(), input.getId(), TYPE_V1, input.getTitle(), InputWithExtractors.create(input, extractors));
}
Also used : Input(org.graylog2.inputs.Input) MessageInput(org.graylog2.plugin.inputs.MessageInput) ValidationException(org.graylog2.plugin.database.ValidationException) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity) MessageInput(org.graylog2.plugin.inputs.MessageInput) NotFoundException(org.graylog2.database.NotFoundException) Extractor(org.graylog2.plugin.inputs.Extractor) GrokExtractor(org.graylog2.inputs.extractors.GrokExtractor) LookupTableExtractor(org.graylog2.inputs.extractors.LookupTableExtractor) NoSuchInputTypeException(org.graylog2.shared.inputs.NoSuchInputTypeException) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) NotFoundException(org.graylog2.database.NotFoundException) ValidationException(org.graylog2.plugin.database.ValidationException) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference)

Example 2 with Persisted

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

the class SearchMetadataResource method metadataForObject.

@POST
@ApiOperation(value = "Metadata for the posted Search object", notes = "Intended for search objects that aren't yet persisted (e.g. for validation or interactive purposes)")
@NoAuditEvent("Only returning metadata for given search, not changing any data")
public SearchMetadata metadataForObject(@ApiParam @NotNull(message = "Search body is mandatory") SearchDTO searchDTO) {
    if (searchDTO == null) {
        throw new IllegalArgumentException("Search must not be null.");
    }
    final Search search = searchDTO.toSearch();
    final Map<String, QueryMetadata> queryMetadatas = StreamEx.of(search.queries()).toMap(Query::id, query -> queryEngine.parse(search, query));
    return SearchMetadata.create(queryMetadatas, Maps.uniqueIndex(search.parameters(), Parameter::name));
}
Also used : QueryMetadata(org.graylog.plugins.views.search.QueryMetadata) Query(org.graylog.plugins.views.search.Query) Search(org.graylog.plugins.views.search.Search) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Example 3 with Persisted

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

the class InputServiceImpl method getExtractors.

@Override
@SuppressWarnings("unchecked")
public List<Extractor> getExtractors(Input input) {
    if (input.getFields().get(InputImpl.EMBEDDED_EXTRACTORS) == null) {
        return Collections.emptyList();
    }
    final ImmutableList.Builder<Extractor> listBuilder = ImmutableList.builder();
    final BasicDBList mEx = (BasicDBList) input.getFields().get(InputImpl.EMBEDDED_EXTRACTORS);
    for (final Object element : mEx) {
        final DBObject ex = (BasicDBObject) element;
        // SOFT MIGRATION: does this extractor have an order set? Implemented for issue: #726
        Long order = 0L;
        if (ex.containsField(Extractor.FIELD_ORDER)) {
            /* We use json format to describe our test fixtures
                   This format will only return Integer on this place,
                   which can't be converted to long. So I first cast
                   it to Number and eventually to long */
            Number num = (Number) ex.get(Extractor.FIELD_ORDER);
            // mongodb driver gives us a java.lang.Long
            order = num.longValue();
        }
        try {
            final Extractor extractor = extractorFactory.factory((String) ex.get(Extractor.FIELD_ID), (String) ex.get(Extractor.FIELD_TITLE), order.intValue(), Extractor.CursorStrategy.valueOf(((String) ex.get(Extractor.FIELD_CURSOR_STRATEGY)).toUpperCase(Locale.ENGLISH)), Extractor.Type.valueOf(((String) ex.get(Extractor.FIELD_TYPE)).toUpperCase(Locale.ENGLISH)), (String) ex.get(Extractor.FIELD_SOURCE_FIELD), (String) ex.get(Extractor.FIELD_TARGET_FIELD), (Map<String, Object>) ex.get(Extractor.FIELD_EXTRACTOR_CONFIG), (String) ex.get(Extractor.FIELD_CREATOR_USER_ID), getConvertersOfExtractor(ex), Extractor.ConditionType.valueOf(((String) ex.get(Extractor.FIELD_CONDITION_TYPE)).toUpperCase(Locale.ENGLISH)), (String) ex.get(Extractor.FIELD_CONDITION_VALUE));
            listBuilder.add(extractor);
        } catch (Exception e) {
            LOG.error("Cannot build extractor 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) Extractor(org.graylog2.plugin.inputs.Extractor) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) NoSuchInputTypeException(org.graylog2.shared.inputs.NoSuchInputTypeException) NotFoundException(org.graylog2.database.NotFoundException) ValidationException(org.graylog2.plugin.database.ValidationException)

Example 4 with Persisted

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

the class PersistedInputsImpl method iterator.

@Override
public Iterator<MessageInput> iterator() {
    List<MessageInput> result = Lists.newArrayList();
    for (Input io : inputService.allOfThisNode(serverStatus.getNodeId().toString())) {
        try {
            final MessageInput input = inputService.getMessageInput(io);
            result.add(input);
        } catch (NoSuchInputTypeException e) {
            LOG.warn("Cannot instantiate persisted input. No such type [{}].", io.getType());
        } catch (Throwable e) {
            LOG.warn("Cannot instantiate persisted input. Exception caught: ", e);
        }
    }
    return result.iterator();
}
Also used : MessageInput(org.graylog2.plugin.inputs.MessageInput) MessageInput(org.graylog2.plugin.inputs.MessageInput) NoSuchInputTypeException(org.graylog2.shared.inputs.NoSuchInputTypeException)

Example 5 with Persisted

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

the class PersistedImplTest method testEqualityForSameRecord.

@Test
public void testEqualityForSameRecord() throws Exception {
    Map<String, Object> fields = Maps.newHashMap();
    fields.put("foo", "bar");
    fields.put("bar", 42);
    ObjectId id = new ObjectId();
    Persisted persisted1 = new PersistedImplSUT(id, fields);
    Persisted persisted2 = new PersistedImplSUT(id, fields);
    assertEquals(persisted1, persisted2);
}
Also used : ObjectId(org.bson.types.ObjectId) Persisted(org.graylog2.plugin.database.Persisted) Test(org.junit.Test)

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