public DocumentBundle retrieve()

in exercises/java/encryption-context-complete/src/main/java/sfw/example/esdkworkshop/Api.java [265:297]


  public DocumentBundle retrieve(
      String key, Set<String> expectedContextKeys, Map<String, String> expectedContext) {
    byte[] data = getObjectData(key);
    CryptoResult<byte[], KmsMasterKey> decryptedMessage = awsEncryptionSdk.decryptData(mkp, data);
    // ENCRYPTION-CONTEXT-COMPLETE: Use Encryption Context on Decrypt
    Map<String, String> actualContext = decryptedMessage.getEncryptionContext();
    PointerItem pointer = PointerItem.fromKeyAndContext(key, actualContext);
    // ENCRYPTION-CONTEXT-COMPLETE: Making Assertions
    boolean allExpectedContextKeysFound = actualContext.keySet().containsAll(expectedContextKeys);
    if (!allExpectedContextKeysFound) {
      // Remove all of the keys that were found
      expectedContextKeys.removeAll(actualContext.keySet());
      String error =
          String.format(
              "Expected context keys were not found in the actual encryption context! "
                  + "Missing keys were: %s",
              expectedContextKeys.toString());
      throw new DocumentBucketException(error, new NoSuchElementException());
    }
    boolean allExpectedContextFound =
        actualContext.entrySet().containsAll(expectedContext.entrySet());
    if (!allExpectedContextFound) {
      Set<Map.Entry<String, String>> expectedContextEntries = expectedContext.entrySet();
      expectedContextEntries.removeAll(actualContext.entrySet());
      String error =
          String.format(
              "Expected context pairs were not found in the actual encryption context! "
                  + "Missing pairs were: %s",
              expectedContextEntries.toString());
      throw new DocumentBucketException(error, new NoSuchElementException());
    }
    return DocumentBundle.fromDataAndPointer(decryptedMessage.getResult(), pointer);
  }