1 | //
|
---|
2 | // $Id: JUploadApplet.java 750 2009-05-06 14:36:50Z etienne_sf $
|
---|
3 | //
|
---|
4 | // jupload - A file upload applet.
|
---|
5 | // Copyright 2007 The JUpload Team
|
---|
6 | //
|
---|
7 | // Created: ?
|
---|
8 | // Creator: William JinHua Kwong
|
---|
9 | // Last modified: $Date: 2009-05-06 16:36:50 +0200 (mer., 06 mai 2009) $
|
---|
10 | //
|
---|
11 | // This program is free software; you can redistribute it and/or modify it under
|
---|
12 | // the terms of the GNU General Public License as published by the Free Software
|
---|
13 | // Foundation; either version 2 of the License, or (at your option) any later
|
---|
14 | // version. This program is distributed in the hope that it will be useful, but
|
---|
15 | // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
---|
16 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
---|
17 | // details. You should have received a copy of the GNU General Public License
|
---|
18 | // along with this program; if not, write to the Free Software Foundation, Inc.,
|
---|
19 | // 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
20 |
|
---|
21 | package wjhk.jupload2.context;
|
---|
22 |
|
---|
23 | import java.awt.Container;
|
---|
24 | import java.awt.Cursor;
|
---|
25 | import java.awt.Frame;
|
---|
26 | import java.io.File;
|
---|
27 | import java.io.IOException;
|
---|
28 | import java.net.URI;
|
---|
29 | import java.net.URISyntaxException;
|
---|
30 | import java.net.URL;
|
---|
31 | import java.util.Vector;
|
---|
32 | import java.util.regex.Matcher;
|
---|
33 |
|
---|
34 | import javax.swing.JApplet;
|
---|
35 | import javax.swing.JFileChooser;
|
---|
36 | import javax.swing.JOptionPane;
|
---|
37 |
|
---|
38 | import netscape.javascript.JSException;
|
---|
39 | import netscape.javascript.JSObject;
|
---|
40 | import wjhk.jupload2.exception.JUploadException;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Implementation of the Jupload Context, for an applet. One such context is
|
---|
44 | * created at run time.
|
---|
45 | *
|
---|
46 | * @see DefaultJUploadContext
|
---|
47 | * @author etienne_sf
|
---|
48 | * @version $Revision: 750 $
|
---|
49 | */
|
---|
50 | public class JUploadContextApplet extends DefaultJUploadContext {
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * The current applet. All applet parameters are reading by using this
|
---|
54 | * attribute.
|
---|
55 | */
|
---|
56 | JApplet theApplet = null;
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * The default constructor.
|
---|
60 | *
|
---|
61 | * @param theApplet
|
---|
62 | * The applet is mandatory, to read the applet parameters.
|
---|
63 | */
|
---|
64 | public JUploadContextApplet(JApplet theApplet) {
|
---|
65 | if (theApplet == null) {
|
---|
66 | throw new IllegalArgumentException("theApplet may not be null");
|
---|
67 | }
|
---|
68 | this.theApplet = theApplet;
|
---|
69 |
|
---|
70 | // The applet must be signed !
|
---|
71 | checkAppletIsSigned();
|
---|
72 |
|
---|
73 | // Let's initialize the DefaultJUploadContext.
|
---|
74 | init(findParentFrame(theApplet), theApplet);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * This method checks that the applet is signed. To do this, it just check
|
---|
79 | * that the current folder is readable, and that the applet can create
|
---|
80 | * temporary files. Should be enough.<BR>
|
---|
81 | * If anyone has a better idea ... I'll be hapy to listen to it!
|
---|
82 | */
|
---|
83 | void checkAppletIsSigned() {
|
---|
84 |
|
---|
85 | // Let's be optimistic! ;-)
|
---|
86 | java.lang.SecurityException ex = null;
|
---|
87 | try {
|
---|
88 | // I found no way to directly get the current directory (access to
|
---|
89 | // user.xxx system properties is prohibited from within applets)
|
---|
90 | JFileChooser fc = new JFileChooser();
|
---|
91 | File currentDir = fc.getCurrentDirectory();
|
---|
92 | if (!currentDir.canRead()) {
|
---|
93 | ex = new java.lang.SecurityException(
|
---|
94 | "The applet must be signed (can't write in '"
|
---|
95 | + currentDir.getAbsolutePath() + "')");
|
---|
96 | }
|
---|
97 |
|
---|
98 | // Let's now check that the applet may create a temporary file. May
|
---|
99 | // be necessary for further processing, and for logging.
|
---|
100 | try {
|
---|
101 | File tmpTestFile = File.createTempFile("jupload", "test");
|
---|
102 | tmpTestFile.delete();
|
---|
103 | } catch (IOException ioe) {
|
---|
104 | String msg = ioe.getClass().getName()
|
---|
105 | + ": Can't create temporary file (the applet is perhaps not signed (original error message: "
|
---|
106 | + ioe.getMessage() + ")";
|
---|
107 | System.out.println(msg);
|
---|
108 | ex = new java.lang.SecurityException(msg, ioe);
|
---|
109 | ioe.printStackTrace();
|
---|
110 | }
|
---|
111 | } catch (java.lang.SecurityException e) {
|
---|
112 | String msg = "The applet must be signed (original error message: "
|
---|
113 | + e.getMessage() + ")";
|
---|
114 | System.out.println(msg);
|
---|
115 | ex = new java.lang.SecurityException(msg, e);
|
---|
116 | }
|
---|
117 |
|
---|
118 | if (ex != null) {
|
---|
119 | String msg = ex.getClass().getName() + " - " + ex.getMessage();
|
---|
120 | JOptionPane.showMessageDialog(null, msg, "Alert",
|
---|
121 | JOptionPane.ERROR_MESSAGE);
|
---|
122 | System.out.println(msg);
|
---|
123 | ex.printStackTrace();
|
---|
124 |
|
---|
125 | throw ex;
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Find the JFrame which contains the applet
|
---|
131 | *
|
---|
132 | * @param theApplet
|
---|
133 | * @return
|
---|
134 | */
|
---|
135 | private Frame findParentFrame(JApplet theApplet) {
|
---|
136 | Container c = theApplet;
|
---|
137 | while (c != null) {
|
---|
138 | if (c instanceof Frame)
|
---|
139 | return (Frame) c;
|
---|
140 |
|
---|
141 | c = c.getParent();
|
---|
142 | }
|
---|
143 | return (Frame) null;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /** {@inheritDoc} */
|
---|
147 | @Override
|
---|
148 | public JApplet getApplet() {
|
---|
149 | return this.theApplet;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /** {@inheritDoc} */
|
---|
153 | @Override
|
---|
154 | public String getParameter(String key, String def) {
|
---|
155 | String paramStr = (this.theApplet.getParameter(key) != null ? this.theApplet
|
---|
156 | .getParameter(key)
|
---|
157 | : def);
|
---|
158 | displayDebugParameterValue(key, paramStr);
|
---|
159 | return paramStr;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** {@inheritDoc} */
|
---|
163 | @Override
|
---|
164 | public int getParameter(String key, int def) {
|
---|
165 | String paramDef = Integer.toString(def);
|
---|
166 | String paramStr = this.theApplet.getParameter(key) != null ? this.theApplet
|
---|
167 | .getParameter(key)
|
---|
168 | : paramDef;
|
---|
169 | displayDebugParameterValue(key, paramStr);
|
---|
170 | return parseInt(paramStr, def);
|
---|
171 | }
|
---|
172 |
|
---|
173 | /** {@inheritDoc} */
|
---|
174 | @Override
|
---|
175 | public float getParameter(String key, float def) {
|
---|
176 | String paramDef = Float.toString(def);
|
---|
177 | String paramStr = this.theApplet.getParameter(key) != null ? this.theApplet
|
---|
178 | .getParameter(key)
|
---|
179 | : paramDef;
|
---|
180 | displayDebugParameterValue(key, paramStr);
|
---|
181 | return parseFloat(paramStr, def);
|
---|
182 | }
|
---|
183 |
|
---|
184 | /** {@inheritDoc} */
|
---|
185 | @Override
|
---|
186 | public long getParameter(String key, long def) {
|
---|
187 | String paramDef = Long.toString(def);
|
---|
188 | String paramStr = this.theApplet.getParameter(key) != null ? this.theApplet
|
---|
189 | .getParameter(key)
|
---|
190 | : paramDef;
|
---|
191 | displayDebugParameterValue(key, paramStr);
|
---|
192 | return parseLong(paramStr, def);
|
---|
193 | }// getParameter(int)
|
---|
194 |
|
---|
195 | /** {@inheritDoc} */
|
---|
196 | @Override
|
---|
197 | public boolean getParameter(String key, boolean def) {
|
---|
198 | String paramDef = (def ? "true" : "false");
|
---|
199 | String paramStr = this.theApplet.getParameter(key) != null ? this.theApplet
|
---|
200 | .getParameter(key)
|
---|
201 | : paramDef;
|
---|
202 | displayDebugParameterValue(key, paramStr);
|
---|
203 | return parseBoolean(paramStr, def);
|
---|
204 | }// getParameter(boolean)
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Loads cookies, and add them to the specific headers for upload requests.
|
---|
208 | * {@inheritDoc}
|
---|
209 | */
|
---|
210 | @Override
|
---|
211 | public void readCookieFromNavigator(Vector<String> headers) {
|
---|
212 | String cookie = null;
|
---|
213 |
|
---|
214 | try {
|
---|
215 | // Patch given by Stani: corrects the use of JUpload for
|
---|
216 | // Firefox on Mac.
|
---|
217 | cookie = (String) JSObject.getWindow(this.theApplet).eval(
|
---|
218 | "document.cookie");
|
---|
219 | } catch (JSException e) {
|
---|
220 | System.out.println("JSException (" + e.getClass() + ": "
|
---|
221 | + e.getMessage()
|
---|
222 | + ") in DefaultUploadPolicy, trying default values.");
|
---|
223 |
|
---|
224 | // If we can't have access to the JS objects, we're in development :
|
---|
225 | // Let's put some 'hard value', to test the juploadContext from the
|
---|
226 | // development tool (mine is eclipse).
|
---|
227 |
|
---|
228 | // felfert: I need different values so let's make that
|
---|
229 | // configurable...
|
---|
230 | cookie = System.getProperty("debug_cookie");
|
---|
231 |
|
---|
232 | System.out
|
---|
233 | .println(" no navigator found, reading 'debug_cookie' from system properties ("
|
---|
234 | + cookie + ")");
|
---|
235 | /*
|
---|
236 | * Example of parameter when calling the JVM:
|
---|
237 | * -Ddebug_cookie="Cookie:cpg146_data=
|
---|
238 | * YTo0OntzOjI6IklEIjtzOjMyOiJhZGU3MWIxZmU4OTZjNThhZjQ5N2FiY2ZiNmFlZTUzOCI7czoyOiJhbSI7aToxO3M6NDoibGFuZyI7czo2OiJmcmVuY2giO3M6MzoibGl2IjthOjI6e2k6MDtOO2k6MTtzOjQ6IjE0ODgiO319
|
---|
239 | * "
|
---|
240 | */
|
---|
241 | }
|
---|
242 | // The cookies and user-agent will be added to the header sent by the
|
---|
243 | // juploadContext:
|
---|
244 | if (cookie != null)
|
---|
245 | headers.add("Cookie: " + cookie);
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Loads userAgent, and add it as a header to the specific headers for
|
---|
250 | * upload requests. {@inheritDoc}
|
---|
251 | */
|
---|
252 | @Override
|
---|
253 | public void readUserAgentFromNavigator(Vector<String> headers) {
|
---|
254 | String userAgent = null;
|
---|
255 |
|
---|
256 | try {
|
---|
257 | // Patch given by Stani: corrects the use of JUpload for
|
---|
258 | // Firefox on Mac.
|
---|
259 | userAgent = (String) JSObject.getWindow(this.theApplet).eval(
|
---|
260 | "navigator.userAgent");
|
---|
261 | } catch (JSException e) {
|
---|
262 | System.out.println("JSException (" + e.getClass() + ": "
|
---|
263 | + e.getMessage()
|
---|
264 | + ") in DefaultUploadPolicy, trying default values.");
|
---|
265 |
|
---|
266 | // If we can't have access to the JS objects, we're in development :
|
---|
267 | // Let's put some 'hard value', to test the juploadContext from the
|
---|
268 | // development tool (mine is eclipse).
|
---|
269 |
|
---|
270 | // felfert: I need different values so let's make that
|
---|
271 | // configurable...
|
---|
272 | userAgent = System.getProperty("debug_agent");
|
---|
273 | System.out
|
---|
274 | .println(" no navigator found, reading 'debug_agent' from system properties ("
|
---|
275 | + userAgent + ")");
|
---|
276 | /*
|
---|
277 | * Example of parameter when calling the JVM:
|
---|
278 | * -Ddebug_agent="userAgent: Mozilla/5.0 (Windows; U; Windows NT
|
---|
279 | * 5.0; fr; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3"
|
---|
280 | */
|
---|
281 | }
|
---|
282 | // The user-agent will be added to the header sent by the
|
---|
283 | // juploadContext:
|
---|
284 | if (userAgent != null)
|
---|
285 | headers.add("User-Agent: " + userAgent);
|
---|
286 |
|
---|
287 | }
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * @return The current cursor
|
---|
291 | * @see JUploadContext#setCursor(Cursor)
|
---|
292 | */
|
---|
293 | @Override
|
---|
294 | public Cursor getCursor() {
|
---|
295 | return this.theApplet.getCursor();
|
---|
296 | }
|
---|
297 |
|
---|
298 | /** @see JUploadContext#setCursor(Cursor) */
|
---|
299 | @Override
|
---|
300 | public Cursor setCursor(Cursor cursor) {
|
---|
301 | Cursor previousCursor = this.theApplet.getCursor();
|
---|
302 | this.theApplet.setCursor(cursor);
|
---|
303 | return previousCursor;
|
---|
304 | }
|
---|
305 |
|
---|
306 | /** {@inheritDoc} */
|
---|
307 | @Override
|
---|
308 | public void showStatus(String status) {
|
---|
309 | this.getUploadPanel().getStatusLabel().setText(status);
|
---|
310 | }
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * @see JUploadContext#displayURL(String, boolean)
|
---|
314 | */
|
---|
315 | @Override
|
---|
316 | public void displayURL(String url, boolean success) {
|
---|
317 | try {
|
---|
318 | if (url.toLowerCase().startsWith("javascript:")) {
|
---|
319 | // A JavaScript expression was specified. Execute it.
|
---|
320 | String expr = url.substring(11);
|
---|
321 |
|
---|
322 | // Replacement of %msg%. Will do something only if the %msg%
|
---|
323 | // string exists in expr.
|
---|
324 | expr = expr.replaceAll("%msg%", Matcher
|
---|
325 | .quoteReplacement(jsString(getUploadPolicy()
|
---|
326 | .getLastResponseMessage())));
|
---|
327 |
|
---|
328 | // Replacement of %body%. Will do something only if the
|
---|
329 | // %body% string exists in expr.
|
---|
330 | expr = expr.replaceAll("%body%", Matcher
|
---|
331 | .quoteReplacement(jsString(getUploadPolicy()
|
---|
332 | .getLastResponseBody())));
|
---|
333 |
|
---|
334 | // Replacement of %success%. Will do something only if the
|
---|
335 | // %success% string exists in expr.
|
---|
336 | expr = expr.replaceAll("%success%", Matcher
|
---|
337 | .quoteReplacement((success) ? "true" : "false"));
|
---|
338 |
|
---|
339 | displayDebug("Calling javascript expression: " + expr, 80);
|
---|
340 | JSObject.getWindow(this.theApplet).eval(expr);
|
---|
341 | } else if (success) {
|
---|
342 | // This is not a javascript URL: we change the current page
|
---|
343 | // only if no error occurred.
|
---|
344 | String target = getUploadPolicy().getAfterUploadTarget();
|
---|
345 | if (getUploadPolicy().getDebugLevel() >= 100) {
|
---|
346 | getUploadPolicy().alertStr(
|
---|
347 | "No switch to getAfterUploadURL, because debug level is "
|
---|
348 | + getUploadPolicy().getDebugLevel()
|
---|
349 | + " (>=100)");
|
---|
350 | } else {
|
---|
351 | // Let's change the current URL to edit names and
|
---|
352 | // comments, for the selected album. Ok, let's go and
|
---|
353 | // add names and comments to the newly updated pictures.
|
---|
354 | this.theApplet.getAppletContext().showDocument(
|
---|
355 | new URL(url), (null == target) ? "_self" : target);
|
---|
356 | }
|
---|
357 | }
|
---|
358 | } catch (Exception ee) {
|
---|
359 | // Oops, no navigator. We are probably in debug mode, within
|
---|
360 | // eclipse for instance.
|
---|
361 | try {
|
---|
362 | getUploadPolicy().displayErr(ee);
|
---|
363 | } catch (JUploadException e) {
|
---|
364 | // Can't use standard JUpload log mode...
|
---|
365 | ee.printStackTrace();
|
---|
366 | }
|
---|
367 | }
|
---|
368 | }
|
---|
369 |
|
---|
370 | /**
|
---|
371 | * Generates a valid URL, from a String. The generation may add the
|
---|
372 | * documentBase of the applet.
|
---|
373 | *
|
---|
374 | * @param url
|
---|
375 | * A url. Can be a path relative to the current one.
|
---|
376 | * @return The normalized URL
|
---|
377 | * @throws JUploadException
|
---|
378 | */
|
---|
379 | @Override
|
---|
380 | public String normalizeURL(String url) throws JUploadException {
|
---|
381 | if (null == url || url.length() == 0)
|
---|
382 | return this.theApplet.getDocumentBase().toString();
|
---|
383 | URI uri = null;
|
---|
384 | try {
|
---|
385 | uri = new URI(url);
|
---|
386 | if (null == uri.getScheme())
|
---|
387 | uri = this.theApplet.getDocumentBase().toURI().resolve(url);
|
---|
388 | if (!uri.getScheme().equals("http")
|
---|
389 | && !uri.getScheme().equals("https")
|
---|
390 | && !uri.getScheme().equals("ftp")) {
|
---|
391 | throw new JUploadException("URI scheme " + uri.getScheme()
|
---|
392 | + " not supported.");
|
---|
393 | }
|
---|
394 | } catch (URISyntaxException e) {
|
---|
395 | throw new JUploadException(e);
|
---|
396 | }
|
---|
397 | return uri.toString();
|
---|
398 | }
|
---|
399 |
|
---|
400 | /**
|
---|
401 | * Generate a js String, that can be written in a javascript expression.
|
---|
402 | * It's up to the caller to put the starting and ending quotes. The double
|
---|
403 | * quotes are replaced by simple quotes (to let simple quotes unchanged, as
|
---|
404 | * it may be used in common language). Thus, start and end of JS string
|
---|
405 | * should be with double quotes, when using the return of this function.
|
---|
406 | *
|
---|
407 | * @param s
|
---|
408 | * @return The transformed string, that can be written in the output, into a
|
---|
409 | * javascript string. It doesn't contain the starting and ending
|
---|
410 | * double quotes.
|
---|
411 | */
|
---|
412 | public String jsString(String s) {
|
---|
413 | String dollarReplacement = Matcher.quoteReplacement("\\$");
|
---|
414 | String singleQuoteReplacement = Matcher.quoteReplacement("\\'");
|
---|
415 | String linefeedReplacement = Matcher.quoteReplacement("\\n");
|
---|
416 |
|
---|
417 | if (s == null || s.equals("")) {
|
---|
418 | return "";
|
---|
419 | } else {
|
---|
420 | s = s.replaceAll("\\$", dollarReplacement);
|
---|
421 | s = s.replaceAll("\"", "'");
|
---|
422 | s = s.replaceAll("'", singleQuoteReplacement);
|
---|
423 | s = s.replaceAll("\n", linefeedReplacement);
|
---|
424 | s = s.replaceAll("\r", "");
|
---|
425 | return s;
|
---|
426 | }
|
---|
427 | }
|
---|
428 | }
|
---|