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 new HttpProtocolFinderThread(uploadPolicy, givenServerProtocol).start();
56 }
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 super("HttpProtocolFinderThread");
68
69 this.uploadPolicy = uploadPolicy;
70 this.givenServerProtocol = givenServerProtocol;
71 }
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 String computedProtocol = null;
79 String postURL = this.uploadPolicy.getPostURL();
80
81 if (null == givenServerProtocol || givenServerProtocol.equals("")) {
82 if (null == postURL || postURL.equals("")) {
83 this.uploadPolicy.displayErr("postURL not set");
84 computedProtocol = UploadPolicy.DEFAULT_SERVER_PROTOCOL;
85 } else if (postURL.substring(0, 3).equals("ftp")) {
86 computedProtocol = "ftp";
87 } else {
88 try {
89 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 this.uploadPolicy
96 .setServerProtocol(UploadPolicy.DEFAULT_SERVER_PROTOCOL);
97 // Then we do the head request to the server
98 computedProtocol = new HttpConnect(this.uploadPolicy)
99 .getProtocol();
100 } 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 this.uploadPolicy.displayErr(
105 "Unable to access to the postURL: '" + postURL
106 + "'", e);
107 // Let's try with default value.
108 computedProtocol = UploadPolicy.DEFAULT_SERVER_PROTOCOL;
109 }
110 }
111 } 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 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 this.uploadPolicy
122 .setServerProtocol(UploadPolicy.DEFAULT_SERVER_PROTOCOL);
123 // Then we do the head request to the server
124 computedProtocol = new HttpConnect(this.uploadPolicy)
125 .getProtocol();
126 } 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 this.uploadPolicy.displayErr(
130 "Unknown to get protocol in the given postURL ("
131 + this.uploadPolicy.getPostURL()
132 + "), due to error: " + e.getMessage(), e);
133 }
134 }
135 this.uploadPolicy.setServerProtocol(computedProtocol);
136 }
137 }