KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > scoping > internal > ScopedResponseImpl


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 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 JavaDoc;
23 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
24 import javax.servlet.http.Cookie JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.apache.log4j.Logger;
33
34
35 /**
36  * A wrapper around HttpServletResponse, associated with a given scope-key. Delegates to the wrapped
37  * response object for some functionality, but prevents output or error codes or forwards from actually
38  * happening.
39  */

40 public class ScopedResponseImpl
41         extends HttpServletResponseWrapper JavaDoc
42         implements ScopedResponse
43 {
44     private boolean _isError = false;
45     private int _statusCode = -1;
46     private String JavaDoc _redirectURI = null;
47     private String JavaDoc _statusMessage = null;
48
49     /** Map of name (String) -> headers (List). There can be more than one for each name. **/
50     private HashMap JavaDoc _headers = new HashMap JavaDoc();
51
52     private static final String JavaDoc SET_COOKIE = "Set-Cookie";
53     private static final Logger logger = Logger.getLogger( ScopedResponseImpl.class );
54
55
56     public ScopedResponseImpl( HttpServletResponse JavaDoc response )
57     {
58         super( response );
59     }
60
61     public void sendError( int i, String JavaDoc s ) throws IOException JavaDoc
62     {
63         _isError = true;
64         _statusCode = i;
65         _statusMessage = s;
66
67         if ( logger.isInfoEnabled() )
68         {
69             StringBuffer JavaDoc msg = new StringBuffer JavaDoc( "ScopedResponse error " ).append( i );
70             logger.info( msg.append( ": " ).append( s ) );
71         }
72     }
73
74     public void sendError( int i ) throws IOException JavaDoc
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 JavaDoc s )
85     {
86         _statusCode = i;
87         _statusMessage = s;
88     }
89
90     public void setContentLength( int i )
91     {
92         // don't do anything
93
}
94
95     public void setContentType( String JavaDoc s )
96     {
97         // don't do anything
98
}
99
100     public void setBufferSize( int i )
101     {
102         // don't do anything
103
}
104
105     public void resetBuffer()
106     {
107         // don't do anything
108
}
109
110     public void reset()
111     {
112         // don't do anything
113
}
114
115      //
116
// Headers: We need some special handling for headers. Since we're
117
// *including* portlets, the response received from WLS will have
118
// no-op methods for all headers. So, this implementation collects
119
// headers explicitly, to avoid losing them.
120
//
121

122     /**
123      * Add a cookie to the response.
124      */

125     public void addCookie( Cookie JavaDoc cookie )
126     {
127         addObjectHeader(SET_COOKIE, cookie);
128     }
129
130     /**
131      * Gets a cookie that was added to the response.
132      */

133     public Cookie JavaDoc getCookie( String JavaDoc cookieName )
134     {
135         List JavaDoc cookies = getHeaders(SET_COOKIE);
136         if(cookies != null){
137             // start looking from the back (ie. the last cookie set)
138
for(int i = cookies.size(); --i > -1;) {
139                 Cookie JavaDoc cookie = (Cookie JavaDoc)cookies.get(i);
140                 if(cookie.getName().equals(cookieName)) {
141                     return cookie;
142                 }
143             }
144         }
145
146         return null;
147     }
148
149     /**
150      * Gets all Cookies that were added to the response.
151      */

152     public Cookie JavaDoc[] getCookies()
153     {
154         List JavaDoc cookies = (List JavaDoc)_headers.get(SET_COOKIE);
155
156         return ( cookies != null ? ( Cookie JavaDoc[] ) cookies.toArray( new Cookie JavaDoc[0] ) : new Cookie JavaDoc[0] );
157     }
158
159     /**
160      * Returns <code>true</code> if this response containes the given header.
161      */

162     public boolean containsHeader( String JavaDoc name )
163     {
164         return _headers.containsKey( name );
165     }
166
167     /**
168      * Sets a response header with the given name and date-value.
169      */

170     public void setDateHeader( String JavaDoc name, long date )
171     {
172         setObjectHeader( name, new Date JavaDoc( date ) );
173     }
174
175     /**
176      * Adds a response header with the given name and date-value.
177      */

178     public void addDateHeader( String JavaDoc name, long date )
179     {
180         addObjectHeader( name, new Date JavaDoc( date ) );
181     }
182
183     /**
184      * Sets a response header with the given name and value.
185      */

186     public void setHeader( String JavaDoc name, String JavaDoc value )
187     {
188         setObjectHeader( name, value );
189     }
190
191     /**
192      * Adds a response header with the given name and value.
193      */

194     public void addHeader( String JavaDoc name, String JavaDoc value )
195     {
196         addObjectHeader( name, value );
197     }
198
199     /**
200      * Sets a response header with the given name and integer value.
201      */

202     public void setIntHeader( String JavaDoc name, int value )
203     {
204         setObjectHeader( name, new Integer JavaDoc( value ) );
205     }
206
207     /**
208      * Adds a response header with the given name and integer value.
209      */

210     public void addIntHeader( String JavaDoc name, int value )
211     {
212         addObjectHeader( name, new Integer JavaDoc( value ) );
213     }
214
215     /**
216      * Gets all headers.
217      *
218      * @return a Map of header-name (String) -> headers (List).
219      */

220     public Map JavaDoc getHeaders()
221     {
222         return _headers;
223     }
224
225     /**
226      * Gets all headers with the given name.
227      *
228      * @return a List of headers (String, Integer, Date, Cookie), or <code>null</code> if none are found.
229      */

230     public List JavaDoc getHeaders( String JavaDoc name )
231     {
232         return ( List JavaDoc ) _headers.get( name );
233     }
234
235     /**
236      * Gets the first header with the given name.
237      * @return an Object (String, Integer, Date, Cookie) that is the first header with the given name,
238      * or <code>null</code> if none is found.
239      */

240     public Object JavaDoc getFirstHeader( String JavaDoc name )
241     {
242         List JavaDoc foundHeaders = ( List JavaDoc ) _headers.get( name );
243         return ( ! foundHeaders.isEmpty() ? foundHeaders.get( 0 ) : null );
244     }
245
246     protected void addObjectHeader( String JavaDoc name, Object JavaDoc val )
247     {
248         List JavaDoc vals = ( List JavaDoc ) _headers.get( name );
249
250         if ( vals == null )
251         {
252             vals = new ArrayList JavaDoc();
253             _headers.put( name, vals );
254         }
255
256         vals.add( val );
257     }
258
259     protected void setObjectHeader( String JavaDoc name, Object JavaDoc val )
260     {
261         ArrayList JavaDoc vals = new ArrayList JavaDoc();
262         vals.add( val );
263         _headers.put( name, vals );
264     }
265
266     public HttpServletResponse JavaDoc getOuterResponse()
267     {
268         return (HttpServletResponse JavaDoc) 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 JavaDoc getStatusMessage()
282     {
283         return _statusMessage;
284     }
285
286     public void sendRedirect( String JavaDoc redirectURI )
287         throws IOException JavaDoc
288     {
289         _redirectURI = redirectURI;
290     }
291
292     /**
293      * Actually send the redirect that was suggested by {@link #sendRedirect}.
294      *
295      * @throws IllegalStateException if {@link #sendRedirect} was not called.
296      */

297     public void applyRedirect()
298         throws IOException JavaDoc
299     {
300         if ( _redirectURI != null )
301         {
302             super.sendRedirect( _redirectURI );
303         }
304         else
305         {
306             throw new IllegalStateException JavaDoc( "No redirect to apply." );
307         }
308     }
309
310     public boolean didRedirect()
311     {
312         return _redirectURI != null;
313     }
314
315     public String JavaDoc getRedirectURI()
316     {
317         return _redirectURI;
318     }
319 }
320
Popular Tags