Java Code Examples for com.jayway.jsonpath.spi.json.JsonProvider#length()

The following examples show how to use com.jayway.jsonpath.spi.json.JsonProvider#length() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FieldLevelEncryption.java    From client-encryption-java with MIT License 6 votes vote down vote up
private static void addDecryptedDataToPayload(DocumentContext payloadContext, String decryptedValue, String jsonPathOut) {
    JsonProvider jsonProvider = jsonPathConfig.jsonProvider();
    Object decryptedValueJsonElement = jsonEngine.parse(decryptedValue);

    if (!jsonEngine.isJsonObject(decryptedValueJsonElement)) {
        // Array or primitive: overwrite
        payloadContext.set(jsonPathOut, decryptedValueJsonElement);
        return;
    }

    // Object: merge
    int length = jsonProvider.length(decryptedValueJsonElement);
    Collection<String> propertyKeys = (0 == length) ? Collections.<String>emptyList() : jsonProvider.getPropertyKeys(decryptedValueJsonElement);
    for (String key : propertyKeys) {
        payloadContext.delete(jsonPathOut + "." + key);
        payloadContext.put(jsonPathOut, key, jsonProvider.getMapValue(decryptedValueJsonElement, key));
    }
}
 
Example 2
Source File: FieldLevelEncryption.java    From client-encryption-java with MIT License 4 votes vote down vote up
private static void decryptPayloadPath(DocumentContext payloadContext, String jsonPathIn, String jsonPathOut,
                                       FieldLevelEncryptionConfig config, FieldLevelEncryptionParams params) throws GeneralSecurityException, EncryptionException {

    JsonProvider jsonProvider = jsonPathConfig.jsonProvider();
    Object inJsonObject = readJsonObject(payloadContext, jsonPathIn);
    if (inJsonObject == null) {
        // Nothing to decrypt
        return;
    }

    // Read and remove encrypted data and encryption fields at the given JSON path
    Object encryptedValueJsonElement = readAndDeleteJsonKey(payloadContext, jsonPathIn, inJsonObject, config.encryptedValueFieldName);
    if (jsonEngine.isNullOrEmptyJson(encryptedValueJsonElement)) {
        // Nothing to decrypt
        return;
    }

    if (!config.useHttpPayloads() && params == null) {
        throw new IllegalStateException("Encryption params have to be set when not stored in HTTP payloads!");
    }

    if (params == null) {
        // Read encryption params from the payload
        Object oaepDigestAlgorithmJsonElement = readAndDeleteJsonKey(payloadContext, jsonPathIn, inJsonObject, config.oaepPaddingDigestAlgorithmFieldName);
        String oaepDigestAlgorithm = jsonEngine.isNullOrEmptyJson(oaepDigestAlgorithmJsonElement) ? config.oaepPaddingDigestAlgorithm : jsonEngine.toJsonString(oaepDigestAlgorithmJsonElement);
        Object encryptedKeyJsonElement = readAndDeleteJsonKey(payloadContext, jsonPathIn, inJsonObject, config.encryptedKeyFieldName);
        Object ivJsonElement = readAndDeleteJsonKey(payloadContext, jsonPathIn, inJsonObject, config.ivFieldName);
        readAndDeleteJsonKey(payloadContext, jsonPathIn, inJsonObject, config.encryptionCertificateFingerprintFieldName);
        readAndDeleteJsonKey(payloadContext, jsonPathIn, inJsonObject, config.encryptionKeyFingerprintFieldName);
        params = new FieldLevelEncryptionParams(jsonEngine.toJsonString(ivJsonElement), jsonEngine.toJsonString(encryptedKeyJsonElement), oaepDigestAlgorithm, config);
    }

    // Decrypt data
    byte[] encryptedValueBytes = decodeValue(jsonEngine.toJsonString(encryptedValueJsonElement), config.fieldValueEncoding);
    byte[] decryptedValueBytes = decryptBytes(params.getSecretKey(), params.getIvSpec(), encryptedValueBytes);

    // Add decrypted data at the given JSON path
    String decryptedValue = new String(decryptedValueBytes, StandardCharsets.UTF_8);
    decryptedValue = sanitizeJson(decryptedValue);
    checkOrCreateOutObject(payloadContext, jsonPathOut);
    addDecryptedDataToPayload(payloadContext, decryptedValue, jsonPathOut);

    // Remove the input if now empty
    Object inJsonElement  = readJsonElement(payloadContext, jsonPathIn);
    if (0 == jsonProvider.length(inJsonElement) && !"$".equals(jsonPathIn)) {
        payloadContext.delete(jsonPathIn);
    }
}