-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathURLStorageWithMetadata.java
More file actions
107 lines (91 loc) · 3.98 KB
/
URLStorageWithMetadata.java
File metadata and controls
107 lines (91 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.uid2.shared.cloud;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
public abstract class URLStorageWithMetadata implements ICloudStorage {
private final Proxy proxy;
public URLStorageWithMetadata() {
this(null);
}
public URLStorageWithMetadata(Proxy proxy) {
this.proxy = proxy;
}
@Override
public void upload(String localPath, String cloudPath) throws CloudStorageException {
// this is a read-only cloud storage
throw new UnsupportedOperationException("URLStorageWithMetadata::upload method is not supported");
}
@Override
public void upload(InputStream input, String cloudPath) throws CloudStorageException {
// this is a read-only cloud storage
throw new UnsupportedOperationException("URLStorageWithMetadata::upload method is not supported");
}
@Override
public InputStream download(String cloudPath) throws CloudStorageException {
try {
URL url = new URL(cloudPath);
HttpURLConnection httpConn;
if (this.proxy != null) {
httpConn = (HttpURLConnection) url.openConnection(proxy);
} else {
httpConn = (HttpURLConnection) url.openConnection();
}
int responseCode = httpConn.getResponseCode();
if (responseCode >= 200 && responseCode < 300) {
return httpConn.getInputStream();
} else {
throw new CloudStorageException("E20: CloudStorageDownloadError, please check error code for the installed environment at"
+ " https://unifiedid.com/docs/guides/integration-options-private-operator, HTTP response code: " + responseCode);
}
}
catch (CloudStorageException e) {
// Directly rethrow without wrapping again
throw e;
}
catch (Throwable t) {
// Do not log the original exception as it may contain sensitive information such as the pre-signed URL
throw new CloudStorageException("E20: CloudStorageDownloadError, please check error code for the installed environment at"
+ " https://unifiedid.com/docs/guides/integration-options-private-operator, exception: " + t.getClass().getSimpleName());
}
}
@Override
public void delete(String cloudPath) throws CloudStorageException {
// this is a read-only cloud storage
throw new UnsupportedOperationException("URLStorageWithMetadata::upload method is not supported");
}
@Override
public void delete(Collection<String> cloudPath) throws CloudStorageException {
// this is a read-only cloud storage
throw new UnsupportedOperationException("URLStorageWithMetadata::upload method is not supported");
}
@Override
public List<String> list(String prefix) throws CloudStorageException {
// TODO improve prefix matching
return extractListFromMetadata().stream()
.filter(url -> url.contains(prefix))
.collect(Collectors.toList());
}
@Override
public URL preSignUrl(String cloudPath) throws CloudStorageException {
throw new UnsupportedOperationException("URLStorageWithMetadata::preSignUrl method is not supported");
}
@Override
public void setPreSignedUrlExpiry(long expiry) {
throw new UnsupportedOperationException("PreSignedURLStorage::setPreSignedUrlExpiry method is not supported");
}
@Override
public String mask(String cloudPath) {
try {
URL url = new URL(cloudPath);
return cloudPath.replace("?" + url.getQuery(), "");
} catch (MalformedURLException e) {
return cloudPath;
}
}
protected abstract List<String> extractListFromMetadata() throws CloudStorageException;
}