KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > core > CmsResponseHttpServlet


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/core/CmsResponseHttpServlet.java,v $
3  * Date : $Date: 2005/06/27 23:27:46 $
4  * Version: $Revision: 1.7 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2001 The OpenCms Group
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about OpenCms, please see the
22  * OpenCms Website: http://www.opencms.org
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27  */

28
29 package com.opencms.core;
30
31 import org.opencms.main.CmsLog;
32 import org.opencms.main.OpenCms;
33
34 import java.io.IOException JavaDoc;
35 import java.io.OutputStream JavaDoc;
36
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
39
40 /**
41  * Implementation of the I_CmsResponse interface which wraps a HttpServletResponse
42  * and provides OpenCms with a facility to handle redirects.
43  *
44  * @author Michael Emmerich
45  *
46  * @version $Revision: 1.7 $ $Date: 2005/06/27 23:27:46 $
47  *
48  * @deprecated Will not be supported past the OpenCms 6 release.
49  */

50 public class CmsResponseHttpServlet implements I_CmsResponse {
51
52     private static String JavaDoc C_LAST_MODIFIED = "Last-Modified";
53
54     /** Debug flag. */
55     private static final boolean DEBUG = false;
56
57     /** Flag to indicate what JSDK is available. */
58     private static boolean jsdk2 = checkJsdk();
59
60     /** String to save the content type. */
61     private String JavaDoc m_contentType;
62
63     /** Buffer for the output stream. */
64     private OutputStream JavaDoc m_orgOutputStream;
65
66     /** Remember, if a redirect was sent. */
67     private boolean m_redir;
68
69     /** The original wrapped request. */
70     private HttpServletRequest JavaDoc m_req;
71
72     /** The original wrapped response. */
73     private HttpServletResponse JavaDoc m_res;
74
75     /** The type of this CmsResponset. */
76     private int m_type = com.opencms.core.I_CmsConstants.C_RESPONSE_HTTP;
77
78     /**
79      * Constructor, creates a new CmsResponseHttpServlet object.<p>
80      *
81      * @param req The original HttpServletRequest used to create this CmsRequest.
82      * @param res The original HttpServletResponse used to create this CmsResponse.
83      */

84     public CmsResponseHttpServlet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
85
86         m_res = res;
87         m_req = req;
88         // write OpenCms server identification in the response header
89
m_res.setHeader("Server", "OpenCms/" + OpenCms.getSystemInfo().getVersionName());
90     }
91
92     /**
93      * Check the JSDK version available at runtime.
94      *
95      * @return <code>true</code> if JSDK 2, <code>false</code> if JSDK 1
96      */

97     public static boolean checkJsdk() {
98
99         // Look for the method "addHeader". This method only is included in JSDK 2
100
Class JavaDoc rc = HttpServletResponse JavaDoc.class;
101         java.lang.reflect.Method JavaDoc m = null;
102         try {
103             m = rc.getMethod("addHeader", new Class JavaDoc[] {String JavaDoc.class, String JavaDoc.class});
104         } catch (Exception JavaDoc e) {
105             m = null;
106         }
107
108         // If m != null, the method could be found.
109
boolean result = (m != null);
110
111         if (CmsLog.INIT.isDebugEnabled()) {
112             if (result) {
113                 // We have JSDK 2
114
CmsLog.INIT.debug("Compatibility check - JSDK 2 detected. ");
115             } else {
116                 // We have JSDK 1
117
CmsLog.INIT.debug("Compatibility check - JSDK 1 detected. ");
118             }
119         }
120         return result;
121     }
122
123     /**
124      * Add a header-field in the response.
125      *
126      * @param key The key for the header.
127      * @param value The value for the header.
128      */

129     public void addHeader(String JavaDoc key, String JavaDoc value) {
130
131         if (DEBUG) {
132             System.err.println("CmsResponse.addHeader(" + key + "," + value + ")");
133         }
134
135         if (jsdk2) {
136             m_res.addHeader(key, value);
137         } else {
138             m_res.setHeader(key, value);
139         }
140     }
141
142     /**
143      * Checks, if the header was set already.
144      * @param key the header-key to check.
145      * @return true if the header was set before else false.
146      */

147     public boolean containsHeader(String JavaDoc key) {
148
149         if (DEBUG) {
150             System.err.println("CmsResponse.containsHeader(" + key + ")");
151         }
152
153         return m_res.containsHeader(key);
154     }
155
156     /**
157      * Returns the content type of the response which has previously
158      * been set using {@link #setContentType(String)}.
159      *
160      * @return the content type of the response.
161      */

162     public String JavaDoc getContentType() {
163
164         if (DEBUG) {
165             System.err.println("CmsResponse.getContentType()");
166         }
167
168         return m_contentType;
169     }
170
171     /**
172      * Returns the original response that was used to create the CmsResponse.
173      *
174      * @return The original response of the CmsResponse.
175      */

176     public HttpServletResponse JavaDoc getOriginalResponse() {
177
178         return m_res;
179     }
180
181     /**
182      * Returns the type of the response that was used to create the CmsResponse,
183      * which will be a C_RESPONSE_HTTP value for this wrapper implementation.
184      *
185      * @return The type of the CmsResponse which is C_RESPONSE_HTTP
186      */

187     public int getOriginalResponseType() {
188
189         return m_type;
190     }
191
192     /**
193      * Returns an OutputStream for writing the response data.
194      *
195      * @return OutputStream for writing data.
196      * @throws IOException if an error occurs
197      */

198     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
199
200         if (m_orgOutputStream == null) {
201             m_orgOutputStream = m_res.getOutputStream();
202         }
203         return m_orgOutputStream;
204     }
205
206     /**
207      * Check if the output stream was written previously.
208      *
209      * @return <code>true</code> if getOutputStream() was called, <code>false</code> otherwise.
210      */

211     public boolean isOutputWritten() {
212
213         return m_orgOutputStream != null;
214     }
215
216     /**
217      * Check if the current request was redirected. In this case, the
218      * servlet must not write any bytes to the output stream.
219      *
220      * @return <code>true</code> if the request is redirected, <code>false</code> otherwise.
221      */

222
223     public boolean isRedirected() {
224
225         return m_redir;
226     }
227
228     /**
229      * Sets a redirect to send the responst to.
230      * The original HttpServletResponse redirect is used here. Additional information
231      * about the servlets location is taken from the original HttpServletRequest.
232      *
233      * @param location The location the response is send to.
234      * // throws IOException if an error occurs
235      */

236     public void sendCmsRedirect(String JavaDoc location) {
237
238         if (DEBUG) {
239             System.err.println("CmsResponse.sendCmsRedirect(" + location + ")");
240         }
241
242         String JavaDoc hostName = m_req.getScheme() + "://" + m_req.getServerName() + ":" + m_req.getServerPort();
243         m_redir = true;
244         String JavaDoc servlet = m_req.getServletPath();
245         String JavaDoc contextPath = "";
246         try {
247             contextPath = m_req.getContextPath();
248         } catch (NoSuchMethodError JavaDoc err) {
249             // ignore this error - old servlet-api
250
}
251         try {
252             m_res.sendRedirect(hostName + contextPath + servlet + location);
253         } catch (IOException JavaDoc exc) {
254             if (CmsLog.getLog(this).isWarnEnabled()) {
255                 CmsLog.getLog(this).warn(
256                     "Couldn't redirect http response to: " + hostName + contextPath + servlet + location);
257             }
258         }
259     }
260
261     /**
262      * Sets the error code that is returnd by the response. The error code is specified
263      * by a numeric value.
264      *
265      * @param code The error code to be set.
266      * @throws IOException if an error occurs
267      */

268     public void sendError(int code) throws IOException JavaDoc {
269
270         if (DEBUG) {
271             System.err.println("CmsResponse.sendError(" + code + ")");
272         }
273
274         m_res.sendError(code);
275     }
276
277     /**
278      * Sets the error code and a additional message that is returnd by the response.
279      * The error code is specified by a numeric value.
280      *
281      * @param code The error code to be set.
282      * @param msg Additional error message.
283      * @throws IOException if an error occurs
284      */

285     public void sendError(int code, String JavaDoc msg) throws IOException JavaDoc {
286
287         if (DEBUG) {
288             System.err.println("CmsResponse.sendError(" + code + "," + msg + ")");
289         }
290
291         m_res.sendError(code, msg);
292     }
293
294     /**
295      * Helper function for a redirect to the cluster url.
296      * If <code>location</code> has the same hostname as the host of this servlet use the cluster url.
297      *
298      * @param location a full url, eg. http://servername/servlets/opencms/index.html
299      * @throws IOException if an error occurs
300      */

301     public void sendRedirect(String JavaDoc location) throws IOException JavaDoc {
302
303         if (DEBUG) {
304             System.err.println("CmsResponse.sendRedirect(" + location + ")");
305         }
306
307         String JavaDoc shortLocation = location;
308         String JavaDoc hostName = m_req.getServerName() + ":" + m_req.getServerPort();
309         // remove 'http', '://', servername and '/servlets/opencms' and send CmsRedirect
310
if (shortLocation.startsWith(m_req.getScheme())) {
311             shortLocation = shortLocation.substring(m_req.getScheme().length());
312         }
313         if (shortLocation.startsWith("://")) {
314             shortLocation = shortLocation.substring(3);
315         }
316         if (shortLocation.startsWith(hostName)) {
317             shortLocation = shortLocation.substring(hostName.length());
318             String JavaDoc contextPath = "";
319             try {
320                 contextPath = m_req.getContextPath();
321             } catch (NoSuchMethodError JavaDoc err) {
322                 // ignore this error - old servlet-api
323
}
324             if (shortLocation.startsWith(contextPath + m_req.getServletPath())) {
325                 shortLocation = shortLocation.substring((contextPath + m_req.getServletPath()).length());
326             }
327             sendCmsRedirect(shortLocation);
328         } else {
329
330             // wanted to redirect on another site, don't use the cluster url
331
m_res.sendRedirect(location);
332         }
333     }
334
335     /**
336      * Sets the length of the content being returned by the server.
337      *
338      * @param len Number of bytes to be returned by the response.
339      */

340     public void setContentLength(int len) {
341
342         if (DEBUG) {
343             System.err.println("CmsResponse.setContentLength(" + len + ")");
344         }
345
346         m_res.setContentLength(len);
347     }
348
349     /**
350      * Sets the content type of the response to the specified type.
351      *
352      * @param type The contnent type of the response.
353      */

354     public void setContentType(String JavaDoc type) {
355
356         if (DEBUG) {
357             System.err.println("CmsResponse.setContentType(" + type + ")");
358         }
359
360         m_contentType = type;
361         m_res.setContentType(type);
362     }
363
364     /**
365      * Sets a header-field in the response.
366      *
367      * @param key The key for the header.
368      * @param value The value for the header.
369      */

370     public void setHeader(String JavaDoc key, String JavaDoc value) {
371
372         if (DEBUG) {
373             System.err.println("CmsResponse.setHeader(" + key + "," + value + ")");
374         }
375
376         m_res.setHeader(key, value);
377     }
378
379     /**
380      * Sets the last modified header-field in the response.
381      *
382      * @param time The last-modified time.
383      */

384     public void setLastModified(long time) {
385
386         if (DEBUG) {
387             System.err.println("CmsResponse.setLastModified(" + time + ")");
388         }
389
390         m_res.setDateHeader(C_LAST_MODIFIED, time);
391     }
392 }
Popular Tags