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: package org.apache.cocoon.environment.http;
018:
019: import org.apache.cocoon.environment.Cookie;
020: import org.apache.cocoon.environment.Response;
021:
022: import javax.servlet.ServletOutputStream;
023: import javax.servlet.http.HttpServletResponse;
024: import java.io.IOException;
025: import java.io.PrintWriter;
026: import java.util.Locale;
027:
028: /**
029: * Implements the {@link org.apache.cocoon.environment.Response} interface
030: * to provide response functionality in the HTTP servlets environment.
031: *
032: * @author <a href="mailto:dev@cocoon.apache.org">Apache Cocoon Team</a>
033: * @version CVS $Id: HttpResponse.java 433543 2006-08-22 06:22:54Z crossley $
034: */
035:
036: public final class HttpResponse implements Response {
037:
038: /** The real HttpServletResponse object */
039: private final HttpServletResponse res;
040:
041: /**
042: * Creates a HttpServletResponse based on a real HttpServletResponse object
043: */
044: protected HttpResponse(HttpServletResponse res) {
045: this .res = res;
046: }
047:
048: /**
049: * Create a new cookie which is not added to the response
050: */
051: public Cookie createCookie(String name, String value) {
052: return new HttpCookie(name, value);
053: }
054:
055: public void addCookie(Cookie cookie) {
056: if (cookie instanceof HttpCookie) {
057: this .res
058: .addCookie(((HttpCookie) cookie).getServletCookie());
059: } else {
060: javax.servlet.http.Cookie newCookie;
061: newCookie = new javax.servlet.http.Cookie(cookie.getName(),
062: cookie.getValue());
063: newCookie.setComment(cookie.getComment());
064: newCookie.setDomain(cookie.getDomain());
065: newCookie.setMaxAge(cookie.getMaxAge());
066: newCookie.setPath(cookie.getPath());
067: newCookie.setSecure(cookie.getSecure());
068: newCookie.setVersion(cookie.getVersion());
069: this .res.addCookie(newCookie);
070: }
071: }
072:
073: public boolean containsHeader(String name) {
074: return this .res.containsHeader(name);
075: }
076:
077: public String encodeURL(String url) {
078: if (url != null && url.indexOf(";jsessionid=") != -1)
079: return url;
080: return this .res.encodeURL(url);
081: }
082:
083: public String encodeRedirectURL(String url) {
084: if (url != null && url.indexOf(";jsessionid=") != -1) {
085: return url;
086: }
087:
088: return this .res.encodeRedirectURL(url);
089: }
090:
091: public void sendError(int sc, String msg) throws IOException {
092: this .res.sendError(sc, msg);
093: }
094:
095: public void sendError(int sc) throws IOException {
096: this .res.sendError(sc);
097: }
098:
099: public void sendRedirect(String location) throws IOException {
100: this .res.sendRedirect(location);
101: }
102:
103: public void sendPermanentRedirect(String location)
104: throws IOException {
105: this .res.setHeader("location", location);
106: this .res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
107: }
108:
109: public void setDateHeader(String name, long date) {
110: this .res.setDateHeader(name, date);
111: }
112:
113: public void addDateHeader(String name, long date) {
114: this .res.addDateHeader(name, date);
115: }
116:
117: public void setHeader(String name, String value) {
118: this .res.setHeader(name, value);
119: }
120:
121: public void addHeader(String name, String value) {
122: this .res.addHeader(name, value);
123: }
124:
125: public void setIntHeader(String name, int value) {
126: this .res.setIntHeader(name, value);
127: }
128:
129: public void addIntHeader(String name, int value) {
130: this .res.addIntHeader(name, value);
131: }
132:
133: public void setStatus(int sc) {
134: this .res.setStatus(sc);
135: }
136:
137: /**
138: * @deprecated As of version 2.1, use encodeURL(String url) instead
139: */
140: public String encodeUrl(String url) {
141: return this .res.encodeUrl(url);
142: }
143:
144: /**
145: * @deprecated As of version 2.1, use
146: * encodeRedirectURL(String url) instead
147: */
148: public String encodeRedirectUrl(String url) {
149: return this .res.encodeRedirectUrl(url);
150: }
151:
152: /**
153: * @deprecated As of version 2.1, due to ambiguous meaning of the
154: * message parameter. To set a status code
155: * use <code>setStatus(int)</code>, to send an error with a description
156: * use <code>sendError(int, String)</code>.
157: */
158: public void setStatus(int sc, String sm) {
159: this .res.setStatus(sc, sm);
160: }
161:
162: /* The ServletResponse interface methods */
163:
164: public String getCharacterEncoding() {
165: return this .res.getCharacterEncoding();
166: }
167:
168: public ServletOutputStream getOutputStream() throws IOException {
169: //throw new IllegalStateException ("you are not a serializer or reader");
170: return this .res.getOutputStream();
171: }
172:
173: public PrintWriter getWriter() throws IOException {
174: //throw new IllegalStateException ("you are not a serializer or reader");
175: return this .res.getWriter();
176: }
177:
178: public void setContentLength(int len) {
179: this .res.setContentLength(len);
180: }
181:
182: public void setContentType(String type) {
183: this .res.setContentType(type);
184: }
185:
186: public void setBufferSize(int size) {
187: this .res.setBufferSize(size);
188: }
189:
190: public int getBufferSize() {
191: return this .res.getBufferSize();
192: }
193:
194: public void flushBuffer() throws IOException {
195: this .res.flushBuffer();
196: }
197:
198: public boolean isCommitted() {
199: return this .res.isCommitted();
200: }
201:
202: public void reset() {
203: this .res.reset();
204: }
205:
206: public void setLocale(Locale loc) {
207: this .res.setLocale(loc);
208: }
209:
210: public Locale getLocale() {
211: return this.res.getLocale();
212: }
213: }
|