Download

Downloads an application snapshot from the Oracle Planning and Budgeting Cloud repository to the local environment.

If the content type of the response is application/JSON, then an error with details is displayed on the server. If the content type of the response is application/octet-stream, then the content to be downloaded is part of the response and can read from the response body.

REST Resource

GET /interop/rest/{api_version}/applicationsnapshots/{applicationSnapshotName}/contents

Request

Supported Media Types: application/json

Parameters:

The following table summarizes the GET request parameters.

Table 6-9 Parameters

Name Description Type Required Default
applicationSnapshotName Application snapshot name that must be downloaded Path Yes None
api_version Specific API version Path Yes None

Response

Parameters

Name Description
fileExtension Specifies a file extension that can be used to create a file locally; can contain values such as zip and csv.

Parameters:

Table 6-10 Parameters

Name Description
Details In case of errors, details are published with the error string
Status See Migration Status Codes
Links Detailed information about the link
Href Links to API call
Action The HTTP call type
Rel Possibly value: self
Data Parameters as key value pairs passed in the request

Example of Response Body

The following shows an example of the response body in JSON format in case there is an error during download

{
    "details":"Not a valid file.",
    "status":8,
    "links":[{
                "href":"https://<SERVICE_NAME>-<TENANT_NAME>.<dcX>.oraclecloud.com/interop/rest/11.1.2.3.600/applicationsnapshots/s112.csv/contents",
                "action":"GET",
                "rel":"self",
                "data":null
    }]
}

Download Sample Code

Example 6-10 Java Sample – downloadFile.java

Prerequisites: java-JSON.jar

Common Functions: See Common Helper Functions for Java.

//
// BEGIN - Download a file from PBCS
//
public void downloadFile(String fileName) throws Exception {    
        HttpURLConnection connection = null;
        InputStream inputStream = null;
        FileOutputStream outputStream = null;

        try {
                URL url = new URL(String.format("%s/interop/rest/%s/applicationsnapshots/%s/contents", serverUrl, apiVersion, fileName));
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setInstanceFollowRedirects(false);
                connection.setDoOutput(true);
                connection.setUseCaches(false);
                connection.setDoInput(true);
                connection.setRequestProperty("Authorization", "Basic " + new sun.misc.BASE64Encoder().encode((userName + ":" + password).getBytes()));
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                int status = connection.getResponseCode();
                if (status == 200) {
                        if (connection.getContentType() != null && connection.getContentType().equals("application/json")) {
                                JSONObject json = new JSONObject(getStringFromInputStream(connection.getInputStream()));
                                System.out.println("Error downloading file : " + json.getString("details"));
                        } else {
                                inputStream = connection.getInputStream();
                                outputStream = new FileOutputStream(new File(fileName));
                                int bytesRead = -1;
                                byte[] buffer = new byte[5 * 1024 * 1024];
                                while ((bytesRead = inputStream.read(buffer)) != -1) 
                                        outputStream.write(buffer, 0, bytesRead);
                                System.out.println("File download completed.");
                        }
                } else {
                        throw new Exception("Http status code: " + status);
                }
        } finally {
                if (connection != null)
                        connection.disconnect();
                if (outputStream != null)
                        outputStream.close();
                if (inputStream != null)
                        inputStream.close();
        }
}       
//
// END - Download a file from PBCS
//

Example 6-11 cURL Sample – DownloadFile.sh

Prerequisites: jq (http://stedolan.github.io/jq/download/linux64/jq)

Common Functions: See Common Helper Functions for cURL

funcDownloadFile() {
        filepath="/u01/$1"
        encodedFileName=$(echo $1 | sed -f urlencode.sed)
        url=$SERVER_URL/interop/rest/$API_VERSION/applicationsnapshots/$encodedFileName/contents
        statusCode=`curl -X GET -s -w "%{http_code}" -u "$USERNAME:$PASSWORD" -o $filepath -H "Content-Type: application/x-www-form-urlencoded" -D respHeader.txt $url`

        if [ $statusCode == 200 ]; then
                contentType=`echo $(grep 'Content-Type:' respHeader.txt) | tr -d [:space:]`
                if [ ! -z $contentType ] && [[ $contentType = *"application/json"* ]]; then
                        output=`cat $filepath`
                        error=`echo $output | jq '.details'`
                        echo "Error occurred. " $error
                        funcRemoveTempFiles $filepath
                else
                        fileExtension=`echo $(grep -r "fileExtension: " respHeader.txt | awk  '{print ($2)}') | tr -d [:space:]`
                        if [ ! -z $fileExtension ]; then
                                if [[ ! $filepath =~ \.$fileExtension$ ]]; then
                                        mv $filepath $filepath.$fileExtension
                                fi
                        fi
                        echo "Downloaded file successfully"
                fi
        else
                echo "Error listing files. "
                if [ $statusCode != 000 ]; then
                        echo "Response error code : " $statusCode
                        funcPrintErrorDetails $filepath
                        funcRemoveTempFiles $filepath
                fi
        fi
        funcRemoveTempFiles "respHeader.txt"
}

Example 6-12 Groovy Sample – DownloadFile.groovy

def downloadFile(filename) {
        def url;
        try {
                        String encodedFileName = URLEncoder.encode(filename, "UTF-8");
                        url = new URL(serverUrl + "/interop/rest/" + apiVersion + "/applicationsnapshots/" + encodedFileName + "/contents");
        } catch (MalformedURLException e) {
                        println "Malformed URL. Please pass valid URL"
                        System.exit(0);
        }
        
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Authorization", basicAuth);

        int statusCode
        try {
                        statusCode = connection.responseCode;
        } catch (all) {
                        println "Error connecting to the URL"
                        System.exit(0);
        }

        if (statusCode == 200) {
                        InputStream is;
                        if (connection.getContentType() != null && connection.getContentType().contains("application/json")) {
                                        is = connection.getInputStream();
                                        if (is != null) {
                                                response = fetchResponse(is)
                                                def object = new JsonSlurper().parseText(response)
                                                println "Error occurred while downloading file"
                                                if (object.details != null)
                                                        println "Error details: " + object.details
                                        }
                        } else {
                                        final int BUFFER_SIZE = 5 * 1024 * 1024;
                                        fileExt = connection.getHeaderField("fileExtension");
                                        saveFilePath = "/u01/" + filename + "." + fileExt;
                                        File f = new File(saveFilePath);
                                        is = connection.getInputStream();
                                        FileOutputStream outputStream = new FileOutputStream(f);
                                        int bytesRead = -1;
                                        byte[] buffer = new byte[BUFFER_SIZE];
                                        while ((bytesRead = is.read(buffer)) != -1) {
                                                        outputStream.write(buffer, 0, bytesRead);
                                        }
                                        println "Downloaded " + filename + " successfully";
                        }
        } else {
                        println "Error occurred while executing request"
                        println "Response error code : " + statusCode
                        InputStream is = connection.getErrorStream();
                        if (is != null && connection.getContentType() != null && connection.getContentType().startsWith("application/json"))
                                println fetchJobStatusFromResponse(fetchResponse(is))
                        System.exit(0);
        }
        connection.disconnect();
}

Prerequisites: java-JSON.jar

Common Functions: See Common Helper Functions for Groovy