001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow.scoping.internal;
020:
021: import org.apache.beehive.netui.pageflow.scoping.ScopedResponse;
022: import org.apache.beehive.netui.util.logging.Logger;
023:
024: import javax.servlet.http.HttpServletResponse;
025: import javax.servlet.http.HttpServletResponseWrapper;
026: import javax.servlet.http.Cookie;
027: import java.io.IOException;
028: import java.util.HashMap;
029: import java.util.Date;
030: import java.util.ArrayList;
031: import java.util.List;
032: import java.util.Map;
033:
034: /**
035: * A wrapper around HttpServletResponse, associated with a given scope-key. Delegates to the wrapped
036: * response object for some functionality, but prevents output or error codes or forwards from actually
037: * happening.
038: */
039: public class ScopedResponseImpl extends HttpServletResponseWrapper
040: implements ScopedResponse {
041: private static final Cookie[] NO_COOKIES = new Cookie[0];
042:
043: private boolean _isError = false;
044: private int _statusCode = -1;
045: private String _redirectURI = null;
046: private String _statusMessage = null;
047:
048: /** Map of name (String) -> headers (List). There can be more than one for each name. **/
049: private HashMap _headers = new HashMap();
050:
051: private static final String SET_COOKIE = "Set-Cookie";
052: private static final Logger logger = Logger
053: .getInstance(ScopedResponseImpl.class);
054:
055: public ScopedResponseImpl(HttpServletResponse servletResponse) {
056: super (servletResponse);
057: }
058:
059: public void sendError(int i, String s) throws IOException {
060: _isError = true;
061: _statusCode = i;
062: _statusMessage = s;
063:
064: if (logger.isInfoEnabled()) {
065: StringBuffer msg = new StringBuffer("ScopedResponse error ")
066: .append(i);
067: logger.info(msg.append(": ").append(s));
068: }
069: }
070:
071: public void sendError(int i) throws IOException {
072: sendError(i, "");
073: }
074:
075: public void setStatus(int i) {
076: setStatus(i, "");
077: }
078:
079: public void setStatus(int i, String s) {
080: _statusCode = i;
081: _statusMessage = s;
082: }
083:
084: public void setContentLength(int i) {
085: // don't do anything
086: }
087:
088: public void setContentType(String s) {
089: // don't do anything
090: }
091:
092: public void setBufferSize(int i) {
093: // don't do anything
094: }
095:
096: public void resetBuffer() {
097: // don't do anything
098: }
099:
100: public void reset() {
101: // don't do anything
102: }
103:
104: //
105: // Headers: We need some special handling for headers. Since we're
106: // *including* portlets, the response received from WLS will have
107: // no-op methods for all headers. So, this implementation collects
108: // headers explicitly, to avoid losing them.
109: //
110:
111: /**
112: * Add a cookie to the response.
113: */
114: public void addCookie(Cookie cookie) {
115: addObjectHeader(SET_COOKIE, cookie);
116: }
117:
118: /**
119: * Gets a cookie that was added to the response.
120: */
121: public Cookie getCookie(String cookieName) {
122: List cookies = getHeaders(SET_COOKIE);
123: if (cookies != null) {
124: // start looking from the back (ie. the last cookie set)
125: for (int i = cookies.size(); --i > -1;) {
126: Cookie cookie = (Cookie) cookies.get(i);
127: if (cookie.getName().equals(cookieName)) {
128: return cookie;
129: }
130: }
131: }
132:
133: return null;
134: }
135:
136: /**
137: * Gets all Cookies that were added to the response.
138: */
139: public Cookie[] getCookies() {
140: List cookies = (List) _headers.get(SET_COOKIE);
141: return cookies != null ? (Cookie[]) cookies
142: .toArray(new Cookie[cookies.size()]) : NO_COOKIES;
143: }
144:
145: /**
146: * Returns <code>true</code> if this response containes the given header.
147: */
148: public boolean containsHeader(String name) {
149: return _headers.containsKey(name);
150: }
151:
152: /**
153: * Sets a response header with the given name and date-value.
154: */
155: public void setDateHeader(String name, long date) {
156: setObjectHeader(name, new Date(date));
157: }
158:
159: /**
160: * Adds a response header with the given name and date-value.
161: */
162: public void addDateHeader(String name, long date) {
163: addObjectHeader(name, new Date(date));
164: }
165:
166: /**
167: * Sets a response header with the given name and value.
168: */
169: public void setHeader(String name, String value) {
170: setObjectHeader(name, value);
171: }
172:
173: /**
174: * Adds a response header with the given name and value.
175: */
176: public void addHeader(String name, String value) {
177: addObjectHeader(name, value);
178: }
179:
180: /**
181: * Sets a response header with the given name and integer value.
182: */
183: public void setIntHeader(String name, int value) {
184: setObjectHeader(name, new Integer(value));
185: }
186:
187: /**
188: * Adds a response header with the given name and integer value.
189: */
190: public void addIntHeader(String name, int value) {
191: addObjectHeader(name, new Integer(value));
192: }
193:
194: /**
195: * Gets all headers.
196: *
197: * @return a Map of header-name (String) -> headers (List).
198: */
199: public Map getHeaders() {
200: return _headers;
201: }
202:
203: /**
204: * Gets all headers with the given name.
205: *
206: * @return a List of headers (String, Integer, Date, Cookie), or <code>null</code> if none are found.
207: */
208: public List getHeaders(String name) {
209: return (List) _headers.get(name);
210: }
211:
212: /**
213: * Gets the first header with the given name.
214: * @return an Object (String, Integer, Date, Cookie) that is the first header with the given name,
215: * or <code>null</code> if none is found.
216: */
217: public Object getFirstHeader(String name) {
218: List foundHeaders = (List) _headers.get(name);
219: return !foundHeaders.isEmpty() ? foundHeaders.get(0) : null;
220: }
221:
222: protected void addObjectHeader(String name, Object val) {
223: List vals = (List) _headers.get(name);
224:
225: if (vals == null) {
226: vals = new ArrayList();
227: _headers.put(name, vals);
228: }
229:
230: vals.add(val);
231: }
232:
233: protected void setObjectHeader(String name, Object val) {
234: ArrayList vals = new ArrayList();
235: vals.add(val);
236: _headers.put(name, vals);
237: }
238:
239: public HttpServletResponse getOuterResponse() {
240: return (HttpServletResponse) getResponse();
241: }
242:
243: public boolean isError() {
244: return _isError;
245: }
246:
247: public int getStatusCode() {
248: return _statusCode;
249: }
250:
251: public String getStatusMessage() {
252: return _statusMessage;
253: }
254:
255: public void sendRedirect(String redirectURI) throws IOException {
256: _redirectURI = redirectURI;
257: }
258:
259: /**
260: * Actually send the redirect that was suggested by {@link #sendRedirect}.
261: *
262: * @throws IllegalStateException if {@link #sendRedirect} was not called.
263: */
264: public void applyRedirect() throws IOException {
265: if (_redirectURI != null) {
266: super .sendRedirect(_redirectURI);
267: } else {
268: throw new IllegalStateException("No redirect to apply.");
269: }
270: }
271:
272: public boolean didRedirect() {
273: return _redirectURI != null;
274: }
275:
276: public String getRedirectURI() {
277: return _redirectURI;
278: }
279: }
|