KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tester > Decoding01


1 /*
2  * Copyright 1999, 2000, 2001 ,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.tester;
18
19
20 import java.io.*;
21 import javax.servlet.*;
22 import javax.servlet.http.*;
23
24
25 /**
26  * Test for proper URL decoding of the getServletPath() and getPathInfo()
27  * methods of HttpServletRequest. The desired values are specified by the
28  * <strong>servlet</strong> and <strong>path</strong> request parameters,
29  * respectively.
30  *
31  * @author Craig R. McClanahan
32  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:56 $
33  */

34
35 public class Decoding01 extends HttpServlet {
36
37     public void doGet(HttpServletRequest request, HttpServletResponse response)
38         throws IOException, ServletException {
39
40         // Identify our configuration parameters
41
String JavaDoc desiredServlet = request.getParameter("servlet");
42         String JavaDoc desiredPath = request.getParameter("path");
43
44         // Prepare for the desired test
45
response.setContentType("text/plain");
46         PrintWriter writer = response.getWriter();
47         StringBuffer JavaDoc results = new StringBuffer JavaDoc();
48
49         // Check the value returned by getServletPath()
50
String JavaDoc servletPath = request.getServletPath();
51         if (desiredServlet == null) {
52             if (servletPath != null)
53                 results.append(" servletPath is '" + servletPath +
54                                "' instead of NULL/");
55         } else {
56             if (servletPath == null)
57                 results.append(" servletPath is NULL instead of '" +
58                                desiredPath + "'/");
59             else if (!servletPath.equals(desiredServlet))
60                 results.append(" servletPath is '" + servletPath +
61                                "' instead of '" + desiredServlet + "'/");
62         }
63
64         // Check the value returned by getPathInfo()
65
String JavaDoc pathInfo = request.getPathInfo();
66         if (desiredPath == null) {
67             if (pathInfo != null)
68                 results.append(" pathInfo is '" + pathInfo +
69                                "' instead of NULL/");
70         } else {
71             if (pathInfo == null)
72                 results.append(" pathInfo is NULL instead of '" +
73                                desiredPath + "'/");
74             else if (!pathInfo.equals(desiredPath))
75                 results.append(" pathInfo is '" + pathInfo +
76                                "' instead of '" + desiredPath + "'/");
77         }
78
79         // Report success or failure
80
if (results.length() < 1)
81             writer.println("Decoding01 PASSED");
82         else {
83             writer.print("Decoding01 FAILED -");
84             writer.println(results.toString());
85         }
86
87         // Add wrapper messages as required
88
while (true) {
89             String JavaDoc message = StaticLogger.read();
90             if (message == null)
91                 break;
92             writer.println(message);
93         }
94         StaticLogger.reset();
95
96     }
97
98 }
99
Popular Tags