KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
19
20 import javax.servlet.RequestDispatcher JavaDoc;
21 import javax.servlet.ServletContext JavaDoc;
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26 import org.apache.avalon.framework.logger.AbstractLogEnabled;
27 import org.apache.avalon.framework.parameters.Parameterizable;
28 import org.apache.avalon.framework.parameters.Parameters;
29 import org.apache.avalon.framework.thread.ThreadSafe;
30
31 /**
32  * Allows a Servlet or JSP to be used as a generator.
33  *
34  * <p>
35  * This implementation includes the servlet response using the
36  * RequestDispatcher from ServletContext.getNamedDispatcher().
37  * </p>
38  *
39  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
40  * @author <a HREF="mailto:bh22351@i-one.at">Bernhard Huber</a>
41  * @version CVS $Id: JSPEngineImplNamedDispatcherInclude.java 30932 2004-07-29 17:35:38Z vgritsenko $
42  */

43 public class JSPEngineImplNamedDispatcherInclude extends AbstractLogEnabled
44     implements JSPEngine, Parameterizable, ThreadSafe {
45
46     /**
47      * 'servlet-name' configuration parameter name for specifying
48      * the servlet name to dispatch to.
49      */

50     public static final String JavaDoc CONFIG_SERVLET_NAME = "servlet-name";
51     
52     /**
53      * 'forward' configuration parameter name for specifying
54      * whether or not the dispather should use forward
55      * instead of include.
56      */

57     public static final String JavaDoc CONFIG_FORWARD = "forward";
58     
59     /**
60      * Default value of CONFIG_SERVLET_NAME.
61      * The value is <code>*.jsp</code>,
62      * this is the WLS JSP servlet default name.
63      */

64     public static final String JavaDoc DEFAULT_SERVLET_NAME = "*.jsp";
65     
66     /**
67      * Default value of CONFIG_FORWARD.
68      * The value is <code>false</code>.
69      */

70     public static final boolean DEFAULT_FORWARD = false;
71     
72     /**
73      * the configured name of the jsp servlet
74      */

75     private String JavaDoc servletName = DEFAULT_SERVLET_NAME;
76     
77     /**
78      * Whether or not to use forward instead of include
79      * when dispatching to the Servlet.
80      */

81     private boolean forward = DEFAULT_FORWARD;
82
83     /**
84      * <p>
85      * The <code>forward</code> configuration parameter allows you to
86      * control whether to use the forward dispatch method instead of
87      * the include method which is used by default.
88      * </p>
89      * <p>
90      * Using the <code>servlet-name</code> configuration parameter
91      * you can specify the name of the Servlet to dispatch to.
92      * </p>
93      */

94     public void parameterize(Parameters params) {
95         this.servletName = params.getParameter(CONFIG_SERVLET_NAME, DEFAULT_SERVLET_NAME);
96         this.forward = params.getParameterAsBoolean(CONFIG_FORWARD, DEFAULT_FORWARD);
97     }
98
99     /**
100      * Execute the Servlet and return the output.
101      */

102     public byte[] executeJSP(String JavaDoc url,
103                              HttpServletRequest JavaDoc servletRequest,
104                              HttpServletResponse JavaDoc servletResponse,
105                              ServletContext JavaDoc servletContext)
106         throws IOException JavaDoc, ServletException JavaDoc, Exception JavaDoc {
107         
108         JSPEngineServletOutputStream output = new JSPEngineServletOutputStream();
109         JSPEngineServletRequest request = new JSPEngineServletRequest(servletRequest,url);
110         JSPEngineServletResponse response = new JSPEngineServletResponse(servletResponse,output);
111         
112         byte[] bytes = null;
113         
114         // dispatch to the named servlet
115
RequestDispatcher JavaDoc rd = servletContext.getNamedDispatcher(servletName);
116         if (rd != null) {
117             if (forward) {
118                 rd.forward(request,response);
119             }
120             else {
121                 rd.include(request,response);
122             }
123             response.flushBuffer();
124             bytes = output.toByteArray();
125         } else {
126             throw new Exception JavaDoc("No RequestDispatcher found. Specify a correct '"
127                                  + CONFIG_SERVLET_NAME + "': " + servletName);
128         }
129         return bytes;
130     }
131 }
132
133
Popular Tags