org.eclipse.ecf.provider.filetransfer.retrieve
Class AbstractRetrieveFileTransfer

java.lang.Object
  extended by org.eclipse.ecf.provider.filetransfer.retrieve.AbstractRetrieveFileTransfer
All Implemented Interfaces:
org.eclipse.core.runtime.IAdaptable, IIdentifiable, IFileTransfer, IFileTransferPausable, IIncomingFileTransfer, IRetrieveFileTransferContainerAdapter, IRetrieveFileTransfer
Direct Known Subclasses:
UrlConnectionRetrieveFileTransfer

public abstract class AbstractRetrieveFileTransfer
extends java.lang.Object
implements IIncomingFileTransfer, IRetrieveFileTransfer, IFileTransferPausable


Field Summary
protected  int buff_length
           
protected  long bytesReceived
           
protected static int CLOSE_TIMEOUT
           
protected  boolean closeOutputStream
           
protected  IConnectContext connectContext
           
static int DEFAULT_BUF_LENGTH
           
protected  boolean done
           
protected  double downloadRateBytesPerSecond
           
protected  java.lang.Exception exception
           
protected  long fileLength
           
protected  org.eclipse.core.runtime.jobs.Job job
           
protected  java.lang.Object jobLock
           
protected  long lastModifiedTime
           
protected  IFileTransferListener listener
           
protected  java.io.OutputStream localFileContents
           
protected  java.util.Map options
           
protected  boolean paused
           
protected static int POLLING_RETRY_ATTEMPTS
           
protected  Proxy proxy
           
protected  IFileRangeSpecification rangeSpecification
           
protected static int READ_TIMEOUT
           
protected  java.io.InputStream remoteFileContents
           
protected  IFileID remoteFileID
           
protected  java.net.URL remoteFileURL
           
protected  java.util.Map responseHeaders
           
protected static int TIMEOUT_INPUTSTREAM_BUFFER_SIZE
           
protected  long transferStartTime
           
 
Constructor Summary
AbstractRetrieveFileTransfer()
           
 
Method Summary
 void cancel()
          Cancel this file transfer.
protected  java.lang.String createJobName()
           
protected  java.lang.String createRangeName()
           
protected abstract  boolean doPause()
          Subclass overridable version of pause().
protected abstract  boolean doResume()
          Subclass overridable version of resume().
protected  void fireReceiveResumedEvent()
           
protected  void fireReceiveStartEvent()
           
protected  void fireTransferReceiveDataEvent()
           
protected  void fireTransferReceiveDoneEvent()
           
protected  void fireTransferReceivePausedEvent()
           
 java.lang.Object getAdapter(java.lang.Class adapter)
           
 long getBytesReceived()
          Get number of bytes received so far.
 java.lang.Exception getException()
          Get any exception associated with this file transfer.
 long getFileLength()
          Return resulting file length (in bytes) for this file transfer instance.
 IFileRangeSpecification getFileRangeSpecification()
          Get file range specification for this incoming file transfer instance.
protected  org.eclipse.core.runtime.IStatus getFinalStatus(java.lang.Throwable exception1)
           
 ID getID()
          Return the ID for this 'identifiable' object.
 IFileTransferListener getListener()
          Get listener assigned to this incoming file transfer.
protected  java.util.Map getOptions()
           
 double getPercentComplete()
          Get the percent complete for this file transfer.
 java.lang.String getRemoteFileName()
          Obtains the name of the remote file if possible.
protected  java.net.URL getRemoteFileURL()
           
 java.util.Date getRemoteLastModified()
          Obtains the timestamp that reflects the time when the remote file was last modified if possible.
 Namespace getRetrieveNamespace()
          Get namespace to be used for creation of remoteFileID for retrieve request.
protected  int getSocketCloseTimeout()
           
protected  int getSocketReadTimeout()
           
protected  void handleReceivedData(byte[] buf, int bytes, double factor, org.eclipse.core.runtime.IProgressMonitor monitor)
           
protected  void hardClose()
           
protected  boolean isCanceled()
           
 boolean isDone()
          Return true if this file transfer is done, false if not yet completed.
 boolean isPaused()
           
protected  UserCancelledException newUserCancelledException()
           
protected abstract  void openStreams()
          Open incoming and outgoing streams associated with this file transfer.
 boolean pause()
          Pause file transfer.
protected  void resetDoneAndException()
           
 boolean resume()
          Resume file transfer after having been paused.
protected  org.eclipse.core.net.proxy.IProxyData selectProxyFromProxies(java.lang.String protocol, org.eclipse.core.net.proxy.IProxyData[] proxies)
          Select a single proxy from a set of proxies available for the given host.
 void sendRetrieveRequest(IFileID rFileID, IFileRangeSpecification rangeSpec, IFileTransferListener transferListener, java.util.Map ops)
          Send request for transfer of a remote file to local file storage.
 void sendRetrieveRequest(IFileID remoteFileID1, IFileTransferListener transferListener, java.util.Map options1)
          Send request for transfer of a remote file to local file storage.
protected  void setCloseOutputStream(boolean close)
           
 void setConnectContextForAuthentication(IConnectContext connectContext)
          Set connect context for authentication upon subsequent IRetrieveFileTransferContainerAdapter.sendRetrieveRequest(IFileID, IFileTransferListener, Map).
protected  void setDone(boolean done)
           
protected  void setDoneCanceled()
           
protected  void setDoneCanceled(java.lang.Exception e)
           
protected  void setDoneException(java.lang.Exception e)
           
protected  void setFileLength(long length)
           
protected  void setInputStream(java.io.InputStream ins)
           
protected  void setLastModifiedTime(long timestamp)
           
protected  void setOutputStream(java.io.OutputStream outs)
           
 void setProxy(Proxy proxy)
          Set proxy for use upon subsequent IRetrieveFileTransferContainerAdapter.sendRetrieveRequest(IFileID, IFileTransferListener, Map).
protected  void setupAndScheduleJob(FileTransferJob fileTransferJob)
           
protected  void setupProxies()
           
protected abstract  void setupProxy(Proxy proxy)
          Setup ECF proxy.
protected  boolean targetHasGzSuffix(java.lang.String target)
           
static java.lang.String toHumanReadableBytes(double size)
           
protected  java.io.InputStream wrapTransferReadInputStream(java.io.InputStream inputStream, org.eclipse.core.runtime.IProgressMonitor monitor)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BUF_LENGTH

public static final int DEFAULT_BUF_LENGTH
See Also:
Constant Field Values

POLLING_RETRY_ATTEMPTS

protected static final int POLLING_RETRY_ATTEMPTS

TIMEOUT_INPUTSTREAM_BUFFER_SIZE

protected static final int TIMEOUT_INPUTSTREAM_BUFFER_SIZE
See Also:
Constant Field Values

READ_TIMEOUT

protected static final int READ_TIMEOUT

CLOSE_TIMEOUT

protected static final int CLOSE_TIMEOUT

jobLock

protected java.lang.Object jobLock

job

protected org.eclipse.core.runtime.jobs.Job job

remoteFileURL

protected java.net.URL remoteFileURL

remoteFileID

protected IFileID remoteFileID

listener

protected IFileTransferListener listener

buff_length

protected int buff_length

done

protected boolean done

bytesReceived

protected volatile long bytesReceived

remoteFileContents

protected java.io.InputStream remoteFileContents

localFileContents

protected java.io.OutputStream localFileContents

closeOutputStream

protected boolean closeOutputStream

exception

protected java.lang.Exception exception

fileLength

protected long fileLength

lastModifiedTime

protected long lastModifiedTime

options

protected java.util.Map options

paused

protected boolean paused

rangeSpecification

protected IFileRangeSpecification rangeSpecification

proxy

protected Proxy proxy

connectContext

protected IConnectContext connectContext

transferStartTime

protected long transferStartTime

downloadRateBytesPerSecond

protected double downloadRateBytesPerSecond

responseHeaders

protected java.util.Map responseHeaders
Since:
3.1
Constructor Detail

AbstractRetrieveFileTransfer

public AbstractRetrieveFileTransfer()
Method Detail

wrapTransferReadInputStream

protected java.io.InputStream wrapTransferReadInputStream(java.io.InputStream inputStream,
                                                          org.eclipse.core.runtime.IProgressMonitor monitor)

getRemoteFileURL

protected java.net.URL getRemoteFileURL()

getSocketReadTimeout

protected int getSocketReadTimeout()

getSocketCloseTimeout

protected int getSocketCloseTimeout()

setInputStream

protected void setInputStream(java.io.InputStream ins)

setOutputStream

protected void setOutputStream(java.io.OutputStream outs)

setCloseOutputStream

protected void setCloseOutputStream(boolean close)

setFileLength

protected void setFileLength(long length)

setLastModifiedTime

protected void setLastModifiedTime(long timestamp)

getOptions

protected java.util.Map getOptions()

handleReceivedData

protected void handleReceivedData(byte[] buf,
                                  int bytes,
                                  double factor,
                                  org.eclipse.core.runtime.IProgressMonitor monitor)
                           throws java.io.IOException
Throws:
java.io.IOException

toHumanReadableBytes

public static java.lang.String toHumanReadableBytes(double size)

getID

public ID getID()
Description copied from interface: IIdentifiable
Return the ID for this 'identifiable' object. The returned ID should be unique within its namespace. May return null.

Specified by:
getID in interface IIdentifiable
Returns:
the ID for this identifiable object. May return null.

getFinalStatus

protected org.eclipse.core.runtime.IStatus getFinalStatus(java.lang.Throwable exception1)

hardClose

protected void hardClose()

fireTransferReceivePausedEvent

protected void fireTransferReceivePausedEvent()

fireTransferReceiveDoneEvent

protected void fireTransferReceiveDoneEvent()

fireTransferReceiveDataEvent

protected void fireTransferReceiveDataEvent()

setConnectContextForAuthentication

public void setConnectContextForAuthentication(IConnectContext connectContext)
Description copied from interface: IRetrieveFileTransferContainerAdapter
Set connect context for authentication upon subsequent IRetrieveFileTransferContainerAdapter.sendRetrieveRequest(IFileID, IFileTransferListener, Map). This method should be called with a non-null connectContext in order to allow authentication to occur during call to IRetrieveFileTransferContainerAdapter.sendRetrieveRequest(IFileID, IFileTransferListener, Map).

Specified by:
setConnectContextForAuthentication in interface IRetrieveFileTransferContainerAdapter
Parameters:
connectContext - the connect context to use for authenticating during subsequent call to IRetrieveFileTransferContainerAdapter.sendRetrieveRequest(IFileID, IFileTransferListener, Map). If null, then no authentication will be attempted.

setProxy

public void setProxy(Proxy proxy)
Description copied from interface: IRetrieveFileTransferContainerAdapter
Set proxy for use upon subsequent IRetrieveFileTransferContainerAdapter.sendRetrieveRequest(IFileID, IFileTransferListener, Map). This method should be called with proxy to allow the given proxy to be used in subsequent calls to IRetrieveFileTransferContainerAdapter.sendRetrieveRequest(IFileID, IFileTransferListener, Map).

When proxy is null or has not been called providers must use the org.eclipse.core.net proxy API to obtain proxy information and proxy credentials if they support proxies of the type(s) supported by that API. The API is provided by an OSGi service of type org.eclipse.core.net.proxy.IProxyService.

If no information is available via IProxyService providers may use other defaults.

Specified by:
setProxy in interface IRetrieveFileTransferContainerAdapter
Parameters:
proxy - the proxy to use for subsequent calls to IRetrieveFileTransferContainerAdapter.sendRetrieveRequest(IFileID, IFileTransferListener, Map). If null, then proxy information is obtained from IProxyService if available. Otherwise provider defined defaults may be used.

getBytesReceived

public long getBytesReceived()
Description copied from interface: IIncomingFileTransfer
Get number of bytes received so far. If provider does not support reporting the number of bytes received, will return -1.

Specified by:
getBytesReceived in interface IIncomingFileTransfer
Returns:
long number of bytes received. Returns -1 if provider does not support reporting of number of bytes received during transfer

newUserCancelledException

protected UserCancelledException newUserCancelledException()
Since:
3.0

resetDoneAndException

protected void resetDoneAndException()

setDone

protected void setDone(boolean done)

setDoneException

protected void setDoneException(java.lang.Exception e)

isCanceled

protected boolean isCanceled()

setDoneCanceled

protected void setDoneCanceled()

setDoneCanceled

protected void setDoneCanceled(java.lang.Exception e)

cancel

public void cancel()
Description copied from interface: IFileTransfer
Cancel this file transfer. If file transfer has already been completed, then this method has no effect. If the file transfer has not been completed then calling this method will result in an IFileTransferEvent being delivered to the IFileTransferListener indicating that transfer is done (IFileTransfer.isDone() returns true), and some exception will be made available

Specified by:
cancel in interface IFileTransfer

getException

public java.lang.Exception getException()
Description copied from interface: IFileTransfer
Get any exception associated with this file transfer. The value returned from this method is valid only if IFileTransfer.isDone() method returns true. If the file transfer completed successfully, IFileTransfer.isDone() will return true, and this method will return null. If the file transfer completed unsuccessfully (some exception occurred), then IFileTransfer.isDone() will return true, and this method will return a non-null Exception instance that occurred.

If the the file transfer was canceled by the user, then the exception returned will be an instance of UserCancelledException.

Specified by:
getException in interface IFileTransfer
Returns:
Exception associated with this file transfer. null if transfer completed successfully, non-null if transfer completed with some exception. Only valid after IFileTransfer.isDone() returns true.

getPercentComplete

public double getPercentComplete()
Description copied from interface: IFileTransfer
Get the percent complete for this file transfer. The returned value will be either -1.0, meaning that the underlying provider does not support reporting percent complete for this file transfer, or a value between 0 and 1 reflecting the percent complete for this file transfer. If 0.0 no data has been sent, if 1.0, the file transfer is 100 percent complete. The value returned from this method should not be used to determine whether the transfer has completed, as it may not show completion in the event of an transfer failure. Note that the IFileTransfer.isDone() method should be consulted to determine if the file transfer has completed (with or without error).

Specified by:
getPercentComplete in interface IFileTransfer
Returns:
double percent complete. Returns -1.0 if the underlying provider does not support reporting percentage complete, or between 0 and 1 to indicate actual percent complete for this file transfer

getFileLength

public long getFileLength()
Description copied from interface: IFileTransfer
Return resulting file length (in bytes) for this file transfer instance. If the length is not known, -1 will be returned. Note that if a IFileRangeSpecification is provided that the returned file length is the expected file length of just the range retrieved (and not the entire file).

Specified by:
getFileLength in interface IFileTransfer

getRemoteLastModified

public java.util.Date getRemoteLastModified()
Description copied from interface: IIncomingFileTransfer
Obtains the timestamp that reflects the time when the remote file was last modified if possible.

Specified by:
getRemoteLastModified in interface IIncomingFileTransfer
Returns:
The time the remote file was last modified or null if that information was not available.

isDone

public boolean isDone()
Description copied from interface: IFileTransfer
Return true if this file transfer is done, false if not yet completed. The file transfer can be completed successfully, or an exception can occur and the file transfer will have failed. In either case of successful or unsuccessful transfer, this method will return true when the file transfer is complete. To determine whether the transfer completed successfully, it is necessary to also consult the IFileTransfer.getException() method.

Specified by:
isDone in interface IFileTransfer
Returns:
boolean true if file transfer is done, false if file transfer is still in progress.

getAdapter

public java.lang.Object getAdapter(java.lang.Class adapter)
Specified by:
getAdapter in interface org.eclipse.core.runtime.IAdaptable

openStreams

protected abstract void openStreams()
                             throws IncomingFileTransferException
Open incoming and outgoing streams associated with this file transfer. Subclasses must implement this method to open input and output streams. The remoteFileContents and localFileContent must be non-null after successful completion of the implementation of this method.

Throws:
IncomingFileTransferException

sendRetrieveRequest

public void sendRetrieveRequest(IFileID remoteFileID1,
                                IFileTransferListener transferListener,
                                java.util.Map options1)
                         throws IncomingFileTransferException
Description copied from interface: IRetrieveFileTransferContainerAdapter
Send request for transfer of a remote file to local file storage. This method is used to initiate a file retrieve for a remoteFileID (first parameter). File transfer events are asynchronously delivered a file transfer listener (second parameter). The given remoteFileID and transferListener must not be null.

NOTE: if this method completes successfully, the given transferListener will be asynchronously notified via an IIncomingFileTransferReceiveDoneEvent (along with other possible events). All implementations are required to issue this event whether successful or failed. Listeners can consult IIncomingFileTransferReceiveDoneEvent.getException() to determine whether the transfer operation completed successfully.

Specified by:
sendRetrieveRequest in interface IRetrieveFileTransferContainerAdapter
Parameters:
remoteFileID1 - reference to the remote target file (e.g. http://www.eclipse.org/index.html) or a reference to a resource that specifies the location of a target file. Implementing providers will determine what protocol schemes are supported (e.g. ftp, http, torrent, file, etc) and the required format of the scheme-specific information. If a protocol is specified that is not supported, or the scheme-specific information is not well-formed, then an IncomingFileTransferException will be thrown. Typically, callers will create IFileID instances via calls such as:
 IFileID remoteFileID = FileIDFactory.getDefault().createID(
                ftc.getRetrieveNamespace(), "http://www.composent.com/index.html");
 
Must not be null.
transferListener - a listener for file transfer events. Must not be null. Must not be null. See Note above.
options1 - a Map of options associated with sendRetrieveRequest. The particular name/value pairs will be unique to the individual providers. May be null.
Throws:
IncomingFileTransferException - if the provider is not connected or is not in the correct state for initiating file transfer

getRetrieveNamespace

public Namespace getRetrieveNamespace()
Description copied from interface: IRetrieveFileTransferContainerAdapter
Get namespace to be used for creation of remoteFileID for retrieve request. Result typically used as first parameter for IDFactory.createID(Namespace, String) to be used as first in IRetrieveFileTransferContainerAdapter.sendRetrieveRequest(IFileID, IFileTransferListener, Map)

Specified by:
getRetrieveNamespace in interface IRetrieveFileTransferContainerAdapter
Returns:
Namespace to use for ID creation via IDFactory.createID(Namespace, String). Will not be null.

isPaused

public boolean isPaused()
Specified by:
isPaused in interface IFileTransferPausable
Returns:
true if file transfer paused, false if not paused

doPause

protected abstract boolean doPause()
Subclass overridable version of pause(). Subclasses must provide an implementation of this method to support IFileTransferPausable .

Returns:
true if the pause is successful. false otherwise.

pause

public boolean pause()
Description copied from interface: IFileTransferPausable
Pause file transfer. Returns true if the associated IFileTransfer is successfully paused. Returns false if the implementing file transfer cannot be paused, or transfer has already completed.

Specified by:
pause in interface IFileTransferPausable
Returns:
true if file transfer successfully paused. False if cannot be paused, or the transfer has already completed

doResume

protected abstract boolean doResume()
Subclass overridable version of resume(). Subclasses must provide an implementation of this method to support IFileTransferPausable.

Returns:
true if the resume is successful. false otherwise.

resume

public boolean resume()
Description copied from interface: IFileTransferPausable
Resume file transfer after having been paused. If successfully resumed, then returns true. If the associated IFileShare is not already paused, or has already completed then this method returns false.

Specified by:
resume in interface IFileTransferPausable

getListener

public IFileTransferListener getListener()
Description copied from interface: IIncomingFileTransfer
Get listener assigned to this incoming file transfer. May be null if no listener has been provided.

Specified by:
getListener in interface IIncomingFileTransfer
Returns:
listener the IFileTransferListener provided for this incoming file transfer.

createRangeName

protected java.lang.String createRangeName()

createJobName

protected java.lang.String createJobName()

setupAndScheduleJob

protected void setupAndScheduleJob(FileTransferJob fileTransferJob)

fireReceiveStartEvent

protected void fireReceiveStartEvent()

fireReceiveResumedEvent

protected void fireReceiveResumedEvent()

getFileRangeSpecification

public IFileRangeSpecification getFileRangeSpecification()
Description copied from interface: IIncomingFileTransfer
Get file range specification for this incoming file transfer instance. Will return null if the retrieval is of the entire file.

Specified by:
getFileRangeSpecification in interface IIncomingFileTransfer
Returns:
file range specification for this incoming file transfer instance. Returns null if the retrieval is of the entire file.

sendRetrieveRequest

public void sendRetrieveRequest(IFileID rFileID,
                                IFileRangeSpecification rangeSpec,
                                IFileTransferListener transferListener,
                                java.util.Map ops)
                         throws IncomingFileTransferException
Description copied from interface: IRetrieveFileTransferContainerAdapter
Send request for transfer of a remote file to local file storage. This method is used to initiate a file retrieve for a remoteFileID (first parameter). File transfer events are asynchronously delivered a file transfer listener (third parameter). The given remoteFileID and transferListener must not be null.

NOTE: if this method completes successfully, the given transferListener will be asynchronously notified via an IIncomingFileTransferReceiveDoneEvent (along with other possible events). All implementations are required to issue this event whether successful or failed. Listeners can consult IIncomingFileTransferReceiveDoneEvent.getException() to determine whether the transfer operation completed successfully.

Specified by:
sendRetrieveRequest in interface IRetrieveFileTransferContainerAdapter
Parameters:
rFileID - reference to the remote target file (e.g. http://www.eclipse.org/index.html) or a reference to a resource that specifies the location of a target file. Implementing providers will determine what protocol schemes are supported (e.g. ftp, http, torrent, file, etc) and the required format of the scheme-specific information. If a protocol is specified that is not supported, or the scheme-specific information is not well-formed, then an IncomingFileTransferException will be thrown. Typically, callers will create IFileID instances via calls such as:
 IFileID remoteFileID = FileIDFactory.getDefault().createID(
                ftc.getRetrieveNamespace(), "http://www.composent.com/index.html");
 
Must not be null.
rangeSpec - a range specification for retrieving a portion of the given remote file. If null the entire file will be retrieved (as per IRetrieveFileTransferContainerAdapter.sendRetrieveRequest(IFileID, IFileTransferListener, Map). If non-null the given file range will be used to retrieve the given file. For example, if the rangeSpecification has a start value of 1 and end value of 3, and the total length of the file is 5 bytes with content [a, b, c, d, e], a successful retrieve request would transfer bytes 'b', 'c', and 'd', but not 'a', and 'e'.
transferListener - a listener for file transfer events. Must not be null. See Note above.
ops - a Map of options associated with sendRetrieveRequest. The particular name/value pairs will be unique to the individual providers. May be null.
Throws:
IncomingFileTransferException

setupProxy

protected abstract void setupProxy(Proxy proxy)
Setup ECF proxy. Subclasses must override this method to do appropriate proxy setup. This method will be called from within sendRetrieveRequest(IFileID, IFileTransferListener, Map) and sendRetrieveRequest(IFileID, IFileRangeSpecification, IFileTransferListener, Map) , prior to the actual call to openStreams().

Parameters:
proxy - the proxy to be setup. Will not be null.

selectProxyFromProxies

protected org.eclipse.core.net.proxy.IProxyData selectProxyFromProxies(java.lang.String protocol,
                                                                       org.eclipse.core.net.proxy.IProxyData[] proxies)
Select a single proxy from a set of proxies available for the given host. This implementation selects in the following manner: 1) If proxies provided is null or array of 0 length, null is returned. If only one proxy is available (array of length 1) then the entry is returned. If proxies provided is length > 1, then if the type of a proxy in the array matches the given protocol (e.g. http, https), then the first matching proxy is returned. If the protocol does not match any of the proxies, then the *first* proxy (i.e. proxies[0]) is returned. Subclasses may override if desired.

Parameters:
protocol - the target protocol (e.g. http, https, scp, etc). Will not be null.
proxies - the proxies to select from. May be null or array of length 0.
Returns:
proxy data selected from the proxies provided.

setupProxies

protected void setupProxies()

getRemoteFileName

public java.lang.String getRemoteFileName()
Description copied from interface: IIncomingFileTransfer
Obtains the name of the remote file if possible. The name will typically but not necessarily be the same as the leaf part of the path to the remote file.

Specified by:
getRemoteFileName in interface IIncomingFileTransfer
Returns:
The name of the remote file or null if no such name can be determined.

targetHasGzSuffix

protected boolean targetHasGzSuffix(java.lang.String target)