wjhk.jupload2.upload
Class DefaultFileUploadThread

java.lang.Object
  extended by java.lang.Thread
      extended by wjhk.jupload2.upload.DefaultFileUploadThread
All Implemented Interfaces:
Runnable, FileUploadThread
Direct Known Subclasses:
FileUploadThreadFTP, FileUploadThreadHTTP

public abstract class DefaultFileUploadThread
extends Thread
implements FileUploadThread

This class is based on the FileUploadThread class. It's an abstract class that contains the default implementation for the FileUploadThread interface.
It contains the following abstract methods, which must be implemented in the children classes. These methods are called in this order:

  • For each upload request (for instance, upload of 3 files with nbFilesPerRequest to 2, makes 2 request: 2 files, then the last one):
  • try
  • startRequest(long, boolean, int, boolean): start of the UploadRequest.
  • Then, for each file to upload (according to the nbFilesPerRequest and maxChunkSize applet parameters)
  • beforeFile(int) is called before writting the bytes for this file (or this chunk)
  • afterFile(int) is called after writting the bytes for this file (or this chunk)
  • finishRequest()
  • finallycleanRequest()
  • Call of cleanAll(), to clean up any used resources, common to the whole upload.

  • Nested Class Summary
     
    Nested classes/interfaces inherited from class java.lang.Thread
    Thread.State, Thread.UncaughtExceptionHandler
     
    Field Summary
    (package private)  UploadFileData[] filesToUpload
              This array will contain a 'copy' of the relevant element of the filesDataParam array (see the constructor).
    (package private)  long maxChunkSize
              The value of the applet parameter maxChunkSize, or its default value.
    (package private)  int nbMaxFilesPerUpload
              Maximum number of files for FTP upload.
    (package private)  String responseBody
              The response message from the application.
    (package private)  String responseMsg
              The full response message from the server, if any.
    (package private)  boolean stop
              If set to 'true', the thread will stop the crrent upload.
    (package private)  Exception uploadException
              Thread Exception, if any occured during upload.
    (package private)  UploadPolicy uploadPolicy
              The upload policy contains all parameters needed to define the way files should be uploaded, including the URL.
     
    Fields inherited from class java.lang.Thread
    MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
     
    Constructor Summary
    DefaultFileUploadThread(FileData[] filesDataParam, UploadPolicy uploadPolicy, JProgressBar progressBar)
              Creates a new instance.
     
    Method Summary
    (package private) abstract  void afterFile(int index)
              Idem as beforeFile(int), but is called after each file (and each chunks for each file).
    (package private) abstract  void beforeFile(int index)
              This method is called before sending the bytes corresponding to the file whose index is given in argument.
    (package private) abstract  void beforeRequest(int firstFileToUploadParam, int nbFilesToUploadParam)
              This method is called before starting of each request.
    (package private) abstract  void cleanAll()
              Clean any used resource, like a 'permanent' connection.
    (package private) abstract  void cleanRequest()
              Clean any used resource of the last executed request.
     void close()
              Closes the connection to the server and releases resources.
    (package private) abstract  int finishRequest()
              This method is called at the end of each request.
    (package private) abstract  long getAdditionnalBytesForUpload(int indexFile)
              This methods upload overhead for the file number indexFile in the filesDataParam given to the constructor.
     Exception getException()
              Get the exception that occurs during upload.
    (package private) abstract  OutputStream getOutputStream()
              Get the output stream where the files should be written for upload.
     String getResponseBody()
              Return the the body for the server response.
     String getResponseMsg()
              Get the server Output.
     long getStartTime()
              Retrieve the start time of this thread.
     long getTotalLength()
               
     long getUploadedLength()
               
     boolean isUploadStopped()
              Returns true if someone asks the thread to stop.
     void nbBytesUploaded(long nbBytes)
              Used by the UploadFileData.uploadFile(java.io.OutputStream, long) for each uploaded buffer
     String normalizeCRLF(String s)
              Replace single \r and \n by uniform end of line characters (CRLF).
     String quoteCRLF(String s)
              Replace \r and \n by correctly displayed end of line characters.
     void run()
              The heart of the program.
    (package private)  void setResponseBody(String body)
              Unused Store the String that contains the server response body.
    (package private)  void setResponseMsg(String msg)
              Add a String that has been read from the server response.
    (package private) abstract  void startRequest(long contentLength, boolean bChunkEnabled, int chunkPart, boolean bLastChunk)
              This method is called for each upload request to the server.
     void stopUpload()
              Stopping the Thread
     
    Methods inherited from class java.lang.Thread
    activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
     
    Methods inherited from interface wjhk.jupload2.upload.FileUploadThread
    isAlive, join, join, start
     

    Field Detail

    filesToUpload

    UploadFileData[] filesToUpload
    This array will contain a 'copy' of the relevant element of the filesDataParam array (see the constructor). After filling the filesToUpload array, the UploadThread will only manipulate it: all access to the file contained by the filesDataParam given to the constructor will be done through the UploadFileData contained by this array.


    uploadPolicy

    UploadPolicy uploadPolicy
    The upload policy contains all parameters needed to define the way files should be uploaded, including the URL.


    maxChunkSize

    long maxChunkSize
    The value of the applet parameter maxChunkSize, or its default value.


    nbMaxFilesPerUpload

    int nbMaxFilesPerUpload
    Maximum number of files for FTP upload.


    stop

    boolean stop
    If set to 'true', the thread will stop the crrent upload. This attribute is not private as the UploadFileData class us it.

    See Also:
    UploadFileData.uploadFile(java.io.OutputStream, long)

    uploadException

    Exception uploadException
    Thread Exception, if any occured during upload.


    responseMsg

    String responseMsg
    The full response message from the server, if any. For instance, in HTTP mode, this contains both the headers and the body.


    responseBody

    String responseBody
    The response message from the application. For instance, in HTTP mode, this contains the body response.
    Note: for easier management on the various server configurations, all end or line characters (CR, LF or CRLF) are changed to uniform CRLF.

    Constructor Detail

    DefaultFileUploadThread

    public DefaultFileUploadThread(FileData[] filesDataParam,
                                   UploadPolicy uploadPolicy,
                                   JProgressBar progressBar)
    Creates a new instance.

    Parameters:
    filesDataParam - The files to be uploaded.
    uploadPolicy - The upload policy to be applied.
    progressBar - The progressBar bar to be updated.
    Method Detail

    getUploadedLength

    public long getUploadedLength()
    Specified by:
    getUploadedLength in interface FileUploadThread
    Returns:
    The current number of bytes, already uploaded in this thread.
    See Also:
    FileUploadThread.getUploadedLength()

    getTotalLength

    public long getTotalLength()
    Specified by:
    getTotalLength in interface FileUploadThread
    Returns:
    The total number of bytes, to be uploaded in this thread.
    See Also:
    FileUploadThread.getTotalLength()

    stopUpload

    public void stopUpload()
    Description copied from interface: FileUploadThread
    Stopping the Thread

    Specified by:
    stopUpload in interface FileUploadThread
    See Also:
    FileUploadThread.stopUpload()

    isUploadStopped

    public boolean isUploadStopped()
    Description copied from interface: FileUploadThread
    Returns true if someone asks the thread to stop.

    Specified by:
    isUploadStopped in interface FileUploadThread
    Returns:
    true if the upload has been requested to stop.
    See Also:
    FileUploadThread.isUploadStopped()

    getException

    public Exception getException()
    Get the exception that occurs during upload.

    Specified by:
    getException in interface FileUploadThread
    Returns:
    The exception, or null if no exception were thrown.

    nbBytesUploaded

    public void nbBytesUploaded(long nbBytes)
    Used by the UploadFileData.uploadFile(java.io.OutputStream, long) for each uploaded buffer

    Specified by:
    nbBytesUploaded in interface FileUploadThread
    Parameters:
    nbBytes - Number of bytes uploaded.
    See Also:
    FileUploadThread.nbBytesUploaded(long)

    getAdditionnalBytesForUpload

    abstract long getAdditionnalBytesForUpload(int indexFile)
                                        throws JUploadIOException
    This methods upload overhead for the file number indexFile in the filesDataParam given to the constructor. For instance, in HTTP, the upload contains a head and a tail for each files.

    Parameters:
    indexFile - The index of the file in the filesDataParam array, whose addtional length is asked.
    Returns:
    The additional number of bytes for this file.
    Throws:
    JUploadIOException

    beforeRequest

    abstract void beforeRequest(int firstFileToUploadParam,
                                int nbFilesToUploadParam)
                         throws JUploadException
    This method is called before starting of each request. It can be used to prepare any work, before starting the request. For instance, in HTTP, the tail must be properly calculated, as the last one must be different from the others.

    Parameters:
    firstFileToUploadParam -
    nbFilesToUploadParam -
    Throws:
    JUploadException

    startRequest

    abstract void startRequest(long contentLength,
                               boolean bChunkEnabled,
                               int chunkPart,
                               boolean bLastChunk)
                        throws JUploadException
    This method is called for each upload request to the server. The number of request to the server depends on:
  • The total number of files to upload.
  • The value of the nbFilesPerRequest applet parameter.
  • The value of the maxChunkSize applet parameter.
  • The main objective of this method is to open the connection to the server, where the files to upload will be written. It should also send any header necessary for this upload request. The getOutputStream() methods is then called to know where the uploaded files should be written.
    Note: it's up to the class containing this method to internally manage the connection.

    Parameters:
    contentLength - The total number of bytes for the files (or the chunk) to upload in this query.
    bChunkEnabled - True if this upload is part of a file (can occurs only if the maxChunkSize applet parameter is set). False otherwise.
    chunkPart - The chunk number. Should be ignored if bChunkEnabled is false.
    bLastChunk - True if in chunk mode, and this upload is the last one. Should be ignored if bChunkEnabled is false.
    Throws:
    JUploadException

    finishRequest

    abstract int finishRequest()
                        throws JUploadException
    This method is called at the end of each request.

    Returns:
    The response status code from the server (200 == OK)
    Throws:
    JUploadException
    See Also:
    startRequest(long, boolean, int, boolean)

    beforeFile

    abstract void beforeFile(int index)
                      throws JUploadException
    This method is called before sending the bytes corresponding to the file whose index is given in argument. If the file is splitted in chunks (see the maxChunkSize applet parameter), this method is called before each chunk for this file.

    Parameters:
    index - The index of the file that will be sent just after
    Throws:
    JUploadException

    afterFile

    abstract void afterFile(int index)
                     throws JUploadException
    Idem as beforeFile(int), but is called after each file (and each chunks for each file).

    Parameters:
    index - The index of the file that was just sent.
    Throws:
    JUploadException

    cleanRequest

    abstract void cleanRequest()
                        throws JUploadException
    Clean any used resource of the last executed request. In HTTP mode, the output stream, input stream and the socket should be cleaned here.

    Throws:
    JUploadException

    cleanAll

    abstract void cleanAll()
                    throws JUploadException
    Clean any used resource, like a 'permanent' connection. This method is called after the end of the last request (see on the top of this page for details).

    Throws:
    JUploadException

    getOutputStream

    abstract OutputStream getOutputStream()
                                   throws JUploadException
    Get the output stream where the files should be written for upload.

    Returns:
    The target output stream for upload.
    Throws:
    JUploadException

    getResponseBody

    public String getResponseBody()
    Return the the body for the server response. That is: the server response without the http header. This is the functional response from the server application, that has been as the HTTP reply body, for instance: all 'echo' PHP commands.

    Returns:
    The last application response (HTTP body, in HTTP upload)

    getResponseMsg

    public String getResponseMsg()
    Get the server Output.

    Specified by:
    getResponseMsg in interface FileUploadThread
    Returns:
    The status message from the first line of the response (e.g. "200 OK").

    setResponseBody

    void setResponseBody(String body)
    Unused Store the String that contains the server response body.

    Parameters:
    body - The response body that has been read.

    setResponseMsg

    void setResponseMsg(String msg)
    Add a String that has been read from the server response.

    Parameters:
    msg - The status message from the first line of the response (e.g. "200 OK").

    getStartTime

    public final long getStartTime()
    Retrieve the start time of this thread.

    Specified by:
    getStartTime in interface FileUploadThread
    Returns:
    The time this thread was started in ms.

    run

    public final void run()
    The heart of the program. This method prepare the upload, then calls doUpload for each HTTP request.

    Specified by:
    run in interface Runnable
    Overrides:
    run in class Thread
    See Also:
    Thread.run()

    close

    public void close()
    Description copied from interface: FileUploadThread
    Closes the connection to the server and releases resources.

    Specified by:
    close in interface FileUploadThread
    See Also:
    FileUploadThread.close()

    normalizeCRLF

    public final String normalizeCRLF(String s)
    Replace single \r and \n by uniform end of line characters (CRLF). This makes it easier, to search for string within the body.

    Parameters:
    s - The original string
    Returns:
    The string with single \r and \n modified changed to CRLF (\r\n).

    quoteCRLF

    public final String quoteCRLF(String s)
    Replace \r and \n by correctly displayed end of line characters. Used to display debug output. It also replace any single \r or \n by \r\n, to make it easier, to search for string within the body.

    Parameters:
    s - The original string
    Returns:
    The string with \r and \n modified, to be correctly displayed.