001: /*
002: * ReflectHandler.java
003: *
004: * Brazil project web application Framework,
005: * export version: 1.1
006: * Copyright (c) 1998-2000 Sun Microsystems, Inc.
007: *
008: * Sun Public License Notice
009: *
010: * The contents of this file are subject to the Sun Public License Version
011: * 1.0 (the "License"). You may not use this file except in compliance with
012: * the License. A copy of the License is included as the file "license.terms",
013: * and also available at http://www.sun.com/
014: *
015: * The Original Code is from:
016: * Brazil project web application Framework release 1.1.
017: * The Initial Developer of the Original Code is: suhler.
018: * Portions created by suhler are Copyright (C) Sun Microsystems, Inc.
019: * All Rights Reserved.
020: *
021: * Contributor(s): cstevens, suhler.
022: *
023: * Version: 1.9
024: * Created by suhler on 98/09/14
025: * Last modified by suhler on 00/05/31 13:45:51
026: */
027:
028: package sunlabs.brazil.handler;
029:
030: import sunlabs.brazil.server.Handler; // Interface that defines a handler
031: import sunlabs.brazil.server.Request; // Encapulates an http request
032: import sunlabs.brazil.server.Server; // Contains data specific to the server
033:
034: import sunlabs.brazil.util.http.HttpUtil;
035:
036: import java.io.IOException; // Request methods can throw this
037:
038: /* Other classes used by this handler */
039:
040: import java.util.Dictionary;
041: import java.util.Enumeration;
042: import java.util.Hashtable;
043:
044: /**
045: * Handler for reflecting query data back to the client.
046: * This is the example handler to demonstrate how a typical
047: * handler is witten. If query data is present, it is formatted into
048: * an HTML table, and displayed to the user.
049: *
050: * @author Stephen Uhler
051: * @version 1.9, 00/05/31
052: */
053:
054: public class ReflectHandler implements Handler {
055:
056: /**
057: * Initialize the handler.
058: * Handler objects are created by the server using newInstance().
059: * The init method is called first, and exactly one for each instance,
060: * and may be used for one-time initializations.
061: * This handler doesn't require any.
062: *
063: * @param server A reference to the server.
064: * @param prefix A string identifying this instance of the
065: * handler. It is used by the
066: * {@link sunlabs.brazil.server.ChainHandler} to
067: * provide the prefix to be prepended onto each
068: * property intended for this handler.
069: * @return true Only if the handler is successfully initialized.
070: */
071:
072: public boolean init(Server server, String prefix) {
073: return true;
074: }
075:
076: /**
077: * Dispatch and handle the request.
078: * This version justs reflects the HTTP header information.
079: * It is commonly placed downstream of the
080: * {@link CgiHandler} to allow HTML forms to be tested before
081: * the cgi script is written.
082: * @param request The request object contains all of the information
083: * about the request, as well as methods to manipulate
084: * it. Although multiple threads may call this method
085: * connurrently, each will have its own request object.
086: */
087:
088: public boolean respond(Request request) throws IOException {
089:
090: // If there is no query information, return false, allowing
091: // another handler in the chain to process the request.
092:
093: Dictionary table = request.getQueryData(null);
094: if (table.size() == 0) {
095: return false;
096: }
097: String queryTable = formatTable(table, "Query Data");
098:
099: String result = "<title>Request Reflector</title>"
100: + "<h1>Request Summary</h1>" + queryTable
101: + formatTable(request.headers, "Http Data")
102: + "<h1>Other Stuff</h1>" + "method=<b>"
103: + request.method + "</b><br>" + "url=<b>" + request.url
104: + "</b><br>" + "version=<b>" + request.protocol
105: + "</b><br>"
106: + formatTable(request.props, "Server State");
107: request.sendResponse(result);
108: return true;
109: }
110:
111: /**
112: * Turn a hash table into html format. This is a static method
113: * so it may be used in other handlers.
114: * @param table The table to format
115: * @return The html fragment
116: */
117:
118: public static String formatTable(Dictionary data, String caption) {
119: StringBuffer html = new StringBuffer();
120: html.append("<table border=1>\r\n");
121: html.append("<caption>" + caption + "</caption>\r\n");
122: Enumeration keys = data.keys();
123: while (keys.hasMoreElements()) {
124: String key = (String) keys.nextElement();
125: html.append("<tr><th>" + HttpUtil.htmlEncode(key)
126: + "</th><td>"
127: + HttpUtil.htmlEncode((String) data.get(key))
128: + "</td></tr>\r\n");
129: }
130: html.append("</table>\r\n");
131: return html.toString();
132: }
133: }
|