Search in sources :

Example 76 with DocumentContext

use of com.jayway.jsonpath.DocumentContext in project nifi by apache.

the class EvaluateJsonPath method onTrigger.

@Override
public void onTrigger(final ProcessContext processContext, final ProcessSession processSession) throws ProcessException {
    FlowFile flowFile = processSession.get();
    if (flowFile == null) {
        return;
    }
    final ComponentLog logger = getLogger();
    DocumentContext documentContext;
    try {
        documentContext = validateAndEstablishJsonContext(processSession, flowFile);
    } catch (InvalidJsonException e) {
        logger.error("FlowFile {} did not have valid JSON content.", new Object[] { flowFile });
        processSession.transfer(flowFile, REL_FAILURE);
        return;
    }
    Set<Map.Entry<String, JsonPath>> attributeJsonPathEntries = attributeToJsonPathEntrySetQueue.poll();
    if (attributeJsonPathEntries == null) {
        attributeJsonPathEntries = processContext.getProperties().entrySet().stream().filter(e -> e.getKey().isDynamic()).collect(Collectors.toMap(e -> e.getKey().getName(), e -> JsonPath.compile(e.getValue()))).entrySet();
    }
    try {
        // We'll only be using this map if destinationIsAttribute == true
        final Map<String, String> jsonPathResults = destinationIsAttribute ? new HashMap<>(attributeJsonPathEntries.size()) : Collections.EMPTY_MAP;
        for (final Map.Entry<String, JsonPath> attributeJsonPathEntry : attributeJsonPathEntries) {
            final String jsonPathAttrKey = attributeJsonPathEntry.getKey();
            final JsonPath jsonPathExp = attributeJsonPathEntry.getValue();
            Object result;
            try {
                Object potentialResult = documentContext.read(jsonPathExp);
                if (returnType.equals(RETURN_TYPE_SCALAR) && !isJsonScalar(potentialResult)) {
                    logger.error("Unable to return a scalar value for the expression {} for FlowFile {}. Evaluated value was {}. Transferring to {}.", new Object[] { jsonPathExp.getPath(), flowFile.getId(), potentialResult.toString(), REL_FAILURE.getName() });
                    processSession.transfer(flowFile, REL_FAILURE);
                    return;
                }
                result = potentialResult;
            } catch (PathNotFoundException e) {
                if (pathNotFound.equals(PATH_NOT_FOUND_WARN)) {
                    logger.warn("FlowFile {} could not find path {} for attribute key {}.", new Object[] { flowFile.getId(), jsonPathExp.getPath(), jsonPathAttrKey }, e);
                }
                if (destinationIsAttribute) {
                    jsonPathResults.put(jsonPathAttrKey, StringUtils.EMPTY);
                    continue;
                } else {
                    processSession.transfer(flowFile, REL_NO_MATCH);
                    return;
                }
            }
            final String resultRepresentation = getResultRepresentation(result, nullDefaultValue);
            if (destinationIsAttribute) {
                jsonPathResults.put(jsonPathAttrKey, resultRepresentation);
            } else {
                flowFile = processSession.write(flowFile, out -> {
                    try (OutputStream outputStream = new BufferedOutputStream(out)) {
                        outputStream.write(resultRepresentation.getBytes(StandardCharsets.UTF_8));
                    }
                });
                processSession.getProvenanceReporter().modifyContent(flowFile, "Replaced content with result of expression " + jsonPathExp.getPath());
            }
        }
        // jsonPathResults map will be empty if this is false
        if (destinationIsAttribute) {
            flowFile = processSession.putAllAttributes(flowFile, jsonPathResults);
        }
        processSession.transfer(flowFile, REL_MATCH);
    } finally {
        attributeToJsonPathEntrySetQueue.offer(attributeJsonPathEntries);
    }
}
Also used : CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) OnRemoved(org.apache.nifi.annotation.lifecycle.OnRemoved) ValidationContext(org.apache.nifi.components.ValidationContext) HashMap(java.util.HashMap) EventDriven(org.apache.nifi.annotation.behavior.EventDriven) ComponentLog(org.apache.nifi.logging.ComponentLog) StringUtils(org.apache.commons.lang3.StringUtils) SideEffectFree(org.apache.nifi.annotation.behavior.SideEffectFree) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ProcessException(org.apache.nifi.processor.exception.ProcessException) BufferedOutputStream(java.io.BufferedOutputStream) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) InvalidJsonException(com.jayway.jsonpath.InvalidJsonException) HashSet(java.util.HashSet) Relationship(org.apache.nifi.processor.Relationship) Map(java.util.Map) Requirement(org.apache.nifi.annotation.behavior.InputRequirement.Requirement) ValidationResult(org.apache.nifi.components.ValidationResult) OutputStream(java.io.OutputStream) OnUnscheduled(org.apache.nifi.annotation.lifecycle.OnUnscheduled) FlowFile(org.apache.nifi.flowfile.FlowFile) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ProcessContext(org.apache.nifi.processor.ProcessContext) Set(java.util.Set) ProcessSession(org.apache.nifi.processor.ProcessSession) JsonPath(com.jayway.jsonpath.JsonPath) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) InputRequirement(org.apache.nifi.annotation.behavior.InputRequirement) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled) DynamicProperty(org.apache.nifi.annotation.behavior.DynamicProperty) SupportsBatching(org.apache.nifi.annotation.behavior.SupportsBatching) DocumentContext(com.jayway.jsonpath.DocumentContext) Queue(java.util.Queue) Tags(org.apache.nifi.annotation.documentation.Tags) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) Collections(java.util.Collections) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) FlowFile(org.apache.nifi.flowfile.FlowFile) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) JsonPath(com.jayway.jsonpath.JsonPath) ComponentLog(org.apache.nifi.logging.ComponentLog) InvalidJsonException(com.jayway.jsonpath.InvalidJsonException) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) DocumentContext(com.jayway.jsonpath.DocumentContext) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BufferedOutputStream(java.io.BufferedOutputStream)

Example 77 with DocumentContext

use of com.jayway.jsonpath.DocumentContext in project nifi by apache.

the class JsonPathEvaluator method evaluate.

@Override
public QueryResult<String> evaluate(final Map<String, String> attributes) {
    final String subjectValue = subject.evaluate(attributes).getValue();
    if (subjectValue == null || subjectValue.length() == 0) {
        throw new AttributeExpressionLanguageException("Subject is empty");
    }
    DocumentContext documentContext = null;
    try {
        documentContext = validateAndEstablishJsonContext(subjectValue);
    } catch (InvalidJsonException e) {
        throw new AttributeExpressionLanguageException("Subject contains invalid JSON: " + subjectValue, e);
    }
    final JsonPath compiledJsonPath;
    if (precompiledJsonPathExp != null) {
        compiledJsonPath = precompiledJsonPathExp;
    } else {
        compiledJsonPath = compileJsonPathExpression(jsonPathExp.evaluate(attributes).getValue());
    }
    Object result = null;
    try {
        result = documentContext.read(compiledJsonPath);
    } catch (Exception e) {
        // assume the path did not match anything in the document
        return EMPTY_RESULT;
    }
    return new StringQueryResult(getResultRepresentation(result, EMPTY_RESULT.getValue()));
}
Also used : StringQueryResult(org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult) AttributeExpressionLanguageException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException) InvalidJsonException(com.jayway.jsonpath.InvalidJsonException) DocumentContext(com.jayway.jsonpath.DocumentContext) JsonPath(com.jayway.jsonpath.JsonPath) AttributeExpressionLanguageException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException) InvalidJsonException(com.jayway.jsonpath.InvalidJsonException)

Example 78 with DocumentContext

use of com.jayway.jsonpath.DocumentContext in project nifi by apache.

the class JsonPathRowRecordReader method convertJsonNodeToRecord.

@Override
protected Record convertJsonNodeToRecord(final JsonNode jsonNode, final RecordSchema schema, final boolean coerceTypes, final boolean dropUnknownFields) throws IOException {
    if (jsonNode == null) {
        return null;
    }
    final DocumentContext ctx = JsonPath.using(STRICT_PROVIDER_CONFIGURATION).parse(jsonNode.toString());
    final Map<String, Object> values = new HashMap<>(schema.getFieldCount());
    for (final Map.Entry<String, JsonPath> entry : jsonPaths.entrySet()) {
        final String fieldName = entry.getKey();
        final DataType desiredType = schema.getDataType(fieldName).orElse(null);
        if (desiredType == null && dropUnknownFields) {
            continue;
        }
        final JsonPath jsonPath = entry.getValue();
        Object value;
        try {
            value = ctx.read(jsonPath);
        } catch (final PathNotFoundException pnfe) {
            logger.debug("Evaluated JSONPath Expression {} but the path was not found; will use a null value", new Object[] { entry.getValue() });
            value = null;
        }
        final Optional<RecordField> field = schema.getField(fieldName);
        final Object defaultValue = field.isPresent() ? field.get().getDefaultValue() : null;
        if (coerceTypes && desiredType != null) {
            value = convert(value, desiredType, fieldName, defaultValue);
        } else {
            final DataType dataType = field.isPresent() ? field.get().getDataType() : null;
            value = convert(value, dataType);
        }
        values.put(fieldName, value);
    }
    return new MapRecord(schema, values);
}
Also used : MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JsonPath(com.jayway.jsonpath.JsonPath) DataType(org.apache.nifi.serialization.record.DataType) RecordDataType(org.apache.nifi.serialization.record.type.RecordDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) DocumentContext(com.jayway.jsonpath.DocumentContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 79 with DocumentContext

use of com.jayway.jsonpath.DocumentContext in project vft-capture by videofirst.

the class CaptureControllerTest method shouldStartMinParamsWithNoRecord.

// ===========================================
// [ /api/captures/start ] POST
// ===========================================
@Test
public void shouldStartMinParamsWithNoRecord() throws JSONException {
    ResponseEntity<String> response = startVideo(CAPTURE_START_PARAMS_MIN_NO_RECORD);
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
    String expectedJson = "{" + "    'state': 'started'," + "    'categories': {" + "        'organisation': 'Acme'," + "        'product': 'Moon Rocket'," + "        'module': 'UI'" + "    }," + "    'feature': 'Bob Feature'," + "    'scenario': 'Dave Scenario'" + "}";
    JSONAssert.assertEquals(expectedJson, response.getBody(), false);
    DocumentContext json = JsonPath.parse(response.getBody());
    Map<String, String> environmentMap = json.read("$.environment");
    assertThat(environmentMap.get("java.awt.graphicsenv")).isNotEmpty();
}
Also used : DocumentContext(com.jayway.jsonpath.DocumentContext) Test(org.junit.Test)

Example 80 with DocumentContext

use of com.jayway.jsonpath.DocumentContext in project vft-capture by videofirst.

the class CaptureControllerTest method shouldGetsStatusWithStartedUsingMinParamsAndNoRecord.

// ===========================================
// [ /api/captures/status ] GET
// ===========================================
@Test
public void shouldGetsStatusWithStartedUsingMinParamsAndNoRecord() throws JSONException {
    startVideo(CAPTURE_START_PARAMS_MIN_NO_RECORD);
    ResponseEntity<String> response = statusVideo();
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
    String expectedJson = "{" + "    'state': 'started'," + "    'categories': {" + "        'organisation': 'Acme'," + "        'product': 'Moon Rocket'," + "        'module': 'UI'" + "    }," + "    'feature': 'Bob Feature'," + "    'scenario': 'Dave Scenario'" + "}";
    JSONAssert.assertEquals(expectedJson, response.getBody(), false);
    DocumentContext json = JsonPath.parse(response.getBody());
    Map<String, String> environmentMap = json.read("$.environment");
    assertThat(environmentMap.get("java.awt.graphicsenv")).isNotEmpty();
}
Also used : DocumentContext(com.jayway.jsonpath.DocumentContext) Test(org.junit.Test)

Aggregations

DocumentContext (com.jayway.jsonpath.DocumentContext)146 Test (org.junit.Test)106 HashMap (java.util.HashMap)14 BaseTest (com.jayway.jsonpath.BaseTest)12 Map (java.util.Map)12 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)8 JsonPath (com.jayway.jsonpath.JsonPath)7 File (java.io.File)7 List (java.util.List)7 JsonPathAssert (com.revinate.assertj.json.JsonPathAssert)6 Query (org.graylog.plugins.views.search.Query)6 MessageList (org.graylog.plugins.views.search.searchtypes.MessageList)6 Time (org.graylog.plugins.views.search.searchtypes.pivot.buckets.Time)6 Values (org.graylog.plugins.views.search.searchtypes.pivot.buckets.Values)6 DateTime (org.joda.time.DateTime)6 Configuration (com.jayway.jsonpath.Configuration)5 ArrayList (java.util.ArrayList)5 Test (org.junit.jupiter.api.Test)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 InvalidJsonException (com.jayway.jsonpath.InvalidJsonException)4