/* this belongs to the WebRaveler package */ package webraveler.vurl; import java.util.*; import webraveler.http.HTTPRequest; import webraveler.http.HTTPResponse; import webraveler.ravel.Raveler; import webraveler.ravel.RavelerException; import webraveler.ravel.RavelerUser; /** * Changes the user's prefences for the plugin * * @author The Web Raveler Development Squad * @version 1.0 */ public class PluginChangePref implements VURL { String pluginName; Hashtable specialChars; /** * Constructs a PluginChangePref given the name of the plugin * * @param pluginName a String containing the name of the plugin */ public PluginChangePref (String pluginName) { this.pluginName = pluginName; specialChars = new Hashtable(); setupSpecialChars(); } // PluginChangePref() /** * Updates the user's preferences for the plugin */ public HTTPResponse resolveVURL(String username, String query, HTTPRequest request, Raveler ravel) { /* Generate response */ String responseString = new String(""); responseString += "" + Raveler.ravelerCSS; responseString += ""+pluginName+" Configuration"; responseString += "

"+pluginName+" Configuration

"; /* Get the RavelerUser associated with the given username */ RavelerUser user = new RavelerUser(); try { user = ravel.getUser(username); /* Parse query string */ int equalsIndex; int amperIndex=0; String name; String value; while ((equalsIndex = query.indexOf("=",amperIndex)) != -1) { /* Check if we are past the first part, if so increment the * amperIndex so that we don"t include the & in the pluginName */ if (amperIndex != 0) amperIndex++; name = query.substring(amperIndex,equalsIndex); amperIndex = query.indexOf("&",equalsIndex); /* Check if we are at the last part, if so, set the amperIndex * to the length of the query */ if (amperIndex == -1) amperIndex = query.length(); value = query.substring(equalsIndex+1,amperIndex); value = handleSpecialChars(value); user.modifyAttribute(pluginName,name,value); } // while responseString += "

Preferences changed

"; } catch (RavelerException re) { responseString += "

"+re.getMessage()+"

"; } responseString += ""; return new HTTPResponse("HTTP/1.0",200,"OK",responseString.getBytes(),new Hashtable()); } // resolveVURL /** * Converts the encoded special characters in the query string to their original form * * @param value a string to be decoded * @return a String with the decoded value */ public String handleSpecialChars(String value) { int percentIndex=0; while ((percentIndex= value.indexOf('%',percentIndex)) != -1) { String encod = value.substring(percentIndex,percentIndex+3); if (specialChars.containsKey(encod)) { String unencod = (String) specialChars.get(encod); value = value.substring(0,percentIndex)+unencod+value.substring(percentIndex+3,value.length()); } //if percentIndex++; } //while /* Change all + to spaces */ value = value.replace('+',' '); return value; } //handleSpecialChars(String) /** * Sets up the hash table of special characters and their equivalent encoding */ public void setupSpecialChars() { /* Put the special characters and their encoding in hash */ specialChars.put("%22"," "); specialChars.put("%5B","["); specialChars.put("%5D","]"); specialChars.put("%7B","{"); specialChars.put("%7D","}"); specialChars.put("%26","&"); specialChars.put("%3C","<"); specialChars.put("%3E",">"); specialChars.put("%2B","+"); specialChars.put("%5C","\\"); specialChars.put("%2F","/"); specialChars.put("%28","("); specialChars.put("%29",")"); specialChars.put("%3F","?"); specialChars.put("%24","$"); specialChars.put("%5E","^"); specialChars.put("%25","%"); specialChars.put("%3A",":"); specialChars.put("%3D","="); specialChars.put("%21","!"); specialChars.put("%40","@"); specialChars.put("%23","#"); specialChars.put("%3B",";"); specialChars.put("%27","'"); specialChars.put("%22","\""); specialChars.put("%2C",","); specialChars.put("%7E","~"); } } // class PluginChangePref