use of org.graylog2.contentpacks.model.entities.references.Reference in project graylog2-server by Graylog2.
the class ObjectMapperModule method configure.
@Override
protected void configure() {
// the ObjectMapperProvider requires at least an empty JacksonSubtypes set.
// if the multibinder wasn't created that reference will be null, so we force its creation here
jacksonSubTypesBinder();
install(new GRNModule());
bind(ClassLoader.class).annotatedWith(GraylogClassLoader.class).toInstance(classLoader);
bind(ObjectMapper.class).toProvider(ObjectMapperProvider.class).asEagerSingleton();
}
use of org.graylog2.contentpacks.model.entities.references.Reference in project graylog2-server by Graylog2.
the class FunctionsSnippetsTest method json.
@Test
public void json() {
final String flatJson = "{\"str\":\"foobar\",\"int\":42,\"float\":2.5,\"bool\":true,\"array\":[1,2,3]}";
final String nestedJson = "{\n" + " \"store\": {\n" + " \"book\": {\n" + " \"category\": \"reference\",\n" + " \"author\": \"Nigel Rees\",\n" + " \"title\": \"Sayings of the Century\",\n" + " \"price\": 8.95\n" + " },\n" + " \"bicycle\": {\n" + " \"color\": \"red\",\n" + " \"price\": 19.95\n" + " }\n" + " },\n" + " \"expensive\": 10\n" + "}";
final Rule rule = parser.parseRule(ruleForTest(), false);
final Message message = new Message("JSON", "test", Tools.nowUTC());
message.addField("flat_json", flatJson);
message.addField("nested_json", nestedJson);
final Message evaluatedMessage = evaluateRule(rule, message);
assertThat(evaluatedMessage.getField("message")).isEqualTo("JSON");
assertThat(evaluatedMessage.getField("flat_json")).isEqualTo(flatJson);
assertThat(evaluatedMessage.getField("nested_json")).isEqualTo(nestedJson);
assertThat(evaluatedMessage.getField("str")).isEqualTo("foobar");
assertThat(evaluatedMessage.getField("int")).isEqualTo(42);
assertThat(evaluatedMessage.getField("float")).isEqualTo(2.5);
assertThat(evaluatedMessage.getField("bool")).isEqualTo(true);
assertThat(evaluatedMessage.getField("array")).isEqualTo(Arrays.asList(1, 2, 3));
assertThat(evaluatedMessage.getField("store")).isInstanceOf(Map.class);
assertThat(evaluatedMessage.getField("expensive")).isEqualTo(10);
}
use of org.graylog2.contentpacks.model.entities.references.Reference in project graylog2-server by Graylog2.
the class IndicesAdapterES6 method move.
@Override
public void move(String source, String target, Consumer<IndexMoveResult> resultCallback) {
// TODO: This method should use the Re-index API: https://www.elastic.co/guide/en/elasticsearch/reference/5.3/docs-reindex.html
final String query = SearchSourceBuilder.searchSource().query(QueryBuilders.matchAllQuery()).size(350).sort(SortBuilders.fieldSort(FieldSortBuilder.DOC_FIELD_NAME)).toString();
final Search request = new Search.Builder(query).setParameter(Parameters.SCROLL, "10s").addIndex(source).build();
final SearchResult searchResult = JestUtils.execute(jestClient, request, () -> "Couldn't process search query response");
final String scrollId = searchResult.getJsonObject().path("_scroll_id").asText(null);
if (scrollId == null) {
throw new ElasticsearchException("Couldn't find scroll ID in search query response");
}
while (true) {
final SearchScroll scrollRequest = new SearchScroll.Builder(scrollId, "1m").build();
final JestResult scrollResult = JestUtils.execute(jestClient, scrollRequest, () -> "Couldn't process result of scroll query");
final JsonNode scrollHits = scrollResult.getJsonObject().path("hits").path("hits");
// No more hits.
if (scrollHits.size() == 0) {
break;
}
final Bulk.Builder bulkRequestBuilder = new Bulk.Builder();
for (JsonNode jsonElement : scrollHits) {
Optional.ofNullable(jsonElement.path("_source")).map(sourceJson -> objectMapper.<Map<String, Object>>convertValue(sourceJson, TypeReferences.MAP_STRING_OBJECT)).ifPresent(doc -> {
final String id = (String) doc.remove("_id");
if (!Strings.isNullOrEmpty(id)) {
bulkRequestBuilder.addAction(indexingHelper.prepareIndexRequest(target, doc, id));
}
});
}
final BulkResult bulkResult = JestUtils.execute(jestClient, bulkRequestBuilder.build(), () -> "Couldn't bulk index messages into index " + target);
final boolean hasFailedItems = !bulkResult.getFailedItems().isEmpty();
final IndexMoveResult result = IndexMoveResult.create(bulkResult.getItems().size(), bulkResult.getJsonObject().path("took").asLong(), hasFailedItems);
resultCallback.accept(result);
}
}
use of org.graylog2.contentpacks.model.entities.references.Reference in project graylog2-server by Graylog2.
the class ReferenceConverter method convert.
@Override
public Reference convert(JsonNode jsonNode) {
if (jsonNode.isObject()) {
final ImmutableSet<String> fieldNames = ImmutableSet.copyOf(jsonNode.fieldNames());
if (fieldNames.equals(EXPECTED_FIELD_NAMES)) {
// TODO: Possible to use ValueTypeDeserializer to avoid duplication?
final String valueTypeText = jsonNode.path(ValueReference.FIELD_TYPE).asText();
final ValueType valueType = ValueType.valueOf(valueTypeText.toUpperCase(Locale.ROOT));
final JsonNode value = jsonNode.path(ValueReference.FIELD_VALUE);
if (valueType == ValueType.BOOLEAN && value.isBoolean()) {
return ValueReference.of(value.booleanValue());
} else if (valueType == ValueType.DOUBLE && value.isDouble()) {
return ValueReference.of(value.doubleValue());
} else if (valueType == ValueType.FLOAT && value.isFloat()) {
return ValueReference.of(value.floatValue());
} else if (valueType == ValueType.INTEGER && value.isInt()) {
return ValueReference.of(value.intValue());
} else if (valueType == ValueType.LONG && (value.isLong() || value.isInt())) {
// Jackson actually creates an int value for a small number so we check for both (long and int value) here
return ValueReference.of(value.longValue());
} else if (valueType == ValueType.STRING && value.isTextual()) {
return ValueReference.of(value.textValue());
} else if (valueType == ValueType.PARAMETER && value.isTextual()) {
return ValueReference.createParameter(value.textValue());
} else {
return null;
}
} else {
final ImmutableMap.Builder<String, Reference> map = ImmutableMap.builder();
final Iterator<Map.Entry<String, JsonNode>> fields = jsonNode.fields();
while (fields.hasNext()) {
final Map.Entry<String, JsonNode> entry = fields.next();
map.put(entry.getKey(), convert(entry.getValue()));
}
return new ReferenceMap(map.build());
}
} else if (jsonNode.isArray()) {
final ImmutableList.Builder<Reference> list = ImmutableList.builder();
for (JsonNode value : jsonNode) {
list.add(convert(value));
}
return new ReferenceList(list.build());
}
return null;
}
use of org.graylog2.contentpacks.model.entities.references.Reference in project graylog2-server by Graylog2.
the class ReferenceConverterTest method convertFloatValue.
@Test
public void convertFloatValue() {
final Reference reference = createReference("float", 100f);
assertThat(reference).isEqualTo(ValueReference.of(100f));
}
Aggregations