public abstract class HttpDownloader extends Object
HttpDownloadListener, condition variable to allow aborting the
download based on a caller specified AtomicBoolean,
default logic for timeout configuration etc.
Use the following properties to set up default values for all downloaders
(unless over-ridden with an more specific configuration for the specific
type of downloader)
com.sptci.HttpDownloader.socket.timeout - Set the socket
timeout for downloaders in milliseconds. Defaults to 5000 (5 seconds)com.sptci.HttpDownloader.socket.retryCount - Set the number
of retries if connection attempt fails. Defaults to 1© Copyright 2005 Sans Pareil Technologies, Inc.
| Modifier and Type | Field and Description |
|---|---|
protected static Logger |
logger
The logger to use to log errors/messages to.
|
| Constructor and Description |
|---|
HttpDownloader() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
abortIfRequested()
Check the value of the caller specified condition variable and abort the
download operation as required.
|
void |
deregisterListener()
De-register any HTTP download status listener for further requests
|
protected HttpURLConnection |
doGET(String url,
String... credentials)
Create a GET request to the specified url.
|
protected HttpURLConnection |
doPOST(String url,
Properties headers,
byte[] body,
String... credentials)
Create a POST request to the specified URL.
|
protected HttpURLConnection |
doPOST(String url,
String body,
String... credentials)
Create a POST request to the URI specified
|
protected AtomicBoolean |
getConditionVariable() |
protected HttpURLConnection |
getConnection(String url,
String method,
String... credentials)
Open a connection to the specified URL using the specified method.
|
protected InputStream |
getInputStream(HttpURLConnection httpConnection)
Return the proper input stream to use to read the response.
|
protected HttpDownloadListener |
getListener() |
protected BufferedReader |
getReader(HttpURLConnection httpConnection)
Read the response from the
HttpURLConnection. |
void |
registerConditionVariable(AtomicBoolean flag)
Register the specified condition variable that will be monitored to
abort download operations.
|
void |
registerListener(HttpDownloadListener downloadListener)
Register the specified listener for HTTP download status notifications.
|
void |
resetAcceptEncoding() |
protected int |
retryCount()
Return the number of attempts to make to fetch the response from a URI.
|
void |
setAcceptEncoding(String encoding)
Set the Accept-Encoding value for the downloader.
|
protected int |
socketTimeout()
Return the socket timeout value to set when downloading data from URL's.
|
protected static final Logger logger
public void registerListener(HttpDownloadListener downloadListener)
downloadListener - The listener instance to send notifications to.public void deregisterListener()
public void registerConditionVariable(AtomicBoolean flag)
false to indicate that a download operation
should abort.
Note: Aborting a download will usually result in an exception
which will be communiated via the HttpDownloadListener registered
or a IOException.flag - The externally modified condition variable to use to abort downloads.public void setAcceptEncoding(String encoding)
encoding - The Accept-Encoding value to use.public void resetAcceptEncoding()
protected HttpDownloadListener getListener()
protected AtomicBoolean getConditionVariable()
protected void abortIfRequested()
throws IOException
IOException - If condition variable was set to false
throws an exception to abort the download.protected int socketTimeout()
com.sptci.HttpDownloader.socket.timeout
or uses a default value of 5000.protected int retryCount()
com.sptci.HttpDownloader.socket.retryCount
or uses a default value of 1.protected HttpURLConnection getConnection(String url, String method, String... credentials) throws IOException
url - The URL from which to retrieve the text data.method - The HTTP method to use (GET/POST/HEAD...)credentials - The optional credentials (username and password) to use
for HTTP Basic authIOException - If errors are encounteredprotected HttpURLConnection doGET(String url, String... credentials) throws IOException
url - The URL from which to retrieve the text data.credentials - The optional credentials (username and password) to use
for HTTP Basic authIOException - If errors are encountered.protected HttpURLConnection doPOST(String url, String body, String... credentials) throws IOException
url - The URL to retrieve text data from.credentials - The optional user and password to authenticate as using HTTP Basic Authbody - The POST request body content.IOException - If errors are encountered.protected HttpURLConnection doPOST(String url, Properties headers, byte[] body, String... credentials) throws IOException
url - The URL to create the POST request forheaders - The headers to specify with the POST requestbody - The request body to post to the servercredentials - Optional username/password combination for HTTP Basic AuthIOException - If errors are encountered while communicating with serverprotected InputStream getInputStream(HttpURLConnection httpConnection) throws IOException
Content-encoding response header to see if the
response is gzip encoded. If it is, use a
GZIPInputStream to read the response.
Note: Callers must check the response code returned by the
connection to determine if a valid response was received or an error
returned.httpConnection - The HttpURLConnection from
which the response is to be read.IOException - If errors are encountered while reading the
response from the server.protected BufferedReader getReader(HttpURLConnection httpConnection) throws IOException
HttpURLConnection. Check
the Content-encoding response header to see if the
response is gzip encoded. If it is, use a
GZIPInputStream to read the response.
Note: Callers must check the response code returned by the
connection to determine if a valid response was received or an error
returned.httpConnection - The HttpURLConnection from
which the response is to be read.IOException - If errors are encountered while reading the
response from the server.