Skip to content

Commit

Permalink
Simplify chained filter configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
codeconsole committed Oct 11, 2024
1 parent b9a5f1f commit 7c6eef9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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);
Expand All @@ -228,7 +222,7 @@ public void onStartAsync(AsyncEvent asyncEvent) throws IOException {
}
});
} else {
processInternally(responseBuffer, request, originalResponse, metaData);
processInternally(responseBuffer, request, response, metaData);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
};
}
Expand All @@ -123,7 +139,7 @@ public FilterRegistrationBean<ConfigurableSiteMeshFilter> sitemesh3() {
public FilterRegistrationBean<ConfigurableSiteMeshFilter> sitemesh3Secured(ServletContext servletContext) throws ServletException {
FilterRegistrationBean<ConfigurableSiteMeshFilter> 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<String, String> initParams = new HashMap<>();
filter.init(new FilterConfig() {
@Override public String getFilterName() { return "sitemesh3Secured"; }
Expand Down

0 comments on commit 7c6eef9

Please sign in to comment.