Skip to content

Commit

Permalink
Simplify auto configuration. There is no need to have separate filter…
Browse files Browse the repository at this point in the history
… since it can be accessed via the getFilter() method of the bean registration.
  • Loading branch information
codeconsole committed Oct 8, 2024
1 parent e44984f commit ba167e6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ public void preCommit() {
}

filterChain.doFilter(wrapRequest(request), responseBuffer);
request.setAttribute(SITEMESH_BUFFER_ATTRIBUTE, null);

if (request.getAttribute(SITEMESH_DECORATED_ATTRIBUTE) != null) {
if (request.getAttribute(SITEMESH_DECORATED_ATTRIBUTE) != null || responseBuffer.getBuffer() == null) {
return;
}
request.setAttribute(SITEMESH_DECORATED_ATTRIBUTE, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.sitemesh.autoconfigure;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import org.sitemesh.builder.SiteMeshFilterBuilder;
import org.sitemesh.config.ConfigurableSiteMeshFilter;
Expand All @@ -33,11 +34,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import jakarta.servlet.DispatcherType;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

@Configuration
@ConfigurationProperties(prefix = "sitemesh.decorator")
Expand Down Expand Up @@ -106,18 +103,12 @@ public boolean shouldBufferForRequest(HttpServletRequest request) {
};
}

@Bean
@ConditionalOnMissingBean(name = "siteMeshFilter")
ConfigurableSiteMeshFilter siteMeshFilter() {
return makeFilter(attribute, defaultPath, metaTagName, prefix, mappings, exclusions, bundles, includeErrorPages, false);
}

@Bean
@ConditionalOnMissingBean(name = "sitemesh3")
public FilterRegistrationBean<ConfigurableSiteMeshFilter> sitemesh3(ConfigurableSiteMeshFilter siteMeshFilter) {
public FilterRegistrationBean<ConfigurableSiteMeshFilter> sitemesh3() {
FilterRegistrationBean<ConfigurableSiteMeshFilter> registrationBean
= new FilterRegistrationBean<>();
registrationBean.setFilter(siteMeshFilter);
registrationBean.setFilter(makeFilter(attribute, defaultPath, metaTagName, prefix, mappings, exclusions, bundles, includeErrorPages, false));
registrationBean.addUrlPatterns("/*");
if (includeErrorPages) {
registrationBean.setDispatcherTypes(EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR));
Expand All @@ -129,10 +120,18 @@ public FilterRegistrationBean<ConfigurableSiteMeshFilter> sitemesh3(Configurable
@Bean
@ConditionalOnMissingBean(name = "sitemesh3Secured")
@ConditionalOnClass(name = "org.springframework.security.config.annotation.web.configuration.EnableWebSecurity")
public FilterRegistrationBean<ConfigurableSiteMeshFilter> sitemesh3Secured() {
public FilterRegistrationBean<ConfigurableSiteMeshFilter> sitemesh3Secured(ServletContext servletContext) throws ServletException {
FilterRegistrationBean<ConfigurableSiteMeshFilter> registrationBean
= new FilterRegistrationBean<>();
registrationBean.setFilter(makeFilter(attribute, defaultPath, metaTagName, prefix, mappings, exclusions, bundles, includeErrorPages, true));
ConfigurableSiteMeshFilter filter = makeFilter(attribute, defaultPath, metaTagName, prefix, mappings, exclusions, bundles, false, true);
Map<String, String> initParams = new HashMap<>();
filter.init(new FilterConfig() {
@Override public String getFilterName() { return "sitemesh3Secured"; }
@Override public ServletContext getServletContext() { return servletContext; }
@Override public String getInitParameter(String name) { return initParams.get(name); }
@Override public Enumeration<String> getInitParameterNames() { return Collections.enumeration(initParams.keySet()); }
});
registrationBean.setFilter(filter);
registrationBean.setEnabled(false);
return registrationBean;
}
Expand Down

0 comments on commit ba167e6

Please sign in to comment.