use of javax.servlet.Filter in project spring-cloud-sleuth by spring-cloud.
the class MyFilter method should_assume_that_a_request_without_span_and_with_trace_is_a_root_span.
@Test
public void should_assume_that_a_request_without_span_and_with_trace_is_a_root_span() throws Exception {
Long expectedTraceId = new Random().nextLong();
whenSentRequestWithTraceIdAndNoSpanId(expectedTraceId);
whenSentRequestWithTraceIdAndNoSpanId(expectedTraceId);
then(this.reporter.getSpans().stream().filter(span -> span.id().equals(span.traceId())).findAny().isPresent()).as("a root span exists").isTrue();
then(this.tracer.currentSpan()).isNull();
}
use of javax.servlet.Filter in project spring-cloud-sleuth by spring-cloud.
the class MyFilter method should_add_a_custom_tag_to_the_span_created_in_controller.
@Test
public void should_add_a_custom_tag_to_the_span_created_in_controller() throws Exception {
Long expectedTraceId = new Random().nextLong();
MvcResult mvcResult = whenSentDeferredWithTraceId(expectedTraceId);
this.mockMvc.perform(asyncDispatch(mvcResult)).andExpect(status().isOk()).andReturn();
Optional<zipkin2.Span> taggedSpan = this.reporter.getSpans().stream().filter(span -> span.tags().containsKey("tag")).findFirst();
then(taggedSpan.isPresent()).isTrue();
then(taggedSpan.get().tags()).containsEntry("tag", "value").containsEntry("mvc.controller.method", "deferredMethod").containsEntry("mvc.controller.class", "TestController");
then(this.tracer.currentSpan()).isNull();
}
use of javax.servlet.Filter in project openmrs-core by openmrs.
the class WebModuleUtil method getFiltersForRequest.
/**
* Return List of Filters that have been loaded through Modules that have mappings that pass for
* the passed request
*
* @param request - The request to check for matching {@link Filter}s
* @return List of all {@link Filter}s that have filter mappings that match the passed request
*/
public static List<Filter> getFiltersForRequest(ServletRequest request) {
List<Filter> filters = new ArrayList<>();
if (request != null) {
HttpServletRequest httpRequest = (HttpServletRequest) request;
String requestPath = httpRequest.getRequestURI();
if (requestPath != null) {
if (requestPath.startsWith(httpRequest.getContextPath())) {
requestPath = requestPath.substring(httpRequest.getContextPath().length());
}
for (ModuleFilterMapping filterMapping : WebModuleUtil.getFilterMappings()) {
if (ModuleFilterMapping.filterMappingPasses(filterMapping, requestPath)) {
Filter passedFilter = moduleFiltersByName.get(filterMapping.getFilterName());
if (passedFilter != null) {
filters.add(passedFilter);
} else {
log.warn("Unable to retrieve filter that has a name of " + filterMapping.getFilterName() + " in filter mapping.");
}
}
}
}
}
return filters;
}
use of javax.servlet.Filter in project openmrs-core by openmrs.
the class WebModuleUtil method loadFilters.
/**
* This method will initialize and store this module's filters
*
* @param module - The Module to load and register Filters
* @param servletContext - The servletContext within which this method is called
*/
public static void loadFilters(Module module, ServletContext servletContext) {
// Load Filters
Map<String, Filter> filters = new HashMap<>();
try {
for (ModuleFilterDefinition def : ModuleFilterDefinition.retrieveFilterDefinitions(module)) {
if (moduleFiltersByName.containsKey(def.getFilterName())) {
throw new ModuleException("A filter with name <" + def.getFilterName() + "> has already been registered.");
}
ModuleFilterConfig config = ModuleFilterConfig.getInstance(def, servletContext);
Filter f = (Filter) ModuleFactory.getModuleClassLoader(module).loadClass(def.getFilterClass()).newInstance();
f.init(config);
filters.put(def.getFilterName(), f);
}
} catch (ModuleException e) {
throw e;
} catch (Exception e) {
throw new ModuleException("An error occurred initializing Filters for module: " + module.getModuleId(), e);
}
moduleFilters.put(module, filters.values());
moduleFiltersByName.putAll(filters);
log.debug("Module: " + module.getModuleId() + " successfully loaded " + filters.size() + " filters.");
// Load Filter Mappings
List<ModuleFilterMapping> modMappings = ModuleFilterMapping.retrieveFilterMappings(module);
moduleFilterMappings.addAll(modMappings);
log.debug("Module: " + module.getModuleId() + " successfully loaded " + modMappings.size() + " filter mappings.");
}
use of javax.servlet.Filter in project oozie by apache.
the class TestHostnameFilter method testMissingHostname.
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 {
Assert.assertTrue(HostnameFilter.get().contains("???"));
invoked.set(true);
}
};
Filter filter = new HostnameFilter();
filter.init(null);
Assert.assertNull(HostnameFilter.get());
filter.doFilter(request, response, chain);
Assert.assertTrue(invoked.get());
Assert.assertNull(HostnameFilter.get());
filter.destroy();
}
Aggregations