1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.util; |
22 |
|
|
23 |
|
import java.io.IOException; |
24 |
|
import java.io.InputStream; |
25 |
|
import java.net.HttpURLConnection; |
26 |
|
import java.net.MalformedURLException; |
27 |
|
import java.net.ProtocolException; |
28 |
|
import java.net.URI; |
29 |
|
import java.net.URISyntaxException; |
30 |
|
import java.net.URL; |
31 |
|
import java.net.URLConnection; |
32 |
|
|
33 |
|
import jalview.bin.Console; |
34 |
|
|
|
|
| 51.5% |
Uncovered Elements: 83 (171) |
Complexity: 52 |
Complexity Density: 0.46 |
|
35 |
|
public class HttpUtils |
36 |
|
{ |
37 |
|
public final static String JALVIEWSCHEMEPREFIX = "jalview"; |
38 |
|
|
|
|
| 56.2% |
Uncovered Elements: 7 (16) |
Complexity: 6 |
Complexity Density: 0.6 |
|
39 |
176 |
public static boolean isPlausibleUri(String s)... |
40 |
|
{ |
41 |
176 |
if (s == null) |
42 |
|
{ |
43 |
0 |
return false; |
44 |
|
} |
45 |
176 |
if (startsWithHttpOrHttps(s) || isJalviewSchemeUri(s)) |
46 |
|
{ |
47 |
0 |
return true; |
48 |
|
} |
49 |
176 |
try |
50 |
|
{ |
51 |
176 |
URI u = new URI(s); |
52 |
|
|
53 |
176 |
if (s.startsWith("file:/")) |
54 |
|
{ |
55 |
0 |
return true; |
56 |
|
} |
57 |
|
} catch (URISyntaxException e) |
58 |
|
{ |
59 |
0 |
return false; |
60 |
|
} |
61 |
176 |
return false; |
62 |
|
} |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
@param |
69 |
|
@return |
70 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 5 |
Complexity Density: 0.5 |
|
71 |
0 |
public static boolean isValidUrl(String url)... |
72 |
|
{ |
73 |
0 |
InputStream is = null; |
74 |
0 |
try |
75 |
|
{ |
76 |
0 |
is = HttpUtils.openStream(new URL(url)); |
77 |
0 |
if (is != null) |
78 |
|
{ |
79 |
0 |
return true; |
80 |
|
} |
81 |
|
} catch (IOException x) |
82 |
|
{ |
83 |
|
|
84 |
0 |
return false; |
85 |
|
} finally |
86 |
|
{ |
87 |
0 |
if (is != null) |
88 |
|
{ |
89 |
0 |
try |
90 |
|
{ |
91 |
0 |
is.close(); |
92 |
|
} catch (IOException e) |
93 |
|
{ |
94 |
|
|
95 |
|
} |
96 |
|
} |
97 |
|
} |
98 |
0 |
return false; |
99 |
|
} |
100 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
101 |
274 |
public static boolean startsWithHttpOrHttps(String file)... |
102 |
|
{ |
103 |
274 |
return file.startsWith("http://") || file.startsWith("https://"); |
104 |
|
} |
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
@param |
110 |
|
@param |
111 |
|
@param |
112 |
|
@return |
113 |
|
@throws |
114 |
|
@throws |
115 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
116 |
0 |
public static boolean checkUrlAvailable(URL url, int readTimeout)... |
117 |
|
throws IOException, ProtocolException |
118 |
|
{ |
119 |
|
|
120 |
|
|
121 |
0 |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
122 |
0 |
connection.setRequestMethod("HEAD"); |
123 |
0 |
connection.setDoInput(true); |
124 |
0 |
connection.setUseCaches(false); |
125 |
0 |
connection.setConnectTimeout(300); |
126 |
0 |
connection.setReadTimeout(readTimeout); |
127 |
|
|
128 |
|
|
129 |
0 |
HttpURLConnection conn = followConnection(connection); |
130 |
0 |
return conn.getResponseCode() == 200; |
131 |
|
} |
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
@param |
139 |
|
|
140 |
|
@return |
141 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
142 |
0 |
public static HttpURLConnection followConnection(HttpURLConnection conn0)... |
143 |
|
throws IOException |
144 |
|
{ |
145 |
0 |
return followConnection(conn0, false); |
146 |
|
} |
147 |
|
|
|
|
| 55.3% |
Uncovered Elements: 17 (38) |
Complexity: 12 |
Complexity Density: 0.5 |
|
148 |
377 |
public static HttpURLConnection followConnection(HttpURLConnection conn0,... |
149 |
|
boolean followAnyway) throws IOException |
150 |
|
{ |
151 |
377 |
URL url = conn0.getURL(); |
152 |
|
|
153 |
|
|
154 |
377 |
if (url == null) |
155 |
|
{ |
156 |
0 |
return conn0; |
157 |
|
} |
158 |
377 |
if (!conn0.getInstanceFollowRedirects()) |
159 |
|
{ |
160 |
0 |
return conn0; |
161 |
|
} |
162 |
377 |
if (!"http".equals(url.getProtocol()) && !followAnyway) |
163 |
|
{ |
164 |
312 |
return conn0; |
165 |
|
} |
166 |
|
|
167 |
|
|
168 |
65 |
HttpURLConnection checkConn = (HttpURLConnection) url.openConnection(); |
169 |
65 |
httpURLConnectionCopyAttributes(conn0, checkConn); |
170 |
|
|
171 |
65 |
boolean redirectToHttps = false; |
172 |
65 |
int response = checkConn.getResponseCode(); |
173 |
65 |
checkConn.disconnect(); |
174 |
65 |
if (response >= 300 && response < 400) |
175 |
|
{ |
176 |
|
|
177 |
0 |
URL loc = new URL(conn0.getHeaderField("Location")); |
178 |
0 |
if (loc != null && "https".equals(loc.getProtocol())) |
179 |
|
{ |
180 |
0 |
redirectToHttps = true; |
181 |
0 |
url = loc; |
182 |
|
} |
183 |
|
} |
184 |
65 |
else if (followAnyway) |
185 |
|
{ |
186 |
|
|
187 |
0 |
url = checkConn.getURL(); |
188 |
|
} |
189 |
|
|
190 |
65 |
if (!redirectToHttps && !followAnyway) |
191 |
|
{ |
192 |
65 |
return conn0; |
193 |
|
} |
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
0 |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
199 |
0 |
httpURLConnectionCopyAttributes(conn0, conn); |
200 |
0 |
return conn; |
201 |
|
} |
202 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
203 |
65 |
private static void httpURLConnectionCopyAttributes(... |
204 |
|
HttpURLConnection conn0, HttpURLConnection conn1) |
205 |
|
throws ProtocolException |
206 |
|
{ |
207 |
65 |
conn1.setRequestMethod(conn0.getRequestMethod()); |
208 |
65 |
conn1.setDoInput(conn0.getDoInput()); |
209 |
65 |
conn1.setUseCaches(conn0.getUseCaches()); |
210 |
65 |
conn1.setConnectTimeout(conn0.getConnectTimeout()); |
211 |
65 |
conn1.setReadTimeout(conn0.getReadTimeout()); |
212 |
65 |
conn1.setInstanceFollowRedirects(conn0.getInstanceFollowRedirects()); |
213 |
|
} |
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
@param |
219 |
|
|
220 |
|
@return |
221 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
222 |
4 |
public static URLConnection openConnection(URL url) throws IOException... |
223 |
|
{ |
224 |
4 |
return openConnection(url, false); |
225 |
|
} |
226 |
|
|
|
|
| 73.7% |
Uncovered Elements: 5 (19) |
Complexity: 5 |
Complexity Density: 0.38 |
|
227 |
4 |
public static URLConnection openConnection(URL url, boolean followAnyway)... |
228 |
|
throws IOException |
229 |
|
{ |
230 |
4 |
if (url == null) |
231 |
|
{ |
232 |
0 |
Console.debug("HttpUtils.openConnection(url) called with null url"); |
233 |
0 |
return null; |
234 |
|
} |
235 |
4 |
Console.debug("HttpUtils.openConnection(url) called with url=" |
236 |
|
+ url.toString()); |
237 |
4 |
URLConnection conn = null; |
238 |
4 |
String protocol = url.getProtocol(); |
239 |
4 |
if ("http".equals(protocol) || "https".equals(protocol)) |
240 |
|
{ |
241 |
2 |
HttpURLConnection conn0 = (HttpURLConnection) url.openConnection(); |
242 |
2 |
if (conn0 != null) |
243 |
|
{ |
244 |
2 |
conn = HttpUtils.followConnection(conn0, followAnyway); |
245 |
|
} |
246 |
|
else |
247 |
|
{ |
248 |
0 |
conn = conn0; |
249 |
|
} |
250 |
|
} |
251 |
|
else |
252 |
|
{ |
253 |
2 |
conn = url.openConnection(); |
254 |
|
} |
255 |
4 |
return conn; |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
@param |
263 |
|
|
264 |
|
@return |
265 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
266 |
536 |
public static InputStream openStream(URL url) throws IOException... |
267 |
|
{ |
268 |
536 |
return openStream(url, false); |
269 |
|
} |
270 |
|
|
|
|
| 81.2% |
Uncovered Elements: 3 (16) |
Complexity: 5 |
Complexity Density: 0.5 |
|
271 |
536 |
public static InputStream openStream(URL url, boolean followAnyway)... |
272 |
|
throws IOException |
273 |
|
{ |
274 |
536 |
if (url == null) |
275 |
|
{ |
276 |
0 |
return null; |
277 |
|
} |
278 |
536 |
InputStream is = null; |
279 |
536 |
String protocol = url.getProtocol(); |
280 |
536 |
if ("http".equals(protocol) || "https".equals(protocol)) |
281 |
|
{ |
282 |
375 |
HttpURLConnection conn = HttpUtils.followConnection( |
283 |
|
(HttpURLConnection) url.openConnection(), followAnyway); |
284 |
375 |
if (conn != null) |
285 |
|
{ |
286 |
375 |
is = conn.getInputStream(); |
287 |
|
} |
288 |
|
} |
289 |
|
else |
290 |
|
{ |
291 |
161 |
is = url.openStream(); |
292 |
|
} |
293 |
431 |
return is; |
294 |
|
} |
295 |
|
|
296 |
|
|
297 |
|
|
298 |
|
|
299 |
|
@param |
300 |
|
|
301 |
|
@return |
302 |
|
|
|
|
| 60% |
Uncovered Elements: 6 (15) |
Complexity: 5 |
Complexity Density: 0.45 |
|
303 |
352 |
public static boolean isJalviewSchemeUri(String jalviewUriString)... |
304 |
|
{ |
305 |
352 |
if (jalviewUriString == null) |
306 |
|
{ |
307 |
0 |
return false; |
308 |
|
} |
309 |
352 |
URI jalviewUri; |
310 |
352 |
try |
311 |
|
{ |
312 |
352 |
jalviewUri = new URI(jalviewUriString); |
313 |
|
} catch (URISyntaxException e) |
314 |
|
{ |
315 |
0 |
return false; |
316 |
|
} |
317 |
352 |
String scheme = jalviewUri.getScheme(); |
318 |
352 |
if (scheme == null || !scheme.startsWith(JALVIEWSCHEMEPREFIX)) |
319 |
|
{ |
320 |
352 |
return false; |
321 |
|
} |
322 |
0 |
int jspl = JALVIEWSCHEMEPREFIX.length(); |
323 |
0 |
return scheme.length() == jspl |
324 |
|
|| scheme.length() == jspl + 1 |
325 |
|
|| scheme.substring(jspl).equals("http") |
326 |
|
|| scheme.substring(jspl).equals("https"); |
327 |
|
} |
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
|
332 |
|
@param |
333 |
|
|
334 |
|
@return |
335 |
|
|
|
|
| 13.6% |
Uncovered Elements: 19 (22) |
Complexity: 8 |
Complexity Density: 0.5 |
|
336 |
176 |
public static String equivalentJalviewUrl(String jalviewUriString)... |
337 |
|
{ |
338 |
176 |
if (!isJalviewSchemeUri(jalviewUriString)) |
339 |
|
{ |
340 |
|
|
341 |
176 |
return jalviewUriString; |
342 |
|
} |
343 |
0 |
URI jalviewUri; |
344 |
0 |
try |
345 |
|
{ |
346 |
0 |
jalviewUri = new URI(jalviewUriString); |
347 |
|
} catch (URISyntaxException e) |
348 |
|
{ |
349 |
0 |
return null; |
350 |
|
} |
351 |
0 |
String scheme = jalviewUri.getScheme(); |
352 |
0 |
String host = jalviewUri.getHost(); |
353 |
0 |
if (host != null && host.length() > 0 || scheme |
354 |
|
.substring(JALVIEWSCHEMEPREFIX.length()).startsWith("http")) |
355 |
|
{ |
356 |
0 |
URI newUri; |
357 |
0 |
try |
358 |
|
{ |
359 |
0 |
newUri = new URI( |
360 |
0 |
scheme.equals(JALVIEWSCHEMEPREFIX + "http") ? "http" |
361 |
|
: "https", |
362 |
|
jalviewUri.getUserInfo(), host, jalviewUri.getPort(), |
363 |
|
jalviewUri.getPath(), jalviewUri.getQuery(), |
364 |
|
jalviewUri.getFragment()); |
365 |
|
|
366 |
0 |
return newUri.toURL().toString(); |
367 |
|
} catch (URISyntaxException | MalformedURLException e) |
368 |
|
{ |
369 |
0 |
ErrorLog.errPrintln("Trying to convert '" + jalviewUriString |
370 |
|
+ "' to URL failed"); |
371 |
|
} |
372 |
|
} |
373 |
|
else |
374 |
|
{ |
375 |
|
|
376 |
0 |
return jalviewUri.getPath(); |
377 |
|
} |
378 |
0 |
return null; |
379 |
|
} |
380 |
|
} |