protected IndexOutput maybeWrapOutput()

in encryption/src/main/java/org/apache/solr/encryption/EncryptionDirectory.java [128:160]


  protected IndexOutput maybeWrapOutput(IndexOutput indexOutput) throws IOException {
    String fileName = indexOutput.getName();
    if (fileName.startsWith(IndexFileNames.PENDING_SEGMENTS)) {
      // The pending_segments file should not be encrypted. Do not wrap the IndexOutput.
      // It also means a commit has started, so set the flag to read the commit user data
      // next time we need it.
      shouldReadCommitUserData = true;
      return indexOutput;
    }
    if (fileName.startsWith(IndexFileNames.SEGMENTS) || !keySupplier.shouldEncrypt(fileName)) {
      // The file should not be encrypted, based on its name. Do not wrap the IndexOutput.
      // (the segments file can be opened by the IndexFetcher)
      return indexOutput;
    }
    boolean success = false;
    try {
      String keyRef = getActiveKeyRefFromCommit(getLatestCommitData().data);
      if (keyRef != null) {
        // Get the key secret first. If it fails, we do not write anything.
        byte[] keySecret = getKeySecret(keyRef);
        // The IndexOutput has to be wrapped to be encrypted with the key.
        writeEncryptionHeader(keyRef, indexOutput);
        indexOutput = new EncryptingIndexOutput(indexOutput, keySecret, encrypterFactory);
      }
      success = true;
    } finally {
      if (!success) {
        // Something went wrong. Close the IndexOutput before the exception continues.
        IOUtils.closeWhileHandlingException(indexOutput);
      }
    }
    return indexOutput;
  }