source: gli/branches/rtl-gli/src/org/greenstone/gatherer/remote/RemoteGreenstoneServerAction.java@ 18361

Last change on this file since 18361 was 18361, checked in by kjdon, 15 years ago

updated the rtl-gli branch with files from trunk. Result of a merge 14807:18318

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