KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > reading > JSPReader


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.reading;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStreamReader JavaDoc;
21 import java.io.OutputStreamWriter JavaDoc;
22 import java.io.Reader JavaDoc;
23 import java.io.Writer JavaDoc;
24
25 import javax.servlet.ServletContext JavaDoc;
26 import javax.servlet.ServletException JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29
30 import org.apache.avalon.framework.configuration.Configurable;
31 import org.apache.avalon.framework.configuration.Configuration;
32 import org.apache.avalon.framework.configuration.ConfigurationException;
33 import org.apache.cocoon.ProcessingException;
34 import org.apache.cocoon.components.jsp.JSPEngine;
35 import org.apache.cocoon.environment.http.HttpEnvironment;
36 import org.apache.excalibur.source.Source;
37
38 /**
39  * The <code>JSPReader</code> component is used to serve Servlet and JSP page
40  * output data in a sitemap pipeline.
41  *
42  * @author <a HREF="mailto:kpiroumian@flagship.ru">Konstantin Piroumian</a>
43  * @version CVS $Id: JSPReader.java 30932 2004-07-29 17:35:38Z vgritsenko $
44  */

45 public class JSPReader extends ServiceableReader implements Configurable {
46
47     private static final int DEFAULT_BUFFER_SIZE = 8192;
48
49     // buffer size for IO
50
private int bufferSize;
51
52     // output encoding
53
private String JavaDoc outputEncoding;
54
55     public void configure(Configuration conf) throws ConfigurationException {
56         bufferSize = conf.getChild("buffer-size").getValueAsInteger(DEFAULT_BUFFER_SIZE);
57         outputEncoding = conf.getChild("output-encoding").getValue(null);
58     }
59
60     /**
61      * Generates the output from JSPEngine.
62      */

63     public void generate() throws IOException JavaDoc, ProcessingException {
64         if (this.source == null) {
65             throw new ProcessingException("JSPReader: source JSP is not specified");
66         }
67
68         HttpServletResponse JavaDoc servletResponse =
69             (HttpServletResponse JavaDoc) super.objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT);
70         HttpServletRequest JavaDoc servletRequest =
71             (HttpServletRequest JavaDoc) super.objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT);
72         ServletContext JavaDoc servletContext =
73             (ServletContext JavaDoc) super.objectModel.get(HttpEnvironment.HTTP_SERVLET_CONTEXT);
74
75         // ensure that we are running in a servlet environment
76
if (servletResponse == null || servletRequest == null || servletContext == null) {
77             throw new ProcessingException("JSPReader can only be used from within a Servlet environment.");
78         }
79
80         JSPEngine engine = null;
81         Source inputSource = null;
82         Source contextSource = null;
83         try {
84             inputSource = this.resolver.resolveURI(this.source);
85             contextSource = this.resolver.resolveURI("context:/");
86
87             String JavaDoc inputSourceURI = inputSource.getURI();
88             String JavaDoc contextSourceURI = contextSource.getURI();
89
90             if (!inputSourceURI.startsWith(contextSourceURI)) {
91                 throw new ProcessingException("You must not reference a file "
92                         + "outside of the servlet context at " + contextSourceURI + ".");
93             }
94
95             String JavaDoc url = inputSourceURI.substring(contextSourceURI.length());
96             if (url.charAt(0) != '/') {
97                 url = "/" + url;
98             }
99
100             if (getLogger().isDebugEnabled()) {
101                 getLogger().debug("JSPReader executing:" + url);
102             }
103
104             engine = (JSPEngine) super.manager.lookup(JSPEngine.ROLE);
105             byte[] bytes = engine.executeJSP(url, servletRequest, servletResponse, servletContext);
106
107             if (this.outputEncoding != null) {
108                 recodeResult (bytes, this.outputEncoding);
109             } else {
110                 out.write(bytes);
111                 out.flush();
112             }
113
114             bytes = null;
115         } catch (ServletException JavaDoc e) {
116             throw new ProcessingException("ServletException while executing JSPEngine", e);
117         } catch (IOException JavaDoc e) {
118             throw new ProcessingException("IOException JSPReader.generate()", e);
119         } catch (ProcessingException e) {
120             throw e;
121         } catch (Exception JavaDoc e) {
122             throw new ProcessingException("Exception JSPReader.generate()", e);
123         } finally {
124             super.manager.release(engine);
125             this.resolver.release(inputSource);
126             this.resolver.release(contextSource);
127         }
128     }
129
130     private void recodeResult(byte[] bytes, String JavaDoc encoding) throws IOException JavaDoc {
131         char[] buffer = new char[this.bufferSize];
132
133         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(bytes);
134         // UTF-8 is the default encoding/contract of the JSPEngine
135
Reader reader = new InputStreamReader JavaDoc(bais, "UTF-8");
136         Writer JavaDoc writer = new OutputStreamWriter JavaDoc(out, encoding);
137
138         int length = -1;
139         while ((length = reader.read(buffer)) > -1) {
140             writer.write(buffer, 0, length);
141         }
142         writer.flush();
143     }
144 }
145
Popular Tags