1 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 ; 26 import java.io.PrintWriter ; 27 import java.io.OutputStream ; 28 import java.io.IOException ; 29 import java.io.BufferedWriter ; 30 import java.io.OutputStreamWriter ; 31 32 import javax.servlet.ServletOutputStream ; 33 import javax.servlet.http.HttpServletResponse ; 34 import javax.servlet.http.HttpServletResponseWrapper ; 35 36 37 public class ResponseWrapper extends HttpServletResponseWrapper { 38 39 private static final Logger log = Logger.getInstance( ResponseWrapper.class ); 40 41 private ByteArrayOutputStream output; 42 private PrintWriter writer; 43 private ServletOutputStream servletStream; 44 private int statusCode = SC_OK; 45 private String reason = ""; 46 private String outputString = null; 47 48 public ResponseWrapper( HttpServletResponse response ) { 49 super( response ); 50 output = new ByteArrayOutputStream ( 2048 ); 51 } 52 53 public PrintWriter getWriter() throws IOException { 54 if ( log.isDebugEnabled() ) { 55 log.debug( "getWriter()" ); 56 } 57 if ( writer == null ) { 58 String encoding = getCharacterEncoding(); 59 writer = new PrintWriter ( new BufferedWriter ( 60 new OutputStreamWriter ( getOutputStream(), encoding ) ) ); 61 } 62 return writer; 63 } 64 65 public ServletOutputStream getOutputStream() throws IOException { 66 if ( log.isDebugEnabled() ) { 67 } 71 if ( servletStream == null ) { 72 servletStream = new InternalServletStream( output ); 73 } 74 return servletStream; 76 } 77 78 public void sendError( int statusCode ) throws IOException { 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 reason ) throws IOException { 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 ( "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 ( "response is already commited, reset buffer not allowed" ); 125 } 126 output.reset(); 127 super.resetBuffer(); 128 } 129 130 int getStatusCode() { 132 return statusCode; 133 } 134 135 String getReason() { 137 return reason; 138 } 139 140 String getOutputString() throws IOException { 142 if ( outputString == null ) { 143 if ( writer != null ) { 144 writer.flush(); 145 writer.close(); 146 } 147 output.flush(); 148 output.close(); 149 String encoding = getCharacterEncoding(); 150 outputString = output.toString( encoding ); 151 } 152 return outputString; 153 } 154 155 159 static class InternalServletStream extends ServletOutputStream { 160 161 private OutputStream stream; 162 163 public InternalServletStream( OutputStream stream ) { 164 this.stream = stream; 165 } 166 167 public void write( int b ) 168 throws IOException { 169 stream.write( b ); 170 } 171 } 172 173 public static ResponseData populate( HttpServletResponse response, 174 ResponseData respData ) throws SessionFailedException { 175 String data = null; 176 try { 177 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 e ) { 187 throw new SessionFailedException( "ERROR: failed to get output stream from wrapper" ); 188 } 189 if ( log.isDebugEnabled() ) { 190 } 192 respData.setBody( data ); 194 return respData; 195 } 196 197 } 198 | Popular Tags |