001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.generation;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.IOException;
021:
022: import javax.servlet.ServletContext;
023: import javax.servlet.ServletException;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import org.apache.cocoon.ProcessingException;
028: import org.apache.cocoon.components.jsp.JSPEngine;
029: import org.apache.cocoon.environment.http.HttpEnvironment;
030: import org.apache.excalibur.source.Source;
031: import org.apache.excalibur.xml.sax.SAXParser;
032: import org.xml.sax.InputSource;
033: import org.xml.sax.SAXException;
034:
035: /**
036: * Allows Servlets and JSPs to be used as a generator.
037: *
038: * @author <a href="mailto:dims@yahoo.com">Davanum Srinivas</a>
039: * @version CVS $Id: JSPGenerator.java 433543 2006-08-22 06:22:54Z crossley $
040: */
041: public class JSPGenerator extends ServiceableGenerator {
042:
043: /**
044: * Generate XML data from JSPEngine output.
045: */
046: public void generate() throws ProcessingException {
047:
048: final HttpServletResponse servletResponse = (HttpServletResponse) this .objectModel
049: .get(HttpEnvironment.HTTP_RESPONSE_OBJECT);
050: final HttpServletRequest servletRequest = (HttpServletRequest) this .objectModel
051: .get(HttpEnvironment.HTTP_REQUEST_OBJECT);
052: final ServletContext servletContext = (ServletContext) this .objectModel
053: .get(HttpEnvironment.HTTP_SERVLET_CONTEXT);
054:
055: // ensure that we are running in a servlet environment
056: if (servletResponse == null || servletRequest == null
057: || servletContext == null) {
058: throw new ProcessingException(
059: "JSPGenerator can only be used from within a Servlet environment.");
060: }
061:
062: JSPEngine engine = null;
063: SAXParser parser = null;
064: Source inputSource = null;
065: Source contextSource = null;
066: try {
067: inputSource = this .resolver.resolveURI(this .source);
068: contextSource = this .resolver.resolveURI("context:/");
069:
070: String inputSourceURI = inputSource.getURI();
071: String contextSourceURI = contextSource.getURI();
072:
073: if (!inputSourceURI.startsWith(contextSourceURI)) {
074: throw new ProcessingException(
075: "You must not reference a file "
076: + "outside of the servlet context at "
077: + contextSourceURI + ".");
078: }
079:
080: String url = inputSourceURI.substring(contextSourceURI
081: .length());
082: if (url.charAt(0) != '/') {
083: url = "/" + url;
084: }
085:
086: if (getLogger().isDebugEnabled()) {
087: getLogger().debug("JSPGenerator executing:" + url);
088: }
089:
090: engine = (JSPEngine) super .manager.lookup(JSPEngine.ROLE);
091: byte[] bytes = engine.executeJSP(url, servletRequest,
092: servletResponse, servletContext);
093:
094: InputSource input = new InputSource(
095: new ByteArrayInputStream(bytes));
096: // utf-8 is default encoding; specified explicitely here as a reminder.
097: input.setEncoding("utf-8");
098:
099: // pipe the results into the parser
100: parser = (SAXParser) super .manager.lookup(SAXParser.ROLE);
101: parser.parse(input, super .xmlConsumer);
102: } catch (ServletException e) {
103: throw new ProcessingException(
104: "ServletException while executing JSPEngine", e);
105: } catch (SAXException e) {
106: throw new ProcessingException(
107: "SAXException while parsing JSPEngine output", e);
108: } catch (IOException e) {
109: throw new ProcessingException(
110: "IOException JSPGenerator.generate()", e);
111: } catch (ProcessingException e) {
112: throw e;
113: } catch (Exception e) {
114: throw new ProcessingException(
115: "Exception JSPGenerator.generate()", e);
116: } finally {
117: super.manager.release(parser);
118: super.manager.release(engine);
119: this.resolver.release(inputSource);
120: this.resolver.release(contextSource);
121: }
122: }
123: }
|