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));
}
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));
}
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();
}
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();
}
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);
}
Aggregations