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
              The array that contains the current packet to upload.
    (package private)  FileUploadManagerThread fileUploadManagerThread
              The upload manager.
    protected  String responseBody
              The response message from the application.
    protected  String responseMsg
              The full response message from the server, if any.
    static long TIME_BEFORE_CHECKING_NEXT_PACKET
              Number of milliseconds that this thread will wait between two calls to FileUploadManagerThread.getNextPacket().
    protected  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(String threadName, UploadPolicy uploadPolicy, FileUploadManagerThread fileUploadManagerThread)
              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()
              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 method is called before the 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.
    (package private) abstract  void interruptionReceived()
              Reaction of the upload thread, when an interruption has been received.
     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()
              This method loops on the FileUploadManagerThread.getNextPacket() method, until a set of files is ready.
     void setFileUploadThreadManager(FileUploadManagerThread fileUploadManagerThread)
              Changes the FileUploadManagerThread.
    (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.
     
    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
    getState, interrupt, isAlive, join, join, start
     

    Field Detail

    TIME_BEFORE_CHECKING_NEXT_PACKET

    public static final long TIME_BEFORE_CHECKING_NEXT_PACKET
    Number of milliseconds that this thread will wait between two calls to FileUploadManagerThread.getNextPacket().

    See Also:
    Constant Field Values

    filesToUpload

    UploadFileData[] filesToUpload
    The array that contains the current packet to upload.

    See Also:
    FileUploadManagerThread.getNextPacket()

    fileUploadManagerThread

    FileUploadManagerThread fileUploadManagerThread
    The upload manager. The thread that prepares files, and is responsible to manage the upload process.

    See Also:
    FileUploadManagerThread

    uploadPolicy

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


    responseMsg

    protected 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

    protected 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(String threadName,
                                   UploadPolicy uploadPolicy,
                                   FileUploadManagerThread fileUploadManagerThread)
    Creates a new instance.

    Parameters:
    threadName - The name of the thread, that will be displayed in the debugger and in the logs.
    uploadPolicy - The upload policy to be applied.
    fileUploadManagerThread - The thread that is managing the upload.
    Method Detail

    getAdditionnalBytesForUpload

    abstract long getAdditionnalBytesForUpload(int indexFile)
                                        throws JUploadIOException
    This method is called before the upload. It calls the FileData.beforeUpload() method for all files to upload, and prepares the progressBar bar (if any), with total number of bytes to upload. final private void beforeUpload() throws JUploadException { for (int i = 0; i < this.filesToUpload.length && !this.fileUploadManager.isUploadStopped(); i++) { this.filesToUpload[i].beforeUpload(); } } /** 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()
                         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.
    The files to prepare are stored in the filesToUpload array.

    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)

    interruptionReceived

    abstract void interruptionReceived()
    Reaction of the upload thread, when an interruption has been received. This method should close all resource to the server, to allow the server to free any resource (temporary file, network connection...).


    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").

    run

    public final void run()
    This method loops on the FileUploadManagerThread.getNextPacket() method, until a set of files is ready. Then, it calls the doUpload() method, to send these files to the server.

    Specified by:
    run in interface Runnable
    Overrides:
    run in class Thread

    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.

    setFileUploadThreadManager

    public void setFileUploadThreadManager(FileUploadManagerThread fileUploadManagerThread)
                                    throws JUploadException
    Changes the FileUploadManagerThread. The standard way is to give the FileUploadManagerThread to the constructor.

    Specified by:
    setFileUploadThreadManager in interface FileUploadThread
    Throws:
    JUploadException
    See Also:
    FileUploadManagerThread