KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > servlet > servlets > EnhydraSnoopServlet


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: EnhydraSnoopServlet.java,v 1.3 2005/03/24 12:22:23 slobodan Exp $
23  */

24
25 package org.enhydra.servlet.servlets;
26
27 import java.io.IOException JavaDoc;
28 import java.util.Enumeration JavaDoc;
29
30 import javax.servlet.ServletException JavaDoc;
31 import javax.servlet.ServletInputStream JavaDoc;
32 import javax.servlet.ServletOutputStream JavaDoc;
33 import javax.servlet.http.HttpServlet JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36 import javax.servlet.http.HttpUtils JavaDoc;
37
38
39 /**
40  * Snoop servlet. This servlet`echos back the request line,
41  * headers and posted content that were sent by the client
42  * This servlet has been written using the Sun Snoop Servlet
43  * as model
44  *
45  * @version 1.00, 06/30/97
46  * @author Christophe Ney
47  */

48 public class EnhydraSnoopServlet extends HttpServlet JavaDoc {
49
50
51     // when the url is requested with a POST method
52
public void doPost (HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
53         throws ServletException JavaDoc, IOException JavaDoc
54     {
55         //value chosen to limit denial of service
56
if (req.getContentLength() > 8*1024) {
57             res.setContentType("text/html");
58             ServletOutputStream JavaDoc out = res.getOutputStream();
59             out.println("<html><head><title>Too big</title></head>");
60             out.println("<body><h1>Error - content length &gt;8k not ");
61             out.println("</h1></body></html>");
62         } else {
63             doGet(req,res);
64             ServletOutputStream JavaDoc out = res.getOutputStream();
65             out.println("<html><body><h1>Posted data:</h1>");
66             out.println("<pre>");
67             printPostedData(out,req);
68             out.println("</pre></body></html>");
69         }
70     }
71     
72     // when the url is requested with a GET method
73
public void doGet (HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
74         throws ServletException JavaDoc, IOException JavaDoc
75     {
76     res.setContentType("text/html");
77     ServletOutputStream JavaDoc out = res.getOutputStream();
78     out.println("<html>");
79     out.println("<head><title>Snoop Servlet</title></head>");
80     out.println("<body>");
81
82     out.println("<h1>Requested URL:</h1>");
83     out.println("<pre>");
84     out.println (HttpUtils.getRequestURL (req).toString ());
85     out.println("</pre>");
86
87     Enumeration JavaDoc enumeration = getServletConfig().getInitParameterNames();
88     if (enumeration != null) {
89         boolean first = true;
90         while (enumeration.hasMoreElements()) {
91         if (first) {
92             out.println("<h1>Init Parameters</h1>");
93             out.println("<pre>");
94             first = false;
95                 }
96         String JavaDoc param = (String JavaDoc) enumeration.nextElement();
97                 out.println(" "+param+": "+getInitParameter(param));
98         }
99         out.println("</pre>");
100     }
101
102     out.println("<h1>Request information:</h1>");
103     out.println("<pre>");
104     print(out, "Request method", req.getMethod());
105     print(out, "Request URI", req.getRequestURI());
106     print(out, "Request protocol", req.getProtocol());
107     print(out, "Servlet path", req.getServletPath());
108     print(out, "Path info", req.getPathInfo());
109     print(out, "Path translated", req.getPathTranslated());
110     print(out, "Query string", req.getQueryString());
111     print(out, "Content length", req.getContentLength());
112     print(out, "Content type", req.getContentType());
113     print(out, "Server name", req.getServerName());
114     print(out, "Server port", req.getServerPort());
115     print(out, "Remote user", req.getRemoteUser());
116     print(out, "Remote address", req.getRemoteAddr());
117     print(out, "Remote host", req.getRemoteHost());
118     print(out, "Authorization scheme", req.getAuthType());
119     out.println("</pre>");
120
121     Enumeration JavaDoc e = req.getHeaderNames();
122     if (e.hasMoreElements()) {
123         out.println("<h1>Request headers:</h1>");
124         out.println("<pre>");
125         while (e.hasMoreElements()) {
126             String JavaDoc name = (String JavaDoc)e.nextElement();
127             out.println(" " + name + ": " + req.getHeader(name));
128         }
129         out.println("</pre>");
130     }
131
132     e = req.getParameterNames();
133     if (e.hasMoreElements()) {
134         out.println("<h1>Servlet parameters (Single Value style):</h1>");
135         out.println("<pre>");
136         while (e.hasMoreElements()) {
137             String JavaDoc name = (String JavaDoc)e.nextElement();
138             out.println(" " + name + " = " + req.getParameter(name));
139         }
140         out.println("</pre>");
141     }
142
143     e = req.getParameterNames();
144     if (e.hasMoreElements()) {
145         out.println("<h1>Servlet parameters (Multiple Value style):</h1>");
146         out.println("<pre>");
147         while (e.hasMoreElements()) {
148             String JavaDoc name = (String JavaDoc)e.nextElement();
149             String JavaDoc vals[] = (String JavaDoc []) req.getParameterValues(name);
150             if (vals != null) {
151                 out.print("<b> " + name + " = </b>");
152                 out.println(vals[0]);
153                 for (int i = 1; i<vals.length; i++)
154                 out.println(" " + vals[i]);
155             }
156             out.println("<p>");
157         }
158         out.println("</pre>");
159     }
160
161     out.println("</body></html>");
162     }
163
164     private void print(ServletOutputStream JavaDoc out, String JavaDoc name, String JavaDoc value)
165         throws IOException JavaDoc
166     {
167         out.print(" " + name + ": ");
168         out.println(value == null ? "&lt;none&gt;" : value);
169     }
170
171     private void print(ServletOutputStream JavaDoc out, String JavaDoc name, int value)
172         throws IOException JavaDoc
173     {
174         out.print(" " + name + ": ");
175         if (value == -1) {
176             out.println("&lt;none&gt;");
177         } else {
178             out.println(value);
179         }
180     }
181
182     private void printPostedData(ServletOutputStream JavaDoc out, HttpServletRequest JavaDoc req)
183         throws IOException JavaDoc
184     {
185         ServletInputStream JavaDoc in = req.getInputStream();
186         out.print("\n ");
187         for (int i=0; i<req.getContentLength();i++) {
188             int b = in.read();
189             switch(b) {
190                 case '\n': out.print("\n "); break;
191                 case '<': out.print("&lt;"); break;
192                 case '>': out.print("&gt;"); break;
193                 default:out.write(b);
194             }
195         }
196         out.print("\n");
197     }
198
199
200     private static final String JavaDoc UNKNOWN = "&lt;unknown&gt;";
201
202     public String JavaDoc getServletInfo() {
203         return "A servlet that shows the data sent by the client";
204     }
205
206 }
207
Popular Tags