Search in sources :

Example 1 with HttpsClient

use of com.axway.ats.core.filetransfer.HttpsClient in project ats-framework by Axway.

the class HtmlFileDownloader method downloadFile.

/**
     * Perform the file/image download.
     *
     * @param downloadUrl
     * @return download file absolute path
     * @throws IOException
     * @throws FileTransferClientException
     */
public String downloadFile(String downloadUrl) throws IOException {
    if (StringUtils.isNullOrEmpty(downloadUrl)) {
        throw new UiElementException("The element you have specified does not link to anything!");
    }
    URL url = new URL(downloadUrl);
    String fileName = url.getPath();
    if (fileName.indexOf('/') > -1) {
        fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
    }
    HttpsClient httpsClient = new HttpsClient();
    httpsClient.connect(url.getHost());
    if (this.mimicWebDriverCookieState) {
        for (org.openqa.selenium.Cookie cookie : this.webDriver.manage().getCookies()) {
            httpsClient.addCookie(cookie.getName(), cookie.getValue(), cookie.getDomain(), cookie.getPath(), cookie.getExpiry(), cookie.isSecure());
        }
        if (this.webDriver instanceof org.openqa.selenium.phantomjs.PhantomJSDriver && System.getProperty(PhantomJsDriver.HTTP_ONLY_COOKIES_PROPERTY) != null) {
            for (org.openqa.selenium.Cookie cookie : PhantomJsDriver.getHttpOnlyCookies()) {
                httpsClient.addCookie(cookie.getName(), cookie.getValue(), cookie.getDomain(), cookie.getPath(), cookie.getExpiry(), cookie.isSecure());
            }
        }
    }
    File downloadedFile = new File(this.downloadDir + fileName);
    log.debug("Downloading file: " + downloadedFile.getName());
    org.apache.commons.io.FileUtils.copyInputStreamToFile(httpsClient.performGetRequest(url.getFile()), downloadedFile);
    String downloadedFileAbsolutePath = downloadedFile.getAbsolutePath();
    log.debug("File downloaded to '" + downloadedFileAbsolutePath + "'");
    return downloadedFileAbsolutePath;
}
Also used : HttpsClient(com.axway.ats.core.filetransfer.HttpsClient) UiElementException(com.axway.ats.uiengine.exceptions.UiElementException) File(java.io.File) URL(java.net.URL)

Aggregations

HttpsClient (com.axway.ats.core.filetransfer.HttpsClient)1 UiElementException (com.axway.ats.uiengine.exceptions.UiElementException)1 File (java.io.File)1 URL (java.net.URL)1