Coverage Report - wjhk.jupload2.upload.helper.HttpProtocolFinderThread
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpProtocolFinderThread
0 %
0/36
0 %
0/12
3,667
 
 1  
 //
 2  
 // $Id: DefaultUploadPolicy.java 289 2007-06-19 10:04:46 +0000 (mar., 19 juin
 3  
 // 2007) etienne_sf $
 4  
 //
 5  
 // jupload - A file upload juploadContext.
 6  
 // Copyright 2007 The JUpload Team
 7  
 //
 8  
 // Created: 2006-05-04
 9  
 // Creator: etienne_sf
 10  
 // Last modified: $Date: 2010-01-23 18:39:37 +0100 (sam., 23 janv. 2010) $
 11  
 //
 12  
 // This program is free software; you can redistribute it and/or modify it under
 13  
 // the terms of the GNU General Public License as published by the Free Software
 14  
 // Foundation; either version 2 of the License, or (at your option) any later
 15  
 // version. This program is distributed in the hope that it will be useful, but
 16  
 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 17  
 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 18  
 // details. You should have received a copy of the GNU General Public License
 19  
 // along with this program; if not, write to the Free Software Foundation, Inc.,
 20  
 // 675 Mass Ave, Cambridge, MA 02139, USA.
 21  
 package wjhk.jupload2.upload.helper;
 22  
 
 23  
 import wjhk.jupload2.policies.UploadPolicy;
 24  
 
 25  
 /**
 26  
  * This Thread executes a HEAD request to the server. From the server response,
 27  
  * the exact HTTP protocol, and any possible redirection are checked and used
 28  
  * (if any) to update the uploadURL.<BR>
 29  
  * The entry point for this method is the static
 30  
  * {@link #computeServerProtocol(UploadPolicy, String)} method.
 31  
  * 
 32  
  * @author etienne_sf
 33  
  */
 34  
 public class HttpProtocolFinderThread extends Thread {
 35  
     /** The current upload policy */
 36  
     UploadPolicy uploadPolicy;
 37  
 
 38  
     /** The given which should be analyzed, to find the server protocol */
 39  
     String givenServerProtocol;
 40  
 
 41  
     /**
 42  
      * This static method is the entry point for this class. It creates a
 43  
      * thread, and launch it, returning immediately. The finding of the server
 44  
      * protocol is then executed in a separated thread. So whenever long it is,
 45  
      * the user won't see it. A default protocol is set immediately, in the run
 46  
      * method, to be sure that there will be no NullPointerException.
 47  
      * 
 48  
      * @param uploadPolicy The current upload policy
 49  
      * @param givenServerProtocol The protocol given as parameter. If valid it
 50  
      *            will be used. If not, the serverProtocol will be computed from
 51  
      *            the post URL (and from a HEAD request for HTTP URL).
 52  
      */
 53  
     static public void computeServerProtocol(UploadPolicy uploadPolicy,
 54  
             String givenServerProtocol) {
 55  0
         new HttpProtocolFinderThread(uploadPolicy, givenServerProtocol).start();
 56  0
     }
 57  
 
 58  
     /**
 59  
      * @param uploadPolicy The current upload policy
 60  
      * @param givenServerProtocol The protocol given as parameter. If valid it
 61  
      *            will be used. If not, the serverProtocol will be computed from
 62  
      *            the post URL (and from a HEAD request for HTTP URL).
 63  
      */
 64  
     public HttpProtocolFinderThread(UploadPolicy uploadPolicy,
 65  
             String givenServerProtocol) {
 66  
         //Let's name this thread. It's easier for debugging.
 67  0
         super("HttpProtocolFinderThread");
 68  
         
 69  0
         this.uploadPolicy = uploadPolicy;
 70  0
         this.givenServerProtocol = givenServerProtocol;
 71  0
     }
 72  
 
 73  
     /**
 74  
      * The job itself. Will do a HEAD request if it's a HTTP URL. Will just note
 75  
      * ftp if FTP. Otherwise: will throw an error.
 76  
      */
 77  
     public void run() {
 78  0
         String computedProtocol = null;
 79  0
         String postURL = this.uploadPolicy.getPostURL();
 80  
 
 81  0
         if (null == givenServerProtocol || givenServerProtocol.equals("")) {
 82  0
             if (null == postURL || postURL.equals("")) {
 83  0
                 this.uploadPolicy.displayErr("postURL not set");
 84  0
                 computedProtocol = UploadPolicy.DEFAULT_SERVER_PROTOCOL;
 85  0
             } else if (postURL.substring(0, 3).equals("ftp")) {
 86  0
                 computedProtocol = "ftp";
 87  
             } else {
 88  
                 try {
 89  0
                     this.uploadPolicy.displayDebug(
 90  
                             "Getting serverProtocol from HEAD request", 30);
 91  
 
 92  
                     // Let's set a default protocol immediatly. It should be
 93  
                     // good, and avoid a NullPointerException if an upload
 94  
                     // starts immediatly.
 95  0
                     this.uploadPolicy
 96  0
                             .setServerProtocol(UploadPolicy.DEFAULT_SERVER_PROTOCOL);
 97  
                     // Then we do the head request to the server
 98  0
                     computedProtocol = new HttpConnect(this.uploadPolicy)
 99  0
                             .getProtocol();
 100  0
                 } catch (Exception e) {
 101  
                     // If we throw an error here, we prevent the applet
 102  
                     // to
 103  
                     // start. So we just log it, and try the default protocol
 104  0
                     this.uploadPolicy.displayErr(
 105  
                             "Unable to access to the postURL: '" + postURL
 106  
                                     + "'", e);
 107  
                     // Let's try with default value.
 108  0
                     computedProtocol = UploadPolicy.DEFAULT_SERVER_PROTOCOL;
 109  0
                 }
 110  
             }
 111  0
         } else if (this.givenServerProtocol.startsWith("HTTP")) {
 112  
             try {
 113  
                 // In HTTP mode, we always give a try to HTTPConnect, to check
 114  
                 // if the page has moved, and other stuff.
 115  
                 // But we keep the parameter given when calling this method.
 116  0
                 this.uploadPolicy.displayDebug(
 117  
                         "Checking any redirect, from HEAD request", 30);
 118  
                 // Let's set a default protocol immediatly. It should be
 119  
                 // good, and avoid a NullPointerException if an upload
 120  
                 // starts immediatly.
 121  0
                 this.uploadPolicy
 122  0
                         .setServerProtocol(UploadPolicy.DEFAULT_SERVER_PROTOCOL);
 123  
                 // Then we do the head request to the server
 124  0
                 computedProtocol = new HttpConnect(this.uploadPolicy)
 125  0
                         .getProtocol();
 126  0
             } catch (Exception e) {
 127  
                 // If we throw an error here, we prevent the applet to
 128  
                 // start. So we just log it, and try the default protocol
 129  0
                 this.uploadPolicy.displayErr(
 130  
                         "Unknown to get protocol in the given postURL ("
 131  0
                                 + this.uploadPolicy.getPostURL()
 132  0
                                 + "), due to error: " + e.getMessage(), e);
 133  0
             }
 134  
         }
 135  0
         this.uploadPolicy.setServerProtocol(computedProtocol);
 136  0
     }
 137  
 }