com.adobe.platform.operation.internal.api.FileDownloadApi Maven / Gradle / Ivy
/*
* Copyright 2019 Adobe
* All Rights Reserved.
*
* NOTICE: Adobe permits you to use, modify, and distribute this file in
* accordance with the terms of the Adobe license agreement accompanying
* it. If you have received this file from a source other than Adobe,
* then your use, modification, or distribution of it requires the prior
* written permission of Adobe.
*/
package com.adobe.platform.operation.internal.api;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import com.adobe.platform.operation.internal.InternalExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.platform.operation.exception.SdkException;
import com.adobe.platform.operation.internal.http.HttpResponse;
import com.adobe.platform.operation.internal.cpf.dto.response.ContentAnalyzerResponse;
public class FileDownloadApi {
private static final Logger LOGGER = LoggerFactory.getLogger(FileDownloadApi.class);
/**
* This method downloads the file from the given location and saves it at the given destination path
*/
public static void downloadAndSave(InternalExecutionContext context,
String location,
String destinationPath,
Class responseType) {
HttpResponse response = CPFApi.cpfStatusApi(context, location, responseType);
InputStream inputStream = response.getResponseAsStream();
File file = new File(destinationPath);
try {
LOGGER.info("Downloading file to {} ", destinationPath);
Files.copy(inputStream, file.toPath());
} catch (IOException e) {
LOGGER.error("Error {} while writing downloaded file to location {} ", e, destinationPath);
throw new SdkException("Exception encountered while downloading file", e);
} finally {
try {
response.consume();
} catch (IOException e) {
LOGGER.error("Error while consuming file download response ", e);
}
}
}
}