KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tools > testrecorder > server > ResponseWrapper


1 /*
2  * Copyright 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  * $Header:$
17  */

18
19 package org.apache.beehive.netui.tools.testrecorder.server;
20
21 import org.apache.beehive.netui.tools.testrecorder.shared.Logger;
22 import org.apache.beehive.netui.tools.testrecorder.shared.ResponseData;
23 import org.apache.beehive.netui.tools.testrecorder.server.state.SessionFailedException;
24
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.BufferedWriter JavaDoc;
30 import java.io.OutputStreamWriter JavaDoc;
31
32 import javax.servlet.ServletOutputStream JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
35
36
37 public class ResponseWrapper extends HttpServletResponseWrapper JavaDoc {
38
39     private static final Logger log = Logger.getInstance( ResponseWrapper.class );
40
41     private ByteArrayOutputStream JavaDoc output;
42     private PrintWriter JavaDoc writer;
43     private ServletOutputStream JavaDoc servletStream;
44     private int statusCode = SC_OK;
45     private String JavaDoc reason = "";
46     private String JavaDoc outputString = null;
47
48     public ResponseWrapper( HttpServletResponse JavaDoc response ) {
49         super( response );
50         output = new ByteArrayOutputStream JavaDoc( 2048 );
51     }
52
53     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
54         if ( log.isDebugEnabled() ) {
55             log.debug( "getWriter()" );
56         }
57         if ( writer == null ) {
58             String JavaDoc encoding = getCharacterEncoding();
59             writer = new PrintWriter JavaDoc( new BufferedWriter JavaDoc(
60                 new OutputStreamWriter JavaDoc( getOutputStream(), encoding ) ) );
61         }
62         return writer;
63     }
64
65     public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
66         if ( log.isDebugEnabled() ) {
67 // log.debug( "getOutputStream()" );
68
// Exception e = new Exception();
69
// e.printStackTrace();
70
}
71         if ( servletStream == null ) {
72             servletStream = new InternalServletStream( output );
73         }
74         // create an internal output
75
return servletStream;
76     }
77
78     public void sendError( int statusCode ) throws IOException JavaDoc {
79         if ( log.isDebugEnabled() ) {
80             log.debug( "sendError(): statusCode( " + statusCode + " )" );
81         }
82         this.statusCode = statusCode;
83         super.sendError( statusCode );
84     }
85
86     public void sendError( int statusCode, String JavaDoc reason ) throws IOException JavaDoc {
87         if ( log.isDebugEnabled() ) {
88             log.debug( "sendError(): statusCode( " + statusCode + " )" );
89             log.debug( "reason( " + reason + " )" );
90         }
91         this.statusCode = statusCode;
92         this.reason = reason;
93         super.sendError( statusCode, reason );
94     }
95
96     public void setStatus( int statusCode ) {
97         if ( log.isDebugEnabled() ) {
98             log.debug( "setStatus(): statusCode( " + statusCode + " )" );
99         }
100         this.statusCode = statusCode;
101         super.setStatus( statusCode );
102         if ( log.isDebugEnabled() ) {
103             log.debug( "setStatus() done" );
104         }
105     }
106
107     public void reset() {
108         if ( log.isDebugEnabled() ) {
109             log.debug( "reset()" );
110         }
111         if ( isCommitted() ) {
112             throw new IllegalStateException JavaDoc( "response is already commited, reset not allowed" );
113         }
114         output.reset();
115         statusCode = SC_OK;
116         super.reset();
117     }
118
119     public void resetBuffer() {
120         if ( log.isDebugEnabled() ) {
121             log.debug( "resetBuffer()" );
122         }
123         if ( isCommitted() ) {
124             throw new IllegalStateException JavaDoc( "response is already commited, reset buffer not allowed" );
125         }
126         output.reset();
127         super.resetBuffer();
128     }
129
130     // package scoped
131
int getStatusCode() {
132         return statusCode;
133     }
134
135     // package scoped
136
String JavaDoc getReason() {
137         return reason;
138     }
139
140     // package scoped
141
String JavaDoc getOutputString() throws IOException JavaDoc {
142         if ( outputString == null ) {
143             if ( writer != null ) {
144                 writer.flush();
145                 writer.close();
146             }
147             output.flush();
148             output.close();
149             String JavaDoc encoding = getCharacterEncoding();
150             outputString = output.toString( encoding );
151         }
152         return outputString;
153     }
154
155     /**
156      * Internal class that implements a ServletOutputStream. This is used to
157      * return our output stream to the JSP world.
158      */

159     static class InternalServletStream extends ServletOutputStream JavaDoc {
160
161         private OutputStream JavaDoc stream;
162
163         public InternalServletStream( OutputStream JavaDoc stream ) {
164             this.stream = stream;
165         }
166
167         public void write( int b )
168                 throws IOException JavaDoc {
169             stream.write( b );
170         }
171     }
172
173     public static ResponseData populate( HttpServletResponse JavaDoc response,
174             ResponseData respData ) throws SessionFailedException {
175         String JavaDoc data = null;
176         try {
177             // may throw ClassCastException
178
ResponseWrapper wrapper = (ResponseWrapper) response;
179             respData.setReason( wrapper.getReason() );
180             if ( log.isDebugEnabled() ) {
181                 log.debug( "populate status code( " + wrapper.getStatusCode() + " )" );
182             }
183             respData.setStatusCode( wrapper.getStatusCode() );
184             data = wrapper.getOutputString();
185         }
186         catch ( Exception JavaDoc e ) {
187             throw new SessionFailedException( "ERROR: failed to get output stream from wrapper" );
188         }
189         if ( log.isDebugEnabled() ) {
190 // log.debug( "data( " + data + " )" );
191
}
192         // data may be null
193
respData.setBody( data );
194         return respData;
195     }
196
197 }
198
Popular Tags