1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 import wjhk.jupload2.gui.filepanel.FilePanel.FileListViewMode;
42
43
44
45
46
47
48
49
50 @SuppressWarnings("restriction")
51 public class JUploadContextApplet extends DefaultJUploadContext {
52
53
54
55
56 JApplet theApplet = null;
57
58
59
60
61
62
63 public JUploadContextApplet(JApplet theApplet) {
64 if (theApplet == null) {
65 throw new IllegalArgumentException("theApplet may not be null");
66 }
67 this.theApplet = theApplet;
68
69
70 checkAppletIsSigned();
71
72
73 init(findParentFrame(theApplet), theApplet);
74 }
75
76
77
78
79
80
81 void checkAppletIsSigned() {
82
83
84 java.lang.SecurityException ex = null;
85 try {
86
87
88 JFileChooser fc = new JFileChooser();
89 File currentDir = fc.getCurrentDirectory();
90 if (!currentDir.canRead()) {
91 ex = new java.lang.SecurityException("The applet must be signed (can't write in '"
92 + currentDir.getAbsolutePath() + "')");
93 }
94
95
96
97 try {
98 File tmpTestFile = File.createTempFile("jupload", "test");
99 tmpTestFile.delete();
100 } catch (IOException ioe) {
101 String msg = ioe.getClass().getName()
102 + ": Can't create temporary file (the applet is perhaps not signed (original error message: "
103 + ioe.getMessage() + ")";
104 System.out.println(msg);
105 ex = new java.lang.SecurityException(msg, ioe);
106 ioe.printStackTrace();
107 }
108 } catch (java.lang.SecurityException e) {
109 String msg = "The applet must be signed (original error message: " + e.getMessage() + ")";
110 System.out.println(msg);
111 ex = new java.lang.SecurityException(msg, e);
112 }
113
114 if (ex != null) {
115 String msg = ex.getClass().getName() + " - " + ex.getMessage();
116 JOptionPane.showMessageDialog(null, msg, "Alert", JOptionPane.ERROR_MESSAGE);
117 System.out.println(msg);
118 ex.printStackTrace();
119
120 throw ex;
121 }
122 }
123
124
125
126
127
128
129
130 private Frame findParentFrame(JApplet theApplet) {
131 Container c = theApplet;
132 while (c != null) {
133 if (c instanceof Frame)
134 return (Frame) c;
135
136 c = c.getParent();
137 }
138 return (Frame) null;
139 }
140
141
142 @Override
143 public JApplet getApplet() {
144 return this.theApplet;
145 }
146
147
148 @Override
149 public String getParameter(String key, String def) {
150 String paramStr = (this.theApplet.getParameter(key) != null ? this.theApplet.getParameter(key) : def);
151 displayDebugParameterValue(key, paramStr);
152 return paramStr;
153 }
154
155
156 @Override
157 public int getParameter(String key, int def) {
158 String paramDef = Integer.toString(def);
159 String paramStr = this.theApplet.getParameter(key) != null ? this.theApplet.getParameter(key) : paramDef;
160 displayDebugParameterValue(key, paramStr);
161 return parseInt(paramStr, def);
162 }
163
164
165 @Override
166 public float getParameter(String key, float def) {
167 String paramDef = Float.toString(def);
168 String paramStr = this.theApplet.getParameter(key) != null ? this.theApplet.getParameter(key) : paramDef;
169 displayDebugParameterValue(key, paramStr);
170 return parseFloat(paramStr, def);
171 }
172
173
174 @Override
175 public long getParameter(String key, long def) {
176 String paramDef = Long.toString(def);
177 String paramStr = this.theApplet.getParameter(key) != null ? this.theApplet.getParameter(key) : paramDef;
178 displayDebugParameterValue(key, paramStr);
179 return parseLong(paramStr, def);
180 }
181
182
183 @Override
184 public boolean getParameter(String key, boolean def) {
185 String paramDef = (def ? "true" : "false");
186 String paramStr = this.theApplet.getParameter(key) != null ? this.theApplet.getParameter(key) : paramDef;
187 displayDebugParameterValue(key, paramStr);
188 return parseBoolean(paramStr, def);
189 }
190
191
192 public FileListViewMode getParameter(String key, FileListViewMode def) {
193 String paramDef = def.toString();
194 String paramStr = this.theApplet.getParameter(key) != null ? this.theApplet.getParameter(key) : paramDef;
195 displayDebugParameterValue(key, paramStr);
196 return parseFileListViewMode(paramStr, def);
197 }
198
199
200
201
202 @Override
203 public void readCookieFromNavigator(Vector<String> headers) {
204 String cookie = null;
205
206 try {
207
208
209 cookie = (String) JSObject.getWindow(this.theApplet).eval("document.cookie");
210 uploadPolicy.displayDebug("Cookie read from the navigator: " + cookie, 80);
211 } catch (JSException e) {
212 String msg = "JSException (" + e.getClass() + ": " + e.getMessage()
213 + ") in DefaultUploadPolicy, trying default values.";
214 System.out.println(msg);
215 uploadPolicy.displayWarn(msg);
216
217
218
219
220
221
222
223 cookie = System.getProperty("debug_cookie");
224
225 msg = " no navigator found, reading 'debug_cookie' from system properties (" + cookie + ")";
226 System.out.println(msg);
227 uploadPolicy.displayDebug(msg, 80);
228
229
230
231
232
233 }
234
235
236 if (cookie != null)
237 headers.add("Cookie: " + cookie);
238 }
239
240
241
242
243 @Override
244 public void readUserAgentFromNavigator(Vector<String> headers) {
245 String userAgent = null;
246
247 try {
248
249
250 userAgent = (String) JSObject.getWindow(this.theApplet).eval("navigator.userAgent");
251 uploadPolicy.displayDebug("userAgent read from the navigator: " + userAgent, 80);
252 } catch (JSException e) {
253 String msg = "JSException (" + e.getClass() + ": " + e.getMessage()
254 + ") in DefaultUploadPolicy, trying default values.";
255 System.out.println(msg);
256 uploadPolicy.displayWarn(msg);
257
258
259
260
261
262
263
264 userAgent = System.getProperty("debug_agent");
265 msg = " no navigator found, reading 'debug_agent' from system properties (" + userAgent + ")";
266 System.out.println(msg);
267 uploadPolicy.displayDebug(msg, 80);
268
269
270
271
272 }
273
274
275 if (userAgent != null)
276 headers.add("User-Agent: " + userAgent);
277
278 }
279
280
281
282
283
284 @Override
285 public Cursor getCursor() {
286 return this.theApplet.getCursor();
287 }
288
289
290 @Override
291 public Cursor setCursor(Cursor cursor) {
292 Cursor previousCursor = this.theApplet.getCursor();
293 this.theApplet.setCursor(cursor);
294 return previousCursor;
295 }
296
297
298 @Override
299 public void showStatus(String status) {
300 this.getUploadPanel().getStatusLabel().setText(status);
301 }
302
303
304
305
306 @Override
307 public void displayURL(String url, boolean success) {
308 try {
309 if (url.toLowerCase().startsWith("javascript:")) {
310
311 String expr = url.substring(11);
312
313
314
315 expr = expr.replaceAll("%msg%",
316 Matcher.quoteReplacement(jsString(getUploadPolicy().getLastResponseMessage())));
317
318
319
320 expr = expr.replaceAll("%body%",
321 Matcher.quoteReplacement(jsString(getUploadPolicy().getLastResponseBody())));
322
323
324
325 expr = expr.replaceAll("%success%", Matcher.quoteReplacement((success) ? "true" : "false"));
326
327 displayDebug("Calling javascript expression: " + expr, 80);
328 JSObject.getWindow(this.theApplet).eval(expr);
329 } else if (success) {
330
331
332 String target = getUploadPolicy().getAfterUploadTarget();
333 if (getUploadPolicy().getDebugLevel() >= 100) {
334 getUploadPolicy().alertStr(
335 "No switch to getAfterUploadURL, because debug level is "
336 + getUploadPolicy().getDebugLevel() + " (>=100)");
337 } else {
338
339
340
341 this.theApplet.getAppletContext().showDocument(new URL(url), (null == target) ? "_self" : target);
342 }
343 }
344 } catch (Exception ee) {
345
346
347 try {
348 getUploadPolicy().displayErr(ee);
349 } catch (JUploadException e) {
350
351 ee.printStackTrace();
352 }
353 }
354 }
355
356
357
358
359
360
361
362
363 @Override
364 public String normalizeURL(String url) throws JUploadException {
365 if (null == url || url.length() == 0)
366 return this.theApplet.getDocumentBase().toString();
367 URI uri = null;
368 try {
369 uri = new URI(url);
370 if (null == uri.getScheme())
371 uri = this.theApplet.getDocumentBase().toURI().resolve(url);
372 if (!uri.getScheme().equals("http") && !uri.getScheme().equals("https") && !uri.getScheme().equals("ftp")) {
373 throw new JUploadException("URI scheme " + uri.getScheme() + " not supported.");
374 }
375 } catch (URISyntaxException e) {
376 throw new JUploadException(e);
377 }
378 return uri.toString();
379 }
380
381
382
383
384
385
386
387
388
389
390
391 public String jsString(String s) {
392 String dollarReplacement = Matcher.quoteReplacement("\\$");
393 String singleQuoteReplacement = Matcher.quoteReplacement("\\'");
394 String linefeedReplacement = Matcher.quoteReplacement("\\n");
395
396 if (s == null || s.equals("")) {
397 return "";
398 } else {
399 s = s.replaceAll("\\$", dollarReplacement);
400 s = s.replaceAll("\"", "'");
401 s = s.replaceAll("'", singleQuoteReplacement);
402 s = s.replaceAll("\n", linefeedReplacement);
403 s = s.replaceAll("\r", "");
404 return s;
405 }
406 }
407 }