Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 17:01:39 GMT
  2. Package jalview.util

File HttpUtils.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
0% of files have more coverage

Code metrics

46
112
13
1
380
267
52
0.46
8.62
13
4

Classes

Class Line # Actions
HttpUtils 35 112 52
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
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   
 
35    public class HttpUtils
36    {
37    public final static String JALVIEWSCHEMEPREFIX = "jalview";
38   
 
39  0 toggle public static boolean isPlausibleUri(String s)
40    {
41  0 if (s == null)
42    {
43  0 return false;
44    }
45  0 if (startsWithHttpOrHttps(s) || isJalviewSchemeUri(s))
46    {
47  0 return true;
48    }
49  0 try
50    {
51  0 URI u = new URI(s);
52    // allow file:/home/... as well as file:///home... as java copes
53  0 if (s.startsWith("file:/"))
54    {
55  0 return true;
56    }
57    } catch (URISyntaxException e)
58    {
59  0 return false;
60    }
61  0 return false;
62    }
63   
64    /**
65    * Returns true if it is possible to open an input stream at the given URL,
66    * else false. The input stream is closed.
67    *
68    * @param url
69    * @return
70    */
 
71  0 toggle 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    // MalformedURLException, FileNotFoundException
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    // ignore
95    }
96    }
97    }
98  0 return false;
99    }
100   
 
101  0 toggle public static boolean startsWithHttpOrHttps(String file)
102    {
103  0 return file.startsWith("http://") || file.startsWith("https://");
104    }
105   
106    /**
107    * wrapper to get/post to a URL or check headers
108    *
109    * @param url
110    * @param ids
111    * @param readTimeout
112    * @return
113    * @throws IOException
114    * @throws ProtocolException
115    */
 
116  0 toggle public static boolean checkUrlAvailable(URL url, int readTimeout)
117    throws IOException, ProtocolException
118    {
119    // jalview.bin.Console.outPrintln(System.currentTimeMillis() + " " + url);
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    // HttpURLConnection doesn't follow redirects from http to https. It should!
129  0 HttpURLConnection conn = followConnection(connection);
130  0 return conn.getResponseCode() == 200;
131    }
132   
133    /**
134    * wrapper to return a new HttpURLConnection to a new URL when there is a
135    * redirect from http to https, otherwise return the unused original
136    * HttpURLConnection
137    *
138    * @param HttpURLConnection
139    * conn0
140    * @return HttpUrlConnection conn
141    */
 
142  0 toggle public static HttpURLConnection followConnection(HttpURLConnection conn0)
143    throws IOException
144    {
145  0 return followConnection(conn0, false);
146    }
147   
 
148  0 toggle public static HttpURLConnection followConnection(HttpURLConnection conn0,
149    boolean followAnyway) throws IOException
150    {
151  0 URL url = conn0.getURL();
152    // we are only checking for a redirect from http to https otherwise the java
153    // connection will follow when called (if not unset)
154  0 if (url == null)
155    {
156  0 return conn0;
157    }
158  0 if (!conn0.getInstanceFollowRedirects())
159    {
160  0 return conn0;
161    }
162  0 if (!"http".equals(url.getProtocol()) && !followAnyway)
163    {
164  0 return conn0;
165    }
166   
167    // check the response code
168  0 HttpURLConnection checkConn = (HttpURLConnection) url.openConnection();
169  0 httpURLConnectionCopyAttributes(conn0, checkConn);
170   
171  0 boolean redirectToHttps = false;
172  0 int response = checkConn.getResponseCode();
173  0 checkConn.disconnect();
174  0 if (response >= 300 && response < 400)
175    {
176    // we are only checking for a redirect from http to https
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  0 else if (followAnyway)
185    {
186    // checkConn might have followed a https->https redirect
187  0 url = checkConn.getURL();
188    }
189   
190  0 if (!redirectToHttps && !followAnyway)
191    {
192  0 return conn0;
193    }
194   
195    // We want to return an HttpURLConnection to the new (probably https) URL
196    // that is unconnected in case further manipulation of the request is
197    // required.
198  0 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
199  0 httpURLConnectionCopyAttributes(conn0, conn);
200  0 return conn;
201    }
202   
 
203  0 toggle private static void httpURLConnectionCopyAttributes(
204    HttpURLConnection conn0, HttpURLConnection conn1)
205    throws ProtocolException
206    {
207  0 conn1.setRequestMethod(conn0.getRequestMethod());
208  0 conn1.setDoInput(conn0.getDoInput());
209  0 conn1.setUseCaches(conn0.getUseCaches());
210  0 conn1.setConnectTimeout(conn0.getConnectTimeout());
211  0 conn1.setReadTimeout(conn0.getReadTimeout());
212  0 conn1.setInstanceFollowRedirects(conn0.getInstanceFollowRedirects());
213    }
214   
215    /**
216    * wrapper to follow a URL connection ALLOWING redirects from http to https
217    *
218    * @param URL
219    * url
220    * @return HttpUrlConnection conn
221    */
 
222  0 toggle public static URLConnection openConnection(URL url) throws IOException
223    {
224  0 return openConnection(url, false);
225    }
226   
 
227  0 toggle public static URLConnection openConnection(URL url, boolean followAnyway)
228    throws IOException
229    {
230  0 if (url == null)
231    {
232  0 Console.debug("HttpUtils.openConnection(url) called with null url");
233  0 return null;
234    }
235  0 Console.debug("HttpUtils.openConnection(url) called with url="
236    + url.toString());
237  0 URLConnection conn = null;
238  0 String protocol = url.getProtocol();
239  0 if ("http".equals(protocol) || "https".equals(protocol))
240    {
241  0 HttpURLConnection conn0 = (HttpURLConnection) url.openConnection();
242  0 if (conn0 != null)
243    {
244  0 conn = HttpUtils.followConnection(conn0, followAnyway);
245    }
246    else
247    {
248  0 conn = conn0;
249    }
250    }
251    else
252    {
253  0 conn = url.openConnection();
254    }
255  0 return conn;
256    }
257   
258    /**
259    * wrapper to follow a URL connection ALLOWING redirects from http to https
260    * and return the followed InputStream
261    *
262    * @param URL
263    * url
264    * @return HttpUrlConnection conn
265    */
 
266  0 toggle public static InputStream openStream(URL url) throws IOException
267    {
268  0 return openStream(url, false);
269    }
270   
 
271  0 toggle public static InputStream openStream(URL url, boolean followAnyway)
272    throws IOException
273    {
274  0 if (url == null)
275    {
276  0 return null;
277    }
278  0 InputStream is = null;
279  0 String protocol = url.getProtocol();
280  0 if ("http".equals(protocol) || "https".equals(protocol))
281    {
282  0 HttpURLConnection conn = HttpUtils.followConnection(
283    (HttpURLConnection) url.openConnection(), followAnyway);
284  0 if (conn != null)
285    {
286  0 is = conn.getInputStream();
287    }
288    }
289    else
290    {
291  0 is = url.openStream();
292    }
293  0 return is;
294    }
295   
296    /**
297    * check if a jalview:// scheme URL is given
298    *
299    * @param String
300    * uri
301    * @return boolean
302    */
 
303  0 toggle public static boolean isJalviewSchemeUri(String jalviewUriString)
304    {
305  0 if (jalviewUriString == null)
306    {
307  0 return false;
308    }
309  0 URI jalviewUri;
310  0 try
311    {
312  0 jalviewUri = new URI(jalviewUriString);
313    } catch (URISyntaxException e)
314    {
315  0 return false;
316    }
317  0 String scheme = jalviewUri.getScheme();
318  0 if (scheme == null || !scheme.startsWith(JALVIEWSCHEMEPREFIX))
319    {
320  0 return false;
321    }
322  0 int jspl = JALVIEWSCHEMEPREFIX.length();
323  0 return scheme.length() == jspl // jalview
324    || scheme.length() == jspl + 1 // jalviewX
325    || scheme.substring(jspl).equals("http") // jalviewhttp
326    || scheme.substring(jspl).equals("https"); // jalviewhttps
327    }
328   
329    /**
330    * convert a jalview scheme URI to its equivalent URL or path
331    *
332    * @param String
333    * uri
334    * @return String
335    */
 
336  0 toggle public static String equivalentJalviewUrl(String jalviewUriString)
337    {
338  0 if (!isJalviewSchemeUri(jalviewUriString))
339    {
340    // not a jalviewUriString, hand it back
341  0 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    // return a URL
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    // return a file path (not a file URI)
376  0 return jalviewUri.getPath();
377    }
378  0 return null;
379    }
380    }