use of javax.servlet.Filter in project spring-boot by spring-projects.
the class AbstractFilterRegistrationBean method onStartup.
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
Filter filter = getFilter();
Assert.notNull(filter, "Filter must not be null");
String name = getOrDeduceName(filter);
if (!isEnabled()) {
this.logger.info("Filter " + name + " was not registered (disabled)");
return;
}
FilterRegistration.Dynamic added = servletContext.addFilter(name, filter);
if (added == null) {
this.logger.info("Filter " + name + " was not registered " + "(possibly already registered?)");
return;
}
configure(added);
}
use of javax.servlet.Filter in project hadoop by apache.
the class TestHostnameFilter method testMissingHostname.
@Test
public void testMissingHostname() throws Exception {
ServletRequest request = Mockito.mock(ServletRequest.class);
Mockito.when(request.getRemoteAddr()).thenReturn(null);
ServletResponse response = Mockito.mock(ServletResponse.class);
final AtomicBoolean invoked = new AtomicBoolean();
FilterChain chain = new FilterChain() {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
assertTrue(HostnameFilter.get().contains("???"));
invoked.set(true);
}
};
Filter filter = new HostnameFilter();
filter.init(null);
assertNull(HostnameFilter.get());
filter.doFilter(request, response, chain);
assertTrue(invoked.get());
assertNull(HostnameFilter.get());
filter.destroy();
}
use of javax.servlet.Filter in project hadoop by apache.
the class TestMDCFilter method mdc.
@Test
public void mdc() throws Exception {
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getUserPrincipal()).thenReturn(null);
Mockito.when(request.getMethod()).thenReturn("METHOD");
Mockito.when(request.getPathInfo()).thenReturn("/pathinfo");
ServletResponse response = Mockito.mock(ServletResponse.class);
final AtomicBoolean invoked = new AtomicBoolean();
FilterChain chain = new FilterChain() {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
assertEquals(MDC.get("hostname"), null);
assertEquals(MDC.get("user"), null);
assertEquals(MDC.get("method"), "METHOD");
assertEquals(MDC.get("path"), "/pathinfo");
invoked.set(true);
}
};
MDC.clear();
Filter filter = new MDCFilter();
filter.init(null);
filter.doFilter(request, response, chain);
assertTrue(invoked.get());
assertNull(MDC.get("hostname"));
assertNull(MDC.get("user"));
assertNull(MDC.get("method"));
assertNull(MDC.get("path"));
Mockito.when(request.getUserPrincipal()).thenReturn(new Principal() {
@Override
public String getName() {
return "name";
}
});
invoked.set(false);
chain = new FilterChain() {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
assertEquals(MDC.get("hostname"), null);
assertEquals(MDC.get("user"), "name");
assertEquals(MDC.get("method"), "METHOD");
assertEquals(MDC.get("path"), "/pathinfo");
invoked.set(true);
}
};
filter.doFilter(request, response, chain);
assertTrue(invoked.get());
HostnameFilter.HOSTNAME_TL.set("HOST");
invoked.set(false);
chain = new FilterChain() {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
assertEquals(MDC.get("hostname"), "HOST");
assertEquals(MDC.get("user"), "name");
assertEquals(MDC.get("method"), "METHOD");
assertEquals(MDC.get("path"), "/pathinfo");
invoked.set(true);
}
};
filter.doFilter(request, response, chain);
assertTrue(invoked.get());
HostnameFilter.HOSTNAME_TL.remove();
filter.destroy();
}
use of javax.servlet.Filter in project camel by apache.
the class JettyHttpComponent method enableMultipartFilter.
private void enableMultipartFilter(HttpCommonEndpoint endpoint, Server server, String connectorKey) throws Exception {
ServletContextHandler context = server.getChildHandlerByClass(ServletContextHandler.class);
CamelContext camelContext = this.getCamelContext();
FilterHolder filterHolder = new FilterHolder();
filterHolder.setInitParameter("deleteFiles", "true");
if (ObjectHelper.isNotEmpty(camelContext.getProperty(TMP_DIR))) {
File file = new File(camelContext.getProperty(TMP_DIR));
if (!file.isDirectory()) {
throw new RuntimeCamelException("The temp file directory of camel-jetty is not exists, please recheck it with directory name :" + camelContext.getProperties().get(TMP_DIR));
}
context.setAttribute("javax.servlet.context.tempdir", file);
}
// if a filter ref was provided, use it.
Filter filter = ((JettyHttpEndpoint) endpoint).getMultipartFilter();
if (filter == null) {
// if no filter ref was provided, use the default filter
filter = new MultiPartFilter();
}
filterHolder.setFilter(new CamelFilterWrapper(filter));
String pathSpec = endpoint.getPath();
if (pathSpec == null || "".equals(pathSpec)) {
pathSpec = "/";
}
if (endpoint.isMatchOnUriPrefix()) {
pathSpec = pathSpec.endsWith("/") ? pathSpec + "*" : pathSpec + "/*";
}
addFilter(context, filterHolder, pathSpec);
LOG.debug("using multipart filter implementation " + filter.getClass().getName() + " for path " + pathSpec);
}
use of javax.servlet.Filter in project camel by apache.
the class JettyHttpComponent method setFilters.
private void setFilters(JettyHttpEndpoint endpoint, Server server, String connectorKey) {
ServletContextHandler context = server.getChildHandlerByClass(ServletContextHandler.class);
List<Filter> filters = endpoint.getFilters();
for (Filter filter : filters) {
FilterHolder filterHolder = new FilterHolder();
if (endpoint.getFilterInitParameters() != null) {
filterHolder.setInitParameters(endpoint.getFilterInitParameters());
}
filterHolder.setFilter(new CamelFilterWrapper(filter));
String pathSpec = endpoint.getPath();
if (pathSpec == null || "".equals(pathSpec)) {
pathSpec = "/";
}
if (endpoint.isMatchOnUriPrefix()) {
pathSpec = pathSpec.endsWith("/") ? pathSpec + "*" : pathSpec + "/*";
}
addFilter(context, filterHolder, pathSpec);
}
}
Aggregations