KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.*;
22 import java.util.*;
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25
26
27 /**
28  * Negative test for access to Catalina internals through the objects that
29  * are exposed to this servlet by the container.
30  *
31  * @author Craig R. McClanahan
32  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:57 $
33  */

34
35 public class Reflection01 extends HttpServlet {
36
37     public void service(HttpServletRequest request,
38                         HttpServletResponse response)
39         throws IOException, ServletException {
40
41         response.setContentType("text/plain");
42         PrintWriter writer = response.getWriter();
43         StringBuffer JavaDoc results = new StringBuffer JavaDoc();
44
45         // Check the ServletConfig object
46
try {
47             ServletConfig servletConfig = getServletConfig();
48             Method method = servletConfig.getClass().getMethod
49                 ("getParent", new Class JavaDoc[] {});
50             Object JavaDoc parent = method.invoke(servletConfig,
51                                           new Object JavaDoc[] {});
52             results.append(" Can reflect on ServletConfig/");
53         } catch (Throwable JavaDoc t) {
54             StaticLogger.write("ServletConfig: " + t);
55         }
56
57         // Check the ServletContext object
58
try {
59             ServletContext servletContext = getServletContext();
60             Method method = servletContext.getClass().getMethod
61                 ("getResources", new Class JavaDoc[] {});
62             Object JavaDoc resources = method.invoke(servletContext,
63                                              new Object JavaDoc[] {});
64             results.append(" Can reflect on ServletContext/");
65         } catch (Throwable JavaDoc t) {
66             StaticLogger.write("ServletContext: " + t);
67         }
68
69         // Check the HttpServletRequest object
70
try {
71             Method method = request.getClass().getMethod
72                 ("getInfo", new Class JavaDoc[] {});
73             Object JavaDoc info = method.invoke(request,
74                                         new Object JavaDoc[] {});
75             results.append(" Can reflect on HttpServletRequest/");
76         } catch (Throwable JavaDoc t) {
77             StaticLogger.write("HttpServletRequest: " + t);
78         }
79
80         // Check the HttpServletResponse object
81
try {
82             Method method = request.getClass().getMethod
83                 ("getInfo", new Class JavaDoc[] {});
84             Object JavaDoc info = method.invoke(request,
85                                         new Object JavaDoc[] {});
86             results.append(" Can reflect on HttpServletResponse/");
87         } catch (Throwable JavaDoc t) {
88             StaticLogger.write("HttpServletResponse: " + t);
89         }
90
91         // Check the HttpSession object
92
try {
93             HttpSession session = request.getSession(true);
94             Method method = session.getClass().getMethod
95                 ("getInfo", new Class JavaDoc[] {});
96             results.append(" Can reflect on HttpSession/");
97         } catch (Throwable JavaDoc t) {
98             StaticLogger.write("HttpSession: " + t);
99         }
100
101         // Check the RequestDispatcher object
102
try {
103             RequestDispatcher rd =
104                 getServletContext().getRequestDispatcher("/index.shtml");
105             Method method = rd.getClass().getMethod
106                 ("getInfo", new Class JavaDoc[] {});
107             results.append(" Can reflect on RequestDispatcher/");
108         } catch (Throwable JavaDoc t) {
109             StaticLogger.write("RequestDispatcher: " + t);
110         }
111
112         // Report final results
113
if (results.length() < 1)
114             writer.println("Reflection01 PASSED");
115         else {
116             writer.print("Reflection01 FAILED -");
117             writer.println(results.toString());
118         }
119
120         while (true) {
121             String JavaDoc message = StaticLogger.read();
122             if (message == null)
123                 break;
124             writer.println(message);
125         }
126         StaticLogger.reset();
127
128     }
129
130 }
131
Popular Tags