KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > jsp > JSPEngineImplWLS


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.components.jsp;
17
18 import org.apache.avalon.framework.logger.AbstractLogEnabled;
19 import org.apache.avalon.framework.parameters.ParameterException;
20 import org.apache.avalon.framework.parameters.Parameters;
21 import org.apache.avalon.framework.parameters.Parameterizable;
22 import org.apache.avalon.framework.thread.ThreadSafe;
23
24 import javax.servlet.RequestDispatcher JavaDoc;
25 import javax.servlet.ServletContext JavaDoc;
26 import javax.servlet.ServletException JavaDoc;
27 import javax.servlet.http.Cookie JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30 import java.io.ByteArrayOutputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.Locale JavaDoc;
33
34 /**
35  * Allows WLS JSP to be used as a generator.
36  *
37  * This implementation includes via ServletContext.getNamedDispatcher() the
38  * jsp-response. This a WLS-specific implementation.
39  * This code contain WLS 5.1 specific classes, and uses WLS internal classes.
40  *
41  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
42  * @author <a HREF="mailto:bh22351@i-one.at">Bernhard Huber</a>
43  * @version CVS $Id: JSPEngineImplWLS.java 124676 2005-01-08 20:19:34Z antonio $
44  */

45 public class JSPEngineImplWLS extends AbstractLogEnabled
46     implements JSPEngine, Parameterizable, ThreadSafe {
47
48     /** The Servlet Include Path */
49     public static final String JavaDoc INC_SERVLET_PATH = "javax.servlet.include.servlet_path";
50
51     /** config-parameter name for specifying the jsp servlet-name.
52       ie. servlet-name
53     */

54     public static final String JavaDoc CONFIG_SERVLET_NAME = "servlet-name";
55     /** default value of CONFIG_SERVLET_NAME.
56       ie. *jsp, this is the WLS JSP servlet default name
57     */

58     public static final String JavaDoc DEFAULT_SERVLET_NAME = "*.jsp";
59     /** the configured name of the jsp servlet
60     */

61     
62     String JavaDoc servletName = DEFAULT_SERVLET_NAME;
63
64     /**
65      * parameterize
66      * @param params Parameters
67      * @exception ParameterException
68      */

69     public void parameterize(Parameters params) throws ParameterException {
70         this.servletName = params.getParameter( CONFIG_SERVLET_NAME, DEFAULT_SERVLET_NAME);
71     }
72
73     /**
74      * execute the JSP and return the output
75      *
76      * @param url
77      * @param servletRequest
78      * @param servletResponse
79      * @param servletContext
80      * @exception IOException
81      * @exception ServletException
82      * @exception Exception
83      */

84     public byte[] executeJSP(String JavaDoc url,
85                              HttpServletRequest JavaDoc servletRequest,
86                              HttpServletResponse JavaDoc servletResponse,
87                              ServletContext JavaDoc servletContext)
88         throws IOException JavaDoc, ServletException JavaDoc, Exception JavaDoc {
89
90         byte[] bytes = null;
91
92         HttpServletRequest JavaDoc request = servletRequest;
93         String JavaDoc inc_servlet_path_was = (String JavaDoc) servletRequest.getAttribute(INC_SERVLET_PATH);
94         request.setAttribute(INC_SERVLET_PATH, url);
95         MyWLSResponse response = new MyWLSResponse( servletResponse,
96           (weblogic.servlet.internal.ServletContextImpl) servletContext);
97
98         // dispatch to the named servlet
99
RequestDispatcher JavaDoc rd = servletContext.getNamedDispatcher(servletName);
100         if (rd != null) {
101           rd.include(request,response);
102           response.flushBuffer();
103           
104           if (getLogger().isDebugEnabled()) {
105               getLogger().debug("JSP response: " + response.getResponseContentAsString());
106           }
107           
108           bytes = response.getResponseContentAsByteArray();
109
110           if (inc_servlet_path_was != null) {
111             servletRequest.setAttribute( INC_SERVLET_PATH, inc_servlet_path_was );
112           }
113         } else {
114           getLogger().error( "Specify a correct " + CONFIG_SERVLET_NAME + " " + servletName );
115         }
116
117         return bytes;
118     }
119
120     /** WLS jsp servlet hack.
121       <p>
122         Here WLS specific classes are used.
123       </p>
124       <p>
125         The weblogic.servlet.JSPServlet, and
126         weblogic.servlet.internal.RequesDispatcherImpl expects
127         objects weblogic.servlet.internal.ServletOutputStreamImpl,
128         and weblogic.servlet.internal.ServletResponseImpl.
129         Thus we have to use <i>exactly</i> these classes!
130       </p>
131     */

132     static class MyWLSResponse extends weblogic.servlet.internal.ServletResponseImpl {
133       /* the cocoon2 response. Let's use this response to forward headers
134       , cookies, etc generated inside the jsp-response
135       */

136       HttpServletResponse JavaDoc response;
137
138       ByteArrayOutputStream JavaDoc baos;
139       weblogic.servlet.internal.ServletOutputStreamImpl wlsOutputStream;
140
141       public MyWLSResponse( HttpServletResponse JavaDoc response,
142         weblogic.servlet.internal.ServletContextImpl servlet_context ) {
143
144         super( servlet_context );
145         this.response = response;
146
147         baos = new ByteArrayOutputStream JavaDoc();
148
149         wlsOutputStream =
150           new weblogic.servlet.internal.ServletOutputStreamImpl( baos );
151         this.setOutputStream( wlsOutputStream );
152         wlsOutputStream.setImpl( this );
153       }
154
155       /** flush response content.
156       */

157       public void flushBuffer() throws IOException JavaDoc {
158         super.flushBuffer();
159         baos.flush();
160       }
161
162       /** return response as byte array.
163         <p>Note: http-headers are skipped. More exactly all chars until first
164         '&lt;?xml', or '\r\n\r\n&lt; sequence. This may be a bit heuristic.
165         </p>
166         <p>Note: we are expecting the xml prolog, without the xml prolog http
167         -headers are passed further, and the xml parser will surly complain!
168         </p>
169       */

170       public byte[] getResponseContentAsByteArray() {
171         byte[] baos_arr = baos.toByteArray();
172
173         int baos_arr_length = baos_arr.length;
174         int i = 0;
175         boolean matched = false;
176         final int I_MAX = 8192; // check only header
177

178         final byte MATCH_0d = (byte)'\r';
179         final byte MATCH_0a = (byte)'\n';
180
181         final byte MATCH_FIRST = (byte)'<';
182         final byte MATCH_SECOND = (byte)'?';
183         final byte MATCH_THIRD = (byte)'x';
184         final byte MATCH_FOURTH = (byte)'m';
185         final byte MATCH_FIFTH = (byte)'l';
186
187         final int MATCH_COUNT = 5;
188
189         while (i + MATCH_COUNT < baos_arr_length && i < I_MAX && !matched) {
190
191           matched = (baos_arr[i] == MATCH_FIRST && baos_arr[i+1] == MATCH_SECOND &&
192              baos_arr[i+2] == MATCH_THIRD && baos_arr[i+3] == MATCH_FOURTH &&
193              baos_arr[i+4] == MATCH_FIFTH);
194           if (matched) break;
195
196           matched = (baos_arr[i] == MATCH_0d && baos_arr[i+1] == MATCH_0a &&
197             baos_arr[i+2] == MATCH_0d && baos_arr[i+3] == MATCH_0a &&
198             baos_arr[i+4] == MATCH_FIRST);
199           if (matched) {
200             i += 4; // skip leading \r\n\r\n, too
201
break;
202           }
203           i += 2;
204         }
205         if (matched && i > 0) {
206           int baos_arr_new_length = baos_arr_length - i;
207
208           byte []new_baos_arr = new byte[baos_arr_new_length];
209           System.arraycopy( baos_arr, i, new_baos_arr, 0, baos_arr_new_length );
210           baos_arr = new_baos_arr;
211         }
212         return baos_arr;
213       }
214
215       public String JavaDoc getResponseContentAsString() {
216         String JavaDoc s = new String JavaDoc( getResponseContentAsByteArray() );
217         return s;
218       }
219
220       // following methods forwarding from jsp-repsonse to cocoon2-repsonse
221

222       public String JavaDoc getCharacterEncoding() { return this.response.getCharacterEncoding();}
223       public Locale JavaDoc getLocale(){ return this.response.getLocale();}
224       public void addCookie(Cookie JavaDoc cookie){ response.addCookie(cookie); }
225       public boolean containsHeader(String JavaDoc s){ return response.containsHeader(s); }
226       /** @deprecated use encodeURL(String url) instead. */
227       public String JavaDoc encodeUrl(String JavaDoc s){ return response.encodeUrl(s); }
228       public String JavaDoc encodeURL(String JavaDoc s){ return response.encodeURL(s); }
229       /** @deprecated use encodeRedirectURL(String url) instead. */
230       public String JavaDoc encodeRedirectUrl(String JavaDoc s){ return response.encodeRedirectUrl(s); }
231       public String JavaDoc encodeRedirectURL(String JavaDoc s){ return response.encodeRedirectURL(s); }
232       public void sendError(int i, String JavaDoc s)
233           throws IOException JavaDoc{response.sendError(i,s); }
234       public void sendError(int i)
235           throws IOException JavaDoc{response.sendError(i); }
236       public void sendRedirect(String JavaDoc s)
237           throws IOException JavaDoc{response.sendRedirect(s); }
238       public void setDateHeader(String JavaDoc s, long l){response.setDateHeader(s, l); }
239       public void addDateHeader(String JavaDoc s, long l){response.addDateHeader(s, l); }
240       public void setHeader(String JavaDoc s, String JavaDoc s1){response.setHeader(s, s1); }
241       public void addHeader(String JavaDoc s, String JavaDoc s1){response.addHeader(s, s1); }
242       public void setIntHeader(String JavaDoc s, int i){response.setIntHeader(s, i); }
243       public void addIntHeader(String JavaDoc s, int i){response.addIntHeader(s, i); }
244       public void setStatus(int i){response.setStatus(i); }
245       /** @deprecated use sendError(int, String) instead */
246       public void setStatus(int i, String JavaDoc s){response.setStatus(i, s); }
247     }
248 }
249
Popular Tags