Class URLConnectionTools
java.lang.Object
org.biojava.nbio.structure.align.util.URLConnectionTools
A class that takes care about opening URLConnections and sets the proper timeouts
- Since:
- 5.0
- Author:
- Andreas Prlic, Anthony Bradley
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The default connection timeout in ms - 15 seconds -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic InputStream
Do a POST to a URL and return the response stream for further processing elsewhere.static InputStream
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 Details
-
DEFAULT_CONNECTION_TIMEOUT
The default connection timeout in ms - 15 seconds- See Also:
-
-
Constructor Details
-
URLConnectionTools
public URLConnectionTools()
-
-
Method Details
-
openURLConnection
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
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
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
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
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
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
-