[ 
https://issues.apache.org/jira/browse/CLOUDSTACK-10146?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16310967#comment-16310967
 ] 

ASF GitHub Bot commented on CLOUDSTACK-10146:
---------------------------------------------

marcaurele commented on a change in pull request #2379: CLOUDSTACK-10146: 
Bypass Secondary Storage for KVM templates
URL: https://github.com/apache/cloudstack/pull/2379#discussion_r159598178
 
 

 ##########
 File path: utils/src/main/java/com/cloud/utils/UriUtils.java
 ##########
 @@ -289,6 +297,93 @@ public static long getRemoteSize(String url) {
         }
     }
 
+    protected static Map<String, List<String>> 
getMultipleValuesFromXML(InputStream is, String[] tagNames) {
+        Map<String, List<String>> returnValues = new HashMap<String, 
List<String>>();
+        try {
+            DocumentBuilderFactory factory = 
DocumentBuilderFactory.newInstance();
+            DocumentBuilder docBuilder = factory.newDocumentBuilder();
+            Document doc = docBuilder.parse(is);
+            Element rootElement = doc.getDocumentElement();
+            for (int i = 0; i < tagNames.length; i++) {
+                NodeList targetNodes = 
rootElement.getElementsByTagName(tagNames[i]);
+                if (targetNodes.getLength() <= 0) {
+                    s_logger.error("no " + tagNames[i] + " tag in XML 
response...returning null");
+                } else {
+                    List<String> valueList = new ArrayList<String>();
+                    for (int j = 0; j < targetNodes.getLength(); j++) {
+                        Node node = targetNodes.item(j);
+                        valueList.add(node.getTextContent());
+                    }
+                    returnValues.put(tagNames[i], valueList);
+                }
+            }
+        } catch (Exception ex) {
+            s_logger.error(ex);
+        }
+        return returnValues;
+    }
+
+    /**
+     * Check if there is at least one existent URL defined on metalink
+     * @param url metalink url
+     * @return true if at least one existent URL defined on metalink, false if 
not
+     */
+    protected static boolean checkUrlExistenceMetalink(String url) {
+        HttpClient httpClient = new HttpClient(new 
MultiThreadedHttpConnectionManager());
+        GetMethod getMethod = new GetMethod(url);
+        try {
+            if (httpClient.executeMethod(getMethod) == HttpStatus.SC_OK) {
+                InputStream is = getMethod.getResponseBodyAsStream();
+                Map<String, List<String>> metalinkUrls = 
getMultipleValuesFromXML(is, new String[] {"url"});
+                if (metalinkUrls.containsKey("url")) {
+                    List<String> urls = metalinkUrls.get("url");
+                    boolean validUrl = false;
+                    for (String u : urls) {
+                        if (url.endsWith("torrent")) {
+                            continue;
+                        }
+                        try {
+                            UriUtils.checkUrlExistence(u);
+                            validUrl = true;
+                            break;
+                        }
+                        catch (IllegalArgumentException e) {
+                            s_logger.warn(e.getMessage());
+                        }
+                    }
+                    return validUrl;
+                }
+            }
+        } catch (IOException e) {
+            s_logger.warn(e.getMessage());
+        }
+        return false;
+    }
+
+    /**
+     * Get list of urls on metalink
+     * @param metalinkUrl
+     * @return
+     */
+    public static List<String> getMetalinkUrls(String metalinkUrl) {
+        HttpClient httpClient = new HttpClient(new 
MultiThreadedHttpConnectionManager());
+        GetMethod getMethod = new GetMethod(metalinkUrl);
+        List<String> urls = new ArrayList<>();
+        try {
+            if (httpClient.executeMethod(getMethod) == HttpStatus.SC_OK) {
+                InputStream is = getMethod.getResponseBodyAsStream();
 
 Review comment:
   same remark for the try with resource statement here. Currently the 
inputstream close is missing.

----------------------------------------------------------------
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:
[email protected]


> Bypass Secondary Storage for KVM templates
> ------------------------------------------
>
>                 Key: CLOUDSTACK-10146
>                 URL: https://issues.apache.org/jira/browse/CLOUDSTACK-10146
>             Project: CloudStack
>          Issue Type: Bug
>      Security Level: Public(Anyone can view this level - this is the 
> default.) 
>    Affects Versions: 4.11.0.0
>            Reporter: Nicolas Vazquez
>            Assignee: Nicolas Vazquez
>




--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to