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; /** * Some of the basic operations that many VURLs will want to use. * You can provide these operations by extending this class. * * @author Samuel A. Rebelsky * @version 1.0 */ public abstract class GenericVURL implements VURL { // +--------+------------------------------------------------------------ // | Fields | // +--------+ Hashtable specialChars; // +--------------+------------------------------------------------------ // | Constructors | // +--------------+ /** * Constructs a GenericVURL. */ public GenericVURL() { this.specialChars = new Hashtable(); this.setupSpecialChars(); } // PluginChangePref() // +---------+----------------------------------------------------------- // | Methods | // +---------+ /** * Generate a standard page response. * * @param htmlContents * The contents of that page. */ protected HTTPResponse standardPage(String htmlContents) { return new HTTPResponse("HTTP/1.0",200,"OK",htmlContents.getBytes(),new Hashtable()); } // standardPage(String) /** * Generate a redirect page. */ protected HTTPResponse redirectPage(String uri) { HTTPResponse red = new HTTPResponse("HTTP/1.1",307,"TEMPORARY REDIRECT",uri.getBytes(),new Hashtable()); red.setHeader("Location", uri); red.correctContentLength(); return red; } // redirectPage(String) /** * 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 */ protected 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) // +-----------------+--------------------------------------------------- // | Private Methods | // +-----------------+ /** * Sets up the hash table of special characters and their equivalent encoding */ private 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 GenericVURL