use of io.openk9.json.api.JsonNode in project openk9 by smclab.
the class JsEnrichProcessor method process.
@Override
public Mono<ObjectNode> process(ObjectNode objectNode, DatasourceContext context, EnrichItem enrichItem, PluginDriverDTO pluginDriverName) {
return Mono.defer(() -> {
JsonNode datasourceConfiguration = _jsonFactory.fromJsonToJsonNode(enrichItem.getJsonConfig());
if (!datasourceConfiguration.isObject()) {
return Mono.error(new RuntimeException("jsonConfig must be an instance of ObjectNode " + datasourceConfiguration.toString()));
}
ObjectNode request = prepareRequestRawContent(objectNode, datasourceConfiguration.toObjectNode(), context, pluginDriverName);
return Mono.from(_httpClient.request(getMethod(), getPath(), request.toString(), getHeaders())).map(_jsonFactory::fromJsonToJsonNode).map(JsonNode::toObjectNode).map(objectNode::merge);
});
}
use of io.openk9.json.api.JsonNode in project openk9 by smclab.
the class GrammarProvider method activate.
@Activate
void activate(AnnotatorConfig config, BundleContext bundleContext) throws IOException {
_config = config;
String ruleJsonPath = _config.ruleJsonPath();
List<Rule> rules = List.of();
if (!ruleJsonPath.isBlank() && Files.exists(Paths.get(ruleJsonPath))) {
byte[] bytes = Files.readAllBytes(Paths.get(ruleJsonPath));
JsonNode jsonNode = _jsonFactory.fromJsonToJsonNode(bytes);
rules = _toJavaRules(jsonNode);
} else {
URL resource = bundleContext.getBundle().getResource("rule.json");
try (InputStream is = resource.openStream()) {
byte[] bytes = is.readAllBytes();
JsonNode jsonNode = _jsonFactory.fromJsonToJsonNode(bytes);
rules = _toJavaRules(jsonNode);
} catch (IOException e) {
e.printStackTrace();
}
}
String[] nerAnnotator = _config.nerAnnotator();
Stream<Annotator> nerAnnotatorStream = Arrays.stream(nerAnnotator).map(keyword -> new NerAnnotator(keyword, _config, _restHighLevelClientProvider));
String[] aggregatorAnnotator = _config.aggregatorAnnotator();
Stream<Annotator> aggregatorAnnotatorStream = Arrays.stream(aggregatorAnnotator).map(keyword -> new AggregatorAnnotator(keyword, _config, _restHighLevelClientProvider));
List<Annotator> newAnnotators = Stream.of(_annotatorList.stream(), nerAnnotatorStream, aggregatorAnnotatorStream).flatMap(Function.identity()).collect(Collectors.toList());
_grammar = new Grammar(List.of(GrammarMixin.of(rules, newAnnotators)));
}
use of io.openk9.json.api.JsonNode in project openk9 by smclab.
the class JsEnrichProcessor method prepareRequestRawContent.
protected ObjectNode prepareRequestRawContent(ObjectNode objectNode, ObjectNode datasourceConfiguration, DatasourceContext context, PluginDriverDTO pluginDriverDTO) {
JsonNode rawContentNode = objectNode.get(Constants.RAW_CONTENT);
JsonNode codeNode = datasourceConfiguration.get(Constants.CODE);
ObjectNode request = _jsonFactory.createObjectNode();
request.put(Constants.CODE, codeNode);
request.put(Constants.CONTENT, rawContentNode);
JsonNode typeNode = objectNode.get(Constants.TYPE);
ObjectNode datasourcePayload = _jsonFactory.createObjectNode();
if (typeNode != null && typeNode.isArray()) {
ArrayNode types = typeNode.toArrayNode();
for (JsonNode typeJsonNode : types) {
String type = typeJsonNode.asText();
datasourcePayload.put(type, objectNode.get(type));
}
}
request.put(Constants.DATASOURCE_PAYLOAD, datasourcePayload);
request.put(Constants.TENANT_ID, context.getTenant().getTenantId());
request.put(Constants.DATASOURCE_ID, context.getDatasource().getDatasourceId());
request.put(Constants.CONTENT_ID, objectNode.get(Constants.CONTENT_ID));
return request;
}
use of io.openk9.json.api.JsonNode in project openk9 by smclab.
the class EnrichPipelineProcessor method _adaptIngestionPayload.
private Tuple3<DatasourceContext, ObjectNode, PluginDriverDTO> _adaptIngestionPayload(DatasourceContext datasourceContext, ObjectNode ingestionPayload, PluginDriverDTO pluginDriverDTO) {
ObjectNode newIngestionPayload = ingestionPayload;
if (newIngestionPayload.hasNonNull(io.openk9.core.api.constant.Constants.DATASOURCE_PAYLOAD)) {
JsonNode datasourcePayload = newIngestionPayload.remove(io.openk9.core.api.constant.Constants.DATASOURCE_PAYLOAD);
if (datasourcePayload.isObject()) {
ObjectNode jsonNodes = datasourcePayload.toObjectNode();
for (Map.Entry<String, JsonNode> field : jsonNodes.fields()) {
newIngestionPayload.set(field.getKey(), field.getValue());
}
}
}
if (!ingestionPayload.hasNonNull(Constants.DATASOURCE_NAME)) {
newIngestionPayload = newIngestionPayload.deepCopy().put(Constants.DATASOURCE_NAME, pluginDriverDTO.getName());
}
if (ingestionPayload.hasNonNull(io.openk9.core.api.constant.Constants.TYPE) && !ingestionPayload.get(io.openk9.core.api.constant.Constants.TYPE).toArrayNode().isEmpty()) {
return Tuples.of(datasourceContext, ingestionPayload, pluginDriverDTO);
}
DocumentTypeDTO documentType = pluginDriverDTO.getDefaultDocumentType();
return Tuples.of(datasourceContext, newIngestionPayload.set(io.openk9.core.api.constant.Constants.TYPE, _jsonFactory.createArrayNode().add(documentType.getName())), pluginDriverDTO);
}
use of io.openk9.json.api.JsonNode in project openk9 by smclab.
the class BasePluginDriver method invokeDataParser.
@Override
public Mono<Void> invokeDataParser(Datasource datasource, Date fromDate, Date toDate) {
String jsonConfig = datasource.getJsonConfig();
JsonNode jsonNode = getJsonFactory().fromJsonToJsonNode(jsonConfig);
ObjectNode objectNode = jsonNode.toObjectNode();
ObjectNode requestJson = getJsonFactory().createObjectNode();
objectNode.stream().filter(e -> _containsKey(e.getKey())).forEach(e -> requestJson.set(e.getKey(), e.getValue()));
requestJson.put("timestamp", fromDate.getTime());
requestJson.put("datasourceId", datasource.getDatasourceId());
Map<String, Object> headers = headersObject();
Publisher<byte[]> request = getHttpClient().request(method(), path(), requestJson.toString(), headers);
return Mono.from(request).then();
}
Aggregations