Search in sources :

Example 6 with JsonNode

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);
    });
}
Also used : ObjectNode(io.openk9.json.api.ObjectNode) JsonNode(io.openk9.json.api.JsonNode)

Example 7 with JsonNode

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)));
}
Also used : InputStream(java.io.InputStream) AggregatorAnnotator(io.openk9.search.query.internal.query.parser.annotator.AggregatorAnnotator) JsonNode(io.openk9.json.api.JsonNode) IOException(java.io.IOException) URL(java.net.URL) AggregatorAnnotator(io.openk9.search.query.internal.query.parser.annotator.AggregatorAnnotator) NerAnnotator(io.openk9.search.query.internal.query.parser.annotator.NerAnnotator) Annotator(io.openk9.search.api.query.parser.Annotator) NerAnnotator(io.openk9.search.query.internal.query.parser.annotator.NerAnnotator) Activate(org.osgi.service.component.annotations.Activate)

Example 8 with JsonNode

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;
}
Also used : ObjectNode(io.openk9.json.api.ObjectNode) JsonNode(io.openk9.json.api.JsonNode) ArrayNode(io.openk9.json.api.ArrayNode)

Example 9 with JsonNode

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);
}
Also used : ObjectNode(io.openk9.json.api.ObjectNode) JsonNode(io.openk9.json.api.JsonNode) Map(java.util.Map) DocumentTypeDTO(io.openk9.plugin.driver.manager.model.DocumentTypeDTO)

Example 10 with JsonNode

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();
}
Also used : Arrays(java.util.Arrays) Datasource(io.openk9.model.Datasource) Date(java.util.Date) JsonNode(io.openk9.json.api.JsonNode) Publisher(org.reactivestreams.Publisher) Map(java.util.Map) ObjectNode(io.openk9.json.api.ObjectNode) Mono(reactor.core.publisher.Mono) HttpClient(io.openk9.http.client.HttpClient) Collectors(java.util.stream.Collectors) JsonFactory(io.openk9.json.api.JsonFactory) ObjectNode(io.openk9.json.api.ObjectNode) JsonNode(io.openk9.json.api.JsonNode)

Aggregations

JsonNode (io.openk9.json.api.JsonNode)12 ObjectNode (io.openk9.json.api.ObjectNode)8 ArrayNode (io.openk9.json.api.ArrayNode)7 JsonFactory (io.openk9.json.api.JsonFactory)3 Map (java.util.Map)3 Mono (reactor.core.publisher.Mono)3 Entity (io.openk9.entity.manager.model.Entity)2 Request (io.openk9.entity.manager.model.payload.Request)2 Response (io.openk9.entity.manager.model.payload.Response)2 HttpClient (io.openk9.http.client.HttpClient)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 Activate (org.osgi.service.component.annotations.Activate)2 Constants (io.openk9.core.api.constant.Constants)1 EntityManagerClient (io.openk9.entity.manager.client.api.EntityManagerClient)1 EntityRequest (io.openk9.entity.manager.model.payload.EntityRequest)1 RelationRequest (io.openk9.entity.manager.model.payload.RelationRequest)1 ResponseList (io.openk9.entity.manager.model.payload.ResponseList)1