Author: nbubna
Date: Mon Jul 6 16:06:52 2009
New Revision: 791530
URL: http://svn.apache.org/viewvc?rev=791530&view=rev
Log:
VELTOOLS-115 improve exception and http management (particularly when for
ResourceNotFoundExceptions), thanks to Antonio Petrelli
Modified:
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityView.java
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java
Modified:
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityView.java
URL:
http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityView.java?rev=791530&r1=791529&r2=791530&view=diff
==============================================================================
---
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityView.java
(original)
+++
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityView.java
Mon Jul 6 16:06:52 2009
@@ -865,7 +865,11 @@
return velocity.getTemplate(name, encoding);
}
}
- catch (Exception e)
+ catch (RuntimeException e) // FIXME This is useless with Velocity 1.7
+ {
+ throw e;
+ }
+ catch (Exception e) // FIXME This is useless with Velocity 1.7
{
throw new RuntimeException(e);
}
@@ -896,6 +900,9 @@
vw.recycle(writer);
}
performMerge(template, context, vw);
+
+ // flush writer but don't close to allow us to play nicely with
others.
+ vw.flush();
}
finally
{
@@ -903,10 +910,6 @@
{
try
{
- // flush and put back into the pool
- // don't close to allow us to play
- // nicely with others.
- vw.flush();
/* This hack sets the VelocityWriter's internal ref to the
* PrintWriter to null to keep memory free while
* the writer is pooled. See bug report #18951 */
Modified:
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java
URL:
http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java?rev=791530&r1=791529&r2=791530&view=diff
==============================================================================
---
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java
(original)
+++
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java
Mon Jul 6 16:06:52 2009
@@ -19,9 +19,9 @@
* under the License.
*/
+import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
-import java.io.IOException;
import java.io.Writer;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
@@ -33,6 +33,7 @@
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.exception.MethodInvocationException;
+import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.log.Log;
/**
@@ -199,6 +200,7 @@
* @param response HttpServletResponse object for the response
*/
protected void doRequest(HttpServletRequest request, HttpServletResponse
response)
+ throws IOException
{
Context context = null;
try
@@ -216,10 +218,18 @@
// merge the template and context into the response
mergeTemplate(template, context, response);
+ } catch (IOException e) {
+ error(request, response, e);
+ throw e;
+ }
+ catch (ResourceNotFoundException e)
+ {
+ manageResourceNotFound(request, response, e);
}
- catch (Throwable e)
+ catch (RuntimeException e)
{
error(request, response, e);
+ throw e;
}
finally
{
@@ -228,7 +238,6 @@
}
-
/**
* <p>This was a common extension point, but now it is usually
* simpler to override {...@link #fillContext} to add custom things
@@ -243,7 +252,7 @@
*/
protected Template handleRequest(HttpServletRequest request,
HttpServletResponse response,
- Context ctx) throws Exception
+ Context ctx)
{
return getTemplate(request, response);
}
@@ -328,14 +337,21 @@
HttpServletResponse response,
Throwable e)
{
+ if (!response.isCommitted())
+ {
+ return;
+ }
+
try
{
+ String path = ServletUtils.getPath(request);
+ getLog().error("Error processing a template for path '" + path +
"'", e);
StringBuilder html = new StringBuilder();
html.append("<html>\n");
html.append("<head><title>Error</title></head>\n");
html.append("<body>\n");
html.append("<h2>VelocityView : Error processing a template for
path '");
- html.append(ServletUtils.getPath(request));
+ html.append(path);
html.append("'</h2>\n");
Throwable cause = e;
@@ -377,6 +393,36 @@
}
}
+ /**
+ * Manages the {...@link ResourceNotFoundException} to send an HTTP 404
result
+ * when needed.
+ *
+ * @param request The request object.
+ * @param response The response object.
+ * @param e The exception to check.
+ * @throws IOException If something goes wrong when sending the HTTP error.
+ */
+ protected void manageResourceNotFound(HttpServletRequest request,
+ HttpServletResponse response, ResourceNotFoundException e)
+ throws IOException
+ {
+ String path = ServletUtils.getPath(request);
+ if (getLog().isDebugEnabled())
+ {
+ getLog().debug("Resource not found for path '" + path + "'", e);
+ }
+ String message = e.getMessage();
+ if (!response.isCommitted() && path != null &&
+ message != null && message.contains("'" + path + "'"))
+ {
+ response.sendError(HttpServletResponse.SC_NOT_FOUND, path);
+ }
+ else
+ {
+ error(request, response, e);
+ throw e;
+ }
+ }
/**
* Cleanup routine called at the end of the request processing sequence