Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Managed to get the VelocityDecoratorServlet (now extends from Velocit…

…yViewServlet from Velocity Tools, the old VelocityServlet was deprecated) working. The example deploys successfully on Tomcat 4.1.29 with all examples working. Will test on other containers too.
  • Loading branch information...
commit eeca4e533138b2b1ba946adca1cc5c7ed800372f 1 parent f846969
mbogaert authored
View
7 build.xml
@@ -76,8 +76,10 @@
<target name="blank" depends="jar" description="Builds simple web-app to get started with SiteMesh">
<mkdir dir="${dist.dir}" />
- <war warfile="${dist.dir}/${name}-blank.war" webxml="${example.dir}/WEB-INF/web.xml">
- <fileset dir="${blank.dir}"/>
+ <war warfile="${dist.dir}/${name}-blank.war" webxml="${blank.dir}/WEB-INF/web.xml">
+ <fileset dir="${blank.dir}">
+ <exclude name="WEB-INF/web.xml" />
+ </fileset>
<lib dir="${dist.dir}">
<include name="${name}-${version}.jar" />
</lib>
@@ -108,6 +110,7 @@
<include name="${name}-${version}.jar" />
</lib>
<lib dir="${lib.dir}">
+ <include name="commons-*.jar" />
<include name="velocity-*.jar" />
</lib>
</war>
View
BIN  lib/commons-collections.jar
Binary file not shown
View
BIN  lib/velocity-1.4-rc1.jar
Binary file not shown
View
BIN  lib/velocity-dep-1.3.1.jar
Binary file not shown
View
BIN  lib/velocity-tools-view-1.0.jar
Binary file not shown
View
2  src/etc/blank/WEB-INF/decorators.xml
@@ -1,3 +1,5 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+
<decorators defaultdir="/decorators">
<decorator name="main" page="main.jsp">
<pattern>/*</pattern>
View
16 src/etc/blank/WEB-INF/web.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
+
+<web-app>
+
+ <filter>
+ <filter-name>sitemesh</filter-name>
+ <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
+ </filter>
+
+ <filter-mapping>
+ <filter-name>sitemesh</filter-name>
+ <url-pattern>/*</url-pattern>
+ </filter-mapping>
+
+</web-app>
View
2  src/example-webapp/WEB-INF/decorators.xml
@@ -1,3 +1,5 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+
<decorators defaultdir="/decorators">
<decorator name="main" page="main.jsp">
<pattern>/*</pattern>
View
11 src/example-webapp/WEB-INF/web.xml
@@ -13,4 +13,15 @@
<url-pattern>/*</url-pattern>
</filter-mapping>
+ <servlet>
+ <servlet-name>sitemesh-velocity</servlet-name>
+ <servlet-class>com.opensymphony.module.sitemesh.velocity.VelocityDecoratorServlet</servlet-class>
+ <load-on-startup>10</load-on-startup>
+ </servlet>
+
+ <servlet-mapping>
+ <servlet-name>sitemesh-velocity</servlet-name>
+ <url-pattern>*.vm</url-pattern>
+ </servlet-mapping>
+
</web-app>
View
2  src/example-webapp/velocity.html
@@ -6,5 +6,7 @@
<p>This page is decorated by the VelocityDecoratorServlet.</p>
+ <p>Go <a href="index.html">back</a>.</p>
+
</body>
</html>
View
4 src/java/com/opensymphony/module/sitemesh/Factory.java
@@ -24,7 +24,7 @@
* If this doesn't exist, it defaults to {@link com.opensymphony.module.sitemesh.factory.DefaultFactory} .</p>
*
* @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
public abstract class Factory {
/** @label Web context lookup key */
@@ -36,7 +36,7 @@
* entry <code>sitemesh.factory</code>.
*/
public static Factory getInstance(Config config) {
- Factory instance = (Factory)config.getServletContext().getAttribute(SITEMESH_FACTORY);
+ Factory instance = (Factory)config.getServletContext().getAttribute(SITEMESH_FACTORY);
if (instance == null) {
String factoryClass = getEnvEntry("sitemesh.factory", "com.opensymphony.module.sitemesh.factory.DefaultFactory");
try {
View
129 src/java/com/opensymphony/module/sitemesh/velocity/VelocityDecoratorServlet.java
@@ -9,48 +9,31 @@
package com.opensymphony.module.sitemesh.velocity;
-import java.util.Properties;
-import java.io.IOException;
-import java.io.FileNotFoundException;
-import java.io.FileInputStream;
-import java.io.StringWriter;
-
+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.servlet.VelocityServlet;
+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.ServletConfig;
+import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-
-import com.opensymphony.module.sitemesh.HTMLPage;
-import com.opensymphony.module.sitemesh.RequestConstants;
-import com.opensymphony.module.sitemesh.Factory;
-import com.opensymphony.module.sitemesh.Config;
-import com.opensymphony.module.sitemesh.Decorator;
-import com.opensymphony.module.sitemesh.util.OutputConverter;
+import java.io.*;
/**
* Servlet that allows Velocity templates to be used as decorators.
*
* @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
- * @version $Revision: 1.3 $
+ * @version $Revision: 1.4 $
*/
-public class VelocityDecoratorServlet extends VelocityServlet {
- protected Properties loadConfiguration(ServletConfig config) throws IOException, FileNotFoundException {
- String propsFile = config.getInitParameter(INIT_PROPS_KEY);
- propsFile = getServletContext().getRealPath(propsFile);
- Properties p = new Properties();
- p.load(new FileInputStream(propsFile));
- {
- String path = p.getProperty("file.resource.loader.path");
- p.setProperty("file.resource.loader.path", getServletContext().getRealPath(path));
- path = p.getProperty("runtime.log");
- p.setProperty("runtime.log", getServletContext().getRealPath(path));
- }
- return p;
- }
+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);
@@ -80,12 +63,90 @@ public Template handleRequest(HttpServletRequest request, HttpServletResponse re
Decorator decorator = factory.getDecoratorMapper().getDecorator(request, htmlPage);
template = decorator.getPage();
}
+
+ 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 {
- return getTemplate(template);
+ vw = (VelocityWriter) writerPool.get();
+
+ if (vw == null) {
+ vw = new VelocityWriter(responseWriter, 4 * 1024, true);
+ }
+ else {
+ vw.recycle(responseWriter);
+ }
+
+ template.merge(context, vw);
}
- catch (ResourceNotFoundException e) {
- response.sendError(HttpServletResponse.SC_NOT_FOUND, "Decorator template " + template + " not found.");
- return null;
+ 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());
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.