KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > module > sitemesh > filter > DebugResponseWrapper


1 /* This software is published under the terms of the OpenSymphony Software
2  * License version 1.1, of which a copy has been included with this
3  * distribution in the LICENSE.txt file. */

4 package com.opensymphony.module.sitemesh.filter;
5
6 import javax.servlet.ServletOutputStream JavaDoc;
7 import javax.servlet.http.Cookie JavaDoc;
8 import javax.servlet.http.HttpServletResponse JavaDoc;
9 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.PrintWriter JavaDoc;
12 import java.util.Locale JavaDoc;
13
14 /**
15  * @author <a HREF="joe@truemesh.com">Joe Walnes</a>
16  * @version $Revision: 1.2 $
17  */

18 public class DebugResponseWrapper extends HttpServletResponseWrapper JavaDoc {
19     private static int lastCount = 0;
20     private int count = 0;
21
22     public DebugResponseWrapper(HttpServletResponse JavaDoc response) {
23         super(response);
24         if (enabled()) {
25             lastCount++;
26             count = lastCount;
27             debug("<CONSTRUCT>", null, null);
28         }
29     }
30
31     public void addCookie(Cookie JavaDoc cookie) {
32         if (enabled()) debug("addCookie", cookie.getName(), cookie.toString());
33         super.addCookie(cookie);
34     }
35
36     public void addDateHeader(String JavaDoc name, long date) {
37         if (enabled()) debug("addDateHeader", name, String.valueOf(date));
38         super.addDateHeader(name, date);
39     }
40
41     public void addHeader(String JavaDoc name, String JavaDoc value) {
42         if (enabled()) debug("addHeader", name, value);
43         super.addHeader(name, value);
44     }
45
46     public void addIntHeader(String JavaDoc name, int value) {
47         if (enabled()) debug("addIntHeader", name, String.valueOf(value));
48         super.addIntHeader(name, value);
49     }
50
51     public boolean containsHeader(String JavaDoc name) {
52         return super.containsHeader(name);
53     }
54
55     public String JavaDoc encodeRedirectUrl(String JavaDoc url) {
56         return super.encodeRedirectUrl(url);
57     }
58
59     public String JavaDoc encodeRedirectURL(String JavaDoc url) {
60         return super.encodeRedirectURL(url);
61     }
62
63     public void sendError(int sc) throws IOException JavaDoc {
64         if (enabled()) debug("sendError", String.valueOf(sc), null);
65         super.sendError(sc);
66     }
67
68     public void sendError(int sc, String JavaDoc msg) throws IOException JavaDoc {
69         if (enabled()) debug("sendError", String.valueOf(sc), msg);
70         super.sendError(sc, msg);
71     }
72
73     public void sendRedirect(String JavaDoc location) throws IOException JavaDoc {
74         if (enabled()) debug("sendRedirect", location, null);
75         super.sendRedirect(location);
76     }
77
78     public void setDateHeader(String JavaDoc name, long date) {
79         if (enabled()) debug("setDateHeader", name, String.valueOf(date));
80         super.setDateHeader(name, date);
81     }
82
83     public void setHeader(String JavaDoc name, String JavaDoc value) {
84         if (enabled()) debug("setHeader", name, value);
85         super.setHeader(name, value);
86     }
87
88     public void setIntHeader(String JavaDoc name, int value) {
89         if (enabled()) debug("setIntHeader", name, String.valueOf(value));
90         super.setIntHeader(name, value);
91     }
92
93     public void setStatus(int sc) {
94         if (enabled()) debug("setStatus", String.valueOf(sc), null);
95         super.setStatus(sc);
96     }
97
98     public void setStatus(int sc, String JavaDoc msg) {
99         if (enabled()) debug("setStatus", String.valueOf(sc), msg);
100         super.setStatus(sc, msg);
101     }
102
103     public void flushBuffer() throws IOException JavaDoc {
104         if (enabled()) debug("flushBuffer", null, null);
105         super.flushBuffer();
106     }
107
108     public int getBufferSize() {
109         //
110
return super.getBufferSize();
111     }
112
113     public String JavaDoc getCharacterEncoding() {
114         //
115
return super.getCharacterEncoding();
116     }
117
118     public Locale JavaDoc getLocale() {
119         //
120
return super.getLocale();
121     }
122
123     public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
124         if (enabled()) debug("getOutputStream", null, null);
125         return super.getOutputStream();
126     }
127
128     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
129         if (enabled()) debug("getWriter", null, null);
130         return super.getWriter();
131     }
132
133     public boolean isCommitted() {
134         //
135
return super.isCommitted();
136     }
137
138     public void reset() {
139         if (enabled()) debug("reset", null, null);
140         super.reset();
141     }
142
143     /*public void resetBuffer() {
144         super.resetBuffer();
145     }*/

146
147     public void setBufferSize(int size) {
148         if (enabled()) debug("setBufferSize", String.valueOf(size), null);
149         super.setBufferSize(size);
150     }
151
152     public void setContentLength(int len) {
153         if (enabled()) debug("setContentLength", String.valueOf(len), null);
154         super.setContentLength(len);
155     }
156
157     public void setContentType(String JavaDoc type) {
158         if (enabled()) debug("setContentType", type, null);
159         super.setContentType(type);
160     }
161
162     public void setLocale(Locale JavaDoc locale) {
163         if (enabled()) debug("setBufferSize", locale.getDisplayName(), null);
164         super.setLocale(locale);
165     }
166
167     private boolean enabled() {
168         return true;
169     }
170
171     private void debug(String JavaDoc methodName, String JavaDoc arg1, String JavaDoc arg2) {
172         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
173         s.append("[debug ");
174         s.append(count);
175         s.append("] ");
176         s.append(methodName);
177         s.append("()");
178         if (arg1 != null) {
179             s.append(" : '");
180             s.append(arg1);
181             s.append("'");
182         }
183         if (arg2 != null) {
184             s.append(" = '");
185             s.append(arg2);
186             s.append("'");
187         }
188         System.out.println(s);
189     }
190 }
Popular Tags