KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > monitor > server > MonitorResponseWrapper


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /**
21  * MonitorResponseWrapper.java
22  *
23  *
24  * Created: Tue Feb 27 18:32:32 2001
25  *
26  * @author Ana von Klopp
27  * @version
28  */

29
30 package org.netbeans.modules.web.monitor.server;
31
32 import java.io.*;
33 import java.net.*;
34 import java.util.*;
35 import java.text.*;
36 import javax.servlet.*;
37 import javax.servlet.http.*;
38
39 public class MonitorResponseWrapper extends HttpServletResponseWrapper {
40
41     private Vector cookies = null;
42     private int status;
43     private boolean cookieSetOnInclude = false;
44
45     private HttpServletResponse response = null;
46     private HttpServletRequest request = null;
47
48     private static boolean debug = false;
49
50     /**
51      * The constructor needs to have access both to the response
52      * object that it wraps and to the request. The latter is needed
53      * to determine whether the resource which is currently accessing
54      * the response was dispatched to as an include or not
55      *
56      * @param response The HTTPServletResponse object that this object
57      * wraps.
58      * $param request The HttpServletRequest object that is processed
59      * in synch with the response.
60      *
61      */

62     public MonitorResponseWrapper(HttpServletResponse response,
63                   HttpServletRequest request) {
64     super(response);
65     this.response = (HttpServletResponse)getResponse();
66     this.request = request;
67     cookies = new Vector();
68     }
69
70     /**
71      * Returns the outgoing cookies that were added to the response as
72      * it was processed by the servlets and JSPs.
73      *
74      * @return An enumeration of cookies
75      *
76      */

77     public Enumeration getCookies() {
78     return cookies.elements();
79     }
80
81     /**
82      * Returns the HTTP status of the response
83      *
84      * @return An integer representing the HTTP status
85      *
86      */

87     public int getStatus() {
88     return status;
89     }
90
91
92     /**
93      * Wraps the addCookie method on the response. Since the Servlet
94      * APIs does not allow us to retrieve the outgoing cookies, the
95      * wrapper itself maintains a duplicate list which can be accessed
96      * with the getCookies method.
97      *
98      * @param A cookie
99      *
100      */

101     public void addCookie (Cookie cookie) {
102     String JavaDoc str = (String JavaDoc)request.getAttribute("javax.servlet.include.request_uri"); //NOI18N
103
if(str == null) {
104         cookies.add(cookie);
105         response.addCookie(cookie);
106     }
107     else {
108         // An included resource tried to set a cookie, which is
109
// illegal but swallowed by the reference implementation
110
cookieSetOnInclude = true;
111     }
112     }
113     
114     /**
115      * Wraps the sendError method on the response. The HTTP status is
116      * not accessible through the Servlet APIs, so the wrapper
117      * maintains a copy of the value which can be accessed with the
118      * getStatus method.
119      *
120      * @param status an integer representing the HTTP status
121      * @param detail a message explaining the error
122      *
123      */

124     public void sendError (int status, String JavaDoc detail) throws
125     IOException, IllegalStateException JavaDoc {
126     this.status = status;
127     response.sendError (status, detail);
128     }
129
130     /**
131      * Wraps the sendError method on the response. The HTTP status is
132      * not accessible through the Servlet APIs, so the wrapper
133      * maintains a copy of the value which can be accessed with the
134      * getStatus method.
135      *
136      * @param status an integer representing the HTTP status
137      *
138      */

139     public void sendError (int status) throws IOException,
140     IllegalStateException JavaDoc {
141     this.status = status;
142     response.sendError (status, null);
143     }
144
145     /**
146      * Wraps the setStatus method on the response. The HTTP status is
147      * not accessible through the Servlet APIs, so the wrapper
148      * maintains a copy of the value which can be accessed with the
149      * getStatus method.
150      *
151      * @param status an integer representing the HTTP status
152      *
153      */

154     public void setStatus(int code) {
155     this.status = code;
156     response.setStatus(code);
157     }
158
159     /**
160      * Wraps the setStatus method on the response. The HTTP status is
161      * not accessible through the Servlet APIs, so the wrapper
162      * maintains a copy of the value which can be accessed with the
163      * getStatus method.
164      *
165      * @param status an integer representing the HTTP status
166      * @param msg a message explaining the status
167      *
168      */

169     public void setStatus (int code, String JavaDoc msg) {
170     this.status = code;
171     response.setStatus(code, msg);
172     }
173
174     /**
175      * Logger method so that this object can log messages to the log
176      * file for the servlet context. Note that this will fail if there
177      * is no HTTP session.
178      *
179      * @param msg The message to log.
180      */

181     private void log(String JavaDoc msg) {
182     try {
183         request.getSession(false).getServletContext().log("MonitorResponseWrapper::" + msg); //NOI18N
184
}
185     catch(Throwable JavaDoc t) {
186         if(debug) t.printStackTrace();
187     }
188     }
189
190     /**
191      * This method can be used by a tool that warns a developer that
192      * an included resource tried to do something it is not allowed to
193      * do. The Monitor client could show this.
194      *
195      * @return true if an included resource attempted to set a cookie
196      *
197      */

198     public boolean cookieSetOnInclude() {
199     return cookieSetOnInclude;
200     }
201 } // MonitorResponseWrapper
202
Popular Tags