use of javax.servlet.Filter in project druid by druid-io.
the class TrustedDomainAuthenticatorTest method testForwardedForTrustedHost.
@Test
public void testForwardedForTrustedHost() throws IOException, ServletException {
Authenticator authenticator = new TrustedDomainAuthenticator("test-authenticator", "test.com", true, "my-auth", "myUser");
HttpServletRequest req = EasyMock.createMock(HttpServletRequest.class);
EasyMock.expect(req.getRemoteAddr()).andReturn("serverA.test2.com");
EasyMock.expect(req.getHeader("X-Forwarded-For")).andReturn("serverA.test.com");
req.setAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT, new AuthenticationResult("myUser", "my-auth", "test-authenticator", null));
EasyMock.expectLastCall().times(1);
EasyMock.replay(req);
HttpServletResponse resp = EasyMock.createMock(HttpServletResponse.class);
EasyMock.replay(resp);
FilterChain filterChain = EasyMock.createMock(FilterChain.class);
filterChain.doFilter(req, resp);
EasyMock.expectLastCall().times(1);
EasyMock.replay(filterChain);
Filter authenticatorFilter = authenticator.getFilter();
authenticatorFilter.doFilter(req, resp, filterChain);
EasyMock.verify(req, resp, filterChain);
}
use of javax.servlet.Filter in project roboguice by roboguice.
the class FilterDefinitionTest method testFilterInitAndConfig.
public final void testFilterInitAndConfig() throws ServletException {
Injector injector = createMock(Injector.class);
Binding binding = createMock(Binding.class);
final MockFilter mockFilter = new MockFilter();
expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
expect(injector.getBinding(Key.get(Filter.class))).andReturn(binding);
expect(injector.getInstance(Key.get(Filter.class))).andReturn(mockFilter).anyTimes();
replay(binding, injector);
// some init params
// noinspection SSBasedInspection
final Map<String, String> initParams = new ImmutableMap.Builder<String, String>().put("ahsd", "asdas24dok").put("ahssd", "asdasd124ok").build();
ServletContext servletContext = createMock(ServletContext.class);
final String contextName = "thing__!@@44";
expect(servletContext.getServletContextName()).andReturn(contextName);
replay(servletContext);
String pattern = "/*";
final FilterDefinition filterDef = new FilterDefinition(pattern, Key.get(Filter.class), UriPatternType.get(UriPatternType.SERVLET, pattern), initParams, null);
filterDef.init(servletContext, injector, Sets.<Filter>newIdentityHashSet());
assertTrue(filterDef.getFilter() instanceof MockFilter);
final FilterConfig filterConfig = mockFilter.getConfig();
assertTrue(null != filterConfig);
assertEquals(filterConfig.getServletContext().getServletContextName(), contextName);
assertEquals(filterConfig.getFilterName(), Key.get(Filter.class).toString());
final Enumeration names = filterConfig.getInitParameterNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
assertTrue(initParams.containsKey(name));
assertEquals(filterConfig.getInitParameter(name), initParams.get(name));
}
verify(binding, injector, servletContext);
}
use of javax.servlet.Filter in project roboguice by roboguice.
the class FilterChainInvocation method doFilter.
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
GuiceFilter.Context previous = GuiceFilter.localContext.get();
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpServletRequest originalRequest = (previous != null) ? previous.getOriginalRequest() : request;
GuiceFilter.localContext.set(new GuiceFilter.Context(originalRequest, request, response));
try {
Filter filter = findNextFilter(request);
if (filter != null) {
// call to the filter, which can either consume the request or
// recurse back into this method. (in which case we will go to find the next filter,
// or dispatch to the servlet if no more filters are left)
filter.doFilter(servletRequest, servletResponse, this);
} else {
// we've reached the end of the filterchain, let's try to dispatch to a servlet
final boolean serviced = servletPipeline.service(servletRequest, servletResponse);
// dispatch to the normal filter chain only if one of our servlets did not match
if (!serviced) {
proceedingChain.doFilter(servletRequest, servletResponse);
}
}
} catch (Throwable t) {
// want to iterate through the stack elements for every filter.
if (!cleanedStacks) {
cleanedStacks = true;
pruneStacktrace(t);
}
Throwables.propagateIfInstanceOf(t, ServletException.class);
Throwables.propagateIfInstanceOf(t, IOException.class);
throw Throwables.propagate(t);
} finally {
GuiceFilter.localContext.set(previous);
}
}
use of javax.servlet.Filter in project undertow by undertow-io.
the class ManagedFilter method createFilter.
public void createFilter() throws ServletException {
synchronized (this) {
if (filter == null) {
try {
handle = filterInfo.getInstanceFactory().createInstance();
} catch (Exception e) {
throw UndertowServletMessages.MESSAGES.couldNotInstantiateComponent(filterInfo.getName(), e);
}
Filter filter = handle.getInstance();
new LifecyleInterceptorInvocation(servletContext.getDeployment().getDeploymentInfo().getLifecycleInterceptors(), filterInfo, filter, new FilterConfigImpl(filterInfo, servletContext)).proceed();
this.filter = filter;
}
}
}
use of javax.servlet.Filter in project stanbol by apache.
the class SolrServerPublishingComponent method updateFilter.
/**
* A change was made to the tracked CoreContainer (adding ,removal, ranking change).
* This removes and re-add the Servlet filter to apply such changes.
* @param name The name of the filter to be updated
* @param ref The serviceReference for the new CoreContainer to be added for
* the parsed name. <code>null</code> if the filter for that name needs only
* to be removed
* @param server The {@link CoreContainer} may be parsed in cases a reference
* is already available. If not the {@link #tracker} is used to look it up
* based on the parsed reference. This is basically a workaround for the
* fact that if the call originates form
* {@link ServiceTrackerCustomizer#addingService(ServiceReference)} the
* {@link CoreContainer} is not yet available via the tracker.
*/
protected void updateFilter(String name, ServiceReference ref, CoreContainer server) {
String serverPrefix = gloablPrefix + name;
Filter filter = published.remove(name);
if (filter != null) {
extHttpService.unregisterFilter(filter);
filter = null;
log.info("removed ServletFilter for SolrServer {} and prefix {}", name, serverPrefix);
}
// else no current filter for that name
if (ref != null || server != null) {
if (server == null) {
server = (CoreContainer) tracker.getService(ref);
}
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(CoreContainer.class.getClassLoader());
try {
filter = new SolrFilter(server);
} finally {
Thread.currentThread().setContextClassLoader(classLoader);
}
Dictionary<String, Object> filterPrpoerties = new Hashtable<String, Object>();
filterPrpoerties.put("path-prefix", serverPrefix);
String filterPrefix = serverPrefix + "/.*";
try {
extHttpService.registerFilter(filter, filterPrefix, filterPrpoerties, 0, null);
} catch (ServletException e) {
throw new IllegalStateException("Unable to register SolrDispatchFilter for" + "CoreContainer with name" + name + " (prefix: " + filterPrefix + "| properties: " + filterPrpoerties + ").", e);
}
log.info("added ServletFilter for SolrServer {} and prefix {}", name, serverPrefix);
}
// else no new filter to add
}
Aggregations