source: gli/trunk/src/org/greenstone/gatherer/remote/RemoteGreenstoneServerAction.java@ 18646

Last change on this file since 18646 was 18646, checked in by ak19, 15 years ago

When deleting a file from the remote GS server or checking for its existence, URL encoding the filename can pose a problem in Java if it contains special characters in some character encoding, since you need to specify the correct character encoding when URL encoding. However, base64 encoding does not require this, and is therefore used to preserve filenames for transferring to the remote server where it will be decoded to obtain the name of the file to be deleted or which needs to be checked for whether it exists.

File size: 25.6 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Greenstone Librarian Interface application, part of
5 * the Greenstone digital library suite from the New Zealand Digital
6 * Library Project at the University of Waikato, New Zealand.
7 *
8 * Author: Michael Dewsnip, NZDL Project, University of Waikato
9 *
10 * Copyright (C) 2005 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26*/
27
28package org.greenstone.gatherer.remote;
29
30import java.io.*;
31import java.net.*;
32import java.util.*;
33import java.util.zip.*;
34import javax.swing.*;
35import java.io.ByteArrayOutputStream;
36import org.greenstone.gatherer.Configuration;
37import org.greenstone.gatherer.DebugStream;
38import org.greenstone.gatherer.Dictionary;
39import org.greenstone.gatherer.FedoraInfo;
40import org.greenstone.gatherer.GAuthenticator;
41import org.greenstone.gatherer.Gatherer;
42import org.greenstone.gatherer.collection.CollectionManager;
43import org.greenstone.gatherer.feedback.Base64;
44import org.greenstone.gatherer.shell.GShell;
45import org.greenstone.gatherer.util.UnzipTools;
46import org.greenstone.gatherer.util.Utility;
47import org.apache.commons.httpclient.HttpClient;
48import org.apache.commons.httpclient.methods.PostMethod;
49import org.apache.commons.httpclient.methods.GetMethod;
50import org.apache.commons.httpclient.HttpException;
51import org.apache.commons.httpclient.methods.multipart.FilePart;
52import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
53import org.apache.commons.httpclient.methods.multipart.Part;
54import org.apache.commons.httpclient.methods.multipart.*;
55import org.apache.commons.httpclient.params.*;
56import org.apache.commons.httpclient.HttpStatus;
57
58
59// Code moved here from RemoteGreenstoneServer.java
60// ----------------------------------------------------------------------------------------------------
61// ACTIONS
62// ----------------------------------------------------------------------------------------------------
63/** RemoteGreenstoneServer Actions that can go into the remote GS server's ActionQueue.
64 * Contains many package access inner classes that are Actions.
65*/
66public abstract class RemoteGreenstoneServerAction
67{
68 public String action_output = null;
69 public boolean processed = false;
70 public boolean processed_successfully;
71
72 protected RemoteGreenstoneServer remote = null;
73 protected RemoteGreenstoneServer.ProgressBar progress_bar = null;
74
75 public RemoteGreenstoneServerAction() {
76 remote = Gatherer.remoteGreenstoneServer;
77 progress_bar = remote.getProgressBar();
78 }
79
80 abstract public void perform()
81 throws Exception;
82
83 /*
84 protected String sendCommandToServer(String gliserver_args, GShell shell)
85 throws Exception
86 {
87 return Gatherer.remoteGreenstoneServer.sendCommandToServer(gliserver_args, shell);
88 }
89
90 protected void setAction(String action) {
91 Gatherer.remoteGreenstoneServer.progress_bar.setAction(action);
92 }*/
93
94 static class ActionCancelledException
95 extends Exception
96 {
97 }
98
99 /**
100 * --------------------------------------------------------------------------------------------
101 * DELETE COLLECTION
102 * --------------------------------------------------------------------------------------------
103 */
104 static class DeleteCollectionAction
105 extends RemoteGreenstoneServerAction
106 {
107 private String collection_name;
108
109 public DeleteCollectionAction(String collection_name)
110 {
111 this.collection_name = collection_name;
112 }
113
114 public void perform()
115 throws Exception
116 {
117 progress_bar.setAction("Deleting collection " + collection_name + "...");
118
119 String delete_collection_command = "cmd=delete-collection";
120 delete_collection_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
121 action_output = remote.sendCommandToServer(delete_collection_command, null);
122 }
123 }
124
125
126 /**
127 * --------------------------------------------------------------------------------------------
128 * DELETE COLLECTION FILE
129 * --------------------------------------------------------------------------------------------
130 */
131 static class DeleteCollectionFileAction
132 extends RemoteGreenstoneServerAction
133 {
134 private String collection_name;
135 private File collection_file;
136
137 public DeleteCollectionFileAction(String collection_name, File collection_file)
138 {
139 this.collection_name = collection_name;
140 this.collection_file = collection_file;
141 }
142
143 public void perform()
144 throws Exception
145 {
146 String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
147 String collection_file_relative_path = remote.getPathRelativeToDirectory(collection_file, collection_directory_path);
148 collection_file_relative_path = collection_file_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
149 progress_bar.setAction("Deleting collection file " + collection_file_relative_path + "...");
150
151 String delete_collection_file_command = "cmd=delete-collection-file";
152 delete_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
153 delete_collection_file_command += "&file=" + Base64.encodeBytes(collection_file_relative_path.getBytes());
154 action_output = remote.sendCommandToServer(delete_collection_file_command, null);
155 }
156 }
157
158
159 /**
160 * --------------------------------------------------------------------------------------------
161 * DOWNLOAD COLLECTION
162 * --------------------------------------------------------------------------------------------
163 */
164 static class DownloadCollectionAction
165 extends RemoteGreenstoneServerAction
166 {
167 private String collection_name;
168
169 public DownloadCollectionAction(String collection_name)
170 {
171 this.collection_name = collection_name;
172 }
173
174 public void perform()
175 throws Exception
176 {
177 progress_bar.setAction("Downloading remote collection " + collection_name + "...");
178
179 String download_collection_command = "cmd=download-collection";
180 download_collection_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
181 String zip_file_path = Gatherer.getCollectDirectoryPath() + collection_name + ".zip";
182 action_output = remote.downloadFile(download_collection_command, zip_file_path);
183
184 // Delete the existing (local) collection directory
185 Utility.delete(new File(CollectionManager.getCollectionDirectoryPath(collection_name)));
186
187 // Unzip the collection just downloaded
188 UnzipTools.unzipFile(zip_file_path, Gatherer.getCollectDirectoryPath());
189 }
190 }
191
192
193 /**
194 * --------------------------------------------------------------------------------------------
195 * DOWNLOAD COLLECTION ARCHIVES
196 * --------------------------------------------------------------------------------------------
197 */
198 static class DownloadCollectionArchivesAction
199 extends RemoteGreenstoneServerAction
200 {
201 private String collection_name;
202
203 public DownloadCollectionArchivesAction(String collection_name)
204 {
205 this.collection_name = collection_name;
206 }
207
208 public void perform()
209 throws Exception
210 {
211 progress_bar.setAction("Downloading collection archives for " + collection_name + "...");
212
213 String download_collection_archives_command = "cmd=download-collection-archives";
214 download_collection_archives_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
215 String zip_file_path = Gatherer.getCollectDirectoryPath() + collection_name + "-archives.zip";
216 action_output = remote.downloadFile(download_collection_archives_command, zip_file_path);
217
218 // Delete the existing (local) collection archives
219 Utility.delete(new File(CollectionManager.getLoadedCollectionArchivesDirectoryPath()));
220
221 // Unzip the collection archives just downloaded
222 UnzipTools.unzipFile(zip_file_path, Gatherer.getCollectDirectoryPath());
223 }
224 }
225
226
227 /**
228 * --------------------------------------------------------------------------------------------
229 * DOWNLOAD COLLECTION CONFIGURATIONS
230 * --------------------------------------------------------------------------------------------
231 */
232 static class DownloadCollectionConfigurationsAction
233 extends RemoteGreenstoneServerAction
234 {
235 public DownloadCollectionConfigurationsAction()
236 {
237 }
238
239 public void perform()
240 throws Exception
241 {
242 progress_bar.setAction("Downloading collection configurations...");
243
244 // Delete the existing (local) collect directory
245 Utility.delete(new File(Gatherer.getCollectDirectoryPath()));
246 new File(Gatherer.getCollectDirectoryPath()).mkdirs();
247
248 String download_collection_configurations_command = "cmd=download-collection-configurations";
249 String zip_file_path = Gatherer.getCollectDirectoryPath() + "collections.zip";
250 action_output = remote.downloadFile(download_collection_configurations_command, zip_file_path);
251
252 // Unzip the collection configurations just downloaded
253 UnzipTools.unzipFile(zip_file_path, Gatherer.getCollectDirectoryPath());
254 }
255 }
256
257 /**
258 * --------------------------------------------------------------------------------------------
259 * DISCOVERING WHAT VERSION THE REMOTE GREENSTONE SERVER IS (2 or 3)
260 * --------------------------------------------------------------------------------------------
261 */
262
263 static class VersionAction
264 extends RemoteGreenstoneServerAction
265 {
266 public void perform()
267 throws Exception
268 {
269 action_output = remote.sendCommandToServer("cmd=greenstone-server-version", null);
270 }
271 }
272
273 static class LibraryURLSuffixAction
274 extends RemoteGreenstoneServerAction
275 {
276 public void perform()
277 throws Exception
278 {
279 action_output = remote.sendCommandToServer("cmd=get-library-url-suffix", null);
280 }
281 }
282
283 /**
284 * --------------------------------------------------------------------------------------------
285 * CHECKING IF A FILE/FOLDER EXISTS ON SERVER SIDE
286 * --------------------------------------------------------------------------------------------
287 */
288 static class ExistsAction
289 extends RemoteGreenstoneServerAction
290 {
291 private String collection_name;
292 private File collection_file;
293
294 public ExistsAction(String collection_name, File collection_file)
295 {
296 this.collection_name = collection_name;
297 this.collection_file = collection_file;
298 }
299
300 public void perform()
301 throws Exception
302 {
303 String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
304 String collection_file_relative_path = remote.getPathRelativeToDirectory(collection_file, collection_directory_path);
305 collection_file_relative_path = collection_file_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
306 File file = new File(collection_directory_path, collection_file_relative_path);
307
308 String file_exists_command = "cmd=file-exists";
309 file_exists_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
310 // base64 encode the filename to preserve special characters
311 file_exists_command += "&file=" + Base64.encodeBytes(collection_file_relative_path.getBytes());
312
313 // returns either "File <filename> exists" or "File <filename> does not exist"
314 // for the file/folder collection_file
315 action_output = remote.sendCommandToServer(file_exists_command, null);
316 }
317 }
318
319
320 /**
321 * --------------------------------------------------------------------------------------------
322 * DOWNLOAD COLLECTION FILE
323 * --------------------------------------------------------------------------------------------
324 */
325 static class DownloadCollectionFileAction
326 extends RemoteGreenstoneServerAction
327 {
328 private String collection_name;
329 private File collection_file;
330
331 public DownloadCollectionFileAction(String collection_name, File collection_file)
332 {
333 this.collection_name = collection_name;
334 this.collection_file = collection_file;
335 }
336
337 public void perform()
338 throws Exception
339 {
340 String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
341 String collection_file_relative_path = remote.getPathRelativeToDirectory(collection_file, collection_directory_path);
342 collection_file_relative_path = collection_file_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
343 progress_bar.setAction("Downloading collection file " + collection_file_relative_path + "...");
344
345 String download_collection_file_command = "cmd=download-collection-file";
346 download_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
347 download_collection_file_command += "&file=" + URLEncoder.encode(collection_file_relative_path, "UTF-8");
348 String zip_file_name = collection_name + "-" + collection_file.getName() + ".zip";
349 String zip_file_path = collection_directory_path + zip_file_name;
350 action_output = remote.downloadFile(download_collection_file_command, zip_file_path);
351
352 // Unzip the collection file just downloaded
353 UnzipTools.unzipFile(zip_file_path, collection_directory_path);
354 }
355 }
356
357 /**
358 * --------------------------------------------------------------------------------------------
359 * DOWNLOAD web.xml FILE --for a remote GS3
360 * --------------------------------------------------------------------------------------------
361 */
362 static class DownloadWebXMLFileAction
363 extends RemoteGreenstoneServerAction
364 {
365 public DownloadWebXMLFileAction()
366 {}
367
368 public void perform()
369 throws Exception
370 {
371 String web_xml_directory_path=(Configuration.gli_user_directory_path);
372 String download_web_xml_file_command = "cmd=download-web-xml-file";
373 download_web_xml_file_command += "&file=" + URLEncoder.encode("web.xml", "UTF-8");
374 String zip_file_name = "web-xml.zip";
375 String zip_file_path = web_xml_directory_path + zip_file_name;
376 action_output = remote.downloadFile(download_web_xml_file_command, zip_file_path);
377
378 // Unzip the web.xml file just downloaded
379 UnzipTools.unzipFile(zip_file_path,web_xml_directory_path);
380 }
381 }
382
383 /**
384 * --------------------------------------------------------------------------------------------
385 * GET SCRIPT OPTIONS
386 * --------------------------------------------------------------------------------------------
387 */
388 static class GetScriptOptionsAction
389 extends RemoteGreenstoneServerAction
390 {
391 private String script_name;
392 private String script_arguments;
393
394 public GetScriptOptionsAction(String script_name, String script_arguments)
395 {
396 this.script_name = script_name;
397 this.script_arguments = script_arguments;
398 }
399
400 public void perform()
401 throws Exception
402 {
403 progress_bar.setAction("Getting options for " + script_name + "...");
404
405 String get_script_options_command = "cmd=get-script-options";
406 get_script_options_command += "&script=" + script_name;
407 get_script_options_command += "&xml=";
408 get_script_options_command += "&language=" + Configuration.getLanguage();
409 get_script_options_command += script_arguments;
410 action_output = remote.sendCommandToServer(get_script_options_command, null);
411 }
412 }
413
414 /**
415 * --------------------------------------------------------------------------------------------
416 * GET ALL NAMES OF SITES // for a remote GS3
417 * --------------------------------------------------------------------------------------------
418 */
419 static class GetSiteNamesAction
420 extends RemoteGreenstoneServerAction
421 {
422 public GetSiteNamesAction()
423 {}
424
425 public void perform()
426 throws Exception
427 {
428 progress_bar.setAction("Getting names of sites ...");
429
430 String get_script_options_command = "cmd=get-site-names";
431 action_output = remote.sendCommandToServer(get_script_options_command, null);
432 }
433 }
434
435 /**
436 * --------------------------------------------------------------------------------------------
437 * MOVE COLLECTION FILE
438 * --------------------------------------------------------------------------------------------
439 */
440 static class MoveCollectionFileAction
441 extends RemoteGreenstoneServerAction
442 {
443 private String collection_name;
444 private File source_collection_file;
445 private File target_collection_file;
446
447 public MoveCollectionFileAction(String collection_name, File source_collection_file, File target_collection_file)
448 {
449 this.collection_name = collection_name;
450 this.source_collection_file = source_collection_file;
451 this.target_collection_file = target_collection_file;
452 }
453
454 public void perform()
455 throws Exception
456 {
457 String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
458 String source_collection_file_relative_path = remote.getPathRelativeToDirectory(
459 source_collection_file, collection_directory_path);
460 source_collection_file_relative_path = source_collection_file_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
461 String target_collection_file_relative_path = remote.getPathRelativeToDirectory(
462 target_collection_file, collection_directory_path);
463 target_collection_file_relative_path = target_collection_file_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
464 progress_bar.setAction("Moving file " + source_collection_file_relative_path + " -> " + target_collection_file_relative_path + "...");
465
466 String move_collection_file_command = "cmd=move-collection-file";
467 move_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
468 move_collection_file_command += "&source=" + URLEncoder.encode(source_collection_file_relative_path, "UTF-8");
469 move_collection_file_command += "&target=" + URLEncoder.encode(target_collection_file_relative_path, "UTF-8");
470 action_output = remote.sendCommandToServer(move_collection_file_command, null);
471 }
472 }
473
474
475 /**
476 * --------------------------------------------------------------------------------------------
477 * NEW COLLECTION DIRECTORY
478 * --------------------------------------------------------------------------------------------
479 */
480 static class NewCollectionDirectoryAction
481 extends RemoteGreenstoneServerAction
482 {
483 private String collection_name;
484 private File new_collection_directory;
485
486 public NewCollectionDirectoryAction(String collection_name, File new_collection_directory)
487 {
488 this.collection_name = collection_name;
489 this.new_collection_directory = new_collection_directory;
490 }
491
492 public void perform()
493 throws Exception
494 {
495 String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
496 String new_collection_directory_relative_path = remote.getPathRelativeToDirectory(
497 new_collection_directory, collection_directory_path);
498 new_collection_directory_relative_path = new_collection_directory_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
499 progress_bar.setAction("Creating new directory " + new_collection_directory_relative_path + "...");
500
501 String new_collection_directory_command = "cmd=new-collection-directory";
502 new_collection_directory_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
503 new_collection_directory_command += "&directory=" + URLEncoder.encode(new_collection_directory_relative_path, "UTF-8");
504 action_output = remote.sendCommandToServer(new_collection_directory_command, null);
505 }
506 }
507
508
509 /**
510 * --------------------------------------------------------------------------------------------
511 * RUN SCRIPT
512 * --------------------------------------------------------------------------------------------
513 */
514 static class RunScriptAction
515 extends RemoteGreenstoneServerAction
516 {
517 private String collection_name;
518 private String script_name;
519 private String script_arguments;
520 private GShell shell;
521
522 public RunScriptAction(String collection_name, String script_name, String script_arguments, GShell shell)
523 {
524 this.collection_name = collection_name;
525 this.script_name = script_name;
526 this.script_arguments = script_arguments;
527 this.shell = shell;
528 }
529
530 public void perform()
531 throws Exception
532 {
533 progress_bar.setAction("Running " + script_name + "...");
534
535 String run_script_command = "cmd=run-script";
536 run_script_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
537 run_script_command += "&script=" + script_name;
538 run_script_command += "&language=" + Configuration.getLanguage();
539 run_script_command += script_arguments;
540 action_output = remote.sendCommandToServer(run_script_command, shell);
541 }
542 }
543
544
545 /**
546 * --------------------------------------------------------------------------------------------
547 * UPLOAD COLLECTION FILE
548 * --------------------------------------------------------------------------------------------
549 */
550 static class UploadCollectionFilesAction
551 extends RemoteGreenstoneServerAction
552 {
553 private String collection_name;
554 private File[] collection_files;
555
556
557 public UploadCollectionFilesAction(String collection_name, File[] collection_files)
558 {
559 this.collection_name = collection_name;
560 this.collection_files = collection_files;
561 }
562
563
564 public void perform()
565 throws Exception
566 {
567 progress_bar.setAction("Uploading collection files...");
568
569 // Determine the file paths relative to the collection directory
570 String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
571 String[] collection_file_relative_paths = new String[collection_files.length];
572 for (int i = 0; i < collection_files.length; i++) {
573 collection_file_relative_paths[i] = remote.getPathRelativeToDirectory(collection_files[i], collection_directory_path);
574 }
575
576 // Zip up the files to send to the server
577 String zip_file_name = collection_name + "-" + System.currentTimeMillis() + ".zip";
578 String zip_file_path = collection_directory_path + zip_file_name;
579 ZipTools.zipFiles(zip_file_path, collection_directory_path, collection_file_relative_paths);
580
581 // Upload the zip file
582 String upload_collection_file_command = "cmd=upload-collection-file";
583 upload_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
584 upload_collection_file_command += "&file=" + URLEncoder.encode(zip_file_name, "UTF-8");
585 upload_collection_file_command += "&directory=";
586 upload_collection_file_command += "&zip=true";
587 action_output = remote.uploadFile(upload_collection_file_command, zip_file_path);
588 }
589 }
590
591
592 /**
593 * --------------------------------------------------------------------------------------------
594 * UPLOAD FILES INTO COLLECTION
595 * --------------------------------------------------------------------------------------------
596 */
597 static class UploadFilesIntoCollectionAction
598 extends RemoteGreenstoneServerAction
599 {
600 private String collection_name;
601 private File[] source_files;
602 private File target_collection_directory;
603
604
605 public UploadFilesIntoCollectionAction(String collection_name, File[] source_files, File target_collection_directory)
606 {
607 this.collection_name = collection_name;
608 this.source_files = source_files;
609 this.target_collection_directory = target_collection_directory;
610 }
611
612
613 public void perform()
614 throws Exception
615 {
616 String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
617 String target_collection_directory_relative_path = remote.getPathRelativeToDirectory(
618 target_collection_directory, collection_directory_path);
619 target_collection_directory_relative_path = target_collection_directory_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
620 progress_bar.setAction("Uploading files into collection...");
621
622 String zip_file_name = collection_name + "-" + System.currentTimeMillis() + ".zip";
623 String zip_file_path = Gatherer.getCollectDirectoryPath() + zip_file_name;
624 DebugStream.println("Zip file path: " + zip_file_path);
625
626 String base_directory_path = source_files[0].getParentFile().getAbsolutePath();
627 DebugStream.println("Base directory path: " + base_directory_path);
628 String[] source_file_relative_paths = new String[source_files.length];
629 for (int i = 0; i < source_files.length; i++) {
630 DebugStream.println("Source file path: " + source_files[i]);
631 source_file_relative_paths[i] = remote.getPathRelativeToDirectory(source_files[i], base_directory_path);
632 }
633
634 ZipTools.zipFiles(zip_file_path, base_directory_path, source_file_relative_paths);
635
636 String upload_collection_file_command = "cmd=upload-collection-file";
637 upload_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
638 upload_collection_file_command += "&file=" + URLEncoder.encode(zip_file_name, "UTF-8");
639 upload_collection_file_command += "&directory=" + URLEncoder.encode(target_collection_directory_relative_path, "UTF-8");
640 upload_collection_file_command += "&zip=true";
641 action_output = remote.uploadFile(upload_collection_file_command, zip_file_path);
642 }
643 }
644}
Note: See TracBrowser for help on using the repository browser.