KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > environment > http > HttpResponse


1 /*
2  * Copyright 1999-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 package org.apache.cocoon.environment.http;
17
18 import org.apache.cocoon.environment.Cookie;
19 import org.apache.cocoon.environment.Response;
20
21 import javax.servlet.ServletOutputStream JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.util.Locale JavaDoc;
26
27 /**
28  * Implements the {@link org.apache.cocoon.environment.Response} interface
29  * to provide response functionality in the HTTP servlets environment.
30  *
31  * @author <a HREF="mailto:dev@cocoon.apache.org">Apache Cocoon Team</a>
32  * @version CVS $Id: HttpResponse.java 30932 2004-07-29 17:35:38Z vgritsenko $
33  */

34
35 public final class HttpResponse implements Response {
36
37     /** The real HttpServletResponse object */
38     private final HttpServletResponse JavaDoc res;
39
40     /**
41      * Creates a HttpServletResponse based on a real HttpServletResponse object
42      */

43     protected HttpResponse (HttpServletResponse JavaDoc res) {
44         this.res = res;
45     }
46
47     /**
48      * Create a new cookie which is not added to the response
49      */

50     public Cookie createCookie(String JavaDoc name, String JavaDoc value) {
51         return new HttpCookie(name, value);
52     }
53
54     public void addCookie(Cookie cookie) {
55         if (cookie instanceof HttpCookie) {
56             this.res.addCookie(((HttpCookie)cookie).getServletCookie());
57         } else {
58             javax.servlet.http.Cookie JavaDoc newCookie;
59             newCookie = new javax.servlet.http.Cookie JavaDoc(cookie.getName(), cookie.getValue());
60             newCookie.setComment(cookie.getComment());
61             newCookie.setDomain(cookie.getDomain());
62             newCookie.setMaxAge(cookie.getMaxAge());
63             newCookie.setPath(cookie.getPath());
64             newCookie.setSecure(cookie.getSecure());
65             newCookie.setVersion(cookie.getVersion());
66             this.res.addCookie(newCookie);
67         }
68     }
69
70     public boolean containsHeader(String JavaDoc name) {
71         return this.res.containsHeader(name);
72     }
73
74     public String JavaDoc encodeURL(String JavaDoc url) {
75         if (url != null && url.indexOf(";jsessionid=") != -1)
76             return url;
77         return this.res.encodeURL(url);
78     }
79
80     public String JavaDoc encodeRedirectURL(String JavaDoc url) {
81         if (url != null && url.indexOf(";jsessionid=") != -1) {
82             return url;
83         }
84
85         return this.res.encodeRedirectURL(url);
86     }
87
88     public void sendError(int sc, String JavaDoc msg) throws IOException JavaDoc {
89         this.res.sendError(sc, msg);
90     }
91
92     public void sendError(int sc) throws IOException JavaDoc {
93         this.res.sendError(sc);
94     }
95
96     public void sendRedirect(String JavaDoc location) throws IOException JavaDoc {
97         this.res.sendRedirect(location);
98     }
99
100     public void sendPermanentRedirect(String JavaDoc location) throws IOException JavaDoc {
101         this.res.setHeader("location", location);
102         this.res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
103     }
104     
105     public void setDateHeader(String JavaDoc name, long date) {
106         this.res.setDateHeader(name, date);
107     }
108
109     public void addDateHeader(String JavaDoc name, long date) {
110         this.res.addDateHeader(name, date);
111     }
112
113     public void setHeader(String JavaDoc name, String JavaDoc value) {
114         this.res.setHeader(name, value);
115     }
116
117     public void addHeader(String JavaDoc name, String JavaDoc value) {
118         this.res.addHeader(name, value);
119     }
120
121     public void setIntHeader(String JavaDoc name, int value) {
122         this.res.setIntHeader(name, value);
123     }
124
125     public void addIntHeader(String JavaDoc name, int value) {
126         this.res.addIntHeader(name, value);
127     }
128
129     public void setStatus(int sc) {
130         this.res.setStatus(sc);
131     }
132
133     /**
134      * @deprecated As of version 2.1, use encodeURL(String url) instead
135      */

136     public String JavaDoc encodeUrl(String JavaDoc url) {
137         return this.res.encodeUrl(url);
138     }
139
140     /**
141      * @deprecated As of version 2.1, use
142      * encodeRedirectURL(String url) instead
143      */

144     public String JavaDoc encodeRedirectUrl(String JavaDoc url) {
145         return this.res.encodeRedirectUrl(url);
146     }
147
148     /**
149      * @deprecated As of version 2.1, due to ambiguous meaning of the
150      * message parameter. To set a status code
151      * use <code>setStatus(int)</code>, to send an error with a description
152      * use <code>sendError(int, String)</code>.
153      */

154     public void setStatus(int sc, String JavaDoc sm) {
155         this.res.setStatus(sc, sm);
156     }
157
158     /* The ServletResponse interface methods */
159
160     public String JavaDoc getCharacterEncoding() {
161         return this.res.getCharacterEncoding();
162     }
163
164     public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
165         //throw new IllegalStateException ("you are not a serializer or reader");
166
return this.res.getOutputStream();
167     }
168
169     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
170         //throw new IllegalStateException ("you are not a serializer or reader");
171
return this.res.getWriter();
172     }
173
174     public void setContentLength(int len) {
175         this.res.setContentLength(len);
176     }
177
178     public void setContentType(String JavaDoc type) {
179         this.res.setContentType(type);
180     }
181
182     public void setBufferSize(int size) {
183         this.res.setBufferSize(size);
184     }
185
186     public int getBufferSize() {
187         return this.res.getBufferSize();
188     }
189
190     public void flushBuffer() throws IOException JavaDoc {
191         this.res.flushBuffer();
192     }
193
194     public boolean isCommitted() {
195         return this.res.isCommitted();
196     }
197
198     public void reset() {
199         this.res.reset();
200     }
201
202     public void setLocale(Locale JavaDoc loc) {
203         this.res.setLocale(loc);
204     }
205
206     public Locale JavaDoc getLocale() {
207         return this.res.getLocale();
208     }
209 }
210
211
Popular Tags