ValueStack stack = ActionContext.getContext().getValueStack();
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
JspFactory jspFactory = null;
ServletContext servletContext = ServletActionContext
.getServletContext();
Servlet servlet = JspSupportServlet.jspSupportServlet;
velocityManager.init(servletContext);
boolean usedJspFactory = false;
PageContext pageContext = (PageContext) ActionContext.getContext().get(
ServletActionContext.PAGE_CONTEXT);
if (pageContext == null && servlet != null)
{
jspFactory = JspFactory.getDefaultFactory();
pageContext = jspFactory.getPageContext(servlet, request, response,
null, true, 8192, true);
ActionContext.getContext().put(ServletActionContext.PAGE_CONTEXT,
pageContext);
usedJspFactory = true;
}
try
{
// ------------- 1. render the screen template ------------
String encoding = getEncoding(finalLocation);
String contentType = getContentType(finalLocation);
if (encoding != null)
{
contentType = contentType + ";charset=" + encoding;
}
Template t = getTemplate(stack,
velocityManager.getVelocityEngine(), invocation,
finalLocation, encoding);
Context context = createContext(velocityManager, stack, request,
response, finalLocation);
Writer screenWriter = new StringWriter();
response.setContentType(contentType);
t.merge(context, screenWriter);
context.put(KEY_SCREEN_CONTENT, screenWriter.toString());
// ------------- 2. render the layout template -------------
initLayoutTemplateParameters();
String layout = getLayoutTemplate(context);
try
{
// load the layout template
t = getTemplate(stack, velocityManager.getVelocityEngine(),
invocation, layout, encoding);
}
catch (Exception e)
{
// if it was an alternate layout we couldn't get...
if (!layout.equals(defaultLayout))
{
// try to get the default layout
// if this also fails, let the exception go
t = getTemplate(stack, velocityManager.getVelocityEngine(),
invocation, defaultLayout, encoding);
}
}
Writer writer = new OutputStreamWriter(response.getOutputStream(),
encoding);
// Render the layout template into the response
t.merge(context, writer);
// generator exec template time
Date cur_time = Calendar.getInstance(
invocation.getInvocationContext().getLocale()).getTime();
writer.write("\r\n<!-- Generated by EATELE.NET (");
writer.write(cur_time + "");
writer.write(") ");
writer.write((cur_time.getTime() - runtime) + "");
writer.write("ms -->");
// always flush the writer (we used to only flush it if this was a
// jspWriter, but someone asked
// to do it all the time (WW-829). Since Velocity support is being
// deprecated, we'll oblige :)
writer.flush();
}
catch (Exception e)
{
LOG.error("Unable to render Velocity Template, '" + finalLocation
+ "'", e);
throw e;
}
finally
{
if (usedJspFactory)
{
jspFactory.releasePageContext(pageContext);
}
}
return;
}