KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Part 4 of the ErrorPage Tests. Should be mapped by the container when
26  * the ErrorPage01 servlet returns the appropriate exception.
27  *
28  * @author Craig R. McClanahan
29  * @version $Revision: 1.4 $ $Date: 2004/02/27 14:58:56 $
30  */

31
32 public class ErrorPage04 extends HttpServlet {
33
34
35     public void doGet(HttpServletRequest request, HttpServletResponse response)
36         throws IOException, ServletException {
37
38         response.reset();
39         response.setContentType("text/plain");
40         PrintWriter writer = response.getWriter();
41
42         // Accumulate all the reasons this request might fail
43
ServletException exception = null;
44         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
45         Object JavaDoc value = null;
46
47         value = request.getAttribute("javax.servlet.error.exception");
48         if (value == null)
49             sb.append(" exception is missing/");
50         else if (!(value instanceof org.apache.tester.TesterException)) {
51             sb.append(" exception class is ");
52             sb.append(value.getClass().getName());
53             sb.append("/");
54         } else {
55             TesterException te = (TesterException) value;
56             if (!"ErrorPage03 Threw Exception".equals(te.getMessage())) {
57                 sb.append(" exception message is ");
58                 sb.append(te.getMessage());
59                 sb.append("/");
60             }
61         }
62
63         value = request.getAttribute("javax.servlet.error.exception_type");
64         if (value == null)
65             sb.append(" exception_type is missing/");
66         else if (!(value instanceof Class JavaDoc)) {
67             sb.append(" exception_type class is ");
68             sb.append(value.getClass().getName());
69             sb.append("/");
70         } else {
71             Class JavaDoc clazz = (Class JavaDoc) value;
72             if (!"org.apache.tester.TesterException".equals(clazz.getName())) {
73                 sb.append(" exception_type class is ");
74                 sb.append(clazz.getName());
75                 sb.append("/");
76             }
77         }
78
79         value = request.getAttribute("javax.servlet.error.message");
80         if (value == null)
81             sb.append(" message is missing/");
82         else if (!(value instanceof String JavaDoc)) {
83             sb.append(" message class is ");
84             sb.append(value.getClass().getName());
85             sb.append("/");
86         } else {
87             String JavaDoc message = (String JavaDoc) value;
88             if (!"ErrorPage03 Threw Exception".equals(message)) {
89                 sb.append(" message is ");
90                 sb.append(message);
91                 sb.append("/");
92             }
93         }
94
95         value = request.getAttribute("javax.servlet.error.request_uri");
96         if (value == null)
97             sb.append(" request_uri is missing/");
98         else if (!(value instanceof String JavaDoc)) {
99             sb.append(" request_uri class is ");
100             sb.append(value.getClass().getName());
101             sb.append("/");
102         } else {
103             String JavaDoc request_uri = (String JavaDoc) value;
104             String JavaDoc test1 = request.getContextPath() + "/ErrorPage03";
105             String JavaDoc test2 = request.getContextPath() + "/WrappedErrorPage03";
106             if (!request_uri.equals(test1) && !request_uri.equals(test2)) {
107                 sb.append(" request_uri is ");
108                 sb.append(request_uri);
109                 sb.append("/");
110             }
111         }
112
113         value = request.getAttribute("javax.servlet.error.servlet_name");
114         if (value == null)
115             sb.append(" servlet_name is missing/");
116         else if (!(value instanceof String JavaDoc)) {
117             sb.append(" servlet_name class is ");
118             sb.append(value.getClass().getName());
119             sb.append("/");
120         } else {
121             String JavaDoc servlet_name = (String JavaDoc) value;
122             if (!"ErrorPage03".equals(servlet_name)) {
123                 sb.append(" servlet_name is ");
124                 sb.append(servlet_name);
125                 sb.append("/");
126             }
127         }
128
129         // Report ultimate success or failure
130
if (sb.length() < 1)
131             writer.println("ErrorPage04 PASSED");
132         else
133             writer.println("ErrorPage04 FAILED -" + sb.toString());
134
135         while (true) {
136             String JavaDoc message = StaticLogger.read();
137             if (message == null)
138                 break;
139             writer.println(message);
140         }
141         StaticLogger.reset();
142
143     }
144
145
146 }
147
Popular Tags