use of javax.ws.rs.container.ContainerRequestContext in project instrumentation-java by census-instrumentation.
the class JaxrsContainerExtractorTest method testExtraction.
@Test
@SuppressWarnings("unchecked")
public void testExtraction() throws Exception {
UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getPath()).thenReturn("mypath");
when(uriInfo.getMatchedURIs()).thenReturn(Collections.singletonList("/resource/{route}"));
when(uriInfo.getRequestUri()).thenReturn(URI.create("https://myhost/resource/1"));
ContainerRequestContext requestContext = mock(ContainerRequestContext.class);
when(requestContext.getHeaderString("host")).thenReturn("myhost");
when(requestContext.getMethod()).thenReturn("GET");
when(requestContext.getUriInfo()).thenReturn(uriInfo);
when(requestContext.getHeaderString("user-agent")).thenReturn("java/1.8");
ResourceInfo info = mock(ResourceInfo.class);
when(info.getResourceClass()).thenReturn((Class) MyResource.class);
when(info.getResourceMethod()).thenReturn(MyResource.class.getMethod("route"));
ExtendedContainerRequest extendedRequest = new ExtendedContainerRequest(requestContext, info);
ContainerResponseContext responseContext = mock(ContainerResponseContext.class);
when(responseContext.getStatus()).thenReturn(200);
JaxrsContainerExtractor extractor = new JaxrsContainerExtractor();
assertEquals("myhost", extractor.getHost(extendedRequest));
assertEquals("GET", extractor.getMethod(extendedRequest));
assertEquals("mypath", extractor.getPath(extendedRequest));
assertEquals("/resource/{route}", extractor.getRoute(extendedRequest));
assertEquals("https://myhost/resource/1", extractor.getUrl(extendedRequest));
assertEquals("java/1.8", extractor.getUserAgent(extendedRequest));
assertEquals(200, extractor.getStatusCode(responseContext));
}
use of javax.ws.rs.container.ContainerRequestContext in project tomee by apache.
the class JAXRSUtils method runContainerResponseFilters.
public static void runContainerResponseFilters(ServerProviderFactory pf, ResponseImpl r, Message m, OperationResourceInfo ori, Method invoked) throws IOException, Throwable {
List<ProviderInfo<ContainerResponseFilter>> containerFilters = pf.getContainerResponseFilters(ori == null ? null : ori.getNameBindings());
if (!containerFilters.isEmpty()) {
ContainerRequestContext requestContext = new ContainerRequestContextImpl(m.getExchange().getInMessage(), false, true);
ContainerResponseContext responseContext = new ContainerResponseContextImpl(r, m, ori == null ? null : ori.getClassResourceInfo().getServiceClass(), invoked);
for (ProviderInfo<ContainerResponseFilter> filter : containerFilters) {
InjectionUtils.injectContexts(filter.getProvider(), filter, m);
filter.getProvider().filter(requestContext, responseContext);
}
}
}
use of javax.ws.rs.container.ContainerRequestContext in project tomee by apache.
the class JAXRSUtils method runContainerRequestFilters.
public static boolean runContainerRequestFilters(ServerProviderFactory pf, Message m, boolean preMatch, Set<String> names) throws IOException {
List<ProviderInfo<ContainerRequestFilter>> containerFilters = preMatch ? pf.getPreMatchContainerRequestFilters() : pf.getPostMatchContainerRequestFilters(names);
if (!containerFilters.isEmpty()) {
ContainerRequestContext context = new ContainerRequestContextImpl(m, preMatch, false);
for (ProviderInfo<ContainerRequestFilter> filter : containerFilters) {
InjectionUtils.injectContexts(filter.getProvider(), filter, m);
filter.getProvider().filter(context);
Response response = m.getExchange().get(Response.class);
if (response != null) {
setMessageContentType(m, response);
return true;
}
}
}
return false;
}
use of javax.ws.rs.container.ContainerRequestContext in project graylog2-server by Graylog2.
the class MessageExportFormatFilterTest method mockRequestContextForNonMatchingPath.
private ContainerRequestContext mockRequestContextForNonMatchingPath() {
final UriInfo uriInfo = uriInfo("/api/system/inputs");
final ContainerRequestContext requestContext = mock(ContainerRequestContext.class);
when(requestContext.getUriInfo()).thenReturn(uriInfo);
return requestContext;
}
use of javax.ws.rs.container.ContainerRequestContext in project graylog2-server by Graylog2.
the class MessageExportFormatFilterTest method doesNothingIfRequestPathDoesNotMatch.
@Test
void doesNothingIfRequestPathDoesNotMatch() throws Exception {
final ContainerRequestFilter filter = new MessageExportFormatFilter(Collections.emptySet());
final ContainerRequestContext requestContext = mockRequestContextForNonMatchingPath();
filter.filter(requestContext);
verifyRequestNotAborted(requestContext);
}
Aggregations