/* this belongs to the WebRaveler package */ package webraveler.plugin; import java.util.*; import webraveler.http.HTTPResponse; import webraveler.ravel.PluginAPI; import webraveler.ravel.Raveler; import webraveler.vurl.HelpVURL; /** * When the page returned in a response is for www.grinnellplans.com, * insert a link the user's directory page next to the username * * @author The Web Raveler Development Squad * @version 1.0 */ public class PlanstalkerPlugin extends webraveler.ravel.RavelerPlugin { /* variables */ String fullURL; byte[] imageBytes; /* constructors */ /** * The default constructor, called from WebRaveler2.Raveler */ public PlanstalkerPlugin() { name = "PlanstalkerPlugin"; mimeTypes.add("text/html"); // /* let's load the image at instantiation time */ // System.out.println("PlanstalkerPlugin: Getting monkeyFace image byte array."); // File image = new File("monkeyFace.jpg"); // FileInputStream imageIn; // // try { // imageIn = new FileInputStream(image); // }catch (IOException ioe) { // throw new Exception("PlanstalkerPlugin: couldn't get monkeyFace image file."); // }//catch // // ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); // int input; // while( (input = imageIn.read()) != -1 ) { // byteStream.write(input); /* write the input byte to the stream... inefficient, but who cares at instantiation time? */ // } // // imageBytes = byteStream.toByteArray(); // // /* cache a response for this image */ // HTTPResponse imageResponse = new HTTPResponse(); // imageResponse.localHeaders = new Hashtable(); // imageResponse.localHeaders.put("content-type","image/jpeg"); // imageResponse.status = 200; // imageResponse.reasonPhrase = "OK"; // imageResponse.localHeaders.put("content-length",""+imageBytes.length); } /* methods */ /** * Start executing the plugin when given a response * * @param response an HTTPResponse with the response to be modified * @return an HTTPResponse object, the result of running the plugin on the input response */ public HTTPResponse execute(HTTPResponse response, Hashtable attributes) { // System.out.println("PlanstalkerPlugin: executing."); // String username = new String(); // String text = new String(); // for ( Enumeration e = response.getHtmlPage().getTextBlocks() ; e.hasMoreElements() ; ) { // text = (String) e.nextElement(); // if ( text.equals("Plan of:") ) { // /* the next text block has the username. modify it accordingly */ // System.out.println("THE NEXT TEXT BLOCK IS THE USERNAME!!!!!"); // text = (String) e.nextElement(); // username = text.toString(); // System.out.println("username = '"+username+"'"); // String searchResultsURL = "http://db.grinnell.edu/campusdir/default.asp?transmit=true&blackboardref=false&LastName=&LNameSearch=startswith&FirstName=&FNameSearch=startswith&email="+username+"&campusphonenumber=&campusquery=&Homequery=&Department=&Major=&Gyear=&submit_search=Find"; // text = "" + text + ""; // // /* now, let's get the response for the search result page via // * ProxyClient, then find the href tag that starts with // * "displaydata.asp," and get THAT response. Then all we // * need to do is find the image link, get it, cache it, and // * link to the cached copy */ // HTTPResponse dirResponse; // HTTPRequest dirRequest = HTTPRequest.generateGetRequest(searchResultsURL); // ProxyClient dirProxy = new ProxyClient(dirRequest); // System.out.println("Getting Response for search results page"); // try { // dirResponse = dirProxy.getResponse(); // } catch ( IOException ioe ) { return response; } // // System.out.println("Finding right href element in search results."); // /* find the href starting with "displaydata.asp" */ // for ( Enumeration f = dirResponse.getHtmlPage().getAttributesByName("href").elements() ; f.hasMoreElements() ; ) { // Vector attribute = (Vector) f.nextElement(); // String value = ( (StringBuffer) attribute.lastElement() ).toString(); // System.out.println("value = '"+value+"'"); // if ( value.startsWith("\"displaydata.asp") ) { // String userPageURL = "http://db.grinnell.edu/campusdir/"+value.substring(1,value.length()-1); // dirRequest = HTTPRequest.generateGetRequest(userPageURL); // dirProxy = new ProxyClient(dirRequest); // // System.out.println("Getting response for user data page"); // try { // dirResponse = dirProxy.getResponse(); // } catch ( IOException ioe ) { return response; } // // System.out.println("Finding right img tag in the response"); // /* find the img tag, and steal the link */ // Vector imgTags = dirResponse.htmlPage.getTagsByName("img"); // Vector userImageTag; // try { // userImageTag = (Vector) imgTags.elementAt(5); // } catch ( NoSuchElementException nsee ) { // return response; // } // // /* we have the tag. . . so steal the link */ // System.out.println("USERIMAGETAG = '"+userImageTag.toString()+"'"); // Vector srcVec = (Vector) userImageTag.elementAt(1); // String srcVal = ((StringBuffer) srcVec.lastElement()).toString(); // if( srcVal.endsWith("gone.jpg\"") ) { // text.append("\n
| Directory image: | Unavailable"); // return response; // } // System.out.println("image src = '"+srcVal+"'"); // String srcURL = "http://temp.webraveler2.getunframeddocument/getunframeddocument!http://db.grinnell.edu/campusdir/"+srcVal.substring(1,srcVal.length()-1); // // text.append(" |
Directory image: |