/* * Created on Nov 26, 2004 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package webraveler.ravel; import java.io.File; import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; import webraveler.http.HTTPRequest; import webraveler.http.HTTPResponse; /** * A PluginStack handles running the plugins on * various requests/responses, as well as the * bookkeeping operations such as ordering, etc. * * @author martinsa */ public class PluginStack { private Vector plugins; private Raveler ravel; private boolean DEBUG = false; /** * Make a new PluginStack given a Raveler instance * * @param ravel */ public PluginStack(Raveler ravel) { plugins = new Vector(); this.ravel = ravel; gatherPlugins(); }//PluginStack(Raveler) /** * Return an Enumeration of all the plugins we know about. * * @return an Enumeration of the loaded plugins */ public Enumeration getPlugins() { return plugins.elements(); }//Enumeration plugins() /** * */ public void initializePlugins() { /* Initialize the plugins */ boolean success; for( Enumeration e=getPlugins() ; e.hasMoreElements() ; ) { RavelerPlugin plugin = (RavelerPlugin) e.nextElement(); success = plugin.initialize(); } } /** * Send the request through the applicable plugins, returning the result. * * @param user the RavelerUser making the request * @param request an HTTPRequest object submitted for modifications * @param ravel the Raveler object against which we're matching plugin information * @return an HTTPRequest object, the result of running the plugins on the request */ public HTTPRequest passThroughPlugins(RavelerUser user, HTTPRequest request) throws PluginCallbackException { /* does the user have Raveler on or off? */ if (!user.ravelOn) { if (DEBUG) System.out.println("PluginStack: Raveler turned off, not raveling page"); return request; } //if else { /* Send the request to the plugins */ for (Enumeration e = plugins.elements() ; e.hasMoreElements() ;) { RavelerPlugin plugin = (RavelerPlugin) e.nextElement(); /* only execute the user's active plugins */ if ( !user.activePlugins.contains(plugin.name)) continue; try { request = plugin.execute(request,(Hashtable) user.preferences.get(plugin.name), user.username); } catch (RavelerWarningException rwe) { if ( DEBUG ) System.out.println("PluginStack: plugin "+plugin.name+" generated an warning. Attempting to pass along modified content."); /* Print the warning */ RavelerWarning warn = rwe.getWarning(); String message; if (warn != null) {} else if ((message = rwe.getMessage()) != null) warn = new RavelerWarning(message); else warn = new RavelerWarning(100); //ravel.printWarning(plugin.name,warn,request.uriString()); /* Try to pass along the modified content */ HTTPRequest errRequest = rwe.getRequest(); if (errRequest != null) { request = errRequest; } //if } //catch (RavelerWarningException) catch(RavelerErrorException ree) { if (DEBUG) System.out.println("PluginStack: Plugin "+plugin.name+" generated an error. Skipping to next plugin."); /* Print the error */ RavelerError err = ree.getError(); String message; if (err != null) {} else if (( message = ree.getMessage()) != null) err = new RavelerError(message); else err = new RavelerError(100); //ravel.printError(plugin.name,err,request.uriString()); } //catch } // for }//else return request; }//passThroughPlugins(request) /** * Send the response through the applicable plugins, returning the result. * * @param currentUser the RavelerUser making the request * @param response an HTTPResponse object submitted for modifications * @param url a String containing the url being raveled * @return an HTTPResponse object, the result of running the plugins on the response */ public HTTPResponse passThroughPlugins(RavelerUser currentUser, HTTPResponse response, String url) { HTTPResponse modResponse = response; /* change the original */ modResponse.uriString = url; /* let's send the response through the plugin vector ... */ String contentType = modResponse.getHeader("content-type"); if (contentType != null) contentType = contentType.toLowerCase(); int pluginNum = currentUser.activePlugins.size(); for (int i = 0; i < pluginNum ; i++) { String pluginName = (String) currentUser.activePlugins.get(i); for (Enumeration e = plugins.elements() ; e.hasMoreElements() ;) { RavelerPlugin plugin = (RavelerPlugin) e.nextElement(); // System.out.println("CONTENT TYPE: "+contentType); if ( contentType != null ) { int index = contentType.indexOf(";"); if ( index != -1 ) { contentType = contentType.substring(0, index); }//if } if ( pluginName.equals(plugin.name) && contentType != null && plugin.mimeTypes.contains(contentType)) { try { modResponse = plugin.execute(modResponse,(Hashtable) currentUser.preferences.get(plugin.name), currentUser.username); } catch (RavelerWarningException rwe) { if (DEBUG) System.out.println("GetUnframedDocumentVURL: plugin "+plugin.name+" generated an warning."); /* Print the warning */ RavelerWarning warn = rwe.getWarning(); String message; if (warn != null) {} else if ((message = rwe.getMessage()) != null) warn = new RavelerWarning(message); else warn = new RavelerWarning(100); //ravel.printWarning(plugin.name,warn,url); /* Try to pass along the modified content */ HTTPResponse errResponse = rwe.getResponse(); if (errResponse != null) { if (DEBUG) System.out.println("GetUnframedDocumentVURL: Attempting to pass along modified content."); modResponse = errResponse; } //if } //catch (RavelerWarningException) catch (RavelerErrorException ree) { if ( DEBUG ) System.out.println("GetUnframedDocumentVURL: plugin "+plugin.name+" generated an error. Skipping to next plugin."); /* Print the error */ RavelerError err = ree.getError(); String message; if (err != null) {} else if (( message = ree.getMessage()) != null) err = new RavelerError(message); else err = new RavelerError(100); //ravel.printError(plugin.name,err,url); }//catch (RavelerWarningException) catch( PluginCallbackException pce ) { if ( DEBUG ) System.out.println("GUDVURL: Plugin generated callback response."); return pce.getResponse(); }//catch } //if } // for }// for return modResponse; }//passThroughPlugins(response) /** * Fill the plugins Vector with plugins from the * user's plugin directory */ private void gatherPlugins() { File pluginDir = new File(Raveler.WEBRAVELER_HOME + "/bin/webraveler/plugin"); String pluginFileList[] = pluginDir.list(); String fileName; Class pluginClass; boolean added; int count = 0; if (pluginFileList == null) return; //instantiate Plugin objects as necessary //and add 'em to the plugins Vector for ( int i=0,j=pluginFileList.length; i < j; i++ ) { fileName = pluginFileList[i]; if (fileName.endsWith("Plugin.class")) { try{ pluginClass = Class.forName("webraveler.plugin."+fileName.substring(0,fileName.length()-6)); added = plugins.add(pluginClass.newInstance()); count++; System.out.println("Loaded " + fileName); }catch(Exception e) { e.printStackTrace(); } }//if }//for for (Enumeration e = plugins.elements(); e.hasMoreElements() ;) { RavelerPlugin plugin = (RavelerPlugin) e.nextElement(); plugin.ravel = ravel; } //for if ( DEBUG ) System.out.println("PluginStack: Found "+count+" plugins in plugin directory"); }//void gatherPlugins() }//class PluginStack