001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: EnhydraSnoopServlet.java,v 1.2 2006-06-15 14:07:00 sinisa Exp $
022: */
023:
024: package org.enhydra.servlet.servlets;
025:
026: import java.io.IOException;
027: import java.util.Enumeration;
028:
029: import javax.servlet.ServletException;
030: import javax.servlet.ServletInputStream;
031: import javax.servlet.ServletOutputStream;
032: import javax.servlet.http.HttpServlet;
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpServletResponse;
035: import javax.servlet.http.HttpUtils;
036:
037: /**
038: * Snoop servlet. This servlet`echos back the request line,
039: * headers and posted content that were sent by the client
040: * This servlet has been written using the Sun Snoop Servlet
041: * as model
042: *
043: * @version 1.00, 06/30/97
044: * @author Christophe Ney
045: */
046: public class EnhydraSnoopServlet extends HttpServlet {
047:
048: // when the url is requested with a POST method
049: public void doPost(HttpServletRequest req, HttpServletResponse res)
050: throws ServletException, IOException {
051: //value chosen to limit denial of service
052: if (req.getContentLength() > 8 * 1024) {
053: res.setContentType("text/html");
054: ServletOutputStream out = res.getOutputStream();
055: out.println("<html><head><title>Too big</title></head>");
056: out.println("<body><h1>Error - content length >8k not ");
057: out.println("</h1></body></html>");
058: } else {
059: doGet(req, res);
060: ServletOutputStream out = res.getOutputStream();
061: out.println("<html><body><h1>Posted data:</h1>");
062: out.println("<pre>");
063: printPostedData(out, req);
064: out.println("</pre></body></html>");
065: }
066: }
067:
068: // when the url is requested with a GET method
069: public void doGet(HttpServletRequest req, HttpServletResponse res)
070: throws ServletException, IOException {
071: res.setContentType("text/html");
072: ServletOutputStream out = res.getOutputStream();
073: out.println("<html>");
074: out.println("<head><title>Snoop Servlet</title></head>");
075: out.println("<body>");
076:
077: out.println("<h1>Requested URL:</h1>");
078: out.println("<pre>");
079: out.println(HttpUtils.getRequestURL(req).toString());
080: out.println("</pre>");
081:
082: Enumeration enumeration = getServletConfig()
083: .getInitParameterNames();
084: if (enumeration != null) {
085: boolean first = true;
086: while (enumeration.hasMoreElements()) {
087: if (first) {
088: out.println("<h1>Init Parameters</h1>");
089: out.println("<pre>");
090: first = false;
091: }
092: String param = (String) enumeration.nextElement();
093: out.println(" " + param + ": "
094: + getInitParameter(param));
095: }
096: out.println("</pre>");
097: }
098:
099: out.println("<h1>Request information:</h1>");
100: out.println("<pre>");
101: print(out, "Request method", req.getMethod());
102: print(out, "Request URI", req.getRequestURI());
103: print(out, "Request protocol", req.getProtocol());
104: print(out, "Servlet path", req.getServletPath());
105: print(out, "Path info", req.getPathInfo());
106: print(out, "Path translated", req.getPathTranslated());
107: print(out, "Query string", req.getQueryString());
108: print(out, "Content length", req.getContentLength());
109: print(out, "Content type", req.getContentType());
110: print(out, "Server name", req.getServerName());
111: print(out, "Server port", req.getServerPort());
112: print(out, "Remote user", req.getRemoteUser());
113: print(out, "Remote address", req.getRemoteAddr());
114: print(out, "Remote host", req.getRemoteHost());
115: print(out, "Authorization scheme", req.getAuthType());
116: out.println("</pre>");
117:
118: Enumeration e = req.getHeaderNames();
119: if (e.hasMoreElements()) {
120: out.println("<h1>Request headers:</h1>");
121: out.println("<pre>");
122: while (e.hasMoreElements()) {
123: String name = (String) e.nextElement();
124: out.println(" " + name + ": " + req.getHeader(name));
125: }
126: out.println("</pre>");
127: }
128:
129: e = req.getParameterNames();
130: if (e.hasMoreElements()) {
131: out
132: .println("<h1>Servlet parameters (Single Value style):</h1>");
133: out.println("<pre>");
134: while (e.hasMoreElements()) {
135: String name = (String) e.nextElement();
136: out
137: .println(" " + name + " = "
138: + req.getParameter(name));
139: }
140: out.println("</pre>");
141: }
142:
143: e = req.getParameterNames();
144: if (e.hasMoreElements()) {
145: out
146: .println("<h1>Servlet parameters (Multiple Value style):</h1>");
147: out.println("<pre>");
148: while (e.hasMoreElements()) {
149: String name = (String) e.nextElement();
150: String vals[] = (String[]) req.getParameterValues(name);
151: if (vals != null) {
152: out.print("<b> " + name + " = </b>");
153: out.println(vals[0]);
154: for (int i = 1; i < vals.length; i++)
155: out.println(" " + vals[i]);
156: }
157: out.println("<p>");
158: }
159: out.println("</pre>");
160: }
161:
162: out.println("</body></html>");
163: }
164:
165: private void print(ServletOutputStream out, String name,
166: String value) throws IOException {
167: out.print(" " + name + ": ");
168: out.println(value == null ? "<none>" : value);
169: }
170:
171: private void print(ServletOutputStream out, String name, int value)
172: throws IOException {
173: out.print(" " + name + ": ");
174: if (value == -1) {
175: out.println("<none>");
176: } else {
177: out.println(value);
178: }
179: }
180:
181: private void printPostedData(ServletOutputStream out,
182: HttpServletRequest req) throws IOException {
183: ServletInputStream in = req.getInputStream();
184: out.print("\n ");
185: for (int i = 0; i < req.getContentLength(); i++) {
186: int b = in.read();
187: switch (b) {
188: case '\n':
189: out.print("\n ");
190: break;
191: case '<':
192: out.print("<");
193: break;
194: case '>':
195: out.print(">");
196: break;
197: default:
198: out.write(b);
199: }
200: }
201: out.print("\n");
202: }
203:
204: private static final String UNKNOWN = "<unknown>";
205:
206: public String getServletInfo() {
207: return "A servlet that shows the data sent by the client";
208: }
209:
210: }
|