* Generates the output from JSP page.
*/
public void generate() throws IOException, ProcessingException {
// ensure that we are running in a servlet environment
if (this.source == null) {
throw new ProcessingException("JSPReader: source JSP is not specified");
}
HttpServletResponse httpResponse =
(HttpServletResponse)this.objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT);
HttpServletRequest httpRequest =
(HttpServletRequest)this.objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT);
ServletContext httpContext =
(ServletContext)this.objectModel.get(HttpEnvironment.HTTP_SERVLET_CONTEXT);
if (httpResponse == null || httpRequest == null || httpContext == null) {
throw new ProcessingException("JSPReader can be used only in a Servlet/JSP environment");
}
JSPEngine engine = null;
try {
// FIXME (KP): Should we exclude not supported protocols, say 'context'?
String url = this.source;
// absolute path is processed as is
if (!url.startsWith("/")) {
// get current request path
String servletPath = httpRequest.getServletPath();
// remove file part
servletPath = servletPath.substring(0, servletPath.lastIndexOf('/') + 1);
url = servletPath + url;
}
engine = (JSPEngine)this.manager.lookup(JSPEngine.ROLE);
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("JSPReader executing JSP:" + url);
}
byte[] bytes = engine.executeJSP(url, httpRequest, httpResponse, httpContext);
// FIXME (KP): Make buffer size configurable
byte[] buffer = new byte[8192];
int length = -1;
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
while ((length = bais.read(buffer)) > -1) {
out.write(buffer, 0, length);
}
bais.close();
bais = null;
out.flush();
//
} catch (ServletException e) {
throw new ProcessingException("ServletException in JSPReader.generate()",e.getRootCause());
} catch (IOException e) {
throw new ProcessingException("IOException JSPReader.generate()",e);
} catch (ProcessingException e) {
throw e;
} catch (Exception e) {
throw new ProcessingException("Exception JSPReader.generate()",e);
} finally {
if (engine != null) this.manager.release(engine);
}
}