2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
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;
29 import java.net.URISyntaxException;
31 import java.net.URLConnection;
33 public class HttpUtils
35 public final static String JALVIEWSCHEMEPREFIX = "jalview";
37 public static boolean isPlausibleUri(String s)
43 if (startsWithHttpOrHttps(s) || isJalviewSchemeUri(s))
50 // allow file:/home/... as well as file:///home... as java copes
51 if (s.startsWith("file:/"))
55 } catch (URISyntaxException e)
63 * Returns true if it is possible to open an input stream at the given URL,
64 * else false. The input stream is closed.
69 public static boolean isValidUrl(String url)
71 InputStream is = null;
74 is = HttpUtils.openStream(new URL(url));
79 } catch (IOException x)
81 // MalformedURLException, FileNotFoundException
90 } catch (IOException e)
99 public static boolean startsWithHttpOrHttps(String file)
101 return file.startsWith("http://") || file.startsWith("https://");
105 * wrapper to get/post to a URL or check headers
111 * @throws IOException
112 * @throws ProtocolException
114 public static boolean checkUrlAvailable(URL url, int readTimeout)
115 throws IOException, ProtocolException
117 // jalview.bin.Console.outPrintln(System.currentTimeMillis() + " " + url);
119 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
120 connection.setRequestMethod("HEAD");
121 connection.setDoInput(true);
122 connection.setUseCaches(false);
123 connection.setConnectTimeout(300);
124 connection.setReadTimeout(readTimeout);
126 // HttpURLConnection doesn't follow redirects from http to https. It should!
127 HttpURLConnection conn = followConnection(connection);
128 return conn.getResponseCode() == 200;
132 * wrapper to follow a URL connection ALLOWING redirects from http to https
134 * @param HttpURLConnection
136 * @return HttpUrlConnection conn
138 public static HttpURLConnection followConnection(HttpURLConnection conn0)
141 URL url = conn0.getURL();
146 HttpURLConnection conn = null;
147 int response = conn0.getResponseCode();
148 boolean followed = false;
149 if (response >= 300 && response < 400 && conn0.getFollowRedirects())
151 // we are only checking for a redirect from http to https
152 if ("http".equals(url.getProtocol()))
154 URL loc = new URL(conn0.getHeaderField("Location"));
155 if (loc != null && "https".equals(loc.getProtocol()))
157 conn = (HttpURLConnection) loc.openConnection();
158 conn.setRequestMethod(conn0.getRequestMethod());
159 conn.setDoInput(conn0.getDoInput());
160 conn.setUseCaches(conn0.getUseCaches());
161 conn.setConnectTimeout(conn0.getConnectTimeout());
162 conn.setReadTimeout(conn0.getReadTimeout());
163 conn.setInstanceFollowRedirects(
164 conn0.getInstanceFollowRedirects());
169 return followed && conn != null ? conn : conn0;
173 * wrapper to follow a URL connection ALLOWING redirects from http to https
177 * @return HttpUrlConnection conn
179 public static URLConnection openConnection(URL url) throws IOException
185 URLConnection conn = null;
186 String protocol = url.getProtocol();
187 if ("http".equals(protocol) || "https".equals(protocol))
189 HttpURLConnection conn0 = (HttpURLConnection) url.openConnection();
192 conn = HttpUtils.followConnection(conn0);
201 conn = url.openConnection();
207 * wrapper to follow a URL connection ALLOWING redirects from http to https
208 * and return the followed InputStream
212 * @return HttpUrlConnection conn
214 public static InputStream openStream(URL url) throws IOException
220 InputStream is = null;
221 String protocol = url.getProtocol();
222 if ("http".equals(protocol) || "https".equals(protocol))
224 HttpURLConnection conn = HttpUtils
225 .followConnection((HttpURLConnection) url.openConnection());
228 is = conn.getInputStream();
233 is = url.openStream();
239 * check if a jalview:// scheme URL is given
245 public static boolean isJalviewSchemeUri(String jalviewUriString)
247 if (jalviewUriString == null)
254 jalviewUri = new URI(jalviewUriString);
255 } catch (URISyntaxException e)
259 String scheme = jalviewUri.getScheme();
260 if (scheme == null || !scheme.startsWith(JALVIEWSCHEMEPREFIX))
264 int jspl = JALVIEWSCHEMEPREFIX.length();
265 return scheme.length() == jspl // jalview
266 || scheme.length() == jspl + 1 // jalviewX
267 || scheme.substring(jspl).equals("http") // jalviewhttp
268 || scheme.substring(jspl).equals("https"); // jalviewhttps
272 * convert a jalview scheme URI to its equivalent URL or path
278 public static String equivalentJalviewUrl(String jalviewUriString)
280 if (!isJalviewSchemeUri(jalviewUriString))
282 // not a jalviewUriString, hand it back
283 return jalviewUriString;
288 jalviewUri = new URI(jalviewUriString);
289 } catch (URISyntaxException e)
293 String scheme = jalviewUri.getScheme();
294 String host = jalviewUri.getHost();
295 if (host != null && host.length() > 0 || scheme
296 .substring(JALVIEWSCHEMEPREFIX.length()).startsWith("http"))
302 scheme.equals(JALVIEWSCHEMEPREFIX + "http") ? "http"
304 jalviewUri.getUserInfo(), host, jalviewUri.getPort(),
305 jalviewUri.getPath(), jalviewUri.getQuery(),
306 jalviewUri.getFragment());
308 return newUri.toURL().toString();
309 } catch (URISyntaxException | MalformedURLException e)
311 ErrorLog.errPrintln("Trying to convert '" + jalviewUriString
312 + "' to URL failed");
317 // return a file path (not a file URI)
318 return jalviewUri.getPath();