001: //========================================================================
002: //Copyright 2006 Mort Bay Consulting Pty. Ltd.
003: //------------------------------------------------------------------------
004: //Licensed under the Apache License, Version 2.0 (the "License");
005: //you may not use this file except in compliance with the License.
006: //You may obtain a copy of the License at
007: //http://www.apache.org/licenses/LICENSE-2.0
008: //Unless required by applicable law or agreed to in writing, software
009: //distributed under the License is distributed on an "AS IS" BASIS,
010: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: //See the License for the specific language governing permissions and
012: //limitations under the License.
013: //========================================================================
014:
015: package org.mortbay.jetty;
016:
017: import java.io.ByteArrayOutputStream;
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.io.OutputStream;
021: import java.io.OutputStreamWriter;
022: import java.io.Writer;
023: import java.util.Enumeration;
024:
025: import javax.servlet.ServletException;
026: import javax.servlet.http.Cookie;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.mortbay.jetty.handler.AbstractHandler;
031: import org.mortbay.util.StringUtil;
032: import org.mortbay.util.ajax.Continuation;
033: import org.mortbay.util.ajax.ContinuationSupport;
034:
035: /* ------------------------------------------------------------ */
036: /** Dump request handler.
037: * Dumps GET and POST requests.
038: * Useful for testing and debugging.
039: *
040: * @version $Id: DumpHandler.java,v 1.14 2005/08/13 00:01:26 gregwilkins Exp $
041: * @author Greg Wilkins (gregw)
042: */
043: public class DumpHandler extends AbstractHandler {
044: String label = "Dump HttpHandler";
045:
046: public DumpHandler() {
047: }
048:
049: public DumpHandler(String label) {
050: this .label = label;
051: }
052:
053: /* ------------------------------------------------------------ */
054: /*
055: * @see org.mortbay.jetty.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
056: */
057: public void handle(String target, HttpServletRequest request,
058: HttpServletResponse response, int dispatch)
059: throws IOException, ServletException {
060: Request base_request = (request instanceof Request) ? (Request) request
061: : HttpConnection.getCurrentConnection().getRequest();
062:
063: if (!isStarted())
064: return;
065:
066: if (request.getParameter("continue") != null) {
067: Continuation continuation = ContinuationSupport
068: .getContinuation(request, null);
069: continuation.suspend(Long.parseLong(request
070: .getParameter("continue")));
071: }
072:
073: base_request.setHandled(true);
074: response.setHeader(HttpHeaders.CONTENT_TYPE,
075: MimeTypes.TEXT_HTML);
076:
077: OutputStream out = response.getOutputStream();
078: ByteArrayOutputStream buf = new ByteArrayOutputStream(2048);
079: Writer writer = new OutputStreamWriter(buf,
080: StringUtil.__ISO_8859_1);
081: writer.write("<html><h1>" + label + "</h1>");
082: writer.write("<pre>\npathInfo=" + request.getPathInfo()
083: + "\n</pre>\n");
084: writer.write("<pre>\ncontentType=" + request.getContentType()
085: + "\n</pre>\n");
086: writer.write("<pre>\nencoding="
087: + request.getCharacterEncoding() + "\n</pre>\n");
088: writer.write("<h3>Header:</h3><pre>");
089: writer.write(request.toString());
090: writer.write("</pre>\n<h3>Parameters:</h3>\n<pre>");
091: Enumeration names = request.getParameterNames();
092: while (names.hasMoreElements()) {
093: String name = names.nextElement().toString();
094: String[] values = request.getParameterValues(name);
095: if (values == null || values.length == 0) {
096: writer.write(name);
097: writer.write("=\n");
098: } else if (values.length == 1) {
099: writer.write(name);
100: writer.write("=");
101: writer.write(values[0]);
102: writer.write("\n");
103: } else {
104: for (int i = 0; i < values.length; i++) {
105: writer.write(name);
106: writer.write("[" + i + "]=");
107: writer.write(values[i]);
108: writer.write("\n");
109: }
110: }
111: }
112:
113: String cookie_name = request.getParameter("CookieName");
114: if (cookie_name != null && cookie_name.trim().length() > 0) {
115: String cookie_action = request.getParameter("Button");
116: try {
117: Cookie cookie = new Cookie(cookie_name.trim(), request
118: .getParameter("CookieVal"));
119: if ("Clear Cookie".equals(cookie_action))
120: cookie.setMaxAge(0);
121: response.addCookie(cookie);
122: } catch (IllegalArgumentException e) {
123: writer.write("</pre>\n<h3>BAD Set-Cookie:</h3>\n<pre>");
124: writer.write(e.toString());
125: }
126: }
127:
128: writer.write("</pre>\n<h3>Cookies:</h3>\n<pre>");
129: Cookie[] cookies = request.getCookies();
130: if (cookies != null && cookies.length > 0) {
131: for (int c = 0; c < cookies.length; c++) {
132: Cookie cookie = cookies[c];
133: writer.write(cookie.getName());
134: writer.write("=");
135: writer.write(cookie.getValue());
136: writer.write("\n");
137: }
138: }
139:
140: writer.write("</pre>\n<h3>Attributes:</h3>\n<pre>");
141: Enumeration attributes = request.getAttributeNames();
142: if (attributes != null && attributes.hasMoreElements()) {
143: while (attributes.hasMoreElements()) {
144: String attr = attributes.nextElement().toString();
145: writer.write(attr);
146: writer.write("=");
147: writer.write(request.getAttribute(attr).toString());
148: writer.write("\n");
149: }
150: }
151:
152: writer.write("</pre>\n<h3>Content:</h3>\n<pre>");
153: byte[] content = new byte[4096];
154: int len;
155: try {
156: InputStream in = request.getInputStream();
157: while ((len = in.read(content)) >= 0)
158: writer.write(new String(content, 0, len));
159: } catch (IOException e) {
160: writer.write(e.toString());
161: }
162:
163: writer.write("</pre>");
164: writer.write("</html>");
165:
166: // commit now
167: writer.flush();
168: response.setContentLength(buf.size() + 1000);
169: buf.writeTo(out);
170:
171: buf.reset();
172: writer.flush();
173: for (int pad = 998 - buf.size(); pad-- > 0;)
174: writer.write(" ");
175: writer.write("\015\012");
176: writer.flush();
177: buf.writeTo(out);
178:
179: response.setHeader("IgnoreMe", "ignored");
180: }
181: }
|