1 16 package org.apache.cocoon.reading; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.IOException ; 20 import java.io.InputStreamReader ; 21 import java.io.OutputStreamWriter ; 22 import java.io.Reader ; 23 import java.io.Writer ; 24 25 import javax.servlet.ServletContext ; 26 import javax.servlet.ServletException ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 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 45 public class JSPReader extends ServiceableReader implements Configurable { 46 47 private static final int DEFAULT_BUFFER_SIZE = 8192; 48 49 private int bufferSize; 51 52 private String 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 63 public void generate() throws IOException , ProcessingException { 64 if (this.source == null) { 65 throw new ProcessingException("JSPReader: source JSP is not specified"); 66 } 67 68 HttpServletResponse servletResponse = 69 (HttpServletResponse ) super.objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT); 70 HttpServletRequest servletRequest = 71 (HttpServletRequest ) super.objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT); 72 ServletContext servletContext = 73 (ServletContext ) super.objectModel.get(HttpEnvironment.HTTP_SERVLET_CONTEXT); 74 75 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 inputSourceURI = inputSource.getURI(); 88 String 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 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 e) { 116 throw new ProcessingException("ServletException while executing JSPEngine", e); 117 } catch (IOException e) { 118 throw new ProcessingException("IOException JSPReader.generate()", e); 119 } catch (ProcessingException e) { 120 throw e; 121 } catch (Exception 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 encoding) throws IOException { 131 char[] buffer = new char[this.bufferSize]; 132 133 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 134 Reader reader = new InputStreamReader (bais, "UTF-8"); 136 Writer writer = new OutputStreamWriter (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 |