Class URLConnectionTools
- java.lang.Object
-
- org.biojava.nbio.structure.align.util.URLConnectionTools
-
public class URLConnectionTools extends Object
A class that takes care about opening URLConnections and sets the proper timeouts- Since:
- 5.0
- Author:
- Andreas Prlic, Anthony Bradley
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_CONNECTION_TIMEOUT
The default connection timeout in ms - 15 seconds
-
Constructor Summary
Constructors Constructor Description URLConnectionTools()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static InputStream
doPOST(URL url, String data)
Do a POST to a URL and return the response stream for further processing elsewhere.static InputStream
doPOST(URL url, String data, int timeout)
Do a POST to a URL and return the response stream for further processing elsewhere.static InputStream
getInputStream(URL url)
Connect to a URL and return result as an InputStream.static InputStream
getInputStream(URL url, boolean acceptGzipEncoding, int timeout)
Open a URL and return an InputStream to it if acceptGzipEncoding == true, use GZIPEncoding to compress communication.static InputStream
getInputStream(URL url, int timeout)
Connect to server and return result as an InputStream.static URLConnection
openURLConnection(URL url)
Open HttpURLConnection.static URLConnection
openURLConnection(URL url, int timeout)
Open HttpURLConnection.
-
-
-
Field Detail
-
DEFAULT_CONNECTION_TIMEOUT
public static final int DEFAULT_CONNECTION_TIMEOUT
The default connection timeout in ms - 15 seconds- See Also:
- Constant Field Values
-
-
Constructor Detail
-
URLConnectionTools
public URLConnectionTools()
-
-
Method Detail
-
openURLConnection
public static URLConnection openURLConnection(URL url, int timeout) throws IOException
Open HttpURLConnection. Recommended way to open URL connections in Java 1.7 and 1.8. https://eventuallyconsistent.net/2011/08/02/working-with-urlconnection-and-timeouts/- Parameters:
url
- URL to opentimeout
- timeout in milli seconds- Throws:
IOException
- an error in opening the URL
-
openURLConnection
public static URLConnection openURLConnection(URL url) throws IOException
Open HttpURLConnection. Recommended way to open HttpURLConnections, since this take care of setting timeouts properly for java 1.4 and 1.5 uses the DEFAULT_CONNECTION_TIMEOUT (= 15 seconds)- Parameters:
url
- a URL to open a http connection to- Returns:
- HttpURLConnect the opened connection
- Throws:
IOException
- an error in opening the URL
-
getInputStream
public static InputStream getInputStream(URL url, int timeout) throws IOException
Connect to server and return result as an InputStream. always asks for response to be in GZIP encodedThe caller is responsible to close the returned InputStream not to cause resource leaks.
- Parameters:
url
- the URL to connect totimeout
- the timeout for the connection- Returns:
- an
InputStream
of response - Throws:
IOException
- due to an error opening the URL
-
getInputStream
public static InputStream getInputStream(URL url) throws IOException
Connect to a URL and return result as an InputStream. always asks for response to be in GZIP encodedThe caller is responsible to close the returned InputStream not to cause resource leaks.
- Parameters:
url
- the input URL to be read- Returns:
- an
InputStream
of response - Throws:
IOException
- due to an error opening the URL
-
getInputStream
public static InputStream getInputStream(URL url, boolean acceptGzipEncoding, int timeout) throws IOException
Open a URL and return an InputStream to it if acceptGzipEncoding == true, use GZIPEncoding to compress communication.The caller is responsible to close the returned InputStream not to cause resource leaks.
- Parameters:
url
- the input URL to be readacceptGzipEncoding
- whether to accept Gzip encodingtimeout
-- Returns:
- an
InputStream
of response - Throws:
IOException
- due to an error opening the URL
-
doPOST
public static InputStream doPOST(URL url, String data) throws IOException
Do a POST to a URL and return the response stream for further processing elsewhere.The caller is responsible to close the returned InputStream not to cause resource leaks.
- Parameters:
url
- the input URL to be readdata
- the post data- Returns:
- an
InputStream
of response - Throws:
IOException
- due to an error opening the URL
-
doPOST
public static InputStream doPOST(URL url, String data, int timeout) throws IOException
Do a POST to a URL and return the response stream for further processing elsewhere.The caller is responsible to close the returned InputStream not to cause resource leaks.
- Parameters:
url
- the input URL to be readdata
- the post datatimeout
-- Returns:
- an
InputStream
of response - Throws:
IOException
- due to an error opening the URL
-
-