Extract ApplicationContextHeaderFilter

Extract ApplicationContextHeaderFilter to a top-level class from
EndpointWebMvcAutoConfiguration and make the header field public.

Fixes gh-5726
Closes gh-5784
This commit is contained in:
Venil Noronha
2016-04-25 07:59:26 +05:30
committed by Phillip Webb
parent 78639fffd0
commit 0d47abbac4
2 changed files with 60 additions and 29 deletions

View File

@@ -16,13 +16,7 @@
package org.springframework.boot.actuate.autoconfigure;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -60,6 +54,7 @@ import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.boot.context.web.ApplicationContextHeaderFilter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
@@ -77,7 +72,6 @@ import org.springframework.core.env.PropertySource;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.core.type.MethodMetadata;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.servlet.DispatcherServlet;
/**
@@ -93,6 +87,7 @@ import org.springframework.web.servlet.DispatcherServlet;
* @author Andy Wilkinson
* @author Johannes Edmeier
* @author Eddú Meléndez
* @author Venil Noronha
*/
@Configuration
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class })
@@ -239,28 +234,6 @@ public class EndpointWebMvcAutoConfiguration
protected static class EndpointWebMvcConfiguration {
}
/**
* {@link OncePerRequestFilter} to add the {@literal X-Application-Context} if
* required.
*/
private static class ApplicationContextHeaderFilter extends OncePerRequestFilter {
private final ApplicationContext applicationContext;
ApplicationContextHeaderFilter(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
response.addHeader("X-Application-Context", this.applicationContext.getId());
filterChain.doFilter(request, response);
}
}
/**
* {@link ApplicationListener} to propagate the {@link ContextClosedEvent} from a
* parent to a child.