|
1 | 1 | package com.uid2.shared.cloud; |
2 | 2 |
|
3 | 3 | import java.io.InputStream; |
| 4 | +import java.net.HttpURLConnection; |
4 | 5 | import java.net.MalformedURLException; |
5 | 6 | import java.net.Proxy; |
6 | 7 | import java.net.URL; |
@@ -35,15 +36,30 @@ public void upload(InputStream input, String cloudPath) throws CloudStorageExcep |
35 | 36 | public InputStream download(String cloudPath) throws CloudStorageException { |
36 | 37 | try { |
37 | 38 | URL url = new URL(cloudPath); |
38 | | - |
| 39 | + HttpURLConnection httpConn; |
| 40 | + |
39 | 41 | if (this.proxy != null) { |
40 | | - return url.openConnection(proxy).getInputStream(); |
| 42 | + httpConn = (HttpURLConnection) url.openConnection(proxy); |
| 43 | + } else { |
| 44 | + httpConn = (HttpURLConnection) url.openConnection(); |
| 45 | + } |
| 46 | + |
| 47 | + int responseCode = httpConn.getResponseCode(); |
| 48 | + if (responseCode >= 200 && responseCode < 300) { |
| 49 | + return httpConn.getInputStream(); |
41 | 50 | } else { |
42 | | - return url.openStream(); |
| 51 | + throw new CloudStorageException("Cannot download required files, HTTP response code " + responseCode |
| 52 | + + ", please visit UID2 guides for more details"); |
43 | 53 | } |
44 | | - } catch (Throwable t) { |
45 | | - // Do not log the message or the original exception as that may contain the pre-signed url |
46 | | - throw new CloudStorageException("url download error: " + t.getClass().getSimpleName()); |
| 54 | + } |
| 55 | + catch (CloudStorageException e) { |
| 56 | + // Directly rethrow without wrapping again |
| 57 | + throw e; |
| 58 | + } |
| 59 | + catch (Throwable t) { |
| 60 | + // Do not log the original exception as it may contain sensitive information such as the pre-signed URL |
| 61 | + throw new CloudStorageException("Cannot download required files, exception: " + t.getClass().getSimpleName() + |
| 62 | + ", please visit UID2 guides for more details"); |
47 | 63 | } |
48 | 64 | } |
49 | 65 |
|
|
0 commit comments