wt.clients.util.http
Class Download

java.lang.Object
  extended bywt.clients.util.http.Download

public final class Download
extends Object

Basic HTTP download utility.

Supported API:false

Extendable:false


Nested Class Summary
(package private)  class Download.DownloadThread
          Inner class used to avoid declaring Download as Runnable.
 
Field Summary
private static int BUF_SIZ
           
private  boolean checked
          Whether or not a checksum should be calculated.
private  long checksum
          The checksum of the downloaded file.
private static Class CHECKSUM_CLASS
          The class to use to compute checksums.
private static Class DEFAULT_CHECKSUM_CLASS
           
static int FAILURE_FILE_NOT_FOUND
          Returned by getOperationFailureCode().
static int FAILURE_HTTP_ERROR
          Returned by getOperationFailureCode().
static int FAILURE_IO_ERROR
          Returned by getOperationFailureCode().
static int FAILURE_NONE
          Returned by getOperationFailureCode().
private  int httpResponseCode
          The http response code returned from the url connection.
private  String httpResponseMessage
          The http response message returned from the url connection.
private  Hashtable listeners
           
static int OPERATION_CANCEL
          Returned by getOperationStatus().
static int OPERATION_CANCEL_REQUEST
          Returned by getOperationStatus().
static int OPERATION_COMPLETE
          Returned by getOperationStatus().
static int OPERATION_FAILURE
          Returned by getOperationStatus().
static int OPERATION_INACTIVE
          Returned by getOperationStatus().
static int OPERATION_START
          Returned by getOperationStatus().
private  Exception operationException
          Return the operation exception.
private  int operationFailureCode
          Returned by getOperationFailureCode().
private  int operationStatus
          Returned by getOperationStatus().
private  String path
          The path of the file to download to.
private  URL url
          The url to download from.
private static boolean VERBOSE
           
 
Constructor Summary
Download()
          Create an unitialized download object.
Download(URL inputURL, String outputPath)
           
Download(URL inputURL, String outputPath, boolean a_Checked)
          Create a download object which is ready to have the perform operation called.
 
Method Summary
 void addDownloadListener(DownloadListener l)
           
 void cancelOperation()
          Cancel the download operation.
 void clear()
          Clear the download to a state ready for downloading.
 long getChecksum()
           
private static Class getChecksumClass(String className)
           
 int getHTTPResponseCode()
          Return the HTTP response code when getOperationFailureCode() returns FAILURE_HTTP_ERROR.
 String getHTTPResponseMessage()
          Return the HTTP response message when getOperationFailureCode() returns FAILURE_HTTP_ERROR.
 Exception getOperationException()
          Return the operation exception.
 int getOperationFailureCode()
          Return the type of failure encountered.
 int getOperationStatus()
          Return the status of the download operation (if any).
 String getPath()
          Return the path for the download.
 URL getURL()
          Return the URL for the download.
 boolean isChecked()
           
private  void operationProgress(int byteCount)
           
private  void operationStatusChange(int newStatus)
           
private  int parseHTTPResponse(URLConnection urlConnection)
          Return HTTP reponse code.
 Thread performOperation(boolean separateThread)
          Perform the download operation.
 void removeDownloadListener(DownloadListener l)
           
 void setChecked(boolean a_Checked)
           
private  void setHTTPResponseCode(int code)
           
private  void setHTTPResponseMessage(String message)
           
private  void setOperationException(Exception e)
           
private  void setOperationFailureCode(int code)
           
private  void setOperationStatus(int status)
           
 void setPath(String path)
          Set the path for the download.
 void setURL(URL inputURL)
          Set the url to be used in the download.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OPERATION_INACTIVE

public static final int OPERATION_INACTIVE
Returned by getOperationStatus(). Indicates that no operation is active.

See Also:
Constant Field Values

OPERATION_START

public static final int OPERATION_START
Returned by getOperationStatus(). Indicates that the download operation is active.

See Also:
Constant Field Values

OPERATION_CANCEL_REQUEST

public static final int OPERATION_CANCEL_REQUEST
Returned by getOperationStatus(). Indicates that the application has requested that the operation be cancelled.

See Also:
Constant Field Values

OPERATION_CANCEL

public static final int OPERATION_CANCEL
Returned by getOperationStatus(). Indicates that the operation was cancelled at application request.

See Also:
Constant Field Values

OPERATION_COMPLETE

public static final int OPERATION_COMPLETE
Returned by getOperationStatus(). Indicates that the operation completed successfully.

See Also:
Constant Field Values

OPERATION_FAILURE

public static final int OPERATION_FAILURE
Returned by getOperationStatus(). Indicates that the operation failed. The operationFailure code indicates the reason for the failure.

See Also:
Constant Field Values

FAILURE_NONE

public static final int FAILURE_NONE
Returned by getOperationFailureCode(). Indicates that no failure has occurred.

See Also:
Constant Field Values

FAILURE_IO_ERROR

public static final int FAILURE_IO_ERROR
Returned by getOperationFailureCode(). Indicates that some IOException was thrown during the operation.

See Also:
Constant Field Values

FAILURE_HTTP_ERROR

public static final int FAILURE_HTTP_ERROR
Returned by getOperationFailureCode(). Indicates that an HTTP error was returned.

See Also:
Constant Field Values

FAILURE_FILE_NOT_FOUND

public static final int FAILURE_FILE_NOT_FOUND
Returned by getOperationFailureCode(). Indicates that the file does not exist.

See Also:
Constant Field Values

url

private URL url
The url to download from.


path

private String path
The path of the file to download to.


httpResponseCode

private int httpResponseCode
The http response code returned from the url connection. This is only valid if the operation status is OPERATION_COMPLETE or OPERATION_FAILURE.


httpResponseMessage

private String httpResponseMessage
The http response message returned from the url connection. This is only valid if the operation status is OPERATION_COMPLETE or OPERATION_FAILURE.


operationStatus

private int operationStatus
Returned by getOperationStatus(). Indicates the status of the current operation (if any). Values returned are OPERATION_INACTIVE, OPERATION_START, OPERATION_CANCEL, OPERATION_COMPLETE, OPERATION_FAILURE.


operationFailureCode

private int operationFailureCode
Returned by getOperationFailureCode(). Indicates the nature of the operation failure. This value is only valid when getOperationStatus() returns OPERATION_FAILURE. Values returned are FAILURE_NONE, FAILURE_IO_ERROR, and FAILURE_FILE_NOT_FOUND.


operationException

private Exception operationException
Return the operation exception. Valid when getOperationFailureCode() is FAILURE_IO_ERROR or FAILURE_FILE_NOT_FOUND_ERROR.


checksum

private long checksum
The checksum of the downloaded file.


DEFAULT_CHECKSUM_CLASS

private static final Class DEFAULT_CHECKSUM_CLASS

CHECKSUM_CLASS

private static Class CHECKSUM_CLASS
The class to use to compute checksums. You can configure the class with the wt.util.checksum.checksumClass property. The class must implement java.util.zip.Checksum.


VERBOSE

private static boolean VERBOSE

BUF_SIZ

private static int BUF_SIZ

checked

private boolean checked
Whether or not a checksum should be calculated.


listeners

private Hashtable listeners
Constructor Detail

Download

public Download()
Create an unitialized download object. Operations setURL() and setPath() must be set before calling operation perform().


Download

public Download(URL inputURL,
                String outputPath)

Download

public Download(URL inputURL,
                String outputPath,
                boolean a_Checked)
Create a download object which is ready to have the perform operation called.

Method Detail

isChecked

public boolean isChecked()

setChecked

public void setChecked(boolean a_Checked)

getChecksum

public long getChecksum()

clear

public void clear()
Clear the download to a state ready for downloading. The HTTPResponseCode, HTTPReponseMessage, operationFailureCode and operationStatus will be reset to initial values. The URL and the path will not be reset.


getOperationStatus

public int getOperationStatus()
Return the status of the download operation (if any). Values returned are OPERATION_INACTIVE, OPERATION_START, OPERATION_CANCEL, OPERATION_COMPLETE and OPERATION_CANCEL.


setOperationStatus

private void setOperationStatus(int status)

getOperationFailureCode

public int getOperationFailureCode()
Return the type of failure encountered. This is only valid when getOperationStatus() returns OPERATION_FAILURE. Values returned are FAILURE_NONE, FAILURE_IO_ERROR, FAILURE_HTTP_ERROR. When FAILURE_HTTP_ERROR is returned the http response code can be accessed by calling getHTTPResponseCode().


setOperationFailureCode

private void setOperationFailureCode(int code)

getOperationException

public Exception getOperationException()
Return the operation exception. Valid only when getOperationFailureCode() is FAILURE_IO_ERROR or FAILURE_FILE_NOT_FOUND error.


setOperationException

private void setOperationException(Exception e)

getURL

public URL getURL()
Return the URL for the download.


setURL

public void setURL(URL inputURL)
Set the url to be used in the download.


getPath

public String getPath()
Return the path for the download.


setPath

public void setPath(String path)
Set the path for the download.


getHTTPResponseCode

public int getHTTPResponseCode()
Return the HTTP response code when getOperationFailureCode() returns FAILURE_HTTP_ERROR.


setHTTPResponseCode

private void setHTTPResponseCode(int code)

getHTTPResponseMessage

public String getHTTPResponseMessage()
Return the HTTP response message when getOperationFailureCode() returns FAILURE_HTTP_ERROR.


setHTTPResponseMessage

private void setHTTPResponseMessage(String message)

performOperation

public Thread performOperation(boolean separateThread)
Perform the download operation. Operation is complete when getOprerationStatus() returns OPERATION_COMPLETE, OPERATION_CANCEL, or OPERATION_FAILURE.

Parameters:
separateThread - when true a separate thread for the download will be created.
Returns:
the thread for the download operation. Null when param separateThread is false.

cancelOperation

public void cancelOperation()
Cancel the download operation.


addDownloadListener

public void addDownloadListener(DownloadListener l)

removeDownloadListener

public void removeDownloadListener(DownloadListener l)

parseHTTPResponse

private int parseHTTPResponse(URLConnection urlConnection)
                       throws IOException
Return HTTP reponse code. See HttpURLConnection for reponse values.

Throws:
IOException

operationStatusChange

private void operationStatusChange(int newStatus)

operationProgress

private void operationProgress(int byteCount)

getChecksumClass

private static final Class getChecksumClass(String className)