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;
32 public class HttpUtils
36 * Returns true if it is possible to open an input stream at the given URL,
37 * else false. The input stream is closed.
42 public static boolean isValidUrl(String url)
44 InputStream is = null;
47 is = HttpUtils.openStream(new URL(url));
52 } catch (IOException x)
54 // MalformedURLException, FileNotFoundException
63 } catch (IOException e)
72 public static boolean startsWithHttpOrHttps(String file)
74 return file.startsWith("http://") || file.startsWith("https://");
78 * wrapper to get/post to a URL or check headers
85 * @throws ProtocolException
87 public static boolean checkUrlAvailable(URL url, int readTimeout)
88 throws IOException, ProtocolException
90 // jalview.bin.Console.outPrintln(System.currentTimeMillis() + " " + url);
92 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
93 connection.setRequestMethod("HEAD");
94 connection.setDoInput(true);
95 connection.setUseCaches(false);
96 connection.setConnectTimeout(300);
97 connection.setReadTimeout(readTimeout);
99 // HttpURLConnection doesn't follow redirects from http to https. It should!
100 HttpURLConnection conn = followConnection(connection);
101 return conn.getResponseCode() == 200;
105 * wrapper to follow a URL connection ALLOWING redirects from http to https
107 * @param HttpURLConnection
109 * @return HttpUrlConnection conn
111 public static HttpURLConnection followConnection(HttpURLConnection conn0)
114 URL url = conn0.getURL();
119 HttpURLConnection conn = null;
120 int response = conn0.getResponseCode();
121 boolean followed = false;
122 if (response >= 300 && response < 400 && conn0.getFollowRedirects())
124 // we are only checking for a redirect from http to https
125 if ("http".equals(url.getProtocol()))
127 URL loc = new URL(conn0.getHeaderField("Location"));
128 if (loc != null && "https".equals(loc.getProtocol()))
130 conn = (HttpURLConnection) loc.openConnection();
131 conn.setRequestMethod(conn0.getRequestMethod());
132 conn.setDoInput(conn0.getDoInput());
133 conn.setUseCaches(conn0.getUseCaches());
134 conn.setConnectTimeout(conn0.getConnectTimeout());
135 conn.setReadTimeout(conn0.getReadTimeout());
136 conn.setInstanceFollowRedirects(
137 conn0.getInstanceFollowRedirects());
142 return followed && conn != null ? conn : conn0;
146 * wrapper to follow a URL connection ALLOWING redirects from http to https
150 * @return HttpUrlConnection conn
152 public static HttpURLConnection openConnection(URL url) throws IOException
158 HttpURLConnection conn = null;
159 String protocol = url.getProtocol();
160 if ("http".equals(protocol) || "https".equals(protocol))
162 HttpURLConnection conn0 = (HttpURLConnection) url.openConnection();
165 conn = HttpUtils.followConnection(conn0);
176 * wrapper to follow a URL connection ALLOWING redirects from http to https
177 * and return the followed InputStream
181 * @return HttpUrlConnection conn
183 public static InputStream openStream(URL url) throws IOException
189 InputStream is = null;
190 String protocol = url.getProtocol();
191 if ("http".equals(protocol) || "https".equals(protocol))
193 HttpURLConnection conn = HttpUtils
194 .followConnection((HttpURLConnection) url.openConnection());
197 is = conn.getInputStream();
202 is = url.openStream();
208 * check if a jalview:// scheme URL is given
214 public static boolean isJalviewSchemeUri(String jalviewUriString)
219 jalviewUri = new URI(jalviewUriString);
220 } catch (URISyntaxException e)
224 String scheme = jalviewUri.getScheme();
225 if (scheme == null || !scheme.startsWith("jalview"))
229 return scheme.length() == 7 // jalview
230 || scheme.length() == 8 // jalviewX
231 || scheme.substring(7).equals("http") // jalviewhttp
232 || scheme.substring(7).equals("https"); // jalviewhttps
236 * convert a jalview scheme URI to its equivalent URL or path
242 public static String equivalentJalviewUrl(String jalviewUriString)
244 if (!isJalviewSchemeUri(jalviewUriString))
251 jalviewUri = new URI(jalviewUriString);
252 } catch (URISyntaxException e)
256 String scheme = jalviewUri.getScheme();
257 String host = jalviewUri.getHost();
258 if (host != null && host.length() > 0)
263 newUri = new URI(scheme.equals("jalviewhttp") ? "http" : "https",
264 jalviewUri.getUserInfo(), host, jalviewUri.getPort(),
265 jalviewUri.getPath(), jalviewUri.getQuery(),
266 jalviewUri.getFragment());
268 return newUri.toURL().toString();
269 } catch (URISyntaxException | MalformedURLException e)
271 ErrorLog.errPrintln("Trying to convert '" + jalviewUriString
272 + "' to URL failed");
277 // return a file path (not a file URI)
278 return jalviewUri.getPath();