package webraveler.plugin; import java.util.*; import webraveler.html.HTMLPage2; import webraveler.http.HTTPRequest; import webraveler.http.HTTPResponse; import webraveler.ravel.PluginAPI; import webraveler.ravel.Raveler; import webraveler.ravel.RavelerUser; import webraveler.vurl.GenericVURL; /** * A quick demo of a quest with an accompanying quiz. * * @author Samuel A. Rebelsky * @version 1.0 of June 2005 */ public class QuestQuizPlugin extends webraveler.ravel.RavelerPlugin { // +--------+------------------------------------------------------------ // | Fields | // +--------+ // +--------------+------------------------------------------------------ // | Constructors | // +--------------+ public QuestQuizPlugin() { name = "QuestQuizPlugin"; mimeTypes.add("text/html"); } // QuestQuizPlugin // +---------+----------------------------------------------------------- // | Methods | // +---------+ public void setDefaultAttributes() { if (this.attributes == null) this.attributes = new Hashtable(); this.attributes.put("status","foo"); } // setDefaultAttributes public boolean initialize() { if (this.attributes == null) this.attributes = new Hashtable(); String submitVURL = PluginAPI.addPermanentVirtualURL( ravel, name, "submit", new GenericVURL() { public HTTPResponse resolveVURL(String username, String query, HTTPRequest request, Raveler ravel) { System.out.println("QuickQuiz: Received " + query); try { RavelerUser user = ravel.getUser(username); int count = Integer.parseInt(user.getAttribute(name,"quizstatus")); count = count + 1; user.modifyAttribute(name,"quizstatus",""+count); System.out.println("QuickQuiz: Submit succeeded"); } catch (Exception e) { System.out.println("QuickQuiz: Submit failure: " + e.toString()); } webraveler.samr.SendMail.send("rebelsky@grinnell.edu", username + " submitted URL " + query, query); System.out.println("User submitted: " + query); return standardPage("Submitted

Your URL was submitted. Return to the page and reload.

"); } // resolveVURL(String, String, HTTPRequest, ravel) } // new GenericVURL() ); this.attributes.put("submit",submitVURL); String resetVURL = PluginAPI.addPermanentVirtualURL( ravel, name, "reset", new GenericVURL() { public HTTPResponse resolveVURL(String username, String query, HTTPRequest request, Raveler ravel) { try { RavelerUser user = ravel.getUser(username); user.modifyAttribute(name,"quizstatus","0"); System.out.println("Reset quiz"); } catch (Exception e) { System.out.println("Failed to eset quiz because " + e.toString()); } return standardPage("Submitted

Your quiz has been reset. Return to the page and reload.

"); } // resolveVURL(String, String, HTTPRequest, ravel) } // new GenericVURL() ); this.attributes.put("reset",resetVURL); return true; }// initialize() public HTTPRequest execute (HTTPRequest request, Hashtable attributes, String userName) { System.out.println("QuestQuiz: Processing request"); String status = (String) attributes.get("quizstatus"); if (status == null) { System.out.println("QuestQuiz: No status. Setting it."); status = "0"; attributes.put("quizstatus", status); } System.out.println("QuestQuiz: Finished processing request"); return request; }//execute() public HTTPResponse execute (HTTPResponse response, Hashtable userAttributes, String userName){ System.out.println("QuestQuiz: Processing response"); String status = (String) userAttributes.get("quizstatus"); String alpha = "
" + "QuestQuiz: Your goal is to find something related to Curtis Bonk.
" + "Status: " + status + " pages submitted." + " [Submit this page]" + " [Reset]" + "
" ; String omega = "
"; String body = ""; if (status.equals("0")) { body = "You need to submit at least one page. Please keep looking.
"; } // if (status.equals("0") else { body = "You have submitted a page. Feel free to submit more." ; } // if (!status.equals("0")); response.getHtmlPage().insertAtTop(alpha + body + omega); response.correctContentLength(); System.out.println("QuestQuiz: DONE"); return response; } //execute(HTTPResponse) public void terminate() { } // terminate() // +-----------------+--------------------------------------------------- // | Private Methods | // +-----------------+ }// QuestQuizPlugin