1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
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 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
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 | |
} |