1 16 package org.apache.cocoon.generation; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.IOException ; 20 21 import javax.servlet.ServletContext ; 22 import javax.servlet.ServletException ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 26 import org.apache.cocoon.ProcessingException; 27 import org.apache.cocoon.components.jsp.JSPEngine; 28 import org.apache.cocoon.environment.http.HttpEnvironment; 29 import org.apache.excalibur.source.Source; 30 import org.apache.excalibur.xml.sax.SAXParser; 31 import org.xml.sax.InputSource ; 32 import org.xml.sax.SAXException ; 33 34 40 public class JSPGenerator extends ServiceableGenerator { 41 42 45 public void generate() throws ProcessingException { 46 47 final HttpServletResponse servletResponse = 48 (HttpServletResponse ) this.objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT); 49 final HttpServletRequest servletRequest = 50 (HttpServletRequest ) this.objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT); 51 final ServletContext servletContext = 52 (ServletContext ) this.objectModel.get(HttpEnvironment.HTTP_SERVLET_CONTEXT); 53 54 if (servletResponse == null || servletRequest == null || servletContext == null) { 56 throw new ProcessingException("JSPGenerator can only be used from within a Servlet environment."); 57 } 58 59 JSPEngine engine = null; 60 SAXParser parser = null; 61 Source inputSource = null; 62 Source contextSource = null; 63 try { 64 inputSource = this.resolver.resolveURI(this.source); 65 contextSource = this.resolver.resolveURI("context:/"); 66 67 String inputSourceURI = inputSource.getURI(); 68 String contextSourceURI = contextSource.getURI(); 69 70 if (!inputSourceURI.startsWith(contextSourceURI)) { 71 throw new ProcessingException("You must not reference a file " 72 + "outside of the servlet context at " + contextSourceURI + "."); 73 } 74 75 String url = inputSourceURI.substring(contextSourceURI.length()); 76 if (url.charAt(0) != '/') { 77 url = "/" + url; 78 } 79 80 if (getLogger().isDebugEnabled()) { 81 getLogger().debug("JSPGenerator executing:" + url); 82 } 83 84 engine = (JSPEngine) super.manager.lookup(JSPEngine.ROLE); 85 byte[] bytes = engine.executeJSP(url, servletRequest, servletResponse, servletContext); 86 87 InputSource input = new InputSource (new ByteArrayInputStream (bytes)); 88 input.setEncoding("utf-8"); 90 91 parser = (SAXParser) super.manager.lookup(SAXParser.ROLE); 93 parser.parse(input, super.xmlConsumer); 94 } catch (ServletException e) { 95 throw new ProcessingException("ServletException while executing JSPEngine", e); 96 } catch (SAXException e) { 97 throw new ProcessingException("SAXException while parsing JSPEngine output", e); 98 } catch (IOException e) { 99 throw new ProcessingException("IOException JSPGenerator.generate()", e); 100 } catch (ProcessingException e) { 101 throw e; 102 } catch (Exception e) { 103 throw new ProcessingException("Exception JSPGenerator.generate()", e); 104 } finally { 105 super.manager.release(parser); 106 super.manager.release(engine); 107 this.resolver.release(inputSource); 108 this.resolver.release(contextSource); 109 } 110 } 111 } 112 | Popular Tags |