From 8e67b9144aa643769b261e2492cb561e04d016ab Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Wed, 17 Aug 2016 15:35:11 +0200 Subject: [PATCH] Adds double check if resource exists --- .../convention/ConventionUnknownHandler.java | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/plugins/convention/src/main/java/org/apache/struts2/convention/ConventionUnknownHandler.java b/plugins/convention/src/main/java/org/apache/struts2/convention/ConventionUnknownHandler.java index ed708588e7..2be1cecdff 100644 --- a/plugins/convention/src/main/java/org/apache/struts2/convention/ConventionUnknownHandler.java +++ b/plugins/convention/src/main/java/org/apache/struts2/convention/ConventionUnknownHandler.java @@ -43,6 +43,7 @@ import javax.servlet.ServletContext; import java.net.MalformedURLException; +import java.net.URL; import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; @@ -332,27 +333,34 @@ protected Result findResult(String path, String resultCode, String ext, ActionCo Map resultsByExtension) { try { boolean traceEnabled = LOG.isTraceEnabled(); - if (traceEnabled) + if (traceEnabled) { LOG.trace("Checking ServletContext for [#0]", path); + } - if (servletContext.getResource(path) != null) { - if (traceEnabled) - LOG.trace("Found"); + URL resource = servletContext.getResource(path); + if (resource != null && resource.getPath().endsWith(path)) { + if (traceEnabled) { + LOG.trace("Found resource #0", resource); + } return buildResult(path, resultCode, resultsByExtension.get(ext), actionContext); } - if (traceEnabled) - LOG.trace("Checking ClasLoader for #0", path); + if (traceEnabled) { + LOG.trace("Checking ClassLoader for #0", path); + } String classLoaderPath = path.startsWith("/") ? path.substring(1, path.length()) : path; - if (ClassLoaderUtil.getResource(classLoaderPath, getClass()) != null) { - if (traceEnabled) - LOG.trace("Found"); + resource = ClassLoaderUtil.getResource(classLoaderPath, getClass()); + if (resource != null && resource.getPath().endsWith(classLoaderPath)) { + if (traceEnabled) { + LOG.trace("Found resource #0", resource); + } return buildResult(path, resultCode, resultsByExtension.get(ext), actionContext); } } catch (MalformedURLException e) { - if (LOG.isErrorEnabled()) + if (LOG.isErrorEnabled()) { LOG.error("Unable to parse template path: [#0] skipping...", path); + } } return null;