Changeset 10726 for trunk/gli/src


Ignore:
Timestamp:
2005-10-14T15:19:36+13:00 (19 years ago)
Author:
mdewsnip
Message:

(MAJOR CHANGE) This is the remote Greenstone building functionality implemented for the West Yorkshire Fire and Rescue Service. It allows collections to be built without having a local version of Greenstone, using either a stand-alone version of the GLI or the applet.

The collections are stored on the server (allowing people to collaborate on collections -- but not at the same time), and only what is necessary to let the user edit the collection is downloaded. Any changes to the collection are uploaded to the server.

An access restriction mechanism is implemented which uses the standard Greenstone user database to control who has access to collections.

Location:
trunk/gli/src/org/greenstone/gatherer
Files:
4 added
18 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/Gatherer.java

    r10575 r10726  
    4747import org.greenstone.gatherer.gui.URLField;
    4848import org.greenstone.gatherer.gui.WarningDialog;
     49import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
    4950import org.greenstone.gatherer.util.JarTools;
    5051import org.greenstone.gatherer.util.StaticStrings;
     
    9394    static public boolean isApplet = false;
    9495    static public boolean isGsdlRemote = false;
    95     static public String cgiBase = null;
    9696
    9797    // feedback stuff
     
    293293        if (Configuration.gliserver_url != null) {
    294294            gliserver_url_string = Configuration.gliserver_url.toString();
    295             cgiBase = gliserver_url_string.substring(0, gliserver_url_string.lastIndexOf('/') + 1);
    296295        }
    297296        }
     
    508507    if (LocalLibraryServer.isRunning() == true) {
    509508        LocalLibraryServer.stop();
     509    }
     510
     511    // If we're using a remote Greenstone server we need to make sure that all jobs have completed first
     512    if (isGsdlRemote) {
     513        RemoteGreenstoneServer.exit();
    510514    }
    511515
  • trunk/gli/src/org/greenstone/gatherer/GathererApplet.java

    r10564 r10726  
    123123              "-gliserver_url", gliserver_url_string,
    124124              "-library_url", library_url_string,
     125              "-debug",
    125126                    };
    126127
     
    133134        //   for documented example collections so "base this on
    134135        //   existing collection" works
    135         ZipTools.unzipFromJar("collect.zip", Gatherer.getGLIUserDirectoryPath());
     136        unzipFromJar("collect.zip", Gatherer.getGLIUserDirectoryPath());
    136137        }
    137138        else {
     
    146147    if (!metadata_directory.exists()) {
    147148        // dig out metadata.zip from JAR file and unzip it
    148         ZipTools.unzipFromJar("metadata.zip", Gatherer.getGLIDirectoryPath());
     149        unzipFromJar("metadata.zip", Gatherer.getGLIDirectoryPath());
    149150    }
    150151
     
    159160
    160161
    161 
    162162    public void start()
    163163    {
     
    166166
    167167
    168 
    169168    public void stop()
    170169    {
     
    172171
    173172    }
     173
    174174
    175175    public void destroy()
     
    186186
    187187    // If there was an open collection last session, reopen it
    188     Gatherer.c_man.openCollectionFromLastTime();
     188    new OpenCollectionFromLastTimeTask().start();
     189    }
     190
     191
     192    /** This is in it's own thread because this action cannot happen on the GUI thread. */
     193    private class OpenCollectionFromLastTimeTask
     194    extends Thread
     195    {
     196    public void run()
     197    {
     198        Gatherer.c_man.openCollectionFromLastTime();
     199    }
     200    }
     201
     202
     203    /**
     204     * Method which unzips a given metadata resoure.
     205     * @param jar_zip_fname The name of the jar file as a <strong>String</strong>.
     206     * @param dst_dir The destination directory for unzipping, also as a <strong>String</strong>.
     207     * @see JarTools.extractFromJar(String, String, boolean)
     208     */
     209    static public void unzipFromJar(String jar_zip_fname, String dst_dir)
     210    {
     211    if (!dst_dir.endsWith(File.separator)) {
     212        dst_dir += File.separator;
     213    }
     214
     215    // !! TO DO: Test this !!
     216    JarTools.extractFromJar(jar_zip_fname, dst_dir, true);
     217    ZipTools.unzipFile(dst_dir + jar_zip_fname, dst_dir);
    189218    }
    190219}
  • trunk/gli/src/org/greenstone/gatherer/cdm/ClassifierManager.java

    r10576 r10726  
    4343import org.greenstone.gatherer.gui.GComboBox;
    4444import org.greenstone.gatherer.gui.GLIButton;
     45import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
    4546import org.greenstone.gatherer.util.JarTools;
    4647import org.greenstone.gatherer.util.StaticStrings;
     
    311312        StringBuffer xml = null;
    312313        if (Gatherer.isGsdlRemote) {
    313         String launch  = Gatherer.cgiBase + "launch";
    314         launch += "?cmd=classinfo.pl";
    315         launch += "&xml=&language="+lang;
    316         launch += "&class=" + getClassifierName(classifier);
    317        
    318         System.err.println("*** launch = " + launch);
    319        
    320         URL launch_url = new URL(launch);
    321         URLConnection launch_connection = launch_url.openConnection();
    322         input_stream = launch_connection.getInputStream();
    323         xml = Utility.readXMLStream(input_stream);
     314        String classinfo_output = RemoteGreenstoneServer.getScriptOptions("classinfo.pl", "&classifier=" + classifier);
     315        xml = new StringBuffer(classinfo_output);
    324316        }
    325317        else {
     
    386378
    387379        if (Gatherer.isGsdlRemote) {
    388 
    389         String lang = Configuration.getLanguage();
    390 
    391         String launch  = Gatherer.cgiBase + "launch";
    392         launch += "?cmd=classinfo.pl";
    393         launch += "&xml=&language="+lang;
    394         launch += "&listall=";
    395 
    396         System.err.println("*** launch = " + launch);
    397 
    398         try {
    399             URL launch_url = new URL(launch);
    400             URLConnection launch_connection = launch_url.openConnection();
    401             InputStream input_stream = launch_connection.getInputStream();
    402             StringBuffer xml = Utility.readXMLStream(input_stream);
    403             loadClassifiers(xml);
    404         }
    405         catch (Exception error) {
    406             System.err.println("Failed when trying to connect to : " + launch);
    407             error.printStackTrace();
    408         }
    409 
    410         }
     380        String classinfo_output = RemoteGreenstoneServer.getScriptOptions("classinfo.pl", "&listall=");
     381        loadClassifiers(new StringBuffer(classinfo_output));
     382        }
     383
    411384        else {
    412 
    413385        // Retrieve the gsdl home directory...
    414386        String directory = LocalGreenstone.getDirectoryPath();
  • trunk/gli/src/org/greenstone/gatherer/cdm/CollectionConfiguration.java

    r10611 r10726  
    511511        if (Gatherer.isGsdlRemote) {
    512512            String collection_name = Gatherer.c_man.getCollection().getName();
    513             ZipTools.zipup(Gatherer.getCollectDirectoryPath(), collection_name, "collect.cfg", null, "", "");
    514             RemoteGreenstoneServer.upload_url_zip(collection_name, "etc", "", null);
     513            RemoteGreenstoneServer.uploadCollectionFile(collection_name, collect_cfg_file);
    515514        }
    516515        }
  • trunk/gli/src/org/greenstone/gatherer/cdm/PluginManager.java

    r10576 r10726  
    4747import org.greenstone.gatherer.gui.ModalDialog;
    4848import org.greenstone.gatherer.gui.WarningDialog;
     49import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
    4950import org.greenstone.gatherer.util.JarTools;
    5051import org.greenstone.gatherer.util.StaticStrings;
     
    443444        StringBuffer xml = null;
    444445        if (Gatherer.isGsdlRemote) {
    445         String launch  = Gatherer.cgiBase + "launch";
    446         launch += "?cmd=pluginfo.pl";
    447         launch += "&xml=&language="+lang;
    448         launch += "&plug=" + getPluginName(plugin);
    449        
    450         System.err.println("*** launch = " + launch);
    451        
    452         URL launch_url = new URL(launch);
    453         URLConnection launch_connection = launch_url.openConnection();
    454         input_stream = launch_connection.getInputStream();
    455         xml = Utility.readXMLStream(input_stream);
     446        String pluginfo_output = RemoteGreenstoneServer.getScriptOptions("pluginfo.pl", "&plugin=" + plugin);
     447        xml = new StringBuffer(pluginfo_output);
    456448        }
    457449        else {
     
    514506    if(library == null) {
    515507        library = new ArrayList();
     508
    516509        if (Gatherer.isGsdlRemote) {
    517 
    518         String lang = Configuration.getLanguage();
    519 
    520         String launch  = Gatherer.cgiBase + "launch";
    521         launch += "?cmd=pluginfo.pl";
    522         launch += "&xml=&language="+lang;
    523         launch += "&listall=";
    524 
    525         System.err.println("*** launch = " + launch);
    526 
    527         try {
    528             URL launch_url = new URL(launch);
    529             URLConnection launch_connection = launch_url.openConnection();
    530             InputStream input_stream = launch_connection.getInputStream();
    531             StringBuffer xml = Utility.readXMLStream(input_stream);
    532             loadPlugins(xml);
    533         }
    534         catch (Exception error) {
    535             System.err.println("Failed when trying to connect to : " + launch);
    536             error.printStackTrace();
    537         }
    538 
    539         }
     510        String pluginfo_output = RemoteGreenstoneServer.getScriptOptions("pluginfo.pl", "&listall=");
     511        loadPlugins(new StringBuffer(pluginfo_output));
     512        }
     513
    540514        else {
    541515        // Retrieve the gsdl home directory...
  • trunk/gli/src/org/greenstone/gatherer/collection/CollectionManager.java

    r10634 r10726  
    100100    private GShellProgressMonitor import_monitor = null;
    101101
     102    /** Whether we have downloaded the collection configurations in this session, when using a remote Greenstone. */
     103    static public boolean downloaded_collection_configurations = false;
     104
    102105    /** The name of the standard lock file. */
    103106    static final public String LOCK_FILE = "gli.lck";
     
    117120    MetadataXMLFileManager.addMetadataChangedListener(this);
    118121
    119     // If using a remote Greenstone server, download the collection configurations
     122    // If using a remote Greenstone server, delete the local collect directory because it will be out of date
    120123    if (Gatherer.isGsdlRemote) {
    121         RemoteGreenstoneServer.downloadCollectionConfigurations();
     124        System.err.println("Deleting user's local collect directory...");
     125        Utility.delete(new File(Gatherer.getCollectDirectoryPath()));
     126        System.err.println("Done.");
     127        new File(Gatherer.getCollectDirectoryPath()).mkdirs();
    122128    }
    123129    }
     
    142148    public void buildCollection(boolean incremental_build)
    143149    {
    144     DebugStream.println("In CollectionManager.buildCollection(), incremental_build: " + incremental_build);
     150    System.err.println("In CollectionManager.buildCollection(), incremental_build: " + incremental_build);
     151    System.err.println("Is event dispatch thread: " + SwingUtilities.isEventDispatchThread());
    145152    building = true;
    146153
     
    175182    shell.addGShellListener(Gatherer.g_man.create_pane);
    176183    shell.start();
    177     DebugStream.println("CollectionManager.buildCollection().return");
     184    System.err.println("CollectionManager.buildCollection().return");
    178185    }
    179186
     
    217224    if (lock_file.exists()) {
    218225        System.err.println("Warning: Lockfile was not successfully deleted.");
     226    }
     227
     228    // Remove the lock file on the server
     229    if (Gatherer.isGsdlRemote) {
     230        RemoteGreenstoneServer.deleteCollectionFile(collection.getName(), lock_file);
    219231    }
    220232
     
    665677    }
    666678
    667     DebugStream.println("CollectionManager.importCollection()");
     679    System.err.println("CollectionManager.importCollection()");
     680    System.err.println("Is event dispatch thread: " + SwingUtilities.isEventDispatchThread());
    668681    //check that we can remove the old index before starting import
    669682    File index_dir = new File(getCollectionIndexDirectoryPath());
     
    709722    shell.addGShellListener(Gatherer.g_man.create_pane);
    710723    shell.start();
    711     DebugStream.println("CollectionManager.importCollection().return");
     724    System.err.println("CollectionManager.importCollection().return");
    712725
    713726    importing = false;
     
    798811    if (Gatherer.isGsdlRemote) {
    799812        String collection_name = location.substring(location.lastIndexOf(File.separator) + 1, location.length() - ".col".length());
    800         if (!RemoteGreenstoneServer.downloadCollection(collection_name)) {
     813        if (RemoteGreenstoneServer.downloadCollection(collection_name).equals("")) {
    801814        return;
    802815        }
     
    9941007    {
    9951008    // If there was an open collection last session, reopen it
    996     if (Gatherer.open_collection_file_path != null) {
     1009    if (Gatherer.open_collection_file_path != null && !Gatherer.isGsdlRemote) {
    9971010        // If we're using a remote Greenstone server we must download the collection configurations first
    998         if (Gatherer.isGsdlRemote) {
    999         RemoteGreenstoneServer.downloadCollectionConfigurations();
    1000         }
     1011        // if (Gatherer.isGsdlRemote && !CollectionManager.downloaded_collection_configurations) {
     1012        // if (RemoteGreenstoneServer.downloadCollectionConfigurations().equals("")) {
     1013            // Something went wrong downloading the collection configurations
     1014            // return;
     1015        // }
     1016
     1017        // CollectionManager.downloaded_collection_configurations = true;
     1018        // }
    10011019
    10021020        // Load the collection now
     
    10251043     */
    10261044    public synchronized void processComplete(GShellEvent event) {
    1027     DebugStream.println("CollectionManager.processComplete(" + event.getType() + ")");
     1045    System.err.println("CollectionManager.processComplete(" + event.getType() + ")");
    10281046    Gatherer.g_man.lockCollection((event.getType() == GShell.IMPORT), false);
    10291047    ///ystem.err.println("Recieved process complete event - " + event);
     
    12671285            throw new Exception(Dictionary.get("CollectionManager.Index_Not_Deleted"));
    12681286            }
     1287        }
     1288
     1289        if (Gatherer.isGsdlRemote) {
     1290            RemoteGreenstoneServer.deleteCollectionFile(collection.getName(), new File(getCollectionIndexDirectoryPath()));
     1291            RemoteGreenstoneServer.moveCollectionFile(collection.getName(), new File(getCollectionBuildingDirectoryPath()), new File(getCollectionIndexDirectoryPath()));
    12691292        }
    12701293
  • trunk/gli/src/org/greenstone/gatherer/collection/ScriptOptions.java

    r10577 r10726  
    1111import org.greenstone.gatherer.LocalGreenstone;
    1212import org.greenstone.gatherer.cdm.Argument;
     13import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
    1314import org.greenstone.gatherer.util.ArrayTools;
    1415import org.greenstone.gatherer.util.Codec;
     
    291292        Document document;
    292293        if (Gatherer.isGsdlRemote) {
    293         String launch  = Gatherer.cgiBase + "launch";
    294         launch += "?cmd=" + filename;
    295         launch += "&xml=&language="+lang;
    296 
    297         System.err.println("*** launch = " + launch);
    298 
    299         URL launch_url = new URL(launch);
    300         URLConnection launch_connection = launch_url.openConnection();
    301         input_stream = launch_connection.getInputStream();
    302         document = XMLTools.parseXML(input_stream);
     294        String script_output = RemoteGreenstoneServer.getScriptOptions(filename, "");
     295        document = XMLTools.parseXML(new StringReader(script_output));
    303296        }
    304297        else {
  • trunk/gli/src/org/greenstone/gatherer/file/FileManager.java

    r10561 r10726  
    174174            // Perform the appropriate action based on the job type (RemoteGreenstoneServer will queue)
    175175            if (type == FileJob.COPY) {
     176            // Copies: upload all the files at once in one zip file
    176177            File[] source_files = new File[source_nodes.length];
    177178            for (int i = 0; i < source_nodes.length; i++) {
     
    181182            }
    182183            else if (type == FileJob.DELETE) {
    183             RemoteGreenstoneServer.deleteCollectionFile(collection_name, source_nodes[0].getFile());
     184            // Deletes: delete each top-level file/directory one at a time
     185            for (int i = 0; i < source_nodes.length; i++) {
     186                RemoteGreenstoneServer.deleteCollectionFile(collection_name, source_nodes[i].getFile());
     187            }
    184188            }
    185189            else if (type == FileJob.MOVE) {
    186             RemoteGreenstoneServer.moveCollectionFile(collection_name, source_nodes[0].getFile(), target_node.getFile());
     190            // Moves: move each top-level file/directory one at a time
     191            for (int i = 0; i < source_nodes.length; i++) {
     192                RemoteGreenstoneServer.moveCollectionFile(collection_name, source_nodes[i].getFile(), target_node.getFile());
     193            }
    187194            }
    188195        }
     
    218225        // ... but only if it is inside the collection and we haven't already downloaded it
    219226        if (file.getAbsolutePath().startsWith(Gatherer.getCollectDirectoryPath()) && file.length() == 0) {
    220             RemoteGreenstoneServer.downloadCollectionFile(Gatherer.c_man.getCollection().getName(), file);
     227            if (RemoteGreenstoneServer.downloadCollectionFile(Gatherer.c_man.getCollection().getName(), file).equals("")) {
     228            // Something has gone wrong downloading the file
     229            return;
     230            }
    221231        }
    222232        }
     
    239249
    240250    protected void newFolderOrDummyDoc(DragTree tree, CollectionTreeNode parent_node, int type) {
    241     // Ask the user for the directories name.
    242     String extension = "";
    243     if (type == FILE_TYPE) {
    244         extension = ".nul";
    245     }
    246     NewFolderOrFilePrompt new_folder_prompt = new NewFolderOrFilePrompt(parent_node, type, extension);
    247     String name = new_folder_prompt.display();
    248     new_folder_prompt.dispose();
    249     new_folder_prompt = null;
    250     // And if the name is non-null...
    251     if(name != null) {
    252         FileSystemModel model = (FileSystemModel) tree.getModel();
    253         File folder_file = new File(parent_node.getFile(), name);
    254         //... check if it already exists.
    255         if(folder_file.exists()) {
    256         if (type == FILE_TYPE) {
    257             JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.File_Already_Exists_No_Create", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
    258         } else {
    259             JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Already_Exists", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
    260         }
    261         }
    262         // Otherwise create it.
    263         else {
    264         try {
     251    (new NewFolderOrDummyDocumentTask(tree, parent_node, type)).start();
     252    }
     253
     254
     255    private class NewFolderOrDummyDocumentTask
     256    extends Thread
     257    {
     258    private DragTree tree = null;
     259    private CollectionTreeNode parent_node = null;
     260    private int type;
     261
     262    public NewFolderOrDummyDocumentTask(DragTree tree, CollectionTreeNode parent_node, int type)
     263    {
     264        this.tree = tree;
     265        this.parent_node = parent_node;
     266        this.type = type;
     267    }
     268
     269    public void run()
     270    {
     271        // Ask the user for the directories name.
     272        String extension = "";
     273        if (type == FILE_TYPE) {
     274        extension = ".nul";
     275        }
     276
     277        NewFolderOrFilePrompt new_folder_prompt = new NewFolderOrFilePrompt(parent_node, type, extension);
     278        String name = new_folder_prompt.display();
     279        new_folder_prompt.dispose();
     280        new_folder_prompt = null;
     281
     282        // And if the name is non-null...
     283        if (name != null) {
     284        FileSystemModel model = (FileSystemModel) tree.getModel();
     285        File folder_file = new File(parent_node.getFile(), name);
     286
     287        //... check if it already exists.
     288        if (folder_file.exists()) {
    265289            if (type == FILE_TYPE) {
    266             folder_file.createNewFile();
    267             } else {
    268             folder_file.mkdirs();
    269             if (Gatherer.isGsdlRemote) {
    270                 long id = System.currentTimeMillis();
    271                 RemoteGreenstoneServer.newCollectionDirectory(Gatherer.c_man.getCollection().getName(), folder_file);
    272             }
    273             }
    274             // Update the parent node to show the new folder
    275             parent_node.refresh();
    276            
    277             // Refresh workspace tree (collection tree is done automatically)
    278             Gatherer.g_man.refreshWorkspaceTree(DragTree.COLLECTION_CONTENTS_CHANGED);
    279         } catch (Exception e) {
    280             if (type == FILE_TYPE) {
    281             JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.File_Create_Error", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
    282             } else {
    283             JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Create_Error", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
    284             }
    285         }
    286         }
     290            JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.File_Already_Exists_No_Create", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
     291            }
     292            else {
     293            JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Already_Exists", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
     294            }
     295        }
     296        // Otherwise create it.
     297        else {
     298            try {
     299            if (type == FILE_TYPE) {
     300                folder_file.createNewFile();
     301            }
     302            else {
     303                folder_file.mkdirs();
     304                if (Gatherer.isGsdlRemote) {
     305                long id = System.currentTimeMillis();
     306                RemoteGreenstoneServer.newCollectionDirectory(Gatherer.c_man.getCollection().getName(), folder_file);
     307                }
     308            }
     309
     310            // Update the parent node to show the new folder
     311            parent_node.refresh();
     312
     313            // Refresh workspace tree (collection tree is done automatically)
     314            Gatherer.g_man.refreshWorkspaceTree(DragTree.COLLECTION_CONTENTS_CHANGED);
     315            }
     316            catch (Exception exception) {
     317            if (type == FILE_TYPE) {
     318                JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.File_Create_Error", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
     319            }
     320            else {
     321                JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("FileActions.Folder_Create_Error", name), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
     322            }
     323            }
     324        }
    287325       
    288         folder_file = null;
    289         model = null;
    290     }
    291     name = null;
     326        folder_file = null;
     327        model = null;
     328        }
     329        name = null;
     330    }
    292331    }
    293332}
  • trunk/gli/src/org/greenstone/gatherer/gui/CreatePane.java

    r10644 r10726  
    537537     */
    538538    public synchronized void processComplete(GShellEvent event) {
     539    System.err.println("In CreatePane::processComplete(" + event.getType() + ")...");
    539540    if(event.getStatus() == GShell.OK) {
    540541        if(event.getType() == GShell.BUILD) {
  • trunk/gli/src/org/greenstone/gatherer/gui/DeleteCollectionPrompt.java

    r9863 r10726  
    5050import org.greenstone.gatherer.collection.BasicCollectionConfiguration;
    5151import org.greenstone.gatherer.file.WorkspaceTree;  // !!! Don't like this here
     52import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
    5253import org.greenstone.gatherer.util.ArrayTools;
    5354import org.greenstone.gatherer.util.StaticStrings;
     
    293294    public void actionPerformed(ActionEvent event) {
    294295        // Delete the selected collection.
     296        // !! This should all go into CollectionManager !!
    295297
    296298        // First we must release it from the local library
     
    300302
    301303        if (Gatherer.isGsdlRemote) {
    302                
    303         String launch_cgi  = Gatherer.cgiBase + "launch?cmd=deldir";
    304 
    305         String col_name = collection.getShortName();
    306         String dir = ".";
    307 
    308         try {
    309 
    310             String col_name_encoded = URLEncoder.encode(col_name,"UTF-8");
    311             String dir_encoded = URLEncoder.encode(dir,"UTF-8");
    312 
    313             launch_cgi += "&c=" + col_name_encoded;
    314             launch_cgi += "&dir=" + dir_encoded;
    315        
    316             URL launch_url = new URL(launch_cgi);
    317             URLConnection launch_connection = launch_url.openConnection();
    318             InputStream stdis = launch_connection.getInputStream();
    319             InputStreamReader stdisr = new InputStreamReader(stdis, "UTF-8"  );
    320            
    321             BufferedReader stdbr = new BufferedReader(stdisr);
    322            
    323             while(true) {
    324             String line = stdbr.readLine();
    325             // ignore output of running lauch command
    326            
    327             if (line == null) { break; }
    328             }       
    329            
    330             stdbr.close();
    331         }
    332         // Exception
    333         catch (Exception exception) {
    334             DebugStream.println("Exception in GShell.runRemove() - unexpected");
    335             DebugStream.printStackTrace(exception);
    336         }
     304        RemoteGreenstoneServer.deleteCollection(collection.getShortName());
    337305        }
    338306
  • trunk/gli/src/org/greenstone/gatherer/gui/GUIManager.java

    r10587 r10726  
    5454import org.greenstone.gatherer.Gatherer;
    5555import org.greenstone.gatherer.collection.Collection;
     56import org.greenstone.gatherer.collection.CollectionManager;
    5657import org.greenstone.gatherer.file.FileOpenActionListener;
    5758import org.greenstone.gatherer.file.WorkspaceTree;
     
    394395        content_pane.add(tab_pane, BorderLayout.CENTER);
    395396
     397        // Add an extra progress bar at the bottom of every screen when using a remote Greenstone server
     398        if (Gatherer.isGsdlRemote) {
     399        JPanel remote_greenstone_server_progress_panel = new JPanel();
     400        JLabel remote_greenstone_server_progress_label = new JLabel(Dictionary.get("RemoteGreenstoneServer.Progress"));
     401        remote_greenstone_server_progress_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
     402        remote_greenstone_server_progress_panel.setLayout(new BorderLayout());
     403        remote_greenstone_server_progress_panel.add(remote_greenstone_server_progress_label, BorderLayout.WEST);
     404        remote_greenstone_server_progress_panel.add(RemoteGreenstoneServer.getProgressBar(), BorderLayout.CENTER);
     405        content_pane.add(remote_greenstone_server_progress_panel, BorderLayout.SOUTH);
     406        }
     407
    396408        // Call refresh to update all controls to reflect current collection status.
    397409        refresh(-1, Gatherer.c_man.ready());
     
    641653    {
    642654        // If using a remote Greenstone check if the collection configurations have been downloaded; do so if not
    643         if (Gatherer.isGsdlRemote) {
    644         File collect_directory = new File(Gatherer.getCollectDirectoryPath());
    645         if (!collect_directory.exists() || collect_directory.listFiles().length == 0) {
    646             if (!RemoteGreenstoneServer.downloadCollectionConfigurations()) {
    647             // Something went wrong downloading the collection configurations
    648             return;
    649             }
    650         }
     655        if (Gatherer.isGsdlRemote && !CollectionManager.downloaded_collection_configurations) {
     656        if (RemoteGreenstoneServer.downloadCollectionConfigurations().equals("")) {
     657            // Something went wrong downloading the collection configurations
     658            return;
     659        }
     660
     661        CollectionManager.downloaded_collection_configurations = true;
    651662        }
    652663
     
    712723    public void run()
    713724    {
     725        // If using a remote Greenstone check if the collection configurations have been downloaded; do so if not
     726        if (Gatherer.isGsdlRemote && !CollectionManager.downloaded_collection_configurations) {
     727        if (RemoteGreenstoneServer.downloadCollectionConfigurations().equals("")) {
     728            // Something went wrong downloading the collection configurations
     729            return;
     730        }
     731
     732        CollectionManager.downloaded_collection_configurations = true;
     733        }
     734
     735        // Create the collection details prompt from new collection prompt
    714736        NewCollectionMetadataPrompt ncm_prompt = null;
    715         // Create the collection details prompt from new collection prompt
    716737        NewCollectionDetailsPrompt ncd_prompt = new NewCollectionDetailsPrompt();
    717738        // If no previous collection was indicated as a model design, then show the metadata selection prompt from new collection prompt
  • trunk/gli/src/org/greenstone/gatherer/gui/GatherPane.java

    r10325 r10726  
    602602            explode_metadata_database = new JMenuItem(Dictionary.get("Menu.Explode_Metadata_Database"), KeyEvent.VK_E);
    603603            explode_metadata_database.addActionListener(this);
     604            explode_metadata_database.setEnabled(!Gatherer.isGsdlRemote);
    604605            add(explode_metadata_database);
    605606        }
  • trunk/gli/src/org/greenstone/gatherer/gui/MenuBar.java

    r10345 r10726  
    9797    file_cdimage = new JMenuItem();
    9898    file_cdimage.addActionListener(Gatherer.g_man);
     99    file_cdimage.setEnabled(!Gatherer.isGsdlRemote);
    99100    file_cdimage.setMnemonic(KeyEvent.VK_I);
    100101    Dictionary.registerText(file_cdimage, "Menu.File_CDimage");
     
    118119    file_exportas = new JMenuItem();
    119120    file_exportas.addActionListener(Gatherer.g_man);
     121    file_exportas.setEnabled(!Gatherer.isGsdlRemote);
    120122    file_exportas.setMnemonic(KeyEvent.VK_E);
    121123    Dictionary.registerText(file_exportas, "Menu.File_ExportAs");
  • trunk/gli/src/org/greenstone/gatherer/gui/NewCollectionDetailsPrompt.java

    r9753 r10726  
    221221    // Retrieve the first 8 non-whitespace characters of title_final.
    222222    StringBuffer name_buffer = new StringBuffer("");
    223 
    224     if (Gatherer.isGsdlRemote) {
    225         String username = System.getProperty("user.name");
    226         if ((username != null) && (username != "")) {
    227         name_buffer.append(username + "-");
    228         }
    229     }
    230223
    231224    int i = 0;
  • trunk/gli/src/org/greenstone/gatherer/gui/Preferences.java

    r10387 r10726  
    8686    private JTextArea mode_description_textarea;
    8787    private JTextField font_field;
     88    private JTextField gliserver_url_field;
    8889    private JTextField library_path_field;
    8990    private JTextField program_field;
     
    197198        Dictionary.registerTooltip(library_path_field, "Preferences.Connection.Library_Path_Tooltip");
    198199    }
    199    
     200
     201    JPanel gliserver_url_pane = null;
     202    JLabel gliserver_url_label = null;
     203    if (Gatherer.isGsdlRemote) {
     204        gliserver_url_pane = new JPanel();
     205        gliserver_url_pane.setPreferredSize(ROW_SIZE);
     206        gliserver_url_label = new JLabel();
     207        gliserver_url_label.setPreferredSize(LABEL_SIZE);
     208        String gliserver_url_string = "";
     209        if (Configuration.gliserver_url != null) {
     210        gliserver_url_string = Configuration.gliserver_url.toString();
     211        }
     212        gliserver_url_field = new JTextField(gliserver_url_string);
     213        gliserver_url_field.setCaretPosition(0);
     214        Dictionary.registerText(gliserver_url_label, "Preferences.Connection.GLIServer_URL");
     215        Dictionary.registerTooltip(gliserver_url_field, "Preferences.Connection.GLIServer_URL_Tooltip");
     216    }
    200217
    201218    JPanel site_pane = null;
     
    291308        site_combobox.addActionListener(new SiteComboboxListener());
    292309    }
     310
    293311    // Layout
    294312    program_pane.setLayout(new BorderLayout());
     
    300318    library_path_pane.add(library_path_field, BorderLayout.CENTER);
    301319
     320    if (Gatherer.isGsdlRemote) {
     321        gliserver_url_pane.setLayout(new BorderLayout());
     322        gliserver_url_pane.add(gliserver_url_label, BorderLayout.WEST);
     323        gliserver_url_pane.add(gliserver_url_field, BorderLayout.CENTER);
     324    }
     325
    302326    if (Gatherer.GS3) {
    303327        site_pane.setLayout(new BorderLayout());
     
    309333        servlet_pane.add(servlet_combobox, BorderLayout.CENTER);
    310334    }
     335
    311336    proxy_host_pane.setLayout(new BorderLayout());
    312337    proxy_host_pane.add(proxy_host_label, BorderLayout.WEST);
     
    321346    connection_pane.add(program_pane);
    322347    connection_pane.add(library_path_pane);
     348    if (Gatherer.isGsdlRemote) {
     349        connection_pane.add(gliserver_url_pane);
     350    }
    323351    if (Gatherer.GS3) {
    324352        connection_pane.add(site_pane);
     
    636664        Configuration.setString("general.library_url", true, library_url_string);
    637665
     666        if (Gatherer.isGsdlRemote) {
     667        String gliserver_url_string = gliserver_url_field.getText();
     668        if (gliserver_url_string.equals("")) {
     669            Configuration.gliserver_url = null;
     670        }
     671        else {
     672            try {
     673            Configuration.gliserver_url = new URL(gliserver_url_string);
     674            }
     675            catch (MalformedURLException exception) {
     676            DebugStream.printStackTrace(exception);
     677            }
     678        }
     679        Configuration.setString("general.gliserver_url", true, gliserver_url_string);
     680        }
     681
    638682        boolean site_changed = false;
    639683        if (Gatherer.GS3) {
  • trunk/gli/src/org/greenstone/gatherer/remote/RemoteGreenstoneServer.java

    r10562 r10726  
    66 * University of Waikato, New Zealand.
    77 *
    8  * Author: David Bainbridge, Greenstone Digital Library, University of Waikato
     8 * Author: Michael Dewsnip, NZDL Project, University of Waikato
    99 *
    1010 * Copyright (C) 2005 New Zealand Digital Library Project
     
    3030import java.io.*;
    3131import java.net.*;
     32import java.util.*;
     33import java.util.zip.*;
     34import javax.swing.*;
    3235import org.greenstone.gatherer.Configuration;
    3336import org.greenstone.gatherer.DebugStream;
     37import org.greenstone.gatherer.Dictionary;
     38import org.greenstone.gatherer.GAuthenticator;
    3439import org.greenstone.gatherer.Gatherer;
    35 import org.greenstone.gatherer.file.FileNode;
     40import org.greenstone.gatherer.collection.CollectionManager;
    3641import org.greenstone.gatherer.shell.GShell;
     42import org.greenstone.gatherer.util.Utility;
     43import org.greenstone.gatherer.util.ZipTools;
    3744
    3845
    3946public class RemoteGreenstoneServer
    4047{
    41     static public boolean deleteCollectionFile(String collection_name, File collection_file)
    42     {
    43     return true;
    44     }
    45 
    46 
    47     static public boolean downloadCollection(String collection_name)
    48     {
    49     return true;
    50     }
    51 
    52 
    53     static public boolean downloadCollectionConfigurations()
    54     {
    55     return true;
    56     }
    57 
    58 
    59     static public boolean downloadCollectionFile(String collection_name, File collection_file)
    60     {
    61     return true;
    62     }
    63 
    64 
    65     static public Boolean moveCollectionFile(String collection_name, File source_collection_file, File target_collection_file)
    66     {
    67     return null;
    68     }
    69 
    70 
    71     static public Boolean newCollectionDirectory(String collection_name, File new_collection_directory)
    72     {
    73     return null;
    74     }
    75 
    76 
    77     static public boolean uploadCollectionFile(String collection_name, File collection_file)
    78     {
    79     return true;
    80     }
    81 
    82 
    83     static public boolean uploadCollectionFiles(String collection_name, File[] collection_files)
    84     {
    85     return true;
    86     }
    87 
    88 
    89     static public Boolean uploadFilesIntoCollection(String collection_name, File[] source_files, File target_collection_directory)
    90     {
    91     return null;
    92     }
    93 
    94 
    95     static public void download_url_zip(String col_name, String dir, GShell source, String accept_expr, String reject_expr)
    96     {
    97     try {
    98         String download_cgi  = Gatherer.cgiBase + "download";
    99 
    100         // Send col_name and dir arguments to download cgi
    101         String col_name_encoded = URLEncoder.encode(col_name,"UTF-8");
    102         String dir_encoded = URLEncoder.encode(dir,"UTF-8");
    103         String cgi_args = "c=" + col_name_encoded;
    104         cgi_args += "&dir=" + dir_encoded + "&a=" + accept_expr + "&r=" + reject_expr;
    105 
    106         URL download_url = new URL(download_cgi);
    107 
    108         System.err.println("**** download cgi = '" + download_cgi + "'");
    109         System.err.println("**** cgi args = '" + cgi_args + "'");
    110 
    111         URLConnection dl_connection = download_url.openConnection();
    112         dl_connection.setDoOutput(true);       
    113         OutputStream dl_os = dl_connection.getOutputStream();
    114 
    115         PrintWriter dl_out = new PrintWriter(dl_os);
    116         dl_out.println(cgi_args);
    117         dl_out.close();
    118 
    119         // Download result from running cgi script
    120         InputStream dl_is = dl_connection.getInputStream();
    121         BufferedInputStream dl_bis = new BufferedInputStream(dl_is);
    122         DataInputStream dl_dbis = new DataInputStream(dl_bis);
    123            
    124         // set up output stream for zip download
    125         String col_dir;
    126         if (col_name.startsWith("/")) {
    127         col_dir = Configuration.gsdl_path;
     48    // ----------------------------------------------------------------------------------------------------
     49    //   PUBLIC LAYER
     50    // ----------------------------------------------------------------------------------------------------
     51
     52
     53    static public String deleteCollection(String collection_name)
     54    {
     55    return performAction(new RemoteGreenstoneServerDeleteCollectionAction(collection_name));
     56    }
     57
     58
     59    static public String deleteCollectionFile(String collection_name, File collection_file)
     60    {
     61    return performAction(new RemoteGreenstoneServerDeleteCollectionFileAction(collection_name, collection_file));
     62    }
     63
     64
     65    static public String downloadCollection(String collection_name)
     66    {
     67    return performAction(new RemoteGreenstoneServerDownloadCollectionAction(collection_name));
     68    }
     69
     70
     71    static public String downloadCollectionArchives(String collection_name)
     72    {
     73    return performAction(new RemoteGreenstoneServerDownloadCollectionArchivesAction(collection_name));
     74    }
     75
     76
     77    static public String downloadCollectionConfigurations()
     78    {
     79    return performAction(new RemoteGreenstoneServerDownloadCollectionConfigurationsAction());
     80    }
     81
     82
     83    static public String downloadCollectionFile(String collection_name, File collection_file)
     84    {
     85    return performAction(new RemoteGreenstoneServerDownloadCollectionFileAction(collection_name, collection_file));
     86    }
     87
     88
     89    static public String getScriptOptions(String script_name, String script_arguments)
     90    {
     91    return performAction(new RemoteGreenstoneServerGetScriptOptionsAction(script_name, script_arguments));
     92    }
     93
     94
     95    static public String moveCollectionFile(String collection_name, File source_collection_file, File target_collection_file)
     96    {
     97    return performAction(new RemoteGreenstoneServerMoveCollectionFileAction(collection_name, source_collection_file, target_collection_file));
     98    }
     99
     100
     101    static public String newCollectionDirectory(String collection_name, File new_collection_directory)
     102    {
     103    return performAction(new RemoteGreenstoneServerNewCollectionDirectoryAction(collection_name, new_collection_directory));
     104    }
     105
     106
     107    static public String runScript(String collection_name, String script_name, String script_arguments, GShell shell)
     108    {
     109    return performAction(new RemoteGreenstoneServerRunScriptAction(collection_name, script_name, script_arguments, shell));
     110    }
     111
     112
     113    static public String uploadCollectionFile(String collection_name, File collection_file)
     114    {
     115    return performAction(new RemoteGreenstoneServerUploadCollectionFilesAction(collection_name, new File[] { collection_file }));
     116    }
     117
     118
     119    static public String uploadCollectionFiles(String collection_name, File[] collection_files)
     120    {
     121    return performAction(new RemoteGreenstoneServerUploadCollectionFilesAction(collection_name, collection_files));
     122    }
     123
     124
     125    static public String uploadFilesIntoCollection(String collection_name, File[] source_files, File target_collection_directory)
     126    {
     127    return performAction(new RemoteGreenstoneServerUploadFilesIntoCollectionAction(collection_name, source_files, target_collection_directory));
     128    }
     129
     130
     131    // ----------------------------------------------------------------------------------------------------
     132
     133
     134    static public void exit()
     135    {
     136    System.err.println("Exiting, number of jobs on queue: " + remote_greenstone_server_action_queue.size());
     137
     138    // If there are still jobs on the queue we must wait for the jobs to finish
     139    while (remote_greenstone_server_action_queue.size() > 0) {
     140        synchronized (remote_greenstone_server_action_queue) {
     141        try {
     142            DebugStream.println("Waiting for queue to become empty...");
     143            remote_greenstone_server_action_queue.wait(500);
     144        }
     145        catch (InterruptedException exception) {}
     146        }
     147    }
     148    }
     149
     150
     151    // ----------------------------------------------------------------------------------------------------
     152    //   QUEUE LAYER
     153    // ----------------------------------------------------------------------------------------------------
     154
     155
     156    /** Returns null if we cannot wait for the action to finish, "" if the action failed, or the action output. */
     157    static private String performAction(RemoteGreenstoneServerAction remote_greenstone_server_action)
     158    {
     159    // Add the action to the queue
     160    remote_greenstone_server_action_queue.addAction(remote_greenstone_server_action);
     161
     162    // If we're running in the GUI thread we must return immediately
     163    // We cannot wait for the action to complete because this will block any GUI updates
     164    if (SwingUtilities.isEventDispatchThread()) {
     165        System.err.println("WARNING: In event dispatch thread, returning immediately...");
     166        return null;
     167    }
     168
     169    // Otherwise wait until the action is processed
     170    while (!remote_greenstone_server_action.processed) {
     171        synchronized (remote_greenstone_server_action) {
     172        try {
     173            DebugStream.println("Waiting for action to complete...");
     174            remote_greenstone_server_action.wait(500);
     175        }
     176        catch (InterruptedException exception) {}
     177        }
     178    }
     179
     180    // Return "" if the action failed
     181    if (!remote_greenstone_server_action.processed_successfully) {
     182        return "";
     183    }
     184    // Otherwise return the action output
     185    return remote_greenstone_server_action.action_output;
     186    }
     187
     188
     189    static private RemoteGreenstoneServerActionQueue remote_greenstone_server_action_queue = new RemoteGreenstoneServerActionQueue();
     190
     191
     192    static private class RemoteGreenstoneServerActionQueue
     193    extends Thread
     194    {
     195    /** The queue of waiting jobs. */
     196    private ArrayList queue = null;
     197
     198
     199    public RemoteGreenstoneServerActionQueue()
     200    {
     201        if (Gatherer.isGsdlRemote) {
     202        queue = new ArrayList();
     203        start();
     204        }
     205    }
     206
     207
     208    synchronized public void addAction(RemoteGreenstoneServerAction remote_greenstone_server_action)
     209    {
     210        queue.add(remote_greenstone_server_action);
     211        notifyAll();
     212    }
     213
     214
     215    public int size()
     216    {
     217        return queue.size();
     218    }
     219
     220
     221    public void run()
     222    {
     223        while (true) {
     224        // If there are jobs on the queue, get the next in line and process it
     225        if (queue.size() > 0) {
     226            RemoteGreenstoneServerAction remote_greenstone_server_action = (RemoteGreenstoneServerAction) queue.get(0);
     227
     228            try {
     229            remote_greenstone_server_action.perform();
     230
     231            // No exceptions were thrown, so the action was successful
     232            remote_greenstone_server_action.processed_successfully = true;
     233            }
     234            catch (RemoteGreenstoneServerActionCancelledException exception) {
     235            remote_greenstone_server_action.processed_successfully = false;
     236            }
     237            catch (Exception exception) {
     238            DebugStream.printStackTrace(exception);
     239            JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Error", exception.getMessage()), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.ERROR_MESSAGE);
     240            remote_greenstone_server_action.processed_successfully = false;
     241            }
     242
     243            // We're done with this action, for better or worse
     244            remote_greenstone_server_action.processed = true;
     245            progress_bar.setAction(null);
     246            queue.remove(0);
     247        }
     248
     249        // Otherwise the queue is empty
     250        else {
     251            // Wait until we are notify()ed by addAction that there is a new job on the queue
     252            synchronized (this) {
     253            try {
     254                wait();
     255            }
     256            catch (InterruptedException exception) { }
     257            }
     258        }
     259        }
     260    }
     261    }
     262
     263
     264    // ----------------------------------------------------------------------------------------------------
     265    //   PROGRESS BAR
     266    // ----------------------------------------------------------------------------------------------------
     267
     268
     269    static private RemoteGreenstoneServerProgressBar progress_bar = new RemoteGreenstoneServerProgressBar();
     270
     271
     272    static private class RemoteGreenstoneServerProgressBar
     273    extends JProgressBar
     274    {
     275    public RemoteGreenstoneServerProgressBar()
     276    {
     277        setBackground(Configuration.getColor("coloring.collection_tree_background", false));
     278        setForeground(Configuration.getColor("coloring.collection_tree_foreground", false));
     279        setString(Dictionary.get("FileActions.No_Activity"));
     280        setStringPainted(true);
     281    }
     282
     283
     284    public void setAction(String action)
     285    {
     286        if (action != null) {
     287        DebugStream.println(action);
     288        }
     289
     290        // We cannot call this from the GUI thread otherwise the progress bar won't start
     291        if (SwingUtilities.isEventDispatchThread()) {
     292        System.err.println("ERROR: RemoteGreenstoneServerProgressBar.setAction() called from event dispatch thread!");
     293        return;
     294        }
     295
     296        // Set the string on the progress bar, and start or stop it
     297        if (action == null) {
     298        setString(Dictionary.get("FileActions.No_Activity"));
     299        setIndeterminate(false);
    128300        }
    129301        else {
    130         col_dir = Gatherer.getCollectDirectoryPath();
    131         }
    132 
    133         String zip_fname = col_dir + col_name + ".zip";
    134         FileOutputStream zip_fos = new FileOutputStream(zip_fname);
     302        setString(action);
     303        setIndeterminate(true);
     304        }
     305    }
     306    }
     307
     308
     309    static public RemoteGreenstoneServerProgressBar getProgressBar()
     310    {
     311    return progress_bar;
     312    }
     313
     314
     315    // ----------------------------------------------------------------------------------------------------
     316    //   ACTIONS
     317    // ----------------------------------------------------------------------------------------------------
     318
     319
     320    static private abstract class RemoteGreenstoneServerAction
     321    {
     322    public String action_output = null;
     323    public boolean processed = false;
     324    public boolean processed_successfully;
     325
     326    abstract public void perform()
     327        throws Exception;
     328    }
     329
     330
     331    static private class RemoteGreenstoneServerActionCancelledException
     332    extends Exception
     333    {
     334    }
     335
     336
     337    /**
     338     * --------------------------------------------------------------------------------------------
     339     *    DELETE COLLECTION
     340     * --------------------------------------------------------------------------------------------
     341     */
     342    static private class RemoteGreenstoneServerDeleteCollectionAction
     343    extends RemoteGreenstoneServerAction
     344    {
     345    private String collection_name;
     346
     347    public RemoteGreenstoneServerDeleteCollectionAction(String collection_name)
     348    {
     349        this.collection_name = collection_name;
     350    }
     351
     352    public void perform()
     353        throws Exception
     354    {
     355        progress_bar.setAction("Deleting collection " + collection_name + "...");
     356
     357        String delete_collection_command = "cmd=delete-collection";
     358        delete_collection_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
     359        action_output = sendCommandToServer(delete_collection_command, null);
     360    }
     361    }
     362
     363
     364    /**
     365     * --------------------------------------------------------------------------------------------
     366     *    DELETE COLLECTION FILE
     367     * --------------------------------------------------------------------------------------------
     368     */
     369    static private class RemoteGreenstoneServerDeleteCollectionFileAction
     370    extends RemoteGreenstoneServerAction
     371    {
     372    private String collection_name;
     373    private File collection_file;
     374
     375    public RemoteGreenstoneServerDeleteCollectionFileAction(String collection_name, File collection_file)
     376    {
     377        this.collection_name = collection_name;
     378        this.collection_file = collection_file;
     379    }
     380
     381    public void perform()
     382        throws Exception
     383    {
     384        String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
     385        String collection_file_relative_path = getPathRelativeToDirectory(collection_file, collection_directory_path);
     386        collection_file_relative_path = collection_file_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
     387        progress_bar.setAction("Deleting collection file " + collection_file_relative_path + "...");
     388
     389        String delete_collection_file_command = "cmd=delete-collection-file";
     390        delete_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
     391        delete_collection_file_command += "&file=" + URLEncoder.encode(collection_file_relative_path, "UTF-8");
     392        action_output = sendCommandToServer(delete_collection_file_command, null);
     393    }
     394    }
     395
     396
     397    /**
     398     * --------------------------------------------------------------------------------------------
     399     *    DOWNLOAD COLLECTION
     400     * --------------------------------------------------------------------------------------------
     401     */
     402    static private class RemoteGreenstoneServerDownloadCollectionAction
     403    extends RemoteGreenstoneServerAction
     404    {
     405    private String collection_name;
     406
     407    public RemoteGreenstoneServerDownloadCollectionAction(String collection_name)
     408    {
     409        this.collection_name = collection_name;
     410    }
     411
     412    public void perform()
     413        throws Exception
     414    {
     415        progress_bar.setAction("Downloading remote collection " + collection_name + "...");
     416
     417        String download_collection_command = "cmd=download-collection";
     418        download_collection_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
     419        String zip_file_path = Gatherer.getCollectDirectoryPath() + collection_name + ".zip";
     420        action_output = downloadFile(download_collection_command, zip_file_path);
     421
     422        // Delete the existing (local) collection
     423        Gatherer.c_man.deleteCollection(collection_name);
     424
     425        // Unzip the collection just downloaded
     426        ZipTools.unzipFile(zip_file_path, Gatherer.getCollectDirectoryPath());
     427    }
     428    }
     429
     430
     431    /**
     432     * --------------------------------------------------------------------------------------------
     433     *    DOWNLOAD COLLECTION ARCHIVES
     434     * --------------------------------------------------------------------------------------------
     435     */
     436    static private class RemoteGreenstoneServerDownloadCollectionArchivesAction
     437    extends RemoteGreenstoneServerAction
     438    {
     439    private String collection_name;
     440
     441    public RemoteGreenstoneServerDownloadCollectionArchivesAction(String collection_name)
     442    {
     443        this.collection_name = collection_name;
     444    }
     445
     446    public void perform()
     447        throws Exception
     448    {
     449        progress_bar.setAction("Downloading collection archives for " + collection_name + "...");
     450
     451        String download_collection_archives_command = "cmd=download-collection-archives";
     452        download_collection_archives_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
     453        String zip_file_path = Gatherer.getCollectDirectoryPath() + collection_name + "-archives.zip";
     454        action_output = downloadFile(download_collection_archives_command, zip_file_path);
     455
     456        // Delete the existing (local) collection archives
     457        Utility.delete(new File(Gatherer.c_man.getCollectionArchivesDirectoryPath()));
     458
     459        // Unzip the collection archives just downloaded
     460        ZipTools.unzipFile(zip_file_path, Gatherer.getCollectDirectoryPath());
     461    }
     462    }
     463
     464
     465    /**
     466     * --------------------------------------------------------------------------------------------
     467     *    DOWNLOAD COLLECTION CONFIGURATIONS
     468     * --------------------------------------------------------------------------------------------
     469     */
     470    static private class RemoteGreenstoneServerDownloadCollectionConfigurationsAction
     471    extends RemoteGreenstoneServerAction
     472    {
     473    public RemoteGreenstoneServerDownloadCollectionConfigurationsAction()
     474    {
     475    }
     476
     477    public void perform()
     478        throws Exception
     479    {
     480        progress_bar.setAction("Downloading collection configurations...");
     481
     482        // Delete the existing (local) collect directory
     483        Utility.delete(new File(Gatherer.getCollectDirectoryPath()));
     484        new File(Gatherer.getCollectDirectoryPath()).mkdirs();
     485
     486        String download_collection_configurations_command = "cmd=download-collection-configurations";
     487        String zip_file_path = Gatherer.getCollectDirectoryPath() + "collections.zip";
     488        action_output = downloadFile(download_collection_configurations_command, zip_file_path);
     489
     490        // Unzip the collection configurations just downloaded
     491        ZipTools.unzipFile(zip_file_path, Gatherer.getCollectDirectoryPath());
     492    }
     493    }
     494
     495
     496    /**
     497     * --------------------------------------------------------------------------------------------
     498     *    DOWNLOAD COLLECTION FILE
     499     * --------------------------------------------------------------------------------------------
     500     */
     501    static private class RemoteGreenstoneServerDownloadCollectionFileAction
     502    extends RemoteGreenstoneServerAction
     503    {
     504    private String collection_name;
     505    private File collection_file;
     506
     507    public RemoteGreenstoneServerDownloadCollectionFileAction(String collection_name, File collection_file)
     508    {
     509        this.collection_name = collection_name;
     510        this.collection_file = collection_file;
     511    }
     512
     513    public void perform()
     514        throws Exception
     515    {
     516        String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
     517        String collection_file_relative_path = getPathRelativeToDirectory(collection_file, collection_directory_path);
     518        collection_file_relative_path = collection_file_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
     519        progress_bar.setAction("Downloading collection file " + collection_file_relative_path + "...");
     520
     521        String download_collection_file_command = "cmd=download-collection-file";
     522        download_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
     523        download_collection_file_command += "&file=" + URLEncoder.encode(collection_file_relative_path, "UTF-8");
     524        String zip_file_name = collection_name + "-" + collection_file.getName() + ".zip";
     525        String zip_file_path = collection_directory_path + zip_file_name;
     526        action_output = downloadFile(download_collection_file_command, zip_file_path);
     527
     528        // Unzip the collection file just downloaded
     529        ZipTools.unzipFile(zip_file_path, collection_directory_path);
     530    }
     531    }
     532
     533
     534    /**
     535     * --------------------------------------------------------------------------------------------
     536     *    GET SCRIPT OPTIONS
     537     * --------------------------------------------------------------------------------------------
     538     */
     539    static private class RemoteGreenstoneServerGetScriptOptionsAction
     540    extends RemoteGreenstoneServerAction
     541    {
     542    private String script_name;
     543    private String script_arguments;
     544
     545    public RemoteGreenstoneServerGetScriptOptionsAction(String script_name, String script_arguments)
     546    {
     547        this.script_name = script_name;
     548        this.script_arguments = script_arguments;
     549    }
     550
     551    public void perform()
     552        throws Exception
     553    {
     554        progress_bar.setAction("Getting options for " + script_name + "...");
     555
     556        String get_script_options_command = "cmd=get-script-options";
     557        get_script_options_command += "&script=" + script_name;
     558        get_script_options_command += "&xml=";
     559        get_script_options_command += "&language=" + Configuration.getLanguage();
     560        get_script_options_command += script_arguments;
     561        action_output = sendCommandToServer(get_script_options_command, null);
     562    }
     563    }
     564
     565
     566    /**
     567     * --------------------------------------------------------------------------------------------
     568     *    MOVE COLLECTION FILE
     569     * --------------------------------------------------------------------------------------------
     570     */
     571    static private class RemoteGreenstoneServerMoveCollectionFileAction
     572    extends RemoteGreenstoneServerAction
     573    {
     574    private String collection_name;
     575    private File source_collection_file;
     576    private File target_collection_file;
     577
     578    public RemoteGreenstoneServerMoveCollectionFileAction(String collection_name, File source_collection_file, File target_collection_file)
     579    {
     580        this.collection_name = collection_name;
     581        this.source_collection_file = source_collection_file;
     582        this.target_collection_file = target_collection_file;
     583    }
     584
     585    public void perform()
     586        throws Exception
     587    {
     588        String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
     589        String source_collection_file_relative_path = getPathRelativeToDirectory(source_collection_file, collection_directory_path);
     590        source_collection_file_relative_path = source_collection_file_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
     591        String target_collection_file_relative_path = getPathRelativeToDirectory(target_collection_file, collection_directory_path);
     592        target_collection_file_relative_path = target_collection_file_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
     593        progress_bar.setAction("Moving file " + source_collection_file_relative_path + " -> " + target_collection_file_relative_path + "...");
     594
     595        String move_collection_file_command = "cmd=move-collection-file";
     596        move_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
     597        move_collection_file_command += "&source=" + URLEncoder.encode(source_collection_file_relative_path, "UTF-8");
     598        move_collection_file_command += "&target=" + URLEncoder.encode(target_collection_file_relative_path, "UTF-8");
     599        action_output = sendCommandToServer(move_collection_file_command, null);
     600    }
     601    }
     602
     603
     604    /**
     605     * --------------------------------------------------------------------------------------------
     606     *    NEW COLLECTION DIRECTORY
     607     * --------------------------------------------------------------------------------------------
     608     */
     609    static private class RemoteGreenstoneServerNewCollectionDirectoryAction
     610    extends RemoteGreenstoneServerAction
     611    {
     612    private String collection_name;
     613    private File new_collection_directory;
     614
     615    public RemoteGreenstoneServerNewCollectionDirectoryAction(String collection_name, File new_collection_directory)
     616    {
     617        this.collection_name = collection_name;
     618        this.new_collection_directory = new_collection_directory;
     619    }
     620
     621    public void perform()
     622        throws Exception
     623    {
     624        String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
     625        String new_collection_directory_relative_path = getPathRelativeToDirectory(new_collection_directory, collection_directory_path);
     626        new_collection_directory_relative_path = new_collection_directory_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
     627        progress_bar.setAction("Creating new directory " + new_collection_directory_relative_path + "...");
     628
     629        String new_collection_directory_command = "cmd=new-collection-directory";
     630        new_collection_directory_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
     631        new_collection_directory_command += "&directory=" + URLEncoder.encode(new_collection_directory_relative_path, "UTF-8");
     632        action_output = sendCommandToServer(new_collection_directory_command, null);
     633    }
     634    }
     635
     636
     637    /**
     638     * --------------------------------------------------------------------------------------------
     639     *    RUN SCRIPT
     640     * --------------------------------------------------------------------------------------------
     641     */
     642    static private class RemoteGreenstoneServerRunScriptAction
     643    extends RemoteGreenstoneServerAction
     644    {
     645    private String collection_name;
     646    private String script_name;
     647    private String script_arguments;
     648    private GShell shell;
     649
     650    public RemoteGreenstoneServerRunScriptAction(String collection_name, String script_name, String script_arguments, GShell shell)
     651    {
     652        this.collection_name = collection_name;
     653        this.script_name = script_name;
     654        this.script_arguments = script_arguments;
     655        this.shell = shell;
     656    }
     657
     658    public void perform()
     659        throws Exception
     660    {
     661        progress_bar.setAction("Running " + script_name + "...");
     662
     663        String run_script_command = "cmd=run-script";
     664        run_script_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
     665        run_script_command += "&script=" + script_name;
     666        run_script_command += "&language=" + Configuration.getLanguage();
     667        run_script_command += script_arguments;
     668        action_output = sendCommandToServer(run_script_command, shell);
     669    }
     670    }
     671
     672
     673    /**
     674     * --------------------------------------------------------------------------------------------
     675     *    UPLOAD COLLECTION FILE
     676     * --------------------------------------------------------------------------------------------
     677     */
     678    static private class RemoteGreenstoneServerUploadCollectionFilesAction
     679    extends RemoteGreenstoneServerAction
     680    {
     681    private String collection_name;
     682    private File[] collection_files;
     683
     684
     685    public RemoteGreenstoneServerUploadCollectionFilesAction(String collection_name, File[] collection_files)
     686    {
     687        this.collection_name = collection_name;
     688        this.collection_files = collection_files;
     689    }
     690
     691
     692    public void perform()
     693        throws Exception
     694    {
     695        progress_bar.setAction("Uploading collection files...");
     696
     697        // Determine the file paths relative to the collection directory
     698        String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
     699        String[] collection_file_relative_paths = new String[collection_files.length];
     700        for (int i = 0; i < collection_files.length; i++) {
     701        collection_file_relative_paths[i] = getPathRelativeToDirectory(collection_files[i], collection_directory_path);
     702        }
     703
     704        // Zip up the files to send to the server
     705        String zip_file_name = collection_name + "-" + System.currentTimeMillis() + ".zip";
     706        String zip_file_path = collection_directory_path + zip_file_name;
     707        ZipTools.zipFiles(zip_file_path, collection_directory_path, collection_file_relative_paths);
     708
     709        // Upload the zip file
     710        String upload_collection_file_command = "cmd=upload-collection-file";
     711        upload_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
     712        upload_collection_file_command += "&file=" + URLEncoder.encode(zip_file_name, "UTF-8");
     713        upload_collection_file_command += "&directory=";
     714        upload_collection_file_command += "&zip=true";
     715        action_output = uploadFile(upload_collection_file_command, zip_file_path);
     716    }
     717    }
     718
     719
     720    /**
     721     * --------------------------------------------------------------------------------------------
     722     *    UPLOAD FILES INTO COLLECTION
     723     * --------------------------------------------------------------------------------------------
     724     */
     725    static private class RemoteGreenstoneServerUploadFilesIntoCollectionAction
     726    extends RemoteGreenstoneServerAction
     727    {
     728    private String collection_name;
     729    private File[] source_files;
     730    private File target_collection_directory;
     731
     732
     733    public RemoteGreenstoneServerUploadFilesIntoCollectionAction(String collection_name, File[] source_files, File target_collection_directory)
     734    {
     735        this.collection_name = collection_name;
     736        this.source_files = source_files;
     737        this.target_collection_directory = target_collection_directory;
     738    }
     739
     740
     741    public void perform()
     742        throws Exception
     743    {
     744        String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
     745        String target_collection_directory_relative_path = getPathRelativeToDirectory(target_collection_directory, collection_directory_path);
     746        target_collection_directory_relative_path = target_collection_directory_relative_path.replaceAll((Utility.isWindows() ? "\\\\" : "\\/"), "|");
     747        progress_bar.setAction("Uploading files into collection...");
     748
     749        String zip_file_name = collection_name + "-" + System.currentTimeMillis() + ".zip";
     750        String zip_file_path = Gatherer.getCollectDirectoryPath() + zip_file_name;
     751        DebugStream.println("Zip file path: " + zip_file_path);
     752
     753        String base_directory_path = source_files[0].getParentFile().getAbsolutePath();
     754        DebugStream.println("Base directory path: " + base_directory_path);
     755        String[] source_file_relative_paths = new String[source_files.length];
     756        for (int i = 0; i < source_files.length; i++) {
     757        DebugStream.println("Source file path: " + source_files[i]);
     758        source_file_relative_paths[i] = getPathRelativeToDirectory(source_files[i], base_directory_path);
     759        }
     760
     761        ZipTools.zipFiles(zip_file_path, base_directory_path, source_file_relative_paths);
     762
     763        String upload_collection_file_command = "cmd=upload-collection-file";
     764        upload_collection_file_command += "&c=" + URLEncoder.encode(collection_name, "UTF-8");
     765        upload_collection_file_command += "&file=" + URLEncoder.encode(zip_file_name, "UTF-8");
     766        upload_collection_file_command += "&directory=" + URLEncoder.encode(target_collection_directory_relative_path, "UTF-8");
     767        upload_collection_file_command += "&zip=true";
     768        action_output = uploadFile(upload_collection_file_command, zip_file_path);
     769    }
     770    }
     771
     772
     773    // ----------------------------------------------------------------------------------------------------
     774    //   AUTHENTICATION LAYER
     775    // ----------------------------------------------------------------------------------------------------
     776
     777
     778    static private PasswordAuthentication remote_greenstone_server_authentication = null;
     779    // static private PasswordAuthentication remote_greenstone_server_authentication = new PasswordAuthentication(System.getProperty("user.name"), new char[] { });
     780
     781
     782    static private class RemoteGreenstoneServerAuthenticateTask
     783    extends Thread
     784    {
     785    public void run()
     786    {
     787        remote_greenstone_server_authentication = new RemoteGreenstoneServerAuthenticator().getAuthentication();
     788    }
     789
     790
     791    static private class RemoteGreenstoneServerAuthenticator
     792        extends GAuthenticator
     793    {
     794        public PasswordAuthentication getAuthentication()
     795        {
     796        return getPasswordAuthentication();
     797        }
     798
     799        protected String getMessageString()
     800        {
     801        return Dictionary.get("RemoteGreenstoneServer.Authentication_Message");
     802        }
     803    }
     804    }
     805
     806
     807    static private void authenticateUser()
     808    throws RemoteGreenstoneServerActionCancelledException
     809    {
     810    // If we don't have any authentication information then ask for it now
     811    if (remote_greenstone_server_authentication == null) {
     812        try {
     813        // We have to do this on the GUI thread
     814        SwingUtilities.invokeAndWait(new RemoteGreenstoneServerAuthenticateTask());
     815        }
     816        catch (Exception exception) {
     817        DebugStream.printStackTrace(exception);
     818        }
     819
     820        // If it is still null then the user has cancelled the authentication, so the action is cancelled
     821        if (remote_greenstone_server_authentication == null) {
     822        throw new RemoteGreenstoneServerActionCancelledException();
     823        }
     824    }
     825    }
     826
     827
     828    // ----------------------------------------------------------------------------------------------------
     829    //   REQUEST LAYER
     830    // ----------------------------------------------------------------------------------------------------
     831
     832
     833    /** Returns the command output if the action completed, throws some kind of exception otherwise. */
     834    static private String downloadFile(String gliserver_args, String file_path)
     835    throws Exception
     836    {
     837    while (true) {
     838        // Check that Configuration.gliserver_url is set
     839        if (Configuration.gliserver_url == null) {
     840        throw new Exception("Empty gliserver URL: please set this in Preferences before continuing.");
     841        }
     842
     843        // Ask for authentication information (if necessary), then perform the action
     844        authenticateUser();
     845        String gliserver_url_string = Configuration.gliserver_url.toString();
     846        String command_output = downloadFileInternal(gliserver_url_string, gliserver_args, file_path);
     847
     848        // Check the first line to see if authentication has failed; if so, go around the loop again
     849        if (command_output.startsWith("ERROR: Authentication failed:")) {
     850        JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Error", command_output.substring("ERROR: ".length())), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.ERROR_MESSAGE);
     851        remote_greenstone_server_authentication = null;
     852        continue;
     853        }
     854        // Check if the collection is locked by someone else; if so, see if the user wants to steal the lock
     855        else if (command_output.startsWith("ERROR: Collection is locked by: ")) {
     856        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) {
     857            // The user has decided to cancel the action
     858            throw new RemoteGreenstoneServerActionCancelledException();
     859        }
     860
     861        // The user has decided to steal the lock... rerun the command with "&steal_lock="
     862        gliserver_args += "&steal_lock=";
     863        continue;
     864        }
     865        // Handle other types of errors by throwing an exception
     866        else if (command_output.startsWith("ERROR: ")) {
     867        throw new Exception(command_output.substring("ERROR: ".length()));
     868        }
     869
     870        // There were no exceptions thrown so the action must have succeeded
     871        return command_output;
     872    }
     873    }
     874
     875
     876    /** Returns the command output if the action completed, throws some kind of exception otherwise. */
     877    static private String sendCommandToServer(String gliserver_args, GShell shell)
     878    throws Exception
     879    {
     880    while (true) {
     881        // Check that Configuration.gliserver_url is set
     882        if (Configuration.gliserver_url == null) {
     883        throw new Exception("Empty gliserver URL: please set this in Preferences before continuing.");
     884        }
     885
     886        // Ask for authentication information (if necessary), then perform the action
     887        authenticateUser();
     888        String gliserver_url_string = Configuration.gliserver_url.toString();
     889        String command_output = sendCommandToServerInternal(gliserver_url_string, gliserver_args, shell);
     890        // System.err.println("Command output: " + command_output);
     891
     892        // Check the first line to see if authentication has failed; if so, go around the loop again
     893        if (command_output.startsWith("ERROR: Authentication failed:")) {
     894        JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Error", command_output.substring("ERROR: ".length())), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.ERROR_MESSAGE);
     895        remote_greenstone_server_authentication = null;
     896        continue;
     897        }
     898        // Check if the collection is locked by someone else; if so, see if the user wants to steal the lock
     899        else if (command_output.startsWith("ERROR: Collection is locked by: ")) {
     900        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) {
     901            // The user has decided to cancel the action
     902            throw new RemoteGreenstoneServerActionCancelledException();
     903        }
     904
     905        // The user has decided to steal the lock... rerun the command with "&steal_lock="
     906        gliserver_args += "&steal_lock=";
     907        continue;
     908        }
     909        // Handle other types of errors by throwing an exception
     910        else if (command_output.startsWith("ERROR: ")) {
     911        throw new Exception(command_output.substring("ERROR: ".length()));
     912        }
     913
     914        // There were no exceptions thrown so the action must have succeeded
     915        return command_output;
     916    }
     917    }
     918
     919
     920    /** Returns the command output if the action completed, throws some kind of exception otherwise. */
     921    static private String uploadFile(String gliserver_args, String file_path)
     922    throws Exception
     923    {
     924    while (true) {
     925        // Check that Configuration.gliserver_url is set
     926        if (Configuration.gliserver_url == null) {
     927        throw new Exception("Empty gliserver URL: please set this in Preferences before continuing.");
     928        }
     929
     930        // Ask for authentication information (if necessary), then perform the action
     931        authenticateUser();
     932        String gliserver_url_string = Configuration.gliserver_url.toString();
     933        String command_output = uploadFileInternal(gliserver_url_string, gliserver_args, file_path);
     934        // System.err.println("Command output: " + command_output);
     935
     936        // Check the first line to see if authentication has failed; if so, go around the loop again
     937        if (command_output.startsWith("ERROR: Authentication failed:")) {
     938        JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("RemoteGreenstoneServer.Error", command_output.substring("ERROR: ".length())), Dictionary.get("RemoteGreenstoneServer.Error_Title"), JOptionPane.ERROR_MESSAGE);
     939        remote_greenstone_server_authentication = null;
     940        continue;
     941        }
     942        // Check if the collection is locked by someone else; if so, see if the user wants to steal the lock
     943        else if (command_output.startsWith("ERROR: Collection is locked by: ")) {
     944        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) {
     945            // The user has decided to cancel the action
     946            throw new RemoteGreenstoneServerActionCancelledException();
     947        }
     948
     949        // The user has decided to steal the lock... rerun the command with "&steal_lock="
     950        gliserver_args += "&steal_lock=";
     951        continue;
     952        }
     953        // Handle other types of errors by throwing an exception
     954        else if (command_output.startsWith("ERROR: ")) {
     955        throw new Exception(command_output.substring("ERROR: ".length()));
     956        }
     957
     958        // There were no exceptions thrown so the action must have succeeded
     959        return command_output;
     960    }
     961    }
     962
     963
     964    // ----------------------------------------------------------------------------------------------------
     965    //   NETWORK LAYER
     966    // ----------------------------------------------------------------------------------------------------
     967
     968
     969    /** Returns the command output if the action completed, throws some kind of exception otherwise. */
     970    static private String downloadFileInternal(String download_cgi, String cgi_args, String file_path)
     971    throws Exception
     972    {
     973    DebugStream.println("gliserver URL: " + download_cgi);
     974    System.err.println("gliserver args: " + cgi_args);
     975
     976    // Add username and password
     977    cgi_args += "&un=" + remote_greenstone_server_authentication.getUserName();
     978    cgi_args += "&pw=" + new String(remote_greenstone_server_authentication.getPassword());
     979
     980    URL download_url = new URL(download_cgi);
     981    URLConnection dl_connection = download_url.openConnection();
     982    dl_connection.setDoOutput(true);
     983    OutputStream dl_os = dl_connection.getOutputStream();
     984
     985    PrintWriter dl_out = new PrintWriter(dl_os);
     986    dl_out.println(cgi_args);
     987    dl_out.close();
     988
     989    // Download result from running cgi script
     990    InputStream dl_is = dl_connection.getInputStream();
     991    BufferedInputStream dl_bis = new BufferedInputStream(dl_is);
     992    DataInputStream dl_dbis = new DataInputStream(dl_bis);
     993
     994    String first_line = "";
     995    byte[] buf = new byte[1024];
     996    int len = dl_dbis.read(buf);
     997    if (len >= 0) {
     998        String first_chunk = new String(buf, 0, len);
     999        first_line = first_chunk.substring(0, ((first_chunk.indexOf("\n") != -1) ? first_chunk.indexOf("\n") : len));
     1000
     1001        // Save the data to file
     1002        FileOutputStream zip_fos = new FileOutputStream(file_path);
    1351003        BufferedOutputStream zip_bfos = new BufferedOutputStream(zip_fos);
    136        
    137         byte[] buf = new byte[1024];
    138         int len;
    139 
    140         while ((len = dl_dbis.read(buf)) >= 0 && !source.hasSignalledStop()) {
    141         zip_bfos.write(buf,0,len);
    142         }
    143 
    144         dl_dbis.close();
    145         dl_bis.close();
    146         dl_is.close();
     1004
     1005        while (len >= 0) {
     1006        zip_bfos.write(buf, 0, len);
     1007        len = dl_dbis.read(buf);
     1008        }
    1471009
    1481010        zip_bfos.close();
    1491011        zip_fos.close();
    150 
    151         if(source.hasSignalledStop()) {
    152         //A cancel has been called. Delete the zip file.
    153         DebugStream.println("download_url_zip() cancelled. Cleaning up.");
    154         if(new File(zip_fname).delete()) {
    155             DebugStream.println("Zip file " + zip_fname + " deleted");
     1012    }
     1013
     1014    dl_dbis.close();
     1015    dl_bis.close();
     1016    dl_is.close();
     1017    return first_line;
     1018    }
     1019
     1020
     1021    /** Returns the command output if the action completed, throws some kind of exception otherwise. */
     1022    static private String sendCommandToServerInternal(String gliserver_url_string, String cgi_args, GShell shell)
     1023    throws Exception
     1024    {
     1025    DebugStream.println("gliserver URL: " + gliserver_url_string);
     1026    System.err.println("gliserver args: " + cgi_args);
     1027
     1028    // Add username and password
     1029    cgi_args += "&un=" + remote_greenstone_server_authentication.getUserName();
     1030        cgi_args += "&pw=" + new String(remote_greenstone_server_authentication.getPassword());
     1031
     1032    URL gliserver_url = new URL(gliserver_url_string + "?" + cgi_args);
     1033    URLConnection gliserver_connection = gliserver_url.openConnection();
     1034
     1035    // Read the output of the command from the server, and return it
     1036    StringBuffer command_output_buffer = new StringBuffer(2048);
     1037    InputStream gliserver_is = gliserver_connection.getInputStream();
     1038    BufferedReader gliserver_in = new BufferedReader(new InputStreamReader(gliserver_is, "UTF-8"));
     1039    String gliserver_output_line = gliserver_in.readLine();
     1040    while (gliserver_output_line != null) {
     1041        if (shell != null) {
     1042        shell.fireMessage(gliserver_output_line);
     1043        if (shell.hasSignalledStop()) {
     1044            throw new RemoteGreenstoneServerActionCancelledException();
    1561045        }
    157         else {
    158             DebugStream.println("Zip file " + zip_fname + " NOT deleted (no big deal). Does it exist?");
    159         }
    160         }
    161     }
    162     catch (Exception error) {
    163         error.printStackTrace();
    164     }
    165     DebugStream.println("Exited download_url_zip");
    166     }
    167 
    168 
    169     static public void upload_url_zip(String col_name, String dir, String delete_type, GShell source)
    170     {
    171     final String lineEnd = "\r\n";
    172     final String twoHyphens = "--";
    173     final String boundary =  "*****";
     1046        }
     1047        command_output_buffer.append(gliserver_output_line + "\n");
     1048        gliserver_output_line = gliserver_in.readLine();
     1049    }
     1050    gliserver_in.close();
     1051
     1052    return command_output_buffer.toString();
     1053    }
     1054
     1055
     1056    /** Returns the command output if the action completed, throws some kind of exception otherwise. */
     1057    static private String uploadFileInternal(String upload_cgi, String cgi_args, String file_path)
     1058    throws Exception
     1059    {
     1060    DebugStream.println("gliserver URL: " + upload_cgi);
     1061    System.err.println("gliserver args: " + cgi_args);
     1062
     1063    // Add username and password
     1064    cgi_args += "&un=" + remote_greenstone_server_authentication.getUserName();
     1065    cgi_args += "&pw=" + new String(remote_greenstone_server_authentication.getPassword());
     1066
     1067    // Open a HTTP connection to the URL
     1068    URL url = new URL(upload_cgi);
     1069    HttpURLConnection gliserver_connection = (HttpURLConnection) url.openConnection();
     1070
     1071    gliserver_connection.setDoInput(true);         // Allow Inputs
     1072    gliserver_connection.setDoOutput(true);        // Allow Outputs
     1073    gliserver_connection.setUseCaches(false);      // Don't use a cached copy.
     1074
     1075    gliserver_connection.setRequestProperty("Connection", "Keep-Alive");       
     1076
     1077    DataOutputStream dos = new DataOutputStream(gliserver_connection.getOutputStream());
     1078    dos.writeBytes(cgi_args + "\n");
     1079
     1080    // Send zip file to server
     1081    File file = new File(file_path);
     1082    FileInputStream fileInputStream = new FileInputStream(file);
     1083
     1084    // create a buffer of maximum size
    1741085    final int maxBufferSize = 1024;
    175 
    176     String col_dir;
    177     if (col_name.startsWith("/")) {
    178         col_dir = Configuration.gsdl_path;
    179     }
    180     else {
    181         col_dir = Gatherer.getCollectDirectoryPath();
    182     }
    183 
    184     String zip_fname = col_dir + col_name + ".zip";
    185     String upload_cgi = Gatherer.cgiBase + "upload";
    186 
    187     HttpURLConnection conn = null;
    188 
    189     try {
    190         // Send zip file to server
    191         File file = new File(zip_fname);
    192         FileInputStream fileInputStream = new FileInputStream(file);
    193 
    194         // open a URL connection to the Servlet
    195         URL url = new URL(upload_cgi);
    196         System.err.println("**** Uploading to: " + upload_cgi);
    197 
    198         // Open a HTTP connection to the URL
    199         conn = (HttpURLConnection) url.openConnection();
    200 
    201         conn.setDoInput(true);         // Allow Inputs
    202         conn.setDoOutput(true);        // Allow Outputs
    203         conn.setUseCaches(false);      // Don't use a cached copy.
    204         conn.setRequestMethod("POST"); // Use a post method.
    205 
    206         conn.setRequestProperty("Connection", "Keep-Alive");       
    207         conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
    208 
    209         DataOutputStream dos = new DataOutputStream( conn.getOutputStream() );
    210 
    211         dos.writeBytes(twoHyphens + boundary + lineEnd);
    212         dos.writeBytes("Content-Disposition: form-data; name=\"c\"" + lineEnd + lineEnd);
    213         dos.writeBytes(col_name + lineEnd);
    214         System.err.println("**** c="+col_name);
    215 
    216         dos.writeBytes(twoHyphens + boundary + lineEnd);
    217         dos.writeBytes("Content-Disposition: form-data; name=\"dir\"" + lineEnd + lineEnd);
    218         dos.writeBytes(dir + lineEnd); 
    219         System.err.println("**** dir="+dir);
    220    
    221         dos.writeBytes(twoHyphens + boundary + lineEnd);
    222         dos.writeBytes("Content-Disposition: form-data; name=\"del\"" + lineEnd + lineEnd);
    223         dos.writeBytes(delete_type + lineEnd); 
    224         System.err.println("**** del="+delete_type);
    225 
    226         dos.writeBytes(twoHyphens + boundary + lineEnd);
    227         dos.writeBytes("Content-Disposition: form-data; name=\"zip\";"
    228                + " filename=\"" + zip_fname +"\"" + lineEnd);
    229         dos.writeBytes(lineEnd);
    230         System.err.println("**** zip (filename)="+zip_fname);
    231 
    232         // create a buffer of maximum size                 
    233         int bytesAvailable = fileInputStream.available();
    234         int bufferSize = Math.min(bytesAvailable, maxBufferSize);
    235         byte[] buffer = new byte[bufferSize];
    236        
    237         // read file and write it into form...
    238         int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    239 
    240         while (bytesRead > 0) {
    241         // Check to see if action has been cancelled.
    242         if (source != null && source.hasSignalledStop()) {
    243             break;
    244         }
    245         dos.write(buffer, 0, bufferSize);
    246         bytesAvailable = fileInputStream.available();
    247         bufferSize = Math.min(bytesAvailable, maxBufferSize);
    248         bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    249         }
    250        
    251         // send multipart form data necesssary after file data...
    252        
    253         dos.writeBytes(lineEnd);
    254         dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
    255        
    256         // close streams
    257        
    258         fileInputStream.close();
    259         dos.flush();
    260         dos.close();
    261 
    262         // delete zip file
    263         boolean file_deleted = file.delete();
    264         if (file_deleted) {
    265         System.err.println("Zip file " + file.toString() + " deleted");
    266         }
    267         else {
    268         System.err.println("Zip file " + file.toString() + " NOT deleted");
    269         }
    270     }
    271     catch (MalformedURLException ex) {
    272         System.err.println("Failed on: "+ex);
    273     }
    274    
    275     catch (IOException ioe) {
    276         System.err.println("Failed on: "+ioe);
    277     }
    278    
    279     // Read server response
    280     try {
    281         InputStreamReader isr = new InputStreamReader(conn.getInputStream() );
    282         BufferedReader bisr = new BufferedReader (isr);
    283         String str;
    284         while (( str = bisr.readLine()) != null) {
    285         System.err.println(str);
    286         }
    287         bisr.close();
    288        
    289     }
    290     catch (IOException ioex) {
    291         System.err.println("From (ServerResponse): "+ioex);
    292     }
     1086    int bytesAvailable = fileInputStream.available();
     1087    int bufferSize = Math.min(bytesAvailable, maxBufferSize);
     1088    byte[] buffer = new byte[bufferSize];
     1089
     1090    // read file and write it into form...
     1091    int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
     1092    while (bytesRead > 0) {
     1093        dos.write(buffer, 0, bufferSize);
     1094        bytesAvailable = fileInputStream.available();
     1095        bufferSize = Math.min(bytesAvailable, maxBufferSize);
     1096        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
     1097    }
     1098
     1099    // close streams
     1100    fileInputStream.close();
     1101    dos.flush();
     1102    dos.close();
     1103
     1104    // Read the output of the command from the server, and return it
     1105    String command_output = "";
     1106    InputStream gliserver_is = gliserver_connection.getInputStream();
     1107    BufferedReader gliserver_in = new BufferedReader(new InputStreamReader(gliserver_is, "UTF-8"));
     1108    String gliserver_output_line = gliserver_in.readLine();
     1109    while (gliserver_output_line != null) {
     1110        command_output += gliserver_output_line + "\n";
     1111        gliserver_output_line = gliserver_in.readLine();
     1112    }
     1113    gliserver_in.close();
     1114
     1115    return command_output;
     1116    }
     1117
     1118
     1119    // ----------------------------------------------------------------------------------------------------
     1120    //   UTILITIES
     1121    // ----------------------------------------------------------------------------------------------------
     1122
     1123
     1124    static private String getPathRelativeToDirectory(File file, String directory_path)
     1125    {
     1126    String file_path = file.getAbsolutePath();
     1127    if (!file_path.startsWith(directory_path)) {
     1128        System.err.println("ERROR: File path " + file_path + " is not a child of " + directory_path);
     1129        return file_path;
     1130    }
     1131
     1132    String relative_file_path = file_path.substring(directory_path.length());
     1133    if (relative_file_path.startsWith(File.separator)) {
     1134        relative_file_path = relative_file_path.substring(File.separator.length());
     1135    }
     1136    return relative_file_path;
    2931137    }
    2941138}
  • trunk/gli/src/org/greenstone/gatherer/shell/GShell.java

    r10342 r10726  
    6666    /** A flag used to determine if this process has been asked to cancel. */
    6767    private boolean cancel = false;
     68    private BufferedOutputStream buffered_output_stream = null;
    6869    /** The list of listeners associated with this class. */
    6970    private EventListenerList listeners = null;
     
    158159
    159160
    160     protected boolean got_stream_char(InputStreamReader isr, StringBuffer line_buffer,
    161                       BufferedOutputStream bos) throws IOException
    162     {
    163     // Hopefully this doesn't block if the process is trying to write to STDOUT/STDERR.
    164 
    165     boolean input_status = false;
    166 
    167     if(isr.ready()) {
    168         input_status = true;
    169         int c = isr.read();
    170         if(c == '\n' || c == '\r') {
    171         if(line_buffer.length() > 0) {
    172             String line = line_buffer.toString();
    173             DebugStream.println("* " + line + " *");
    174             fireMessage(type, typeAsString(type) + "> " + line, status, bos);
    175             line = null;
    176             line_buffer = new StringBuffer();
    177         }
    178         }
    179         else {
    180         line_buffer.append((char)c);
    181         }
    182     }
    183 
    184     return input_status;
    185     }
     161//     protected boolean got_stream_char(InputStreamReader isr, StringBuffer line_buffer,
     162//                    BufferedOutputStream bos) throws IOException
     163//     {
     164// // Hopefully this doesn't block if the process is trying to write to STDOUT/STDERR.
     165
     166// boolean input_status = false;
     167
     168// if(isr.ready()) {
     169//      input_status = true;
     170//      int c = isr.read();
     171//      if(c == '\n' || c == '\r') {
     172//      if(line_buffer.length() > 0) {
     173//          String line = line_buffer.toString();
     174//          DebugStream.println("* " + line + " *");
     175//          fireMessage(type, typeAsString(type) + "> " + line, status, bos);
     176//          line = null;
     177//          line_buffer = new StringBuffer();
     178//      }
     179//      }
     180//      else {
     181//      line_buffer.append((char)c);
     182//      }
     183// }
     184
     185// return input_status;
     186//     }
    186187
    187188
     
    221222    return error_count;
    222223    }
    223        
    224 
    225     protected void runRemote(String[] args, BufferedOutputStream bos)
     224
     225
     226    private void runRemote(String[] args, BufferedOutputStream bos)
    226227    {
    227     System.err.println("The level of design processing needed is: " + CollectionDesignManager.getRebuildTypeRequired()); //Will add smart design processing
    228     if(hasSignalledStop()) { return; }
    229     int error_count = 0;
    230     StringBuffer error_list = new StringBuffer();
     228    // Make sure the process hasn't been cancelled
     229    if (hasSignalledStop()) {
     230        return;
     231    }
     232
    231233    try {
    232         if (type == IMPORT) {
    233         String col_name = args[args.length-1];
    234         if (progress!=null) {
    235             progress.messageOnProgressBar("Uploading data to server");
    236         }
    237 
    238         String collect_directory_path = Gatherer.getCollectDirectoryPath();
    239        
    240         //Only upload the parts of the import folder (files/metadata) that has been changed.
    241         //Or upload the lot if we are doing a complete rebuild.
    242         if(Gatherer.c_man.getCollection().getFilesChanged() || !Gatherer.g_man.create_pane.isIncremental()) {
    243             // zip up import folder, but exclude metadata
    244             ZipTools.zipup(collect_directory_path, col_name, "import", this, "", ".*metadata\\.xml");
    245 
    246             if(hasSignalledStop()) { return; }
    247             // upload it to gsdl server
    248             RemoteGreenstoneServer.upload_url_zip(col_name, "import", "files", this);
    249 
    250             Gatherer.c_man.getCollection().setFilesChanged(false);
    251             System.err.println("Finished uploading files");
    252         }
    253         if(Gatherer.c_man.getCollection().getMetadataChanged() || !Gatherer.g_man.create_pane.isIncremental()) {
    254             // zip up metadata from import folder
    255             ZipTools.zipup(collect_directory_path, col_name, "import", this, ".*metadata\\.xml", "");
    256 
    257             if(hasSignalledStop()) { return; }
    258             // upload it to gsdl server
    259             RemoteGreenstoneServer.upload_url_zip(col_name, "import", "metadata", this);
    260             Gatherer.c_man.getCollection().setMetadataChanged(false);
    261             System.err.println("Finished uploading metadata.");
    262         }
    263        
    264         if(hasSignalledStop()) { return; }
    265         // upload etc folder to server (need collect.cfg and any hfiles)
    266         ZipTools.zipup(collect_directory_path, col_name, "etc", this, "", "");
    267 
    268         if(hasSignalledStop()) { return; }
    269         RemoteGreenstoneServer.upload_url_zip(col_name, "etc", "", this);
    270 
    271         String collection_directory_path = Gatherer.c_man.getCollectionDirectoryPath(col_name);
    272         File img_dir = new File(Gatherer.c_man.getCollectionImagesDirectoryPath());
    273         if (img_dir.exists() && img_dir.list().length > 0) {
    274             // upload images/ directory to server
    275             if(hasSignalledStop()) { return; }
    276             ZipTools.zipup(collect_directory_path, col_name, "images", this, "", "");
    277 
    278             if(hasSignalledStop()) { return; }
    279             RemoteGreenstoneServer.upload_url_zip(col_name, "images", "", this);
    280 
    281             System.err.println("collect_directory_path: " + collect_directory_path);
    282             System.err.println("col_name: " + col_name);
    283         }
    284 
    285         if(hasSignalledStop()) { return; }
    286         // see if collection specific image needs uploading
    287         CollectionMetaManager cmm
    288             = CollectionDesignManager.collectionmeta_manager;
    289        
    290         CollectionMeta icon_collection_collectionmeta
    291             = cmm.getMetadatum(CollectionConfiguration.COLLECTIONMETADATA_ICONCOLLECTION_STR);
    292         CollectionMeta icon_collection_small_collectionmeta
    293             = cmm.getMetadatum(CollectionConfiguration.COLLECTIONMETADATA_ICONCOLLECTIONSMALL_STR);
    294        
    295         String ics_text = icon_collection_collectionmeta.getValue(CollectionMeta.TEXT);
    296         String icsc_text = icon_collection_small_collectionmeta.getValue(CollectionMeta.TEXT);
    297         if ((ics_text != null) && (ics_text != "")) {
    298             // Stub code for detecting when collectoin image changed
    299             // => need to upload images
    300             // System.err.println("**** ics_text = " + ics_text);
    301         }
    302        
    303         if (progress!=null) {
    304             progress.messageOnProgressBar("");
    305         }
    306         }
    307        
    308         if(hasSignalledStop()) { return; }
    309         String perl_cmd = args[0];
    310         int perl_cmd_root = perl_cmd.lastIndexOf(File.separator);
    311        
    312         if (perl_cmd_root > 0) {
    313         String perl_cmd_cut = perl_cmd.substring(perl_cmd_root+1);
    314         perl_cmd = perl_cmd_cut;
    315         }
    316 
    317         String cols_concat = null;
    318         String launch  = Gatherer.cgiBase + "launch";
    319         launch = launch + "?cmd=" + perl_cmd;
    320        
    321         for(int i = 1; i<args.length; i++) {
    322        
    323         String arg = args[i];
    324        
    325         if (arg.equals("-collectdir") || arg.equals("-importdir")
    326             || arg.equals("-builddir")) {
    327             // skip it
     234        int directory_name_end = args[0].lastIndexOf(File.separator);
     235        String script_name = ((directory_name_end != -1) ? args[0].substring(directory_name_end + 1) : args[0]);
     236        System.err.println("Script name: " + script_name);
     237        String collection_name = args[args.length - 1];
     238        System.err.println("Collection name: " + collection_name);
     239
     240        String script_args = "";
     241        for (int i = 1; i < (args.length - 1); i++) {
     242        // Skip arguments that won't make sense on the server
     243        if (args[i].equals("-collectdir") || args[i].equals("-importdir") || args[i].equals("-builddir")) {
    328244            i++;
    329245            continue;
    330246        }
    331                
    332         if(arg.startsWith(StaticStrings.MINUS_CHARACTER)) {
    333             String name = arg.substring(1);
    334             launch = launch + "&" + name + "=";
    335             if (i+1<args.length-1) {
    336             if (!args[i+1].startsWith(StaticStrings.MINUS_CHARACTER)) {
    337                 i++;
    338                 String val = URLEncoder.encode(args[i],"UTF-8");
    339                 launch = launch + val;
    340             }
     247
     248        // Script arguments get changed to CGI arguments
     249        if (args[i].startsWith("-")) {
     250            script_args += "&" + args[i].substring(1) + "=";
     251            if ((i + 1) < (args.length - 1) && !args[i + 1].startsWith("-")) {
     252            script_args += URLEncoder.encode(args[i + 1], "UTF-8");
     253            i++;
    341254            }
    342255        }
    343         else {
    344             if (cols_concat==null) {
    345             cols_concat = arg;
    346             }
    347             else {
    348             cols_concat = cols_concat + ":" + arg;
    349             }
    350         }
    351         }
    352 
    353         if (cols_concat!=null) {
    354         launch = launch + "&c=" + cols_concat;
    355         }
    356 
    357         System.err.println("**** launch url = " + launch);
    358         // fireMessage(type, Dictionary.get("GShell.Command") + ": " + launch, status, null); // ****
    359        
    360         URL launch_url = new URL(launch);
    361         URLConnection launch_connection = launch_url.openConnection();
    362         InputStream stdis = launch_connection.getInputStream();
    363         InputStreamReader stdisr = new InputStreamReader(stdis, "UTF-8");
    364        
    365         BufferedReader stdbr = new BufferedReader(stdisr);
    366        
    367         if (type == GShell.NEW) {
    368         while(true) {
    369             String line = stdbr.readLine();
    370             if (line == null) { break; }
    371             error_count = check_for_error(line,error_list,error_count);
    372         }
    373         }
    374         else {
    375         while(!hasSignalledStop()) {
    376             String line = stdbr.readLine();
    377             if (line == null) { break; }
    378             error_count = check_for_error(line,error_list,error_count);
    379             fireMessage(type, typeAsString(type) + "> " + line, status, bos);
    380         }
    381         }
    382         stdbr.close();
    383        
    384         if (error_count>0) {
    385         status = ERROR;
    386         System.err.println(error_list);
    387         if (type != GShell.NEW) {
    388             fireMessage(type, typeAsString(type) + "> " + error_list, status, null);
    389         }
    390         }
    391         else if(hasSignalledStop()) {
    392         //User pressed the cancel button.
    393         //Status already set in hasSignalledStop method.
    394         }
    395         else {
    396         status = OK;
    397         fireMessage(type, typeAsString(type) + "> " + Dictionary.get("GShell.Success"), status, null);
    398         }
    399     }
    400     // Exception
     256        }
     257
     258        System.err.println("Script args: " + script_args);
     259        buffered_output_stream = bos;
     260        String command_output = RemoteGreenstoneServer.runScript(collection_name, script_name, script_args, this);
     261        status = (command_output.equals("") ? CANCELLED : OK);
     262    }
    401263    catch (Exception exception) {
    402         System.err.println("Exception in GShell.runRemote() - unexpected");
    403264        DebugStream.printStackTrace(exception);
    404265        status = ERROR;
     
    407268
    408269
    409     protected void runLocal(String[] args, BufferedOutputStream bos)
     270    private void runLocal(String[] args, BufferedOutputStream bos)
    410271    {
    411272    try {
     
    415276        }
    416277       
    417         DebugStream.println("Command: "+command);
     278        System.err.println("Command: "+command);
    418279        ///ystem.err.println("Command: " + command);
    419280        fireMessage(type, Dictionary.get("GShell.Command") + ": " + command, status, null);
     
    543404
    544405    // Issue a processBegun event
     406    System.err.println("\nFiring process begun for " + type + "...");
    545407    fireProcessBegun(type, status);
     408    System.err.println("Done process begun.");
    546409    if (Gatherer.isGsdlRemote) {
    547410        runRemote(args,bos);
     
    550413        runLocal(args,bos);
    551414    }
     415    System.err.println("Done runLocal().");
    552416
    553417    if(status == OK) {
    554418        if (type == NEW) {
    555419        if (Gatherer.isGsdlRemote) {
    556             RemoteGreenstoneServer.download_url_zip(col_name, ".", this, "", "");
    557             if (!hasSignalledStop()) {
    558             ZipTools.unzip(Gatherer.getCollectDirectoryPath(), col_name);
    559             }
     420            RemoteGreenstoneServer.downloadCollection(col_name);
    560421        }
    561422        }
     
    569430            }
    570431
    571             Utility.delete(Gatherer.c_man.getCollectionArchivesDirectoryPath()); // remove current archives
    572             //new File(Gatherer.c_man.getCollectionArchivesDirectoryPath()).mkdir(); //Make a clean dir
    573             RemoteGreenstoneServer.download_url_zip(col_name, "archives", this, ".*doc\\.xml", "");
    574             if (hasSignalledStop()) {
    575             // Clean up, then exit
    576             fireProcessComplete(type, status);
    577             // Close bos
    578             if(bos != null) {
    579                 try {
    580                 bos.close();
    581                 bos = null;
    582                 }
    583                 catch(Exception error) {
    584                 DebugStream.printStackTrace(error);
    585                 }
    586             }
    587             return;
    588             }
    589             ZipTools.unzip(Gatherer.getCollectDirectoryPath(), col_name);
    590            
     432            RemoteGreenstoneServer.downloadCollectionArchives(col_name);
     433
    591434            if (progress!=null) {
    592435            progress.messageOnProgressBar("");
     
    608451            }
    609452
    610             File buildDir = new File(Gatherer.c_man.getCollectionBuildingDirectoryPath());
    611             Utility.delete(buildDir); // remove current build dir
    612             //Only need build.cfg
    613             RemoteGreenstoneServer.download_url_zip(col_name, "building", this, ".*build\\.cfg", "");
    614             if (!hasSignalledStop()) {
    615             ZipTools.unzip(Gatherer.getCollectDirectoryPath(), col_name);
    616             }
     453            // Only need to download build.cfg file
     454            File build_cfg_file = new File(Gatherer.c_man.getCollectionBuildingDirectoryPath(), "build.cfg");
     455            RemoteGreenstoneServer.downloadCollectionFile(col_name, build_cfg_file);
    617456           
    618457            if (progress!=null) {
     
    638477
    639478    // We're done. 
     479    System.err.println("Firing process complete for " + type + "...");
    640480    fireProcessComplete(type, status);
    641481    // Close bos
     
    650490    }
    651491    }
     492
     493
     494    public void fireMessage(String message)
     495    {
     496    fireMessage(type, typeAsString(type) + "> " + message, status, buffered_output_stream);
     497    }
     498
     499
    652500    /** Method for firing a message to all interested listeners.
    653501     * @param type An <strong>int</strong> indicating the process type.
     
    697545    // Start the progres monitor if available
    698546    if(progress != null) {
     547        System.err.println("About to call progress.start().");
    699548        progress.start();
     549        System.err.println("Called progress.start().");
    700550    }
    701551    // Fire an event
  • trunk/gli/src/org/greenstone/gatherer/util/ZipTools.java

    r10267 r10726  
    3131import java.util.*;
    3232import java.util.zip.*;
    33 import org.greenstone.gatherer.DebugStream;
    34 import org.greenstone.gatherer.shell.GShell;
    3533import org.w3c.dom.*;
    3634
     
    4745        ZipEntry zip_entry = (ZipEntry) e.nextElement();
    4846        File zip_entry_file = new File(base_directory_path + zip_entry.getName());
    49         DebugStream.println("    Unzipping: " + zip_entry_file.getAbsolutePath());
     47        // System.err.println("    Unzipping: " + zip_entry_file.getAbsolutePath());
    5048
    5149        // Directory case
     
    6159            // Write out file to disk
    6260
    63             // Make sure it's parent directory exists.
     61            // Make sure its parent directory exists.
    6462            File dir = new File(zip_entry_file.getParent());
    6563            dir.mkdirs();
     
    9290    }
    9391    catch (Exception exception) {
    94         DebugStream.printStackTrace(exception);
     92        exception.printStackTrace();
    9593    }
    9694    }
     
    112110    }
    113111    catch (Exception exception) {
    114         DebugStream.printStackTrace(exception);
     112        exception.printStackTrace();
    115113    }
    116114    }
     
    131129        if (file.isDirectory()) {
    132130        // Add a zip entry for this directory
    133         zos.putNextEntry(new ZipEntry(relative_file_path + File.separator));
     131        // Actually, don't -- this messes things up completely on Windows and doesn't seem to matter on Linux
     132        // zos.putNextEntry(new ZipEntry(relative_file_path + File.separator));
    134133
    135134        // Apply recursively to each of the children of the directory
     
    144143        // Add a zip entry for this file
    145144        if (zip_filter.shouldIncludeFile(relative_file_path)) {
    146             zos.putNextEntry(new ZipEntry(relative_file_path));
     145            // Always use Unix style paths in zip files, even on Windows
     146            zos.putNextEntry(new ZipEntry(relative_file_path.replace('\\', '/')));
    147147
    148148            if (zip_filter.shouldIncludeFileContent(relative_file_path)) {
     
    150150            byte[] data = new byte[1024];
    151151            int bytes_read;
    152             while ((bytes_read = bis.read(data, 0, 1024)) > -1) {
     152            while ((bytes_read = zip_filter.filterFileContent(bis, data, 1024)) > -1) {
    153153                zos.write(data, 0, bytes_read);
    154154            }
     
    159159    }
    160160    catch (Exception exception) {
    161         DebugStream.printStackTrace(exception);
     161        exception.printStackTrace();
    162162    }
    163163    }
     
    169169
    170170    public boolean shouldIncludeFileContent(String relative_file_path);
     171
     172    public int filterFileContent(BufferedInputStream bis, byte[] data, int bytes_to_read);
    171173    }
    172174
     
    187189        return true;
    188190    }
    189     }
    190 
    191 
    192     // ----------------------------------------------------------------------------------------------------
    193 
    194 
    195     static private String unixStylePath(String path)
    196     {
    197     String unix_path = path.replace('\\','/');
    198     return unix_path;
    199     }
    200 
    201 
    202     /**
    203      * Method to handle zipping up of a file (not a directory).
    204      * Called by {@link zipup(String, String, String, GShell, String, String)} and {@link dirFunc(ZipOutputStream, String, int, GShell, boolean, String, String)}
    205      *
    206      * @param zos the zip output stream, as a <strong>ZipOutputStream</strong>
    207      * @param file_path the <strong>String</strong> containing the path to the file/directory tozip up
    208      * @param prefix_strip the <strong>int</strong> used to substring the file_path
    209      * @param source the <strong>GShell</strong> object that is calling this method, so that we can check to see if the process has been cancelled.
    210      *               This is null if GShell is not the calling object.
    211      * @param accept_expr a <strong>String</strong> containing a regular expression of files to include in the archive. All other files will be excluded.
    212      *                    There is a special case of ".*doc.xml", where the doc.xml files will be parsed to only include their metadata.
    213      * @param reject_expr a <strong>String</strong> containing a regular expression of files not to include in the archive.
    214      * @return boolean - true if the zip was created successfully(?).
    215      * @see dirFunc(ZipOutputStream, String, int, GShell, boolean, String, String)
    216      * @see zipup(String, String, String, GShell, String, String)
    217      */
    218     static private boolean zipFunc(ZipOutputStream zos, String file_path, int prefix_strip, GShell source, boolean encountered_file, String accept_expr, String reject_expr)
    219     {
    220     String new_file_path = file_path;
    221 
    222     if((reject_expr != "") && (file_path.matches(reject_expr))) {
    223         // matches reject expression
    224         //DebugStream.println("File \'" + file_path + "\' matches the reject expression \'" + reject_expr + "\'");
    225         return encountered_file;
    226     }
    227 
    228     if ((accept_expr != "") && (!file_path.matches(accept_expr))) {
    229         // does not match accept expression
    230         //DebugStream.println("File \'" + file_path + "\' doesn't match accept expression \'" + accept_expr + "\'");
    231         return encountered_file;
    232     }
    233 
    234     //Special case: we only want the metadata. Parse the xml files.
    235     if(accept_expr.compareTo(".*doc.xml") == 0) {
    236         DebugStream.println("Only want to get metadata");
    237         Document old_document = XMLTools.parseXMLFile(new File(file_path));
    238         NodeList content = old_document.getElementsByTagName("Content");
    239 
     191
     192
     193    public int filterFileContent(BufferedInputStream bis, byte[] data, int bytes_to_read)
     194    {
    240195        try {
    241         for(int i=content.getLength()-1; i >= 0; i--) {
    242             //Remove all content - just leave the metadata.
    243             content.item(i).getParentNode().removeChild(content.item(i));
    244         }
    245         }
    246         catch (Exception ex) {
    247         ex.printStackTrace();
    248         }
    249 
    250         new_file_path = file_path.substring(0, file_path.lastIndexOf(File.separator) + 1) + "new.xml";
    251         //Is there any better way than writing out as a new file, then reading back in??
    252         XMLTools.writeXMLFile(new File(new_file_path), old_document);
    253     }
    254 
    255     // Using try is required because of file io.
    256     try {
    257         // Exit if pressed cancel.
    258         if (source != null && source.hasSignalledStop()) {
    259         return false;
    260         }
    261 
    262         // Create a Zip Entry and put it into the archive (no data yet).
    263 
    264         // Strip off col_dir prefix
    265         String zip_path = file_path.substring(prefix_strip);
    266         // Zip files use '/' for directory separator
    267         String unix_style_path = unixStylePath(zip_path);
    268 
    269         ZipEntry fileEntry = new ZipEntry(unix_style_path);
    270         zos.putNextEntry(fileEntry);
    271 
    272         // Create a file input stream and a buffered input stream.
    273         FileInputStream fis = new FileInputStream(new_file_path);
    274         BufferedInputStream bis = new BufferedInputStream(fis);
    275        
    276         // Create a byte array object named data and declare byte count variable.
    277         byte[] data = new byte[1024];
    278         int byteCount;
    279         // Create a loop that reads from the buffered input stream and writes
    280         // to the zip output stream until the bis has been entirely read.
    281         while ((byteCount = bis.read(data, 0, 1024)) > -1) {
    282         //Check for cancell
    283         if(source != null) {
    284             if(source.hasSignalledStop()) {
    285             break;
    286             }
    287         }
    288         zos.write(data, 0, byteCount);
    289         }
    290         encountered_file = true;
    291     }
    292     catch (IOException e) {
    293         e.printStackTrace();
    294     }
    295    
    296     DebugStream.println("Zipping up: " + file_path);
    297     return encountered_file;
    298     }
    299 
    300     /**
    301      * Method to handle ziping up of a directory. This is called by {@link zipup(String, String, String, GShell, String, String)}
    302      *
    303      * @param zos the <strong>ZipOutputStream</strong> to output to.
    304      * @param dir_name the directory name to zip, as a <strong>String</strong>
    305      * @param prefix_strip the <strong>int</strong> used to substring the file_path
    306      * @param source the <strong>GShell</strong> object that is calling this method, so that we can check to see if the process has been cancelled.
    307      *               This is null if GShell is not the calling object.
    308      * @param encountered_file ??What's this??
    309      * @param accept_expr a <strong>String</strong> containing a regular expression of files to include in the archive. All other files will be excluded.
    310      * @param reject_expr a <strong>String</strong> containing a regular expression of files not to include in the archive.
    311      *
    312      * @see zipup(String, String, String, GShell, String, String)
    313      * @see zipFunc(ZipOutputStream, String, int, GShell, boolean, String, String)
    314      */
    315     static private boolean dirFunc (ZipOutputStream zos, String dir_name, int prefix_strip, GShell source, boolean encountered_file, String accept_expr, String reject_expr)
    316     {
    317     File dirObj = new File(dir_name);
    318 
    319     if (dirObj.exists() == true) {
    320         if (dirObj.isDirectory() == true) {
    321         // Create an array of File objects, one for each file or directory in dirObj.
    322         File [] fileList = dirObj.listFiles();
    323        
    324         // Loop through File array and display.
    325         for (int i = 0; i < fileList.length; i++) {
    326             if(source != null && source.hasSignalledStop()) { break; }
    327             File file = fileList[i];
    328             if (file.isDirectory()) {
    329             String dir_path = file.getPath();
    330             String zip_path = dir_path.substring(prefix_strip);
    331             // Zip files use '/' for directory separator
    332             String unix_style_path
    333                 = unixStylePath(zip_path+File.separator);
    334             ZipEntry dirEntry = new ZipEntry(unix_style_path);
    335 
    336             try {
    337                 zos.putNextEntry(dirEntry);
    338             }
    339             catch (IOException e) {
    340                 e.printStackTrace();
    341             }
    342 
    343             encountered_file = dirFunc(zos, dir_path, prefix_strip, source, encountered_file, accept_expr, reject_expr);
    344             } else if (file.isFile()) {
    345             // Call the zipFunc function
    346             String file_path = fileList[i].getPath();
    347             encountered_file = zipFunc(zos,file_path,prefix_strip, source, encountered_file, accept_expr, reject_expr);
    348             }
    349         }
    350         }
    351         else {
    352         System.err.println (dir_name+" is not a directory.");
    353         }
    354     }
    355     else {
    356         System.err.println ("Directory "+dir_name+" does not exist.");
    357     }
    358     return encountered_file;
    359     }
    360    
    361     /**
    362      * Method to zip up a given file or directory associated with a particular collection.
    363      * This method mainly called from {@link GShell}, with a few exceptions. The zip files produced are used
    364      * to pass files (new files to add, metadata, etc) between the GLI applet and the Greenstone server.
    365      * The actual zipping of files and directories is handled by {@link zipFunc(ZipOutputStream, String, int, GShell, boolean, String, String) and
    366      * {@link dirFunc(ZipOutputStream, String, int, GShell, boolean, String, String)} respectfully.
    367      *
    368      * @param col_dir the path to the collect directory, as a <strong>String</strong>
    369      * @param col_name the collection name, as as <strong>String</strong>
    370      * @param dir_or_file the directory or file within the collection to zip up, as a <strong>String</strong>
    371      * @param source the <strong>GShell</strong> object that is calling this method, so that we can check to see if the process has been cancelled.
    372      *               This is null if GShell is not the calling object.
    373      * @param accept_expr a <strong>String</strong> containing a regular expression of files to include in the archive. All other files will be excluded.
    374      * @param reject_expr a <strong>String</strong> containing a regular expression of files not to include in the archive.
    375      *
    376      * @see zipFunc(ZipOutputStream, String, int, GShell, boolean, String, String)
    377      * @see dirFunc(ZipOutputStream, String, int, GShell, boolean, String, String)
    378      */
    379     static public boolean zipup(String col_dir, String col_name, String dir_or_file, GShell source, String accept_expr, String reject_expr)
    380     {
    381     if (!col_dir.endsWith(File.separator)) {
    382         col_dir += File.separator;
    383     }
    384 
    385     int prefix_strip = col_dir.length();
    386     boolean encountered_file = false;
    387 
    388     String zip_fname = col_dir + col_name + ".zip";
    389     String zip_dir_or_file = col_dir + col_name + File.separator + dir_or_file;
    390     File zip_dof = new File(zip_dir_or_file);
    391 
    392     try {
    393         FileOutputStream fos = new FileOutputStream(zip_fname);
    394         ZipOutputStream zos = new ZipOutputStream(fos);
    395        
    396         if (zip_dof.exists()) {
    397         if (zip_dof.isDirectory()) {
    398             String zip_dir = zip_dir_or_file;
    399             encountered_file = dirFunc(zos, zip_dir, prefix_strip, source, encountered_file, accept_expr, reject_expr);
    400         }
    401         else {
    402             String zip_full_file = zip_dir_or_file;
    403             String zip_path = zip_full_file.substring(prefix_strip);
    404 
    405             for (int i=1; i<zip_path.length(); i++) {
    406             String ch = String.valueOf(zip_path.charAt(i));
    407 
    408             if (ch.equals(File.separator)) {
    409                 String dir_path = zip_path.substring(0,i);
    410                 // Zip files use '/' for directory separator
    411                 String unix_style_path
    412                 = unixStylePath(dir_path+File.separator);
    413                 ZipEntry dirEntry = new ZipEntry(unix_style_path);
    414                 zos.putNextEntry(dirEntry);
    415             }
    416             }
    417             encountered_file = zipFunc(zos, zip_full_file, prefix_strip, source, encountered_file, accept_expr, reject_expr);
    418         }
    419         }
    420         else {
    421         System.err.println("Warning: " + zip_dir_or_file + " does not exist!");
    422         }
    423        
    424         // Close the file output streams for both the file and the zip.
    425         zos.flush();
    426         zos.close();
    427         fos.close();
    428     }
    429     catch (IOException exception) {
    430         DebugStream.printStackTrace(exception);
    431         return false;
    432     }
    433     return encountered_file;
    434     }
    435 
    436 
    437     /**
    438      * Method to unzip a zip file associated with a particular collection.
    439      * The location and name of the zip file should be derrived from the collection. Please see below for details.
    440      *
    441      * @param col_dir the path to the collection directory, as a <strong>String</strong>. This is where the zip file should be located.
    442      * @param col_name the name of the collection, as a <strong>String</strong>. This should be the name of the zip file (with a  .zip extension).
    443      */
    444     static public void unzip(String col_dir, String col_name)
    445     {
    446     if (!col_dir.endsWith(File.separator)) {
    447         col_dir += File.separator;
    448     }
    449 
    450     String zip_fname = col_dir + col_name + ".zip";
    451     // int zip_mode = ZipFile.OPEN_READ | ZipFile.OPEN_DELETE;
    452     int zip_mode = ZipFile.OPEN_READ;
    453 
    454     try {
    455         File cfile = new File(zip_fname);
    456         ZipFile zipfile = new ZipFile(cfile, zip_mode);
    457        
    458         Enumeration e = zipfile.entries();
    459         while (e.hasMoreElements()) {
    460         ZipEntry zipentry = (ZipEntry) e.nextElement();
    461         String zentryname = col_dir + zipentry.getName();
    462         File zfile = new File(zentryname);
    463         DebugStream.println("    Unzipping: " + zentryname);
    464 
    465         if (zipentry.isDirectory()) {
    466             // Create named directory, if it doesn't already exist
    467             if (!zfile.exists() && !zfile.mkdirs()) {
    468             System.err.println("Error: unable to create directory " + zfile);
    469             }
    470         }
    471         else {
    472             // Write out file to disk
    473 
    474             //Make sure it's parent directory exists.
    475             File dir = new File(zfile.getParent());
    476             dir.mkdirs();
    477 
    478             // set up input stream
    479             InputStream zis = zipfile.getInputStream(zipentry);
    480             BufferedInputStream bzis = new BufferedInputStream(zis);
    481             DataInputStream dbzis = new DataInputStream(bzis);
    482            
    483             // set up output stream
    484             FileOutputStream fzos = new FileOutputStream(zentryname);
    485             BufferedOutputStream bfzos = new BufferedOutputStream(fzos);
    486            
    487             byte[] buf = new byte[1024];
    488             int len;
    489             while ((len = dbzis.read(buf)) >= 0) {
    490             bfzos.write(buf,0,len);
    491             }
    492            
    493             dbzis.close();
    494             bzis.close();
    495             zis.close();
    496 
    497             bfzos.close();
    498             fzos.close();
    499         }
    500         }
    501 
    502         zipfile.close();
    503 
    504         // delete zip file
    505         boolean file_deleted = cfile.delete();
    506         if (file_deleted) {
    507         System.err.println("Zip file " + cfile.toString() + " deleted");
    508         }
    509         else {
    510         System.err.println("Zip file " + cfile.toString() + " NOT deleted");
    511         }
    512 
    513 
    514     }
    515     catch (ZipException error) {
    516         System.err.println("Error: Unable to open '"+zip_fname+"'");
    517         System.err.println("This maybe caused by the zip file being empty.");
    518         DebugStream.printStackTrace(error);
    519     }
    520 
    521     catch (Exception error) {
    522         error.printStackTrace();
    523     }
    524     }
    525 
    526 
    527     /**
    528      * Method which unzips a given metadata resoure.
    529      * @param jar_zip_fname The name of the jar file as a <strong>String</strong>.
    530      * @param dst_dir The destination directory for unzipping, also as a <strong>String</strong>.
    531      * @see JarTools.extractFromJar(String, String, boolean)
    532      */
    533     static public void unzipFromJar(String jar_zip_fname, String dst_dir) {
    534 
    535     File file = null;
    536     if (!dst_dir.endsWith(File.separator)) {
    537         dst_dir += File.separator;
    538     }
    539 
    540     JarTools.extractFromJar(jar_zip_fname,dst_dir,true);
    541 
    542     String zip_ofname = dst_dir + jar_zip_fname;
    543     File zip_file = new File(zip_ofname);
    544 
    545     try {
    546         ZipFile zipfile = new ZipFile(zip_file);
    547        
    548         Enumeration e = zipfile.entries();
    549         while (e.hasMoreElements()) {
    550         ZipEntry zipentry = (ZipEntry) e.nextElement();
    551         String zentryname = dst_dir + zipentry.getName();
    552         DebugStream.println("    Unzipping: " + zentryname);
    553 
    554         if (zipentry.isDirectory()) {
    555             // Create named directory, if it doesn't already exist
    556             File zip_entry_file = new File(zentryname);
    557             if (!zip_entry_file.exists() && !zip_entry_file.mkdirs()) {
    558             System.err.println("Error: unable to create directory " + zip_entry_file);
    559             }
    560         }
    561         else {
    562             // Write out file to disk
    563 
    564             // set up input stream
    565             InputStream zis = zipfile.getInputStream(zipentry);
    566             BufferedInputStream bzis = new BufferedInputStream(zis);
    567             DataInputStream dbzis = new DataInputStream(bzis);
    568            
    569             // set up output stream
    570             FileOutputStream fzos = new FileOutputStream(zentryname);
    571             BufferedOutputStream bfzos = new BufferedOutputStream(fzos);
    572            
    573             byte[] buf = new byte[1024];
    574             int len;
    575             while ((len = dbzis.read(buf)) >= 0) {
    576             bfzos.write(buf,0,len);
    577             }
    578            
    579             dbzis.close();
    580             bfzos.close();
    581         }
    582         }
    583     }
    584     catch (ZipException error) {
    585         System.err.println("Error: Unable to open '"+zip_file.getAbsolutePath()+"'");
    586         DebugStream.printStackTrace(error);
    587     }
    588 
    589     catch (Exception error) {
    590         error.printStackTrace();
     196        return bis.read(data, 0, bytes_to_read);
     197        }
     198        catch (Exception exception) {
     199        exception.printStackTrace();
     200        }
     201
     202        return -1;
    591203    }
    592204    }
Note: See TracChangeset for help on using the changeset viewer.