diff --git a/sitemesh/src/main/java/org/sitemesh/webapp/contentfilter/ContentBufferingFilter.java b/sitemesh/src/main/java/org/sitemesh/webapp/contentfilter/ContentBufferingFilter.java index 2192538..de7f9b7 100644 --- a/sitemesh/src/main/java/org/sitemesh/webapp/contentfilter/ContentBufferingFilter.java +++ b/sitemesh/src/main/java/org/sitemesh/webapp/contentfilter/ContentBufferingFilter.java @@ -65,8 +65,6 @@ */ public abstract class ContentBufferingFilter implements Filter { - public static final String SITEMESH_BUFFER_ATTRIBUTE = "sitemesh.buffer" ; - public static final String SITEMESH_RESPONSE_ATTRIBUTE = "sitemesh.response" ; public static final String SITEMESH_DECORATED_ATTRIBUTE = "sitemesh.decorated" ; private final Selector selector; @@ -169,8 +167,7 @@ protected void bufferAndPostProcess(FilterChain filterChain, final HttpServletRe // Apply next filter/servlet, writing response to buffer. final ResponseMetaData metaData = new ResponseMetaData(); - boolean bufferingEnabled = request.getAttribute(SITEMESH_BUFFER_ATTRIBUTE) != null; - final HttpServletResponseBuffer responseBuffer = bufferingEnabled? (HttpServletResponseBuffer) request.getAttribute("sitemesh.buffer") : new HttpServletResponseBuffer(response, metaData, selector) { + final HttpServletResponseBuffer responseBuffer = new HttpServletResponseBuffer(response, metaData, selector) { @Override public void preCommit() { // Ensure both content and decorators are used to generate HTTP caching headers. @@ -186,26 +183,23 @@ public void preCommit() { } } }; - if (!bufferingEnabled) { - request.setAttribute(SITEMESH_BUFFER_ATTRIBUTE, responseBuffer); - request.setAttribute(SITEMESH_RESPONSE_ATTRIBUTE, response); - } filterChain.doFilter(wrapRequest(request), responseBuffer); - request.setAttribute(SITEMESH_BUFFER_ATTRIBUTE, null); - - if (request.getAttribute(SITEMESH_DECORATED_ATTRIBUTE) != null || responseBuffer.getBuffer() == null) { + if (responseBuffer.getBuffer() == null) { + return; + } + if (request.getAttribute(SITEMESH_DECORATED_ATTRIBUTE) != null) { + writeOriginal(response, responseBuffer.getBuffer(), responseBuffer); return; } request.setAttribute(SITEMESH_DECORATED_ATTRIBUTE, true); - HttpServletResponse originalResponse = (HttpServletResponse) request.getAttribute(SITEMESH_RESPONSE_ATTRIBUTE); if (request.isAsyncSupported() && request.isAsyncStarted()) { request.getAsyncContext().addListener(new AsyncListener() { @Override public void onComplete(AsyncEvent asyncEvent) throws IOException { try { - processInternally(responseBuffer, request, originalResponse, metaData); + processInternally(responseBuffer, request, response, metaData); } catch (ServletException e) { throw new RuntimeException("Could not execute request.", e); @@ -228,7 +222,7 @@ public void onStartAsync(AsyncEvent asyncEvent) throws IOException { } }); } else { - processInternally(responseBuffer, request, originalResponse, metaData); + processInternally(responseBuffer, request, response, metaData); } } diff --git a/spring-boot-starter-sitemesh/src/main/java/org/sitemesh/autoconfigure/SiteMeshAutoConfiguration.java b/spring-boot-starter-sitemesh/src/main/java/org/sitemesh/autoconfigure/SiteMeshAutoConfiguration.java index 54e0fbe..b66a33e 100644 --- a/spring-boot-starter-sitemesh/src/main/java/org/sitemesh/autoconfigure/SiteMeshAutoConfiguration.java +++ b/spring-boot-starter-sitemesh/src/main/java/org/sitemesh/autoconfigure/SiteMeshAutoConfiguration.java @@ -24,7 +24,7 @@ import org.sitemesh.config.RequestAttributeDecoratorSelector; import org.sitemesh.content.tagrules.TagRuleBundle; import org.sitemesh.webapp.WebAppContext; -import org.sitemesh.webapp.contentfilter.BasicSelector; +import org.sitemesh.webapp.contentfilter.Selector; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -90,15 +90,31 @@ protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) { for (String bundle : bundles) { builder.addTagRuleBundle((TagRuleBundle) getObjectFactory().create(bundle)); } + builder.setIncludeErrorPages(includeErrorPages); if (alwaysApply) { - builder.setCustomSelector(new BasicSelector() { + Selector basicSelector = builder.getSelector(); + builder.setCustomSelector(new Selector() { + @Override + public boolean shouldBufferForContentType(String contentType, String mimeType, String encoding) { + return basicSelector.shouldBufferForContentType(contentType, mimeType, encoding); + } + + @Override + public boolean shouldAbortBufferingForHttpStatusCode(int statusCode) { + return basicSelector.shouldAbortBufferingForHttpStatusCode(statusCode); + } + @Override public boolean shouldBufferForRequest(HttpServletRequest request) { return true; } + + @Override + public String excludePatternInUse(HttpServletRequest request) { + return basicSelector.excludePatternInUse(request); + } }); } - builder.setIncludeErrorPages(includeErrorPages); } }; } @@ -123,7 +139,7 @@ public FilterRegistrationBean sitemesh3() { public FilterRegistrationBean sitemesh3Secured(ServletContext servletContext) throws ServletException { FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); - ConfigurableSiteMeshFilter filter = makeFilter(attribute, defaultPath, metaTagName, prefix, mappings, exclusions, bundles, false, true); + ConfigurableSiteMeshFilter filter = makeFilter(attribute, defaultPath, metaTagName, prefix, mappings, exclusions, bundles, true, true); Map initParams = new HashMap<>(); filter.init(new FilterConfig() { @Override public String getFilterName() { return "sitemesh3Secured"; }