protected final Future downloadFile()

in priam/src/main/java/com/netflix/priam/restore/EncryptedRestoreBase.java [94:202]


    protected final Future<Path> downloadFile(final AbstractBackupPath path) throws Exception {
        final char[] passPhrase =
                new String(this.pgpCredential.getValue(ICredentialGeneric.KEY.PGP_PASSWORD))
                        .toCharArray();
        File restoreLocation = path.newRestoreFile();
        File tempFile = new File(restoreLocation.getAbsolutePath() + TMP_SUFFIX);

        return executor.submit(
                new RetryableCallable<Path>() {

                    @Override
                    public Path retriableCall() throws Exception {

                        // == download object from source bucket
                        try {
                            // Not retrying to download file here as it is already in RetryCallable.
                            fs.downloadFile(path, TMP_SUFFIX, 0 /* retries */);
                        } catch (Exception ex) {
                            // This behavior is retryable; therefore, lets get to a clean state
                            // before each retry.
                            if (tempFile.exists()) {
                                tempFile.createNewFile();
                            }

                            throw new Exception(
                                    "Exception downloading file from: "
                                            + path.getRemotePath()
                                            + " to: "
                                            + tempFile.getAbsolutePath(),
                                    ex);
                        }

                        // == object downloaded successfully from source, decrypt it.
                        File decryptedFile = new File(tempFile.getAbsolutePath() + ".decrypted");
                        try (OutputStream fOut =
                                        new BufferedOutputStream(
                                                new FileOutputStream(
                                                        decryptedFile)); // destination file after
                                // decryption)
                                InputStream in =
                                        new BufferedInputStream(
                                                new FileInputStream(tempFile.getAbsolutePath()))) {
                            InputStream encryptedDataInputStream =
                                    fileCryptography.decryptStream(
                                            in, passPhrase, tempFile.getAbsolutePath());
                            Streams.pipeAll(encryptedDataInputStream, fOut);
                            logger.info(
                                    "Completed decrypting file: {} to final file dest: {}",
                                    tempFile.getAbsolutePath(),
                                    decryptedFile.getAbsolutePath());

                        } catch (Exception ex) {
                            // This behavior is retryable; therefore, lets get to a clean state
                            // before each retry.
                            if (tempFile.exists()) {
                                tempFile.createNewFile();
                            }

                            if (decryptedFile.exists()) {
                                decryptedFile.createNewFile();
                            }

                            throw new Exception(
                                    "Exception during decryption file:  "
                                            + decryptedFile.getAbsolutePath(),
                                    ex);
                        }

                        // == object is downloaded and decrypted, now uncompress it if necessary
                        if (path.getCompression() == CompressionType.NONE) {
                            Files.move(decryptedFile.toPath(), restoreLocation.toPath());
                        } else {
                            logger.info(
                                    "Start uncompressing file: {} to the FINAL destination stream",
                                    decryptedFile.getAbsolutePath());

                            try (InputStream is =
                                            new BufferedInputStream(
                                                    new FileInputStream(decryptedFile));
                                    BufferedOutputStream finalDestination =
                                            new BufferedOutputStream(
                                                    new FileOutputStream(restoreLocation))) {
                                compress.decompressAndClose(is, finalDestination);
                            } catch (Exception ex) {
                                throw new Exception(
                                        "Exception uncompressing file: "
                                                + decryptedFile.getAbsolutePath()
                                                + " to the FINAL destination stream",
                                        ex);
                            }

                            logger.info(
                                    "Completed uncompressing file: {} to the FINAL destination stream "
                                            + " current worker: {}",
                                    decryptedFile.getAbsolutePath(),
                                    Thread.currentThread().getName());
                        }
                        // if here, everything was successful for this object, lets remove unneeded
                        // file(s)
                        if (tempFile.exists()) tempFile.delete();

                        if (decryptedFile.exists()) {
                            decryptedFile.delete();
                        }

                        return Paths.get(path.getRemotePath());
                    }
                });
    }