1 18 package org.apache.beehive.netui.pageflow.scoping.internal; 19 20 import org.apache.beehive.netui.pageflow.scoping.ScopedResponse; 21 22 import javax.servlet.http.HttpServletResponse ; 23 import javax.servlet.http.HttpServletResponseWrapper ; 24 import javax.servlet.http.Cookie ; 25 import java.io.IOException ; 26 import java.util.HashMap ; 27 import java.util.Date ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import org.apache.log4j.Logger; 33 34 35 40 public class ScopedResponseImpl 41 extends HttpServletResponseWrapper 42 implements ScopedResponse 43 { 44 private boolean _isError = false; 45 private int _statusCode = -1; 46 private String _redirectURI = null; 47 private String _statusMessage = null; 48 49 50 private HashMap _headers = new HashMap (); 51 52 private static final String SET_COOKIE = "Set-Cookie"; 53 private static final Logger logger = Logger.getLogger( ScopedResponseImpl.class ); 54 55 56 public ScopedResponseImpl( HttpServletResponse response ) 57 { 58 super( response ); 59 } 60 61 public void sendError( int i, String s ) throws IOException 62 { 63 _isError = true; 64 _statusCode = i; 65 _statusMessage = s; 66 67 if ( logger.isInfoEnabled() ) 68 { 69 StringBuffer msg = new StringBuffer ( "ScopedResponse error " ).append( i ); 70 logger.info( msg.append( ": " ).append( s ) ); 71 } 72 } 73 74 public void sendError( int i ) throws IOException 75 { 76 sendError( i, "" ); 77 } 78 79 public void setStatus( int i ) 80 { 81 setStatus( i, "" ); 82 } 83 84 public void setStatus( int i, String s ) 85 { 86 _statusCode = i; 87 _statusMessage = s; 88 } 89 90 public void setContentLength( int i ) 91 { 92 } 94 95 public void setContentType( String s ) 96 { 97 } 99 100 public void setBufferSize( int i ) 101 { 102 } 104 105 public void resetBuffer() 106 { 107 } 109 110 public void reset() 111 { 112 } 114 115 122 125 public void addCookie( Cookie cookie ) 126 { 127 addObjectHeader(SET_COOKIE, cookie); 128 } 129 130 133 public Cookie getCookie( String cookieName ) 134 { 135 List cookies = getHeaders(SET_COOKIE); 136 if(cookies != null){ 137 for(int i = cookies.size(); --i > -1;) { 139 Cookie cookie = (Cookie )cookies.get(i); 140 if(cookie.getName().equals(cookieName)) { 141 return cookie; 142 } 143 } 144 } 145 146 return null; 147 } 148 149 152 public Cookie [] getCookies() 153 { 154 List cookies = (List )_headers.get(SET_COOKIE); 155 156 return ( cookies != null ? ( Cookie [] ) cookies.toArray( new Cookie [0] ) : new Cookie [0] ); 157 } 158 159 162 public boolean containsHeader( String name ) 163 { 164 return _headers.containsKey( name ); 165 } 166 167 170 public void setDateHeader( String name, long date ) 171 { 172 setObjectHeader( name, new Date ( date ) ); 173 } 174 175 178 public void addDateHeader( String name, long date ) 179 { 180 addObjectHeader( name, new Date ( date ) ); 181 } 182 183 186 public void setHeader( String name, String value ) 187 { 188 setObjectHeader( name, value ); 189 } 190 191 194 public void addHeader( String name, String value ) 195 { 196 addObjectHeader( name, value ); 197 } 198 199 202 public void setIntHeader( String name, int value ) 203 { 204 setObjectHeader( name, new Integer ( value ) ); 205 } 206 207 210 public void addIntHeader( String name, int value ) 211 { 212 addObjectHeader( name, new Integer ( value ) ); 213 } 214 215 220 public Map getHeaders() 221 { 222 return _headers; 223 } 224 225 230 public List getHeaders( String name ) 231 { 232 return ( List ) _headers.get( name ); 233 } 234 235 240 public Object getFirstHeader( String name ) 241 { 242 List foundHeaders = ( List ) _headers.get( name ); 243 return ( ! foundHeaders.isEmpty() ? foundHeaders.get( 0 ) : null ); 244 } 245 246 protected void addObjectHeader( String name, Object val ) 247 { 248 List vals = ( List ) _headers.get( name ); 249 250 if ( vals == null ) 251 { 252 vals = new ArrayList (); 253 _headers.put( name, vals ); 254 } 255 256 vals.add( val ); 257 } 258 259 protected void setObjectHeader( String name, Object val ) 260 { 261 ArrayList vals = new ArrayList (); 262 vals.add( val ); 263 _headers.put( name, vals ); 264 } 265 266 public HttpServletResponse getOuterResponse() 267 { 268 return (HttpServletResponse ) getResponse(); 269 } 270 271 public boolean isError() 272 { 273 return _isError; 274 } 275 276 public int getStatusCode() 277 { 278 return _statusCode; 279 } 280 281 public String getStatusMessage() 282 { 283 return _statusMessage; 284 } 285 286 public void sendRedirect( String redirectURI ) 287 throws IOException 288 { 289 _redirectURI = redirectURI; 290 } 291 292 297 public void applyRedirect() 298 throws IOException 299 { 300 if ( _redirectURI != null ) 301 { 302 super.sendRedirect( _redirectURI ); 303 } 304 else 305 { 306 throw new IllegalStateException ( "No redirect to apply." ); 307 } 308 } 309 310 public boolean didRedirect() 311 { 312 return _redirectURI != null; 313 } 314 315 public String getRedirectURI() 316 { 317 return _redirectURI; 318 } 319 } 320 | Popular Tags |