Skip to content

Commit

Permalink
Updated to Velocity Tools 1.1 RC1 which uses getWriter() by default -…
Browse files Browse the repository at this point in the history
…> less code in SiteMesh and less memory usage.
  • Loading branch information
mbogaert committed Apr 12, 2004
1 parent 9f5c1ed commit fcd7758
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 94 deletions.
Binary file modified lib/commons-collections.jar
Binary file not shown.
Binary file removed lib/velocity-tools-view-1.0.jar
Binary file not shown.
Binary file added lib/velocity-tools-view-1.1-rc1.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,22 @@

import com.opensymphony.module.sitemesh.*;
import com.opensymphony.module.sitemesh.util.OutputConverter;

import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.io.VelocityWriter;
import org.apache.velocity.tools.view.servlet.VelocityViewServlet;
import org.apache.velocity.util.SimplePool;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.io.StringWriter;

/**
* Servlet that allows Velocity templates to be used as decorators.
*
* @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
* @version $Revision: 1.4 $
* @version $Revision: 1.5 $
*/
public class VelocityDecoratorServlet extends VelocityViewServlet {
/** Cache of writers. */
private static SimplePool writerPool = new SimplePool(40);

public Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context context) throws Exception {
HTMLPage htmlPage = (HTMLPage) request.getAttribute(RequestConstants.PAGE);
String template;
Expand Down Expand Up @@ -66,87 +58,4 @@ public Template handleRequest(HttpServletRequest request, HttpServletResponse re

return getTemplate(template);
}

/**
* Merges the template with the context.
*
* <p>This method has been overridden because the one
* from {@link VelocityViewServlet} uses response.getOutputStream() instead of
* response.getWriter().</p>
*
* @param template template object returned by the handleRequest() method
* @param context context created by the createContext() method
* @param response servlet reponse (use this to get the output stream or Writer
*/
protected void mergeTemplate(Template template, Context context, HttpServletResponse response) throws ResourceNotFoundException, ParseErrorException,
MethodInvocationException, IOException,
UnsupportedEncodingException, Exception {

Writer responseWriter = response.getWriter();
VelocityWriter vw = null;

try {
vw = (VelocityWriter) writerPool.get();

if (vw == null) {
vw = new VelocityWriter(responseWriter, 4 * 1024, true);
}
else {
vw.recycle(responseWriter);
}

template.merge(context, vw);
}
finally {
try {
if (vw != null) {
// flush and put back into the pool
// don't close to allow us to play
// nicely with others.
vw.flush();
writerPool.put(vw);
}
}
catch (Exception e) {
// do nothing
}
}
}

/**
* Invoked when there is an error thrown in any part of doRequest() processing.
* <br><br>
* Default will send a simple HTML response indicating there was a problem.
*
* <p>This method has been overridden because the one
* from {@link VelocityViewServlet} uses response.getOutputStream() instead of
* response.getWriter().</p>
*
* @param request original HttpServletRequest from servlet container.
* @param response HttpServletResponse object from servlet container.
* @param cause Exception that was thrown by some other part of process.
*/
protected void error(HttpServletRequest request, HttpServletResponse response, Exception cause)
throws ServletException, IOException {
StringBuffer html = new StringBuffer();
html.append("<html>");
html.append("<title>Error</title>");
html.append("<body bgcolor=\"#ffffff\">");
html.append("<h2>VelocityDecoratorServlet : Error processing the template</h2>");
html.append("<pre>");
String why = cause.getMessage();
if (why != null && why.trim().length() > 0) {
html.append(why);
html.append("<br>");
}

StringWriter sw = new StringWriter();
cause.printStackTrace(new PrintWriter(sw));

html.append(sw.toString());
html.append("</pre>");
html.append("</body>");
html.append("</html>");
response.getWriter().print(html.toString());
}
}

0 comments on commit fcd7758

Please sign in to comment.