/* * Created on Feb 14, 2005 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package webraveler.ravel; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Enumeration; import java.util.Hashtable; import webraveler.http.HTTPRequest; import webraveler.http.HTTPResponse; import webraveler.vurl.ActivatePluginVURL; import webraveler.vurl.BannerVURL; import webraveler.vurl.DeactivatePluginVURL; import webraveler.vurl.DisablePluginVURL; import webraveler.vurl.ErrorsVURL; import webraveler.vurl.GetUnframedDocumentVURL; import webraveler.vurl.GetUnraveledDocumentVURL; import webraveler.vurl.HelpVURL; import webraveler.vurl.ImageVURL; import webraveler.vurl.LogoutVURL; import webraveler.vurl.MessageVURL; import webraveler.vurl.OffVURL; import webraveler.vurl.OnVURL; import webraveler.vurl.PluginSequenceVURL; import webraveler.vurl.PluginsActivated; import webraveler.vurl.PluginsDisabled; import webraveler.vurl.PluginsEnabled; import webraveler.vurl.SaveConfigVURL; import webraveler.vurl.UserConfigVURL; import webraveler.vurl.VURL; /** * @author schmitzc * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class VURLStack { private Hashtable virtualURLs; private Hashtable pluginVURLs; private Raveler ravel; public VURLStack(Raveler ravel) { this.ravel = ravel; virtualURLs = new Hashtable(); pluginVURLs = new Hashtable(); // load the virtual URLs gatherVURLs(); } //VURLStack(Raveler) /** * Load the virtual URLs */ public void gatherVURLs() { // Check if the file vurls.dat exists, otherwise create a new one and add // the Raveler defaults, then, read the vurls from file and create a hash FileInputStream vurlFileIn = null; FileOutputStream vurlFileOut = null; try { vurlFileIn = new FileInputStream(Raveler.WEBRAVELER_HOME + "/data/vurls.dat"); ObjectInputStream objInput = new ObjectInputStream(vurlFileIn); virtualURLs = (Hashtable) objInput.readObject(); pluginVURLs = (Hashtable) objInput.readObject(); objInput.close(); } catch (FileNotFoundException fnfe) { try{ System.out.println("VURLStack: No vurls.dat found. . . Attempting to create one."); vurlFileOut = new FileOutputStream(Raveler.WEBRAVELER_HOME + "/data/vurls.dat"); addRavelerVURLs(vurlFileOut); // write the defaults to vurls.dat and hash vurlFileOut.close(); // not going to need this one for a while... just creating the file System.out.println("PluginStack: Successfully created vurls.dat"); } catch(IOException ioe1) { System.out.println("PluginStack: Could neither open nor create vurls.dat in your WebRaveler2 directory. Exiting."); ioe1.printStackTrace(); System.exit(1); } } catch (IOException ioe2) { System.out.println("VURLStack: An error occurred while reading vurls.dat. Exiting."); ioe2.printStackTrace(); System.exit(1); } catch (ClassNotFoundException cnfe) { System.out.println("VURLStack: An error occurred while reading vurls.dat. Exiting."); cnfe.printStackTrace(); System.exit(1); } // Add activate/disactivate virtual urls for each available plugin for (Enumeration e = ravel.getPlugins() ; e.hasMoreElements() ;) { RavelerPlugin plugin = (RavelerPlugin) e.nextElement(); ActivatePluginVURL avurl = new ActivatePluginVURL(plugin.name); virtualURLs.put("http://temp.webraveler2.config.userconfig/"+plugin.name+"_activate/",avurl); DeactivatePluginVURL devurl = new DeactivatePluginVURL(plugin.name); virtualURLs.put("http://temp.webraveler2.config.userconfig/"+plugin.name+"_deactivate/",devurl); DisablePluginVURL disvurl = new DisablePluginVURL(plugin.name); virtualURLs.put("http://temp.webraveler2.config.userconfig/"+plugin.name+"_disable/",disvurl); } // for // a couple more vurls to load when we start up Raveler -- these take the // running instance of Raveler as arguments, and therefore can't be // written to disk GetUnframedDocumentVURL getDocVurl = new GetUnframedDocumentVURL(); virtualURLs.put("http://temp.webraveler2.getunframeddocument/getunframeddocument",getDocVurl); // A vurl for unraveling documents GetUnraveledDocumentVURL getUnravelVurl = new GetUnraveledDocumentVURL(); virtualURLs.put("http://temp.webraveler2.getunraveleddocument/getunraveleddocument",getUnravelVurl); // Vurls for errors/warnings ErrorsVURL errors = new ErrorsVURL(); virtualURLs.put("http://temp.webraveler2.errors/geterrors",errors); } //gatherVURLs() /** * Add the default Raveler virtual URLs * * @param vurlFileOut a FileOutputStream to write the Raveler defaults to */ public void addRavelerVURLs(FileOutputStream vurlFileOut) { /* Defaults for Raveler * User Config: webraveler2.config.userconfig * Help: webraveler2.config.help * Groups: webraveler2.config.userconfig/groups? * Logout: webraveler2.config.userconfig/logout */ UserConfigVURL userConfig = new UserConfigVURL(); BannerVURL bannerVurl = new BannerVURL(); SaveConfigVURL saveConfig = new SaveConfigVURL(); HelpVURL help = new HelpVURL(); LogoutVURL logout = new LogoutVURL(); //UnravelVURL unravel= new UnravelVURL(); OnVURL on = new OnVURL(); OffVURL off = new OffVURL(); String messageString = "
"+Raveler.ravelerCSS+""; messageString += "Web Raveler has been turned off.
"; messageString += "Click "; messageString += "here to turn Web Raveler back on.
"; MessageVURL message = new MessageVURL(messageString); //BannerVURL banner = new BannerVURL();73 PluginSequenceVURL pluginSeq = new PluginSequenceVURL(); PluginsActivated activated = new PluginsActivated(); PluginsEnabled enabled = new PluginsEnabled(); PluginsDisabled disabled = new PluginsDisabled(); ImageVURL logo = new ImageVURL("wr_logo_small.gif"); ImageVURL w = new ImageVURL("w10.gif"); ImageVURL e = new ImageVURL("e10.gif"); ImageVURL b = new ImageVURL("b10.gif"); ImageVURL r = new ImageVURL("r10.gif"); ImageVURL a = new ImageVURL("a10.gif"); ImageVURL v = new ImageVURL("v10.gif"); ImageVURL l = new ImageVURL("l10.gif"); virtualURLs.put("http://perm.webraveler2.banner/bannerhtml",bannerVurl); virtualURLs.put("http://perm.webraveler2.config.userconfig/",userConfig); virtualURLs.put("http://perm.webraveler2.config.userconfig/saveconfig/",saveConfig); virtualURLs.put("http://perm.webraveler2.config.help/",help); virtualURLs.put("http://perm.webraveler2.config.userconfig/logout/",logout); virtualURLs.put("http://perm.webraveler2.config.userconfig/pluginsequence/",pluginSeq); virtualURLs.put("http://perm.webraveler2.config/userconfig/activate_plugins/",activated); virtualURLs.put("http://perm.webraveler2.config/userconfig/enable_plugins/",enabled); virtualURLs.put("http://perm.webraveler2.config/userconfig/disable_plugins/",disabled); virtualURLs.put("http://perm.webraveler2.config.userconfig/turnoff/message/",message); virtualURLs.put("http://perm.webraveler2.config.userconfig/turnoff/",off); virtualURLs.put("http://perm.webraveler2.config.userconfig/turnon/",on); // Add virtual urls for the raveler banner images virtualURLs.put("http://perm.webraveler2.images/wr_logo_small.gif",logo); virtualURLs.put("http://perm.webraveler2.images/w10.gif",w); virtualURLs.put("http://perm.webraveler2.images/e10.gif",e); virtualURLs.put("http://perm.webraveler2.images/b10.gif",b); virtualURLs.put("http://perm.webraveler2.images/r10.gif",r); virtualURLs.put("http://perm.webraveler2.images/a10.gif",a); virtualURLs.put("http://perm.webraveler2.images/v10.gif",v); virtualURLs.put("http://perm.webraveler2.images/l10.gif",l); // Write the vurls to a file try { ObjectOutputStream objOut = new ObjectOutputStream(vurlFileOut); objOut.writeObject(virtualURLs); objOut.writeObject(pluginVURLs); objOut.flush(); objOut.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } // addRavelerVURLs(FileOutputStream) /** * Save the virtual urls to disk after * removing the temporary virtual urls */ public void saveVURLs() { /* Save the vurls to disk * However, we only want to save the permanent VURLs, so * remove the temporary virtual VURLs before saving */ Hashtable permVURLs = virtualURLs; for (Enumeration e = virtualURLs.keys() ; e.hasMoreElements() ; ) { String vurl = (String) e.nextElement(); /* Remove the part of the vurl that indicates whether it is temporary or permanent */ String flag = vurl.substring(8,12); if (flag.equals("temp")) { /* The vurl is temporary so remove it */ permVURLs.remove(vurl); } // if } // for try { FileOutputStream vurlFileOut = new FileOutputStream(Raveler.WEBRAVELER_HOME + "/data/vurls.dat"); ObjectOutputStream objOut = new ObjectOutputStream(vurlFileOut); objOut.writeObject(permVURLs); objOut.writeObject(pluginVURLs); objOut.flush(); objOut.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } //saveVURLs() /** * Adds a temporary virtual URL for a plugin - in memory only, per-session. * Flags the virtual URL as temporary. * * @param pluginName a string containing the name of the plugin * @param relativeURL a string containing the name of the relative URL * @param proc an Object containing the procedure with two parameters: * (1) the user name; (2) any available query string * Raveler needs to call when the virtual URL is requested * @return a string containing the full virtual URL */ public String addTemporaryVirtuaLURL(String pluginName, String relativeURL, VURL proc) { /* Generate the full URL */ String fullURL = "http://temp.webraveler2.plugin/"+pluginName+"/"+relativeURL+"/"; /* Add the virtual URL to the hash table if not already in hashtable*/ if (!virtualURLs.containsKey(fullURL)) { virtualURLs.put(fullURL,proc); return fullURL; } return null; }//addTemporaryVirtualURL(String,String,VURL) /** * Adds a temporary virtual URL - in memory only, per-session. * Flags the virtual URL as temporary. * * @param relativeURL a string containing the relative url * @param proc proc an Object containing the procedure with two parameters: * (1) the user name; (2) any available query string * Raveler needs to call when the virtual URL is requested * @return a string containing the full virtual URL */ public String addTemporaryVirtualURL(String relativeURL,VURL proc) { /* Generate the full URL */ String fullURL = "http://temp.webraveler2"+relativeURL+"/"; /* Add the virtual URL to the hash table if not already in hashtable*/ if (!virtualURLs.containsKey(fullURL)) { virtualURLs.put(fullURL,proc); return fullURL; } return null; } //addTemporaryVirtualURL(String,VURL) /** * Adds a permanent virtual URL - written to disk after the session ends * * @param pluginName a string containing the name of the plugin * @param relativeURL a string containing the name of the relative URL * @param proc an Object containing the procedure with two parameters: * (1) the user name; (2) any available query string * Raveler needs to call when the virtual URL is requested * @return a string containing the full virtual url */ public String addPermanentVirtualURL(String pluginName, String relativeURL, VURL proc) { /* Generate the full URL */ String fullURL = "http://perm.webraveler2.plugin/"+pluginName+"/"+relativeURL+"/"; /* Add the virtual URL to the hash table if not already in hashtable*/ if (!virtualURLs.containsKey(fullURL)) { virtualURLs.put(fullURL,proc); return fullURL; } return null; } //addPermanentVirtualURL(String,VURL) /** * Adds a virtual URL for the help page of a plugin * * @param pluginName a string containing the name of the plugin * @param proc an Object containing the procedure with two parameters: * (1) the user name; (2) any available query string * Raveler needs to call when the virtual URL is requested * @return a string containing the full virtual url */ public String addHelpVirtualURL(String pluginName, VURL proc) { /* Generate the full URL */ String fullURL = "http://perm.webraveler2.plugin/"+pluginName+"/help/"; /* Add the virtual URL to the hash table if not already in hashtable*/ if (!virtualURLs.containsKey(fullURL)) { virtualURLs.put(fullURL,proc); return fullURL; } return null; }//addHelpVirtualURL(String,VURL) /** * Adds a virtual URL for the configuration page of a plugin * * @param pluginName a string containing the name of the plugin * @param proc an Object containing the procedure with two parameters: * (1) the user name; (2) any available query string * Raveler needs to call when the virtual URL is requested * @return a string containing the full virtual url */ public String addConfigVirtualURL(String pluginName, VURL proc) { /* Generate the full URL */ String fullURL = "http://perm.webraveler2.plugin/"+pluginName+"/configure/"; /* Add the virtual URL to the hash table if not already in hashtable*/ if (!virtualURLs.containsKey(fullURL)) { virtualURLs.put(fullURL,proc); return fullURL; } return null; }//addConfigVirtualURL(String,VURL) /** * Sets the virtual URL associated with the plugin named on the banner. * When the user clicks on the plugin name, the user is brought to the * specified URL * * @param pluginName a string containing the name of the plugin * @param proc an Object containing the procedure with two parameters: * (1) the user name; (2) any available query string * Raveler needs to call when the virtual URL is requested * @return a string containing the full virtual url */ public String addPluginVirtualURL(String pluginName, VURL proc) { /* Generate the full URL */ String fullURL = "http://perm.webraveler2.plugin/"+pluginName+"/about/"; /* Add the virtual URL to the hash table if not already in hashtable*/ if (!virtualURLs.containsKey(fullURL)) { virtualURLs.put(fullURL,proc); return fullURL; } return null; }//addPluginVirtualURL(String,VURL) /** * Adds the virtual URL to the set of virtual URLs * associated with the plugin named on the banner. * * @param pluginName a string containing the name of the plugin * @param proc an Object containing the procedure with two parameters: * (1) the user name; (2) any available query string * Raveler needs to call when the virtual URL is requested * @param name a string containing the name to be displayed on the banner for the vurl * @return a string containing the full virtual url */ public String addBannerVirtualURL(String pluginName, VURL proc, String name) { /* Generate the full URL */ String fullURL = "http://perm.webraveler2.plugin/"+pluginName+"/"+name+"/"; /* Add the virtual URL to the plugins' set of virtual URLs */ Hashtable vurls; if (pluginVURLs.containsKey(pluginName)) { /* The plugin vurls hashtable already has a hash for this plugin */ vurls = (Hashtable) pluginVURLs.get(pluginName); vurls.put(fullURL,proc); } //if else { /* We must create a hash for this plugin and add it to the plugin vurls hashtable */ vurls = new Hashtable(); vurls.put(fullURL,proc); pluginVURLs.put(pluginName,vurls); } //else /* Add the virtual URL to the hash table if not already in hashtable*/ if (!virtualURLs.containsKey(fullURL)) { virtualURLs.put(fullURL,proc); return fullURL; } return null; }//addBannerVirtualURL(String,VURL,String) /** * Removes a temporary virtual URL * * @param pluginName a string containing the name of the plugin * @param vurlName a string containing the name of the virtual URL to be displayed on the banner * @return a boolean success value */ public boolean removeTemporaryVirtualURL(String pluginName, String vurlName) { /* Generate the full URL */ String fullURL = "http://temp.webraveler2.plugin/"+pluginName+"/"+vurlName+"/"; /* Remove the virtual URL to the hash table */ if (virtualURLs.remove(fullURL) == null) return false; return true; } //removeTemporaryVirtualURL(String,String) /** * Removes a permanent virtual URL * * @param pluginName a string containing the name of the plugin * @param relativeURL a string containing the name of the relative URL * @return a boolean success value */ public boolean removePermanentVirtualURL(String pluginName, String relativeURL) { /* Generate the full URL */ String fullURL = "http://perm.webraveler2.plugin/"+pluginName+"/"+relativeURL+"/"; /* Remove the virtual URL to the hash table */ if (virtualURLs.remove(fullURL) == null) return false; return true; } //removePermanentVirtualURL(String,String) /** * Determines whether a URL is a Virtual URL * * @param vurl a string containing some url * @return a boolean indicating whether or not the string represents a vURL */ //e.g., a VURL w/ query: http://perm.webraveler2.config.userconfig/login/!User=foo&Pass=foo public boolean isVirtualURL(String vurl) { for( Enumeration e=virtualURLs.keys() ; e.hasMoreElements() ; ) { String url = (String) e.nextElement(); //System.out.println("isVirtualURL: current url = \""+url+"\""); try { if (url.equals(vurl) || (vurl.substring(0,vurl.indexOf("!")).equals(url)) ) return true; } catch ( StringIndexOutOfBoundsException s ) { } // if "!" not found, keep looping }//for return false; } //isVirturalURL(String) public VURL getVirtualURL(String vurl) { return (VURL) (virtualURLs.get(vurl)); } //getVirtualURL(String) /** * Gets HTTPResponse from the virtual URL * * @param username a string containing the username of the current user * @param vurlString a string containing the virtual url * @param request an HTTPRequest containing the http request for the virtual url * @param queryString a string containg the query to the vurl * @return an HTTPResponse containing the response from the resolving the virtual url */ public HTTPResponse getVURLResponse(String username,String vurlString, HTTPRequest request, String queryString) { HTTPResponse response = ((VURL) virtualURLs.get(vurlString)).resolveVURL(username,queryString,request,ravel); return response; } //getVURL }