You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by GitBox <gi...@apache.org> on 2018/02/10 17:29:03 UTC

[GitHub] rhtyd closed pull request #2424: CLOUDSTACK-10251: HTTPS downloader for Direct Download templates failure

rhtyd closed pull request #2424: CLOUDSTACK-10251: HTTPS downloader for Direct Download templates failure
URL: https://github.com/apache/cloudstack/pull/2424
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/agent/src/com/cloud/agent/direct/download/HttpsDirectTemplateDownloader.java b/agent/src/com/cloud/agent/direct/download/HttpsDirectTemplateDownloader.java
index 436303f71cf..664181fbda3 100644
--- a/agent/src/com/cloud/agent/direct/download/HttpsDirectTemplateDownloader.java
+++ b/agent/src/com/cloud/agent/direct/download/HttpsDirectTemplateDownloader.java
@@ -21,6 +21,9 @@
 
 import com.cloud.utils.exception.CloudRuntimeException;
 import com.cloud.utils.script.Script;
+import org.apache.commons.io.IOUtils;
+import org.apache.http.HttpEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
@@ -33,6 +36,9 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.FileOutputStream;
 import java.security.KeyManagementException;
 import java.security.KeyStore;
 import java.security.KeyStoreException;
@@ -79,11 +85,32 @@ private SSLContext getSSLContext() throws KeyStoreException, NoSuchAlgorithmExce
 
     @Override
     public boolean downloadTemplate() {
+        CloseableHttpResponse response;
         try {
-            httpsClient.execute(req);
+            response = httpsClient.execute(req);
         } catch (IOException e) {
             throw new CloudRuntimeException("Error on HTTPS request: " + e.getMessage());
         }
-        return performDownload();
+        return consumeResponse(response);
     }
+
+    /**
+     * Consume response and persist it on getDownloadedFilePath() file
+     */
+    protected boolean consumeResponse(CloseableHttpResponse response) {
+        if (response.getStatusLine().getStatusCode() != 200) {
+            throw new CloudRuntimeException("Error on HTTPS response");
+        }
+        try {
+            HttpEntity entity = response.getEntity();
+            InputStream in = entity.getContent();
+            OutputStream out = new FileOutputStream(getDownloadedFilePath());
+            IOUtils.copy(in, out);
+        } catch (Exception e) {
+            s_logger.error("Error parsing response for template " + getTemplateId() + " due to: " + e.getMessage());
+            return false;
+        }
+        return true;
+    }
+
 }
\ No newline at end of file


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services