KickJava   Java API By Example, From Geeks To Geeks.

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


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

31
32 public class ErrorPage02 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
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
44         Object JavaDoc value = null;
45
46         value = request.getAttribute("javax.servlet.error.status_code");
47         if (value == null)
48             sb.append(" status_code is missing/");
49         else if (!(value instanceof Integer JavaDoc)) {
50             sb.append(" status_code class is ");
51             sb.append(value.getClass().getName());
52             sb.append("/");
53         } else {
54             int intValue = ((Integer JavaDoc) value).intValue();
55             if (intValue != HttpServletResponse.SC_PRECONDITION_FAILED) {
56                 sb.append(" status_code is ");
57                 sb.append(value);
58                 sb.append("/");
59             }
60         }
61
62         value = request.getAttribute("javax.servlet.error.message");
63         if (value == null)
64             sb.append(" message is missing/");
65         else if (!(value instanceof String JavaDoc)) {
66             sb.append(" message class is ");
67             sb.append(value.getClass().getName());
68             sb.append("/");
69         } else {
70             String JavaDoc message = (String JavaDoc) value;
71             if (!"ErrorPage01 Returned Status Code 412".equals(message)) {
72                 sb.append(" message is ");
73                 sb.append(message);
74                 sb.append("/");
75             }
76         }
77
78         value = request.getAttribute("javax.servlet.error.request_uri");
79         if (value == null)
80             sb.append(" request_uri is missing/");
81         else if (!(value instanceof String JavaDoc)) {
82             sb.append(" request_uri class is ");
83             sb.append(value.getClass().getName());
84             sb.append("/");
85         } else {
86             String JavaDoc request_uri = (String JavaDoc) value;
87             String JavaDoc test1 = request.getContextPath() + "/ErrorPage01";
88             String JavaDoc test2 = request.getContextPath() + "/WrappedErrorPage01";
89             if (!request_uri.equals(test1) && !request_uri.equals(test2)) {
90                 sb.append(" request_uri is ");
91                 sb.append(request_uri);
92                 sb.append("/");
93             }
94         }
95
96         value = request.getAttribute("javax.servlet.error.servlet_name");
97         if (value == null)
98             sb.append(" servlet_name is missing/");
99         else if (!(value instanceof String JavaDoc)) {
100             sb.append(" servlet_name class is ");
101             sb.append(value.getClass().getName());
102             sb.append("/");
103         } else {
104             String JavaDoc servlet_name = (String JavaDoc) value;
105             if (!"ErrorPage01".equals(servlet_name)) {
106                 sb.append(" servlet_name is ");
107                 sb.append(servlet_name);
108                 sb.append("/");
109             }
110         }
111
112         // Report ultimate success or failure
113
if (sb.length() < 1)
114             writer.println("ErrorPage02 PASSED");
115         else
116             writer.println("ErrorPage02 FAILED -" + sb.toString());
117
118         while (true) {
119             String JavaDoc message = StaticLogger.read();
120             if (message == null)
121                 break;
122             writer.println(message);
123         }
124         StaticLogger.reset();
125
126     }
127
128
129 }
130
Popular Tags