Changeset 18361 for gli/branches/rtl-gli
- Timestamp:
- 2009-01-12T11:19:07+13:00 (15 years ago)
- Location:
- gli/branches/rtl-gli/src/org/greenstone/gatherer/remote
- Files:
-
- 1 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
gli/branches/rtl-gli/src/org/greenstone/gatherer/remote/RemoteGreenstoneServer.java
r14601 r18361 37 37 import org.greenstone.gatherer.DebugStream; 38 38 import org.greenstone.gatherer.Dictionary; 39 import org.greenstone.gatherer.FedoraInfo; 39 40 import org.greenstone.gatherer.GAuthenticator; 40 41 import org.greenstone.gatherer.Gatherer; … … 54 55 import org.apache.commons.httpclient.HttpStatus; 55 56 57 56 58 public class RemoteGreenstoneServer 57 59 { 60 // A PasswordAuthentication object is created whenever it is required 61 static private PasswordAuthentication remote_greenstone_server_authentication = null; 62 // static private PasswordAuthentication remote_greenstone_server_authentication = new PasswordAuthentication(System.getProperty("user.name"), new char[] { }); 63 64 private ActionQueue remote_greenstone_server_action_queue; 65 private RemoteGreenstoneServer.ProgressBar progress_bar; 66 67 public RemoteGreenstoneServer() { 68 // Create the progress_bar first since ActionQueue uses it in its thread 69 // (the thread will start immediately). 70 progress_bar = new RemoteGreenstoneServer.ProgressBar(); 71 remote_greenstone_server_action_queue = new ActionQueue(); 72 } 73 58 74 // ---------------------------------------------------------------------------------------------------- 59 75 // PUBLIC LAYER 60 76 // ---------------------------------------------------------------------------------------------------- 61 62 63 static public String deleteCollection(String collection_name) 64 { 65 return performAction(new RemoteGreenstoneServerDeleteCollectionAction(collection_name)); 66 } 67 68 69 static public String deleteCollectionFile(String collection_name, File collection_file) 70 { 71 return performAction(new RemoteGreenstoneServerDeleteCollectionFileAction(collection_name, collection_file)); 72 } 73 74 75 static public String downloadCollection(String collection_name) 76 { 77 return performAction(new RemoteGreenstoneServerDownloadCollectionAction(collection_name)); 78 } 79 80 81 static public String downloadCollectionArchives(String collection_name) 82 { 83 return performAction(new RemoteGreenstoneServerDownloadCollectionArchivesAction(collection_name)); 84 } 85 86 87 static public String downloadCollectionConfigurations() 88 { 89 return performAction(new RemoteGreenstoneServerDownloadCollectionConfigurationsAction()); 90 } 91 92 93 static public String downloadCollectionFile(String collection_name, File collection_file) 94 { 95 return performAction(new RemoteGreenstoneServerDownloadCollectionFileAction(collection_name, collection_file)); 77 public String deleteCollection(String collection_name) 78 { 79 return performAction(new RemoteGreenstoneServerAction.DeleteCollectionAction(collection_name)); 80 } 81 82 83 public String deleteCollectionFile(String collection_name, File collection_file) 84 { 85 return performAction(new RemoteGreenstoneServerAction.DeleteCollectionFileAction(collection_name, collection_file)); 86 } 87 88 89 public String downloadCollection(String collection_name) 90 { 91 return performAction(new RemoteGreenstoneServerAction.DownloadCollectionAction(collection_name)); 92 } 93 94 95 public String downloadCollectionArchives(String collection_name) 96 { 97 return performAction(new RemoteGreenstoneServerAction.DownloadCollectionArchivesAction(collection_name)); 98 } 99 100 101 public String downloadCollectionConfigurations() 102 { 103 return performAction(new RemoteGreenstoneServerAction.DownloadCollectionConfigurationsAction()); 104 } 105 106 107 public String downloadCollectionFile(String collection_name, File collection_file) 108 { 109 return performAction(new RemoteGreenstoneServerAction.DownloadCollectionFileAction(collection_name, collection_file)); 96 110 } 97 111 98 112 // get web.xml from the server -- for a remote gli of GS3 99 staticpublic String downloadWebXMLFile()100 { 101 return performAction(new RemoteGreenstoneServer DownloadWebXMLFileAction());102 } 103 104 staticpublic String getScriptOptions(String script_name, String script_arguments)105 { 106 return performAction(new RemoteGreenstoneServer GetScriptOptionsAction(script_name, script_arguments));113 public String downloadWebXMLFile() 114 { 115 return performAction(new RemoteGreenstoneServerAction.DownloadWebXMLFileAction()); 116 } 117 118 public String getScriptOptions(String script_name, String script_arguments) 119 { 120 return performAction(new RemoteGreenstoneServerAction.GetScriptOptionsAction(script_name, script_arguments)); 107 121 } 108 122 109 123 //get all available site names from the server -- for a remote gli of GS3 110 static public String getSiteNames() 111 { 112 return performAction(new RemoteGreenstoneServerGetSiteNamesAction()); 113 } 114 115 static public String moveCollectionFile(String collection_name, File source_collection_file, File target_collection_file) 116 { 117 return performAction(new RemoteGreenstoneServerMoveCollectionFileAction(collection_name, source_collection_file, target_collection_file)); 118 } 119 120 121 static public String newCollectionDirectory(String collection_name, File new_collection_directory) 122 { 123 return performAction(new RemoteGreenstoneServerNewCollectionDirectoryAction(collection_name, new_collection_directory)); 124 } 125 126 127 static public String runScript(String collection_name, String script_name, String script_arguments, GShell shell) 128 { 129 return performAction(new RemoteGreenstoneServerRunScriptAction(collection_name, script_name, script_arguments, shell)); 130 } 131 132 133 static public String uploadCollectionFile(String collection_name, File collection_file) 134 { 135 return performAction(new RemoteGreenstoneServerUploadCollectionFilesAction(collection_name, new File[] { collection_file })); 136 } 137 138 139 static public String uploadCollectionFiles(String collection_name, File[] collection_files) 140 { 141 return performAction(new RemoteGreenstoneServerUploadCollectionFilesAction(collection_name, collection_files)); 142 } 143 144 145 static public String uploadFilesIntoCollection(String collection_name, File[] source_files, File target_collection_directory) 146 { 147 return performAction(new RemoteGreenstoneServerUploadFilesIntoCollectionAction(collection_name, source_files, target_collection_directory)); 148 } 149 150 151 // ---------------------------------------------------------------------------------------------------- 152 153 154 static public void exit() 124 public String getSiteNames() 125 { 126 return performAction(new RemoteGreenstoneServerAction.GetSiteNamesAction()); 127 } 128 129 public String moveCollectionFile(String collection_name, File source_collection_file, File target_collection_file) 130 { 131 return performAction(new RemoteGreenstoneServerAction.MoveCollectionFileAction( 132 collection_name, source_collection_file, target_collection_file)); 133 } 134 135 136 public String newCollectionDirectory(String collection_name, File new_collection_directory) 137 { 138 return performAction(new RemoteGreenstoneServerAction.NewCollectionDirectoryAction( 139 collection_name, new_collection_directory)); 140 } 141 142 143 public String runScript(String collection_name, String script_name, String script_arguments, GShell shell) 144 { 145 return performAction(new RemoteGreenstoneServerAction.RunScriptAction( 146 collection_name, script_name, script_arguments, shell)); 147 } 148 149 150 public String uploadCollectionFile(String collection_name, File collection_file) 151 { 152 return performAction(new RemoteGreenstoneServerAction.UploadCollectionFilesAction( 153 collection_name, new File[] { collection_file })); 154 } 155 156 157 public String uploadCollectionFiles(String collection_name, File[] collection_files) 158 { 159 return performAction(new RemoteGreenstoneServerAction.UploadCollectionFilesAction(collection_name, collection_files)); 160 } 161 162 163 public String uploadFilesIntoCollection(String collection_name, File[] source_files, File target_collection_directory) 164 { 165 return performAction(new RemoteGreenstoneServerAction.UploadFilesIntoCollectionAction( 166 collection_name, source_files, target_collection_directory)); 167 } 168 169 public boolean exists(String collection_name, File collection_file) 170 { 171 String result = performAction(new RemoteGreenstoneServerAction.ExistsAction(collection_name, collection_file)); 172 if(result.indexOf("exists") != -1) { 173 return true; 174 } 175 else if(result.indexOf("does not exist") != -1) { 176 return false; 177 } 178 return false; 179 } 180 181 public int getGreenstoneVersion() 182 { 183 // returns message "Greenstone version is: <version number of the Greenstone remote server>" 184 String result = performAction(new RemoteGreenstoneServerAction.VersionAction()); 185 int index = result.indexOf(":"); 186 if(index != -1) { 187 result = result.substring(index+1).trim(); // skip the space after colon, must remove surrounding spaces 188 } 189 int greenstoneVersion = Integer.parseInt(result); 190 return greenstoneVersion; 191 } 192 193 /** For constructing the preview command (the library URL) with */ 194 public String getLibraryURL(String serverHomeURL) 195 { 196 // returns message "Greenstone library URL suffix is: <e.g. /greenstone3/library or /gsdl/cgi-bin/library>" 197 String libSuffix = performAction(new RemoteGreenstoneServerAction.LibraryURLSuffixAction()); 198 int index = libSuffix.indexOf(":"); 199 if(index != -1) { 200 libSuffix = libSuffix.substring(index+1).trim(); // skip the space after colon and remove surrounding spaces 201 } 202 203 // serverHomeURL is of the form, http://domain/other/stuff. We want the prefix upto & including domain 204 // and prepend that to the libraryURLSuffix 205 index = -1; 206 for(int i = 0; i < 3; i++) { 207 index = serverHomeURL.indexOf("/", index+1); 208 if(index == -1) { // shouldn't happen, but if it does, we'll be in an infinite loop 209 break; 210 } 211 } 212 serverHomeURL = serverHomeURL.substring(0, index); 213 return serverHomeURL + libSuffix; 214 } 215 216 // ---------------------------------------------------------------------------------------------------- 217 218 219 public void exit() 155 220 { 156 221 System.err.println("Exiting, number of jobs on queue: " + remote_greenstone_server_action_queue.size()); … … 167 232 } 168 233 } 169 234 170 235 171 236 // ---------------------------------------------------------------------------------------------------- … … 175 240 176 241 /** Returns null if we cannot wait for the action to finish, "" if the action failed, or the action output. */ 177 static private String performAction(RemoteGreenstoneServerAction remote_greenstone_server_action) 178 { 242 private String performAction(RemoteGreenstoneServerAction remote_greenstone_server_action) 243 { 244 // Check for whether the queue thread stopped running because 245 // the user cancelled out. If so, exit GLI. 246 if(remote_greenstone_server_action_queue.hasExited()) { 247 remote_greenstone_server_action_queue.clear(); 248 //remote_greenstone_server_action_queue = null; 249 Gatherer.exit(); 250 return ""; 251 } 252 179 253 // Add the action to the queue 180 254 remote_greenstone_server_action_queue.addAction(remote_greenstone_server_action); 255 String threadName = Thread.currentThread().getName(); 181 256 182 257 // If we're running in the GUI thread we must return immediately 183 258 // We cannot wait for the action to complete because this will block any GUI updates 184 259 if (SwingUtilities.isEventDispatchThread()) { 185 System.err.println("WARNING: In event dispatch thread, returning immediately..."); 260 System.err.println(threadName 261 + "\tACTION QUEUED: In event dispatch thread, returning immediately...\n\t" 262 + remote_greenstone_server_action); 186 263 return null; 187 264 } 188 265 189 266 // Otherwise wait until the action is processed 190 while (!remote_greenstone_server_action.processed){267 try { 191 268 synchronized (remote_greenstone_server_action) { 192 try { 269 while(!remote_greenstone_server_action.processed) { 270 //System.err.println("Waiting for action to complete...: " + remote_greenstone_server_action); 193 271 DebugStream.println("Waiting for action to complete..."); 194 remote_greenstone_server_action.wait( 500);272 remote_greenstone_server_action.wait(); // wait to be notified when the action has been processed 195 273 } 196 catch (InterruptedException exception) {} 197 } 274 } 275 } catch (Exception e) { 276 System.err.println("RemoteGreenstoneServer.performAction() - exception: " + e); 277 e.printStackTrace(); 198 278 } 199 279 … … 207 287 208 288 209 static private RemoteGreenstoneServerActionQueue remote_greenstone_server_action_queue = new RemoteGreenstoneServerActionQueue();210 211 212 static private class RemoteGreenstoneServerActionQueue213 extends Thread214 {215 /** The queue of waiting jobs. */216 private ArrayList queue = null;217 218 219 public RemoteGreenstoneServerActionQueue()220 {221 if (Gatherer.isGsdlRemote) {222 queue = new ArrayList();223 start();224 }225 }226 227 228 synchronized public void addAction(RemoteGreenstoneServerAction remote_greenstone_server_action)229 {230 queue.add(remote_greenstone_server_action);231 notifyAll();232 }233 234 235 public int size()236 {237 return queue.size();238 }239 240 241 public void run()242 {243 while (true) {244 // If there are jobs on the queue, get the next in line and process it245 if (queue.size() > 0) {246 RemoteGreenstoneServerAction remote_greenstone_server_action = (RemoteGreenstoneServerAction) queue.get(0);247 248 try {249 remote_greenstone_server_action.perform();250 251 // No exceptions were thrown, so the action was successful252 remote_greenstone_server_action.processed_successfully = true;253 }254 catch (RemoteGreenstoneServerActionCancelledException exception) {255 remote_greenstone_server_action.processed_successfully = false;256 }257 catch (Exception exception) {258 DebugStream.printStackTrace(exception);259 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Error", exception.getMessage()), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.ERROR_MESSAGE);260 remote_greenstone_server_action.processed_successfully = false;261 }262 263 // We're done with this action, for better or worse264 remote_greenstone_server_action.processed = true;265 queue.remove(0);266 }267 268 // Otherwise the queue is empty269 else {270 progress_bar.setAction(null);271 272 // Wait until we are notify()ed by addAction that there is a new job on the queue273 synchronized (this) {274 try {275 wait();276 }277 catch (InterruptedException exception) { }278 }279 }280 }281 }282 }283 284 285 289 // ---------------------------------------------------------------------------------------------------- 286 290 // PROGRESS BAR 287 291 // ---------------------------------------------------------------------------------------------------- 288 292 289 290 static private RemoteGreenstoneServerProgressBar progress_bar = new RemoteGreenstoneServerProgressBar(); 291 292 293 static private class RemoteGreenstoneServerProgressBar 293 static class ProgressBar 294 294 extends JProgressBar 295 295 { 296 public RemoteGreenstoneServerProgressBar()296 public ProgressBar() 297 297 { 298 298 setBackground(Configuration.getColor("coloring.collection_tree_background", false)); … … 302 302 } 303 303 304 305 public void setAction(String action)304 /** synchronized to avoid conflicts since several threads access this */ 305 synchronized public void setAction(String action) 306 306 { 307 307 if (action != null) { … … 326 326 } 327 327 } 328 329 330 s tatic public RemoteGreenstoneServerProgressBar getProgressBar()328 329 /** synchronized to avoid conflicts since several threads access this */ 330 synchronized public RemoteGreenstoneServer.ProgressBar getProgressBar() 331 331 { 332 332 return progress_bar; … … 335 335 336 336 // ---------------------------------------------------------------------------------------------------- 337 // ACTIONS338 // ----------------------------------------------------------------------------------------------------339 340 341 static private abstract class RemoteGreenstoneServerAction342 {343 public String action_output = null;344 public boolean processed = false;345 public boolean processed_successfully;346 347 abstract public void perform()348 throws Exception;349 }350 351 352 static private class RemoteGreenstoneServerActionCancelledException353 extends Exception354 {355 }356 357 358 /**359 * --------------------------------------------------------------------------------------------360 * DELETE COLLECTION361 * --------------------------------------------------------------------------------------------362 */363 static private class RemoteGreenstoneServerDeleteCollectionAction364 extends RemoteGreenstoneServerAction365 {366 private String collection_name;367 368 public RemoteGreenstoneServerDeleteCollectionAction(String collection_name)369 {370 this.collection_name = collection_name;371 }372 373 public void perform()374 throws Exception375 {376 progress_bar.setAction("Deleting collection " + collection_name + "...");377 378 String delete_collection_command = "cmd=delete-collection";379 delete_collection_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");380 action_output = sendCommandToServer(delete_collection_command, null);381 }382 }383 384 385 /**386 * --------------------------------------------------------------------------------------------387 * DELETE COLLECTION FILE388 * --------------------------------------------------------------------------------------------389 */390 static private class RemoteGreenstoneServerDeleteCollectionFileAction391 extends RemoteGreenstoneServerAction392 {393 private String collection_name;394 private File collection_file;395 396 public RemoteGreenstoneServerDeleteCollectionFileAction(String collection_name, File collection_file)397 {398 this.collection_name = collection_name;399 this.collection_file = collection_file;400 }401 402 public void perform()403 throws Exception404 {405 String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);406 String collection_file_relative_path = getPathRelativeToDirectory(collection_file, collection_directory_path);407 collection_file_relative_path = collection_file_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");408 progress_bar.setAction("Deleting collection file " + collection_file_relative_path + "...");409 410 String delete_collection_file_command = "cmd=delete-collection-file";411 delete_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");412 delete_collection_file_command += "&file=" + URLEncoder.encode(collection_file_relative_path, "UTF-8");413 action_output = sendCommandToServer(delete_collection_file_command, null);414 }415 }416 417 418 /**419 * --------------------------------------------------------------------------------------------420 * DOWNLOAD COLLECTION421 * --------------------------------------------------------------------------------------------422 */423 static private class RemoteGreenstoneServerDownloadCollectionAction424 extends RemoteGreenstoneServerAction425 {426 private String collection_name;427 428 public RemoteGreenstoneServerDownloadCollectionAction(String collection_name)429 {430 this.collection_name = collection_name;431 }432 433 public void perform()434 throws Exception435 {436 progress_bar.setAction("Downloading remote collection " + collection_name + "...");437 438 String download_collection_command = "cmd=download-collection";439 download_collection_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");440 String zip_file_path = Gatherer.getCollectDirectoryPath() + collection_name + ".zip";441 action_output = downloadFile(download_collection_command, zip_file_path);442 443 // Delete the existing (local) collection directory444 Utility.delete(new File(CollectionManager.getCollectionDirectoryPath(collection_name)));445 446 // Unzip the collection just downloaded447 UnzipTools.unzipFile(zip_file_path, Gatherer.getCollectDirectoryPath());448 }449 }450 451 452 /**453 * --------------------------------------------------------------------------------------------454 * DOWNLOAD COLLECTION ARCHIVES455 * --------------------------------------------------------------------------------------------456 */457 static private class RemoteGreenstoneServerDownloadCollectionArchivesAction458 extends RemoteGreenstoneServerAction459 {460 private String collection_name;461 462 public RemoteGreenstoneServerDownloadCollectionArchivesAction(String collection_name)463 {464 this.collection_name = collection_name;465 }466 467 public void perform()468 throws Exception469 {470 progress_bar.setAction("Downloading collection archives for " + collection_name + "...");471 472 String download_collection_archives_command = "cmd=download-collection-archives";473 download_collection_archives_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");474 String zip_file_path = Gatherer.getCollectDirectoryPath() + collection_name + "-archives.zip";475 action_output = downloadFile(download_collection_archives_command, zip_file_path);476 477 // Delete the existing (local) collection archives478 Utility.delete(new File(CollectionManager.getLoadedCollectionArchivesDirectoryPath()));479 480 // Unzip the collection archives just downloaded481 UnzipTools.unzipFile(zip_file_path, Gatherer.getCollectDirectoryPath());482 }483 }484 485 486 /**487 * --------------------------------------------------------------------------------------------488 * DOWNLOAD COLLECTION CONFIGURATIONS489 * --------------------------------------------------------------------------------------------490 */491 static private class RemoteGreenstoneServerDownloadCollectionConfigurationsAction492 extends RemoteGreenstoneServerAction493 {494 public RemoteGreenstoneServerDownloadCollectionConfigurationsAction()495 {496 }497 498 public void perform()499 throws Exception500 {501 progress_bar.setAction("Downloading collection configurations...");502 503 // Delete the existing (local) collect directory504 Utility.delete(new File(Gatherer.getCollectDirectoryPath()));505 new File(Gatherer.getCollectDirectoryPath()).mkdirs();506 507 String download_collection_configurations_command = "cmd=download-collection-configurations";508 String zip_file_path = Gatherer.getCollectDirectoryPath() + "collections.zip";509 action_output = downloadFile(download_collection_configurations_command, zip_file_path);510 511 // Unzip the collection configurations just downloaded512 UnzipTools.unzipFile(zip_file_path, Gatherer.getCollectDirectoryPath());513 }514 }515 516 517 /**518 * --------------------------------------------------------------------------------------------519 * DOWNLOAD COLLECTION FILE520 * --------------------------------------------------------------------------------------------521 */522 static private class RemoteGreenstoneServerDownloadCollectionFileAction523 extends RemoteGreenstoneServerAction524 {525 private String collection_name;526 private File collection_file;527 528 public RemoteGreenstoneServerDownloadCollectionFileAction(String collection_name, File collection_file)529 {530 this.collection_name = collection_name;531 this.collection_file = collection_file;532 }533 534 public void perform()535 throws Exception536 {537 String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);538 String collection_file_relative_path = getPathRelativeToDirectory(collection_file, collection_directory_path);539 collection_file_relative_path = collection_file_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");540 progress_bar.setAction("Downloading collection file " + collection_file_relative_path + "...");541 542 String download_collection_file_command = "cmd=download-collection-file";543 download_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");544 download_collection_file_command += "&file=" + URLEncoder.encode(collection_file_relative_path, "UTF-8");545 String zip_file_name = collection_name + "-" + collection_file.getName() + ".zip";546 String zip_file_path = collection_directory_path + zip_file_name;547 action_output = downloadFile(download_collection_file_command, zip_file_path);548 549 // Unzip the collection file just downloaded550 UnzipTools.unzipFile(zip_file_path, collection_directory_path);551 }552 }553 554 /**555 * --------------------------------------------------------------------------------------------556 * DOWNLOAD web.xml FILE --for a remote GS3557 * --------------------------------------------------------------------------------------------558 */559 static private class RemoteGreenstoneServerDownloadWebXMLFileAction560 extends RemoteGreenstoneServerAction561 {562 public RemoteGreenstoneServerDownloadWebXMLFileAction()563 {}564 565 public void perform()566 throws Exception567 {568 String web_xml_directory_path=(Configuration.gli_user_directory_path);569 String download_web_xml_file_command = "cmd=download-web-xml-file";570 download_web_xml_file_command += "&file=" + URLEncoder.encode("web.xml", "UTF-8");571 String zip_file_name = "web-xml.zip";572 String zip_file_path = web_xml_directory_path + zip_file_name;573 action_output = downloadFile(download_web_xml_file_command, zip_file_path);574 575 // Unzip the web.xml file just downloaded576 UnzipTools.unzipFile(zip_file_path,web_xml_directory_path);577 }578 }579 580 /**581 * --------------------------------------------------------------------------------------------582 * GET SCRIPT OPTIONS583 * --------------------------------------------------------------------------------------------584 */585 static private class RemoteGreenstoneServerGetScriptOptionsAction586 extends RemoteGreenstoneServerAction587 {588 private String script_name;589 private String script_arguments;590 591 public RemoteGreenstoneServerGetScriptOptionsAction(String script_name, String script_arguments)592 {593 this.script_name = script_name;594 this.script_arguments = script_arguments;595 }596 597 public void perform()598 throws Exception599 {600 progress_bar.setAction("Getting options for " + script_name + "...");601 602 String get_script_options_command = "cmd=get-script-options";603 get_script_options_command += "&script=" + script_name;604 get_script_options_command += "&xml=";605 get_script_options_command += "&language=" + Configuration.getLanguage();606 get_script_options_command += script_arguments;607 action_output = sendCommandToServer(get_script_options_command, null);608 }609 }610 611 /**612 * --------------------------------------------------------------------------------------------613 * GET ALL NAMES OF SITES // for a remote GS3614 * --------------------------------------------------------------------------------------------615 */616 static private class RemoteGreenstoneServerGetSiteNamesAction617 extends RemoteGreenstoneServerAction618 {619 public RemoteGreenstoneServerGetSiteNamesAction()620 {}621 622 public void perform()623 throws Exception624 {625 progress_bar.setAction("Getting names of sites ...");626 627 String get_script_options_command = "cmd=get-site-names";628 action_output = sendCommandToServer(get_script_options_command, null);629 }630 }631 632 /**633 * --------------------------------------------------------------------------------------------634 * MOVE COLLECTION FILE635 * --------------------------------------------------------------------------------------------636 */637 static private class RemoteGreenstoneServerMoveCollectionFileAction638 extends RemoteGreenstoneServerAction639 {640 private String collection_name;641 private File source_collection_file;642 private File target_collection_file;643 644 public RemoteGreenstoneServerMoveCollectionFileAction(String collection_name, File source_collection_file, File target_collection_file)645 {646 this.collection_name = collection_name;647 this.source_collection_file = source_collection_file;648 this.target_collection_file = target_collection_file;649 }650 651 public void perform()652 throws Exception653 {654 String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);655 String source_collection_file_relative_path = getPathRelativeToDirectory(source_collection_file, collection_directory_path);656 source_collection_file_relative_path = source_collection_file_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");657 String target_collection_file_relative_path = getPathRelativeToDirectory(target_collection_file, collection_directory_path);658 target_collection_file_relative_path = target_collection_file_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");659 progress_bar.setAction("Moving file " + source_collection_file_relative_path + " -> " + target_collection_file_relative_path + "...");660 661 String move_collection_file_command = "cmd=move-collection-file";662 move_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");663 move_collection_file_command += "&source=" + URLEncoder.encode(source_collection_file_relative_path, "UTF-8");664 move_collection_file_command += "&target=" + URLEncoder.encode(target_collection_file_relative_path, "UTF-8");665 action_output = sendCommandToServer(move_collection_file_command, null);666 }667 }668 669 670 /**671 * --------------------------------------------------------------------------------------------672 * NEW COLLECTION DIRECTORY673 * --------------------------------------------------------------------------------------------674 */675 static private class RemoteGreenstoneServerNewCollectionDirectoryAction676 extends RemoteGreenstoneServerAction677 {678 private String collection_name;679 private File new_collection_directory;680 681 public RemoteGreenstoneServerNewCollectionDirectoryAction(String collection_name, File new_collection_directory)682 {683 this.collection_name = collection_name;684 this.new_collection_directory = new_collection_directory;685 }686 687 public void perform()688 throws Exception689 {690 String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);691 String new_collection_directory_relative_path = getPathRelativeToDirectory(new_collection_directory, collection_directory_path);692 new_collection_directory_relative_path = new_collection_directory_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");693 progress_bar.setAction("Creating new directory " + new_collection_directory_relative_path + "...");694 695 String new_collection_directory_command = "cmd=new-collection-directory";696 new_collection_directory_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");697 new_collection_directory_command += "&directory=" + URLEncoder.encode(new_collection_directory_relative_path, "UTF-8");698 action_output = sendCommandToServer(new_collection_directory_command, null);699 }700 }701 702 703 /**704 * --------------------------------------------------------------------------------------------705 * RUN SCRIPT706 * --------------------------------------------------------------------------------------------707 */708 static private class RemoteGreenstoneServerRunScriptAction709 extends RemoteGreenstoneServerAction710 {711 private String collection_name;712 private String script_name;713 private String script_arguments;714 private GShell shell;715 716 public RemoteGreenstoneServerRunScriptAction(String collection_name, String script_name, String script_arguments, GShell shell)717 {718 this.collection_name = collection_name;719 this.script_name = script_name;720 this.script_arguments = script_arguments;721 this.shell = shell;722 }723 724 public void perform()725 throws Exception726 {727 progress_bar.setAction("Running " + script_name + "...");728 729 String run_script_command = "cmd=run-script";730 run_script_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");731 run_script_command += "&script=" + script_name;732 run_script_command += "&language=" + Configuration.getLanguage();733 run_script_command += script_arguments;734 action_output = sendCommandToServer(run_script_command, shell);735 }736 }737 738 739 /**740 * --------------------------------------------------------------------------------------------741 * UPLOAD COLLECTION FILE742 * --------------------------------------------------------------------------------------------743 */744 static private class RemoteGreenstoneServerUploadCollectionFilesAction745 extends RemoteGreenstoneServerAction746 {747 private String collection_name;748 private File[] collection_files;749 750 751 public RemoteGreenstoneServerUploadCollectionFilesAction(String collection_name, File[] collection_files)752 {753 this.collection_name = collection_name;754 this.collection_files = collection_files;755 }756 757 758 public void perform()759 throws Exception760 {761 progress_bar.setAction("Uploading collection files...");762 763 // Determine the file paths relative to the collection directory764 String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);765 String[] collection_file_relative_paths = new String[collection_files.length];766 for (int i = 0; i < collection_files.length; i++) {767 collection_file_relative_paths[i] = getPathRelativeToDirectory(collection_files[i], collection_directory_path);768 }769 770 // Zip up the files to send to the server771 String zip_file_name = collection_name + "-" + System.currentTimeMillis() + ".zip";772 String zip_file_path = collection_directory_path + zip_file_name;773 ZipTools.zipFiles(zip_file_path, collection_directory_path, collection_file_relative_paths);774 775 // Upload the zip file776 String upload_collection_file_command = "cmd=upload-collection-file";777 upload_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");778 upload_collection_file_command += "&file=" + URLEncoder.encode(zip_file_name, "UTF-8");779 upload_collection_file_command += "&directory=";780 upload_collection_file_command += "&zip=true";781 action_output = uploadFile(upload_collection_file_command, zip_file_path);782 }783 }784 785 786 /**787 * --------------------------------------------------------------------------------------------788 * UPLOAD FILES INTO COLLECTION789 * --------------------------------------------------------------------------------------------790 */791 static private class RemoteGreenstoneServerUploadFilesIntoCollectionAction792 extends RemoteGreenstoneServerAction793 {794 private String collection_name;795 private File[] source_files;796 private File target_collection_directory;797 798 799 public RemoteGreenstoneServerUploadFilesIntoCollectionAction(String collection_name, File[] source_files, File target_collection_directory)800 {801 this.collection_name = collection_name;802 this.source_files = source_files;803 this.target_collection_directory = target_collection_directory;804 }805 806 807 public void perform()808 throws Exception809 {810 String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);811 String target_collection_directory_relative_path = getPathRelativeToDirectory(target_collection_directory, collection_directory_path);812 target_collection_directory_relative_path = target_collection_directory_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");813 progress_bar.setAction("Uploading files into collection...");814 815 String zip_file_name = collection_name + "-" + System.currentTimeMillis() + ".zip";816 String zip_file_path = Gatherer.getCollectDirectoryPath() + zip_file_name;817 DebugStream.println("Zip file path: " + zip_file_path);818 819 String base_directory_path = source_files[0].getParentFile().getAbsolutePath();820 DebugStream.println("Base directory path: " + base_directory_path);821 String[] source_file_relative_paths = new String[source_files.length];822 for (int i = 0; i < source_files.length; i++) {823 DebugStream.println("Source file path: " + source_files[i]);824 source_file_relative_paths[i] = getPathRelativeToDirectory(source_files[i], base_directory_path);825 }826 827 ZipTools.zipFiles(zip_file_path, base_directory_path, source_file_relative_paths);828 829 String upload_collection_file_command = "cmd=upload-collection-file";830 upload_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");831 upload_collection_file_command += "&file=" + URLEncoder.encode(zip_file_name, "UTF-8");832 upload_collection_file_command += "&directory=" + URLEncoder.encode(target_collection_directory_relative_path, "UTF-8");833 upload_collection_file_command += "&zip=true";834 action_output = uploadFile(upload_collection_file_command, zip_file_path);835 }836 }837 838 839 // ----------------------------------------------------------------------------------------------------840 337 // AUTHENTICATION LAYER 841 338 // ---------------------------------------------------------------------------------------------------- 842 843 844 static private PasswordAuthentication remote_greenstone_server_authentication = null;845 // static private PasswordAuthentication remote_greenstone_server_authentication = new PasswordAuthentication(System.getProperty("user.name"), new char[] { });846 847 static public void set_remote_greenstone_server_authentication_to_null(){848 remote_greenstone_server_authentication = null;849 }850 339 851 340 static private class RemoteGreenstoneServerAuthenticateTask … … 856 345 remote_greenstone_server_authentication = new RemoteGreenstoneServerAuthenticator().getAuthentication(); 857 346 } 858 347 859 348 860 349 static private class RemoteGreenstoneServerAuthenticator 861 350 extends GAuthenticator 862 351 { 352 public PasswordAuthentication getAuthentication(String username, String password) 353 { 354 return getPasswordAuthentication(username,password); 355 } 356 863 357 public PasswordAuthentication getAuthentication() 864 358 { … … 875 369 } 876 370 } 877 878 879 static private void authenticateUser() 880 throws RemoteGreenstoneServerActionCancelledException 371 372 public void set_remote_greenstone_server_authentication_to_null() { 373 remote_greenstone_server_authentication = null; 374 } 375 376 private void authenticateUser() 377 throws RemoteGreenstoneServerAction.ActionCancelledException 881 378 { 882 379 // If we don't have any authentication information then ask for it now … … 887 384 } 888 385 catch (Exception exception) { 386 System.err.println("Exception occurred when authenticating the user: " + exception); 889 387 DebugStream.printStackTrace(exception); 890 388 } … … 892 390 // If it is still null then the user has cancelled the authentication, so the action is cancelled 893 391 if (remote_greenstone_server_authentication == null) { 894 throw new RemoteGreenstoneServerAction CancelledException();895 } 896 } 897 } 898 899 900 staticpublic String getUsername()392 throw new RemoteGreenstoneServerAction.ActionCancelledException(); 393 } 394 } 395 } 396 397 398 public String getUsername() 901 399 { 902 400 if (remote_greenstone_server_authentication != null) { … … 914 412 915 413 /** Returns the command output if the action completed, throws some kind of exception otherwise. */ 916 static privateString downloadFile(String gliserver_args, String file_path)414 String downloadFile(String gliserver_args, String file_path) 917 415 throws Exception 918 416 { … … 938 436 if (JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Steal_Lock_Message", command_output.substring("ERROR: Collection is locked by: ".length())), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) { 939 437 // The user has decided to cancel the action 940 throw new RemoteGreenstoneServerAction CancelledException();438 throw new RemoteGreenstoneServerAction.ActionCancelledException(); 941 439 } 942 440 … … 955 453 } 956 454 957 958 /** Returns the command output if the action completed, throws some kind of exception otherwise. */ 959 static private String sendCommandToServer(String gliserver_args, GShell shell) 455 /** Returns true or false depending on whether authentication is required for the cmd 456 * string embedded in the given gliserver_args. No authentication is required for either 457 * of the commands greenstone-server-version and get-library-url-suffix. */ 458 private boolean isAuthenticationRequired(String gliserver_args) { 459 return ((gliserver_args.indexOf("greenstone-server-version") == -1) 460 && (gliserver_args.indexOf("get-library-url-suffix") == -1)); 461 } 462 463 464 /** Returns the command output if the action completed, throws some kind of exception otherwise. 465 * Package access, so that RemoteGreenstoneServerAction.java can call this. 466 */ 467 String sendCommandToServer(String gliserver_args, GShell shell) 960 468 throws Exception 961 469 { 962 470 while (true) { 471 963 472 // Check that Configuration.gliserver_url is set 964 473 if (Configuration.gliserver_url == null) { … … 967 476 968 477 // Ask for authentication information (if necessary), then perform the action 969 authenticateUser(); 478 if(isAuthenticationRequired(gliserver_args)) { 479 authenticateUser(); 480 } 970 481 String gliserver_url_string = Configuration.gliserver_url.toString(); 971 482 String command_output = sendCommandToServerInternal(gliserver_url_string, gliserver_args, shell); 972 // System.err.println("Command output: " + command_output); 973 483 974 484 // Check the first line to see if authentication has failed; if so, go around the loop again 975 485 if (command_output.startsWith("ERROR: Authentication failed:")) { … … 982 492 if (JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Steal_Lock_Message", command_output.substring("ERROR: Collection is locked by: ".length())), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) { 983 493 // The user has decided to cancel the action 984 throw new RemoteGreenstoneServerAction CancelledException();494 throw new RemoteGreenstoneServerAction.ActionCancelledException(); 985 495 } 986 496 … … 990 500 } 991 501 // Handle other types of errors by throwing an exception 992 else if (command_output.startsWith("ERROR: ")) { 502 else if (command_output.startsWith("ERROR: ")) { 993 503 throw new Exception(command_output.substring("ERROR: ".length())); 994 } 995 504 } else if (command_output.indexOf("ERROR: ") != -1) { // check if ERROR occurs anywhere else in the output 505 throw new Exception(command_output); 506 } 507 508 996 509 // There were no exceptions thrown so the action must have succeeded 997 510 return command_output; … … 1001 514 1002 515 /** Returns the command output if the action completed, throws some kind of exception otherwise. */ 1003 static privateString uploadFile(String gliserver_args, String file_path)516 String uploadFile(String gliserver_args, String file_path) 1004 517 throws Exception 1005 518 { … … 1026 539 if (JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Steal_Lock_Message", command_output.substring("ERROR: Collection is locked by: ".length())), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) { 1027 540 // The user has decided to cancel the action 1028 throw new RemoteGreenstoneServerAction CancelledException();541 throw new RemoteGreenstoneServerAction.ActionCancelledException(); 1029 542 } 1030 543 … … 1050 563 1051 564 /** Returns the command output if the action completed, throws some kind of exception otherwise. */ 1052 staticprivate String downloadFileInternal(String download_cgi, String cgi_args, String file_path)565 private String downloadFileInternal(String download_cgi, String cgi_args, String file_path) 1053 566 throws Exception 1054 567 { … … 1106 619 1107 620 /** Returns the command output if the action completed, throws some kind of exception otherwise. */ 1108 staticprivate String sendCommandToServerInternal(String gliserver_url_string, String cgi_args, GShell shell)621 private String sendCommandToServerInternal(String gliserver_url_string, String cgi_args, GShell shell) 1109 622 throws Exception 1110 623 { … … 1113 626 1114 627 // Add username and password, and a timestamp 1115 cgi_args += "&un=" + remote_greenstone_server_authentication.getUserName(); 1116 cgi_args += "&pw=" + new String(remote_greenstone_server_authentication.getPassword()); 628 if(isAuthenticationRequired(cgi_args)) { 629 cgi_args += "&un=" + remote_greenstone_server_authentication.getUserName(); 630 cgi_args += "&pw=" + new String(remote_greenstone_server_authentication.getPassword()); 631 } 1117 632 cgi_args += "&ts=" + System.currentTimeMillis(); 1118 633 if (Gatherer.GS3){ … … 1132 647 shell.fireMessage(gliserver_output_line); 1133 648 if (shell.hasSignalledStop()) { 1134 throw new RemoteGreenstoneServerAction CancelledException();649 throw new RemoteGreenstoneServerAction.ActionCancelledException(); 1135 650 } 1136 651 } … … 1143 658 } 1144 659 1145 660 1146 661 /** Returns the command output if the action completed, throws some kind of exception otherwise. */ 1147 staticprivate String uploadFileInternal(String upload_cgi, String cgi_args, String file_path)662 private String uploadFileInternal(String upload_cgi, String cgi_args, String file_path) 1148 663 throws Exception 1149 664 { 665 System.err.println("gliserver URL: " + upload_cgi); 666 System.err.println("gliserver args: " + cgi_args); 667 1150 668 //For a remote GS3 1151 669 //GS3 is running on Tomcat, and Tomcat requires a connection timeout to be set up at the client 1152 //side while uploading files. As HttpURLCon ection couldn't set the connection timeout, HttpClient.jar670 //side while uploading files. As HttpURLConnection couldn't set the connection timeout, HttpClient.jar 1153 671 //from Jakarta is applied to solve this problem only for uploading files. 1154 if (Gatherer.GS3){ 1155 System.err.println("gliserver URL: " + upload_cgi); 1156 System.err.println("gliserver args: " + cgi_args); 1157 1158 // Setup the POST method 1159 PostMethod httppost = new PostMethod(upload_cgi+"?"+cgi_args); 1160 1161 //read the zip file into a byte array 1162 InputStream in = new FileInputStream (file_path); 1163 File f = new File (file_path); // in order to get the length of the bytes array 1164 ByteArrayOutputStream out = new ByteArrayOutputStream ((int)f.length()); 1165 int r = 0; // amount read 1166 byte[] buf = new byte[1024]; 1167 while((r = in.read(buf, 0, buf.length)) != -1) { 1168 out.write(buf, 0, r); 1169 } 1170 in.close(); 1171 byte[] result = out.toByteArray(); 1172 1173 // construct the multipartrequest form 1174 PartSource partSource = new ByteArrayPartSource("zipFile", result); 1175 FilePart filePart = new FilePart("uploaded_file", partSource); 1176 1177 String[] cgi_array=cgi_args.split("&");// get parameter-value paires from cgi_args string 1178 Part[] parts=new Part[cgi_array.length+5]; 1179 parts[0]=filePart; 1180 parts[1]= new StringPart("un", remote_greenstone_server_authentication.getUserName()); 1181 parts[2]= new StringPart("pw", new String(remote_greenstone_server_authentication.getPassword())); 1182 parts[3]= new StringPart("ts", String.valueOf(System.currentTimeMillis())); 1183 parts[4]= new StringPart("site", Configuration.site_name); 1184 // find all parameters of cgi-agrs and add them into Part[] 1185 for (int i=0; i<cgi_array.length;i++){ 1186 parts[5+i]=new StringPart(cgi_array[i].substring(0,cgi_array[i].indexOf("=")),cgi_array[i].substring(cgi_array[i].indexOf("=")+1,cgi_array[i].length())); 1187 } 1188 // set MutilartRequestEntity on the POST method 1189 httppost.setRequestEntity(new MultipartRequestEntity(parts, httppost.getParams())); 1190 //set up the HttpClient connection 1191 HttpClient client=new HttpClient(); 1192 client.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false); 1193 client.getParams().setConnectionManagerTimeout(200000); //set the connection timeout 1194 // get the output of the command from the server 1195 client.executeMethod(httppost); 1196 String command_output = ""; 1197 try{ 1198 client.executeMethod(httppost); 1199 if (httppost.getStatusCode() == HttpStatus.SC_OK) { 1200 command_output = httppost.getStatusLine().toString(); 1201 } else { 1202 command_output = httppost.getStatusLine().toString(); 1203 System.out.println("Unexpected failure: " + httppost.getStatusLine().toString()); 1204 } 1205 }catch(IOException e){ 1206 e.printStackTrace(); 1207 }finally{ 1208 httppost.releaseConnection(); 1209 } 1210 return command_output; 672 if (Gatherer.GS3) { 673 674 // Setup the POST method 675 PostMethod httppost = new PostMethod(upload_cgi+"?"+cgi_args); // cgi_args: QUERY_STRING on perl server side 676 677 // construct the multipartrequest form 678 String[] cgi_array=cgi_args.split("&");// get parameter-value pairs from cgi_args string 679 Part[] parts=new Part[cgi_array.length+5]; 680 681 // The FilePart: consisting of the (cgi-arg) name and (optional) filename in the Content-Disposition 682 // of the POST request Header (see CGI.pm), and the actual zip file itself. It uses the defaults: 683 // Content-Type: application/octet-stream; charset=ISO-8859-1,Content-Transfer-Encoding: binary 684 parts[0]= new FilePart("uploaded_file", "zipFile", new File(file_path)); 685 686 parts[1]= new StringPart("un", remote_greenstone_server_authentication.getUserName()); 687 parts[2]= new StringPart("pw", new String(remote_greenstone_server_authentication.getPassword())); 688 parts[3]= new StringPart("ts", String.valueOf(System.currentTimeMillis())); 689 parts[4]= new StringPart("site", Configuration.site_name); 690 // find all parameters of cgi-args and add them into Part[] 691 for (int i=0; i<cgi_array.length;i++){ 692 parts[5+i]=new StringPart(cgi_array[i].substring(0,cgi_array[i].indexOf("=")),cgi_array[i].substring(cgi_array[i].indexOf("=")+1,cgi_array[i].length())); 693 } 694 695 // set MultipartRequestEntity on the POST method 696 httppost.setRequestEntity(new MultipartRequestEntity(parts, httppost.getParams())); 697 698 // See file gli/request.txt for the multipart request that's been generated: 699 //httppost.getRequestEntity().writeRequest(new FileOutputStream("request.txt", true)); // true: appends 700 701 //set up the HttpClient connection 702 HttpClient client=new HttpClient(); 703 client.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false); 704 client.getParams().setConnectionManagerTimeout(200000); //set the connection timeout 705 // get the output of the command from the server 706 String command_output = ""; 707 try{ 708 client.executeMethod(httppost); 709 if (httppost.getStatusCode() == HttpStatus.SC_OK) { 710 command_output = httppost.getStatusLine().toString(); 711 } else { 712 command_output = httppost.getStatusLine().toString(); 713 System.out.println("Unexpected failure: " + httppost.getStatusLine().toString()); 714 } 715 }catch(IOException e){ 716 e.printStackTrace(); 717 }finally{ 718 httppost.releaseConnection(); 719 } 720 return command_output; 1211 721 } 1212 722 1213 723 //For a remote GS2 1214 System.err.println("gliserver URL: " + upload_cgi);1215 System.err.println("gliserver args: " + cgi_args);1216 724 // Add username and password, and a timestamp 1217 725 cgi_args += "&un=" + remote_greenstone_server_authentication.getUserName(); … … 1282 790 1283 791 1284 staticpublic String getPathRelativeToDirectory(File file, String directory_path)792 public String getPathRelativeToDirectory(File file, String directory_path) 1285 793 { 1286 794 String file_path = file.getAbsolutePath();
Note:
See TracChangeset
for help on using the changeset viewer.