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";
38 * Returns true if it is possible to open an input stream at the given URL,
39 * else false. The input stream is closed.
44 public static boolean isValidUrl(String url)
46 InputStream is = null;
49 is = HttpUtils.openStream(new URL(url));
54 } catch (IOException x)
56 // MalformedURLException, FileNotFoundException
65 } catch (IOException e)
74 public static boolean startsWithHttpOrHttps(String file)
76 return file.startsWith("http://") || file.startsWith("https://");
79 public static boolean startsWithHttpOrHttpsOrJalviewScheme(String file)
81 if (startsWithHttpOrHttps(file))
85 return isJalviewSchemeUri(file);
89 * wrapper to get/post to a URL or check headers
96 * @throws ProtocolException
98 public static boolean checkUrlAvailable(URL url, int readTimeout)
99 throws IOException, ProtocolException
101 // jalview.bin.Console.outPrintln(System.currentTimeMillis() + " " + url);
103 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
104 connection.setRequestMethod("HEAD");
105 connection.setDoInput(true);
106 connection.setUseCaches(false);
107 connection.setConnectTimeout(300);
108 connection.setReadTimeout(readTimeout);
110 // HttpURLConnection doesn't follow redirects from http to https. It should!
111 HttpURLConnection conn = followConnection(connection);
112 return conn.getResponseCode() == 200;
116 * wrapper to follow a URL connection ALLOWING redirects from http to https
118 * @param HttpURLConnection
120 * @return HttpUrlConnection conn
122 public static HttpURLConnection followConnection(HttpURLConnection conn0)
125 URL url = conn0.getURL();
130 HttpURLConnection conn = null;
131 int response = conn0.getResponseCode();
132 boolean followed = false;
133 if (response >= 300 && response < 400 && conn0.getFollowRedirects())
135 // we are only checking for a redirect from http to https
136 if ("http".equals(url.getProtocol()))
138 URL loc = new URL(conn0.getHeaderField("Location"));
139 if (loc != null && "https".equals(loc.getProtocol()))
141 conn = (HttpURLConnection) loc.openConnection();
142 conn.setRequestMethod(conn0.getRequestMethod());
143 conn.setDoInput(conn0.getDoInput());
144 conn.setUseCaches(conn0.getUseCaches());
145 conn.setConnectTimeout(conn0.getConnectTimeout());
146 conn.setReadTimeout(conn0.getReadTimeout());
147 conn.setInstanceFollowRedirects(
148 conn0.getInstanceFollowRedirects());
153 return followed && conn != null ? conn : conn0;
157 * wrapper to follow a URL connection ALLOWING redirects from http to https
161 * @return HttpUrlConnection conn
163 public static URLConnection openConnection(URL url) throws IOException
169 URLConnection conn = null;
170 String protocol = url.getProtocol();
171 if ("http".equals(protocol) || "https".equals(protocol))
173 HttpURLConnection conn0 = (HttpURLConnection) url.openConnection();
176 conn = HttpUtils.followConnection(conn0);
185 conn = url.openConnection();
191 * wrapper to follow a URL connection ALLOWING redirects from http to https
192 * and return the followed InputStream
196 * @return HttpUrlConnection conn
198 public static InputStream openStream(URL url) throws IOException
204 InputStream is = null;
205 String protocol = url.getProtocol();
206 if ("http".equals(protocol) || "https".equals(protocol))
208 HttpURLConnection conn = HttpUtils
209 .followConnection((HttpURLConnection) url.openConnection());
212 is = conn.getInputStream();
217 is = url.openStream();
223 * check if a jalview:// scheme URL is given
229 public static boolean isJalviewSchemeUri(String jalviewUriString)
234 jalviewUri = new URI(jalviewUriString);
235 } catch (URISyntaxException e)
239 String scheme = jalviewUri.getScheme();
240 if (scheme == null || !scheme.startsWith(JALVIEWSCHEMEPREFIX))
244 int jspl = JALVIEWSCHEMEPREFIX.length();
245 return scheme.length() == jspl // jalview
246 || scheme.length() == jspl + 1 // jalviewX
247 || scheme.substring(jspl).equals("http") // jalviewhttp
248 || scheme.substring(jspl).equals("https"); // jalviewhttps
252 * convert a jalview scheme URI to its equivalent URL or path
258 public static String equivalentJalviewUrl(String jalviewUriString)
260 if (!isJalviewSchemeUri(jalviewUriString))
262 // not a jalviewUriString, hand it back
263 return jalviewUriString;
268 jalviewUri = new URI(jalviewUriString);
269 } catch (URISyntaxException e)
273 String scheme = jalviewUri.getScheme();
274 String host = jalviewUri.getHost();
275 if (host != null && host.length() > 0 || scheme
276 .substring(JALVIEWSCHEMEPREFIX.length()).startsWith("http"))
282 scheme.equals(JALVIEWSCHEMEPREFIX + "http") ? "http"
284 jalviewUri.getUserInfo(), host, jalviewUri.getPort(),
285 jalviewUri.getPath(), jalviewUri.getQuery(),
286 jalviewUri.getFragment());
288 return newUri.toURL().toString();
289 } catch (URISyntaxException | MalformedURLException e)
291 ErrorLog.errPrintln("Trying to convert '" + jalviewUriString
292 + "' to URL failed");
297 // return a file path (not a file URI)
298 return jalviewUri.getPath();