Coverage Report - org.mule.example.launcher.SimpleDownloadManager
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleDownloadManager
0%
0/24
0%
0/8
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.example.launcher;
 8  
 
 9  
 import java.net.URL;
 10  
 import java.net.URLConnection;
 11  
 import java.util.HashMap;
 12  
 import java.util.Map;
 13  
 
 14  
 public class SimpleDownloadManager
 15  
 {
 16  
     private final static String URL_KEY = "url";
 17  
 
 18  
     /**
 19  
          * 
 20  
          */
 21  
     public SimpleDownloadManager()
 22  0
     {
 23  0
     }
 24  
 
 25  
     /**
 26  
      * Performs a HTTP GET or HTTP POST request and returns the contents.
 27  
      * 
 28  
      * @param data <code>Map</code> instance containing the URL (should start with
 29  
      *            http://) and the method (GET or POST)
 30  
      * @return The contents
 31  
      */
 32  
     public Object download(Object data)
 33  
     {
 34  0
         String fileUrl = null;
 35  0
         Map<String, Object> response = new HashMap<String, Object>();
 36  
 
 37  0
         if (data instanceof Map<?, ?>)
 38  
         {
 39  0
             Map<?, ?> params = (Map<?, ?>) data;
 40  
 
 41  0
             fileUrl = String.valueOf(params.get(URL_KEY));
 42  0
         }
 43  0
         else if (data instanceof String)
 44  
         {
 45  0
             fileUrl = (String) data;
 46  
         }
 47  
 
 48  0
         if (fileUrl != null)
 49  
         {
 50  
             try
 51  
             {
 52  0
                 URL url = new URL(fileUrl);
 53  0
                 URLConnection conn = url.openConnection();
 54  0
                 response.put("is", conn.getInputStream());
 55  0
                 response.put("filename", getFilenameFromUrl(fileUrl));
 56  
             }
 57  0
             catch (Exception ex)
 58  
             {
 59  0
                 response.put("error", "Error downloading " + fileUrl + " -> " + ex.getMessage());
 60  0
             }
 61  
         }
 62  
         else
 63  
         {
 64  0
             response.put("error", "Not able to get URL to download");
 65  
         }
 66  0
         return response;
 67  
     }
 68  
 
 69  
     private String getFilenameFromUrl(String fileUrl)
 70  
     {
 71  0
         if (fileUrl != null)
 72  
         {
 73  0
             int i = fileUrl.lastIndexOf("/");
 74  0
             return fileUrl.substring(i + 1);
 75  
         }
 76  0
         return null;
 77  
     }
 78  
 }