Search in sources :

Example 41 with ContainerRequestContext

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));
}
Also used : ResourceInfo(javax.ws.rs.container.ResourceInfo) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 42 with ContainerRequestContext

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);
        }
    }
}
Also used : ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) ContainerRequestContextImpl(org.apache.cxf.jaxrs.impl.ContainerRequestContextImpl) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) ContainerResponseContextImpl(org.apache.cxf.jaxrs.impl.ContainerResponseContextImpl)

Example 43 with ContainerRequestContext

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;
}
Also used : AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) ContainerRequestContextImpl(org.apache.cxf.jaxrs.impl.ContainerRequestContextImpl) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter)

Example 44 with ContainerRequestContext

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;
}
Also used : ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) UriInfo(javax.ws.rs.core.UriInfo)

Example 45 with ContainerRequestContext

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);
}
Also used : ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) Test(org.junit.jupiter.api.Test)

Aggregations

ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)81 Test (org.junit.Test)40 Response (javax.ws.rs.core.Response)32 UriInfo (javax.ws.rs.core.UriInfo)23 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)22 Test (org.junit.jupiter.api.Test)18 Resource (org.glassfish.jersey.server.model.Resource)15 ContainerRequestFilter (javax.ws.rs.container.ContainerRequestFilter)14 ApplicationHandler (org.glassfish.jersey.server.ApplicationHandler)12 ContainerRequest (org.glassfish.jersey.server.ContainerRequest)10 File (java.io.File)8 ContainerResponseContext (javax.ws.rs.container.ContainerResponseContext)8 ResourceInfo (javax.ws.rs.container.ResourceInfo)8 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)8 IOException (java.io.IOException)7 URI (java.net.URI)6 ContainerResponseFilter (javax.ws.rs.container.ContainerResponseFilter)6 FF4jAuthenticationFilter (org.ff4j.web.api.security.FF4jAuthenticationFilter)6 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)6 ApiKeyValidatorFilter (org.ff4j.web.api.filter.ApiKeyValidatorFilter)5