dk.netarkivet.common.distribute
Class HTTPRemoteFile

java.lang.Object
  extended by dk.netarkivet.common.distribute.AbstractRemoteFile
      extended by dk.netarkivet.common.distribute.HTTPRemoteFile
All Implemented Interfaces:
RemoteFile, java.io.Serializable
Direct Known Subclasses:
HTTPSRemoteFile

public class HTTPRemoteFile
extends AbstractRemoteFile

A remote file implemented with point-to-point HTTP communication. Optimised to communicate locally, if file is on the same host. Optimised to transfer 0 byte files inline.

See Also:
Serialized Form

Field Summary
protected  java.lang.String checksum
          If useChecksums is true, contains the file checksum.
protected  java.lang.String hostname
          The name of the host this file originated on.
static java.lang.String HTTPREMOTEFILE_PORT_NUMBER
          settings.common.remoteFile.port:
The setting for the HTTP remotefile port number used.
protected  java.net.URL url
          The url that exposes this remote file.
 
Fields inherited from class dk.netarkivet.common.distribute.AbstractRemoteFile
file, fileDeletable, filesize, multipleDownloads, useChecksums
 
Constructor Summary
protected HTTPRemoteFile(java.io.File file, boolean useChecksums, boolean fileDeletable, boolean multipleDownloads)
          Initialises a remote file implemented by point-to-point HTTP communication.
 
Method Summary
 void cleanup()
          Invalidate all file handles, by asking the remote registry to remove the url for this remote file from the list of shared files.
 void copyTo(java.io.File destFile)
          Copy this remote file to the given file.
 java.lang.String getChecksum()
          Get checksum for file, or null if checksums were not requested.
 java.io.InputStream getInputStream()
          Get an input stream representing the remote file.
static RemoteFile getInstance(java.io.File f, java.lang.Boolean useChecksums, java.lang.Boolean fileDeletable, java.lang.Boolean multipleDownloads)
          Initialises a remote file implemented by point-to-point HTTP communication.
 int getNumberOfRetries()
          Retrieval of the number of retries for retrieving a file from a HTTP server.
protected  HTTPRemoteFileRegistry getRegistry()
          Get the webserver registry for this class of files.
protected  boolean isLocal()
          Helper method to determine if file resides on local machine.
 
Methods inherited from class dk.netarkivet.common.distribute.AbstractRemoteFile
appendTo, getName, getSize
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

hostname

protected final java.lang.String hostname
The name of the host this file originated on.


url

protected final java.net.URL url
The url that exposes this remote file.


checksum

protected final java.lang.String checksum
If useChecksums is true, contains the file checksum.


HTTPREMOTEFILE_PORT_NUMBER

public static java.lang.String HTTPREMOTEFILE_PORT_NUMBER
settings.common.remoteFile.port:
The setting for the HTTP remotefile port number used.

Constructor Detail

HTTPRemoteFile

protected HTTPRemoteFile(java.io.File file,
                         boolean useChecksums,
                         boolean fileDeletable,
                         boolean multipleDownloads)
Initialises a remote file implemented by point-to-point HTTP communication.

Parameters:
file - The file to make a remote file for
useChecksums - Whether communications are checksummed. If true, getChecksum will also return the checksum.
fileDeletable - if true, the file given to this method is deletable, once it is transferred.
multipleDownloads - if true, the file may be transferred more than once. Otherwise, all file handles are attempted to be made invalid after the first transfer, although no guarantees are made.
Throws:
ArgumentNotValid - if file is null, or not a readable file.
IOFailure - if checksums are requested, but i/o errors occur while checksumming.
Method Detail

getInstance

public static RemoteFile getInstance(java.io.File f,
                                     java.lang.Boolean useChecksums,
                                     java.lang.Boolean fileDeletable,
                                     java.lang.Boolean multipleDownloads)
Initialises a remote file implemented by point-to-point HTTP communication.

Parameters:
f - The file to make a remote file for
useChecksums - Whether communications are checksummed. If true, getChecksum will also return the checksum.
fileDeletable - if true, the file given to this method is deletable, once it is transferred.
multipleDownloads - if true, the file may be transferred more than once. Otherwise, all file handles are attempted to be made invalid after the first transfer, although no guarantees are made.
Throws:
ArgumentNotValid - if file is null, or not a readable file.
IOFailure - if checksums are requested, but i/o errors occur while checksumming.

getRegistry

protected HTTPRemoteFileRegistry getRegistry()
Get the webserver registry for this class of files. Meant to be subclassed for specialised versions of this file.

Returns:
The reigstry.

copyTo

public void copyTo(java.io.File destFile)
Copy this remote file to the given file. If the file resides on the current machine, remote file transfer is done locally. Otherwise, the remote file is transferred over http. If the file is not set to be able to be transferred multiple times, it is cleaned up after the transfer.

Specified by:
copyTo in interface RemoteFile
Overrides:
copyTo in class AbstractRemoteFile
Parameters:
destFile - The file to write the remote file to.
Throws:
ArgumentNotValid - on null destFile, or parent to destfile is not a writeable directory, or destfile exists and cannot be overwritten.
IOFailure - on I/O trouble writing remote file to destination.

getInputStream

public java.io.InputStream getInputStream()
Get an input stream representing the remote file. If the file resides on the current machine, the input stream is to the local file. Otherwise, the remote file is transferred over http. The close method of the input stream will cleanup this handle, and if checksums are requested, will check the checksums on close. If the file is not set to be able to be transferred multiple times, it is cleaned up after the transfer.

Specified by:
getInputStream in interface RemoteFile
Specified by:
getInputStream in class AbstractRemoteFile
Returns:
An input stream for the remote file.
Throws:
IOFailure - on I/O trouble generating inputstream for remote file. Also, the returned remote file will throw IOFailure on close, if checksums are requested, but do not match.

cleanup

public void cleanup()
Invalidate all file handles, by asking the remote registry to remove the url for this remote file from the list of shared files. Invalidating a file handle may delete the original files, if deletable. This method does not throw exceptions, but will warn on errors.

Specified by:
cleanup in interface RemoteFile
Specified by:
cleanup in class AbstractRemoteFile

getChecksum

public java.lang.String getChecksum()
Get checksum for file, or null if checksums were not requested.

Specified by:
getChecksum in interface RemoteFile
Specified by:
getChecksum in class AbstractRemoteFile
Returns:
checksum for file, or null if checksums were not requested.

isLocal

protected boolean isLocal()
Helper method to determine if file resides on local machine.

Returns:
true if the file is on the local machine, false otherwise.

getNumberOfRetries

public int getNumberOfRetries()
Retrieval of the number of retries for retrieving a file from a HTTP server. TODO define a setting for HTTP retries, just like for the FTP retries.

Specified by:
getNumberOfRetries in class AbstractRemoteFile
Returns:
The number of retries. Currently a constant: 1.