001: package simpleorm.simplewebapp.requestlet;
002:
003: import simpleorm.simplewebapp.core.WException;
004:
005: import javax.servlet.http.HttpServletRequest;
006: import javax.servlet.http.HttpServletResponse;
007: import javax.servlet.http.HttpServlet;
008: import java.io.PrintWriter;
009: import java.net.URLEncoder;
010:
011: /**
012: /**
013: * An instance of this class is created for each call of Servlet.doGet|doPost.
014: * Contains utility routines needed to make pages without JSPs.
015: */
016: public abstract class WRequestlet {
017:
018: HttpServletRequest request;
019: HttpServletResponse response;
020: HttpServlet servlet;
021: public PrintWriter out;
022: boolean isGet = false; // else Post.
023:
024: public <R extends WRequestlet> R initRequestlet(
025: HttpServletRequest request, HttpServletResponse response,
026: HttpServlet servlet, boolean get) {
027: this .request = request;
028: this .response = response;
029: this .servlet = servlet;
030: isGet = get;
031: return (R) this ;
032: }
033:
034: public void doGetPost() {
035: try {
036: out = response.getWriter();
037: onGetPost();
038: out.close();
039: } catch (RuntimeException re) {
040: throw re;
041: } catch (Throwable ex) {
042: throw new WException(ex);
043: }
044: }
045:
046: abstract public void onGetPost() throws Throwable;
047:
048: /**
049: * Outputs raw HTML, does NOT quote it.
050: */
051: public void outRaw(String html) {
052: out.print(html);
053: }
054:
055: /**
056: * outEscaped(data, false)
057: */
058: public void outEscaped(String data) {
059: outEscaped(data, false);
060: }
061:
062: /**
063: * Outputs data after escaping it so that '<' becomes '&lt;' etc.<p>
064: * <p/>
065: * Converts spaces to '&nbsp;' iff nbsp<p>
066: * <p/>
067: * Amazing that I need to write this myself!<p>
068: *
069: * @see #outRaw
070: */
071: public void outEscaped(String data, boolean nbsp) {
072: if (data == null)
073: out.print("");
074: else {
075: for (int dx = 0; dx < data.length(); dx++) {
076: char ch = data.charAt(dx);
077: switch (ch) {
078: case ' ':
079: out.print(nbsp ? " " : " ");
080: break;
081: case '<':
082: out.print("<");
083: break;
084: case '>':
085: out.print(">");
086: break;
087: case '\"':
088: out.print(""");
089: break;
090: case '\'':
091: out.print("'");
092: break;
093: case '\\':
094: out.print("\");
095: break;
096: case '&':
097: out.print("&");
098: break;
099: default:
100: out.print(ch); // Hopefully will be inlined
101: break;
102: }
103: }
104: }
105: }
106:
107: /**
108: * URL Encodes string in appropriate? encoding.
109: * used for ?=value Get parameters on links
110: */
111: public void outURLEncoded(String data) throws Exception {
112: out.print(URLEncoder.encode(data, "UTF-8"));
113: //response.encode();
114: }
115:
116: /**
117: * Convert a nibble to a hex character
118: * @param nibble the nibble to convert.
119: */
120: public static char toHex(int nibble) {
121: return hexDigit[(nibble & 0xF)];
122: }
123:
124: /** A table of hex digits */
125: private static final char[] hexDigit = { '0', '1', '2', '3', '4',
126: '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
127:
128: /** Mainly to include a JSP page after the main page processing. */
129: public void includeRenderer(String url) throws Exception {
130: servlet.getServletConfig().getServletContext()
131: .getRequestDispatcher(url).include(request, response);
132: // Unclear what the difference between .forward and .inlcude actually is.
133: // Both happen in line, immediately -- forward is not deferred until servlet exits.
134: // forward changes request object to reflect forward's url.
135: // inlclude prefents page status changes from inlcuded page.
136: // Both ways exceptions thrown, but in Tomcat 5.5 converted to useless JasperException,
137: // only useful stack trace in Tomcat logs (fixed 6.0, both forward & include)
138: }
139: }
|