Search in sources :

Example 66 with ContainerRequestContext

use of javax.ws.rs.container.ContainerRequestContext in project ff4j by ff4j.

the class SecurityAuthenticationFilterTest method testNoAuthorizationAttributeInHeader.

@Test(expected = WebApplicationException.class)
public void testNoAuthorizationAttributeInHeader() throws IOException {
    // Given
    FF4jAuthenticationFilter faf = new FF4jAuthenticationFilter();
    ContainerRequestContext mockRequest = mock(ContainerRequestContext.class);
    when(mockRequest.getHeaderString("Authorization")).thenReturn(null);
    when(mockRequest.getMethod()).thenReturn("GET");
    UriInfo mockUriInfo = mock(UriInfo.class);
    when(mockUriInfo.getPath()).thenReturn("someURL");
    when(mockRequest.getUriInfo()).thenReturn(mockUriInfo);
    faf.filter(mockRequest);
}
Also used : ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) FF4jAuthenticationFilter(org.ff4j.web.api.security.FF4jAuthenticationFilter) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 67 with ContainerRequestContext

use of javax.ws.rs.container.ContainerRequestContext in project jaeger-client-java by jaegertracing.

the class FilterIntegrationTest method testExtractorReturnsNullWhenTracerStateHeaderIsMissing.

/*
   * This test exists because opentracing's convention around missing tracer
   * state headers may change to stop supporting the automatic creation of
   * building a span.
   */
@Test
public void testExtractorReturnsNullWhenTracerStateHeaderIsMissing() {
    ContainerRequestContext reqContext = mock(ContainerRequestContext.class);
    given(reqContext.getHeaders()).willReturn(new MultivaluedHashMap<String, String>());
    ServerRequestCarrier carrier = new ServerRequestCarrier(reqContext);
    SpanContext spanCtx = tracer.extract(Format.Builtin.HTTP_HEADERS, carrier);
    assertNull(spanCtx);
}
Also used : SpanContext(io.opentracing.SpanContext) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) ServerRequestCarrier(com.uber.jaeger.filters.jaxrs2.ServerRequestCarrier) Test(org.junit.Test)

Example 68 with ContainerRequestContext

use of javax.ws.rs.container.ContainerRequestContext in project cxf by apache.

the class WadlGeneratorJsonTest method testWadlInJsonFormat.

@Test
public void testWadlInJsonFormat() throws Exception {
    ClassResourceInfo cri = ResourceUtils.createClassResourceInfo(BookChapters.class, BookChapters.class, true, true);
    final Message m = mockMessage("http://localhost:8080/baz", "/bookstore/1", WadlGenerator.WADL_QUERY, cri);
    Map<String, List<String>> headers = new HashMap<>();
    headers.put("Accept", Collections.singletonList("application/json"));
    m.put(Message.PROTOCOL_HEADERS, headers);
    WadlGenerator wg = new WadlGenerator() {

        @Override
        public void filter(ContainerRequestContext context) {
            super.doFilter(context, m);
        }
    };
    wg.setUseJaxbContextForQnames(false);
    wg.setIgnoreMessageWriters(false);
    wg.setExternalLinks(Collections.singletonList("json.schema"));
    Response r = handleRequest(wg, m);
    assertEquals("application/json", r.getMetadata().getFirst("Content-Type").toString());
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    new JSONProvider<Document>().writeTo((Document) r.getEntity(), Document.class, Document.class, new Annotation[] {}, MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(), os);
    String s = os.toString();
    String expected1 = "{\"application\":{\"grammars\":{\"include\":{\"@href\":\"http://localhost:8080/baz" + "/json.schema\"}},\"resources\":{\"@base\":\"http://localhost:8080/baz\"," + "\"resource\":{\"@path\":\"/bookstore/{id}\"";
    assertTrue(s.startsWith(expected1));
    String expected2 = "\"response\":{\"representation\":[{\"@mediaType\":\"application/xml\"}," + "{\"@element\":\"Chapter\",\"@mediaType\":\"application/json\"}]}";
    assertTrue(s.contains(expected2));
}
Also used : Message(org.apache.cxf.message.Message) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) HashMap(java.util.HashMap) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) Response(javax.ws.rs.core.Response) List(java.util.List) Test(org.junit.Test)

Example 69 with ContainerRequestContext

use of javax.ws.rs.container.ContainerRequestContext in project cxf 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 70 with ContainerRequestContext

use of javax.ws.rs.container.ContainerRequestContext in project wildfly by wildfly.

the class OpenTelemetryContainerFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext) {
    if (tracer != null) {
        final boolean remoteContext = requestContext.getHeaders().keySet().stream().anyMatch(k -> W3CTraceContextPropagator.getInstance().fields().contains(k));
        final UriInfo uriInfo = requestContext.getUriInfo();
        final URI requestUri = uriInfo.getRequestUri();
        final String method = requestContext.getMethod();
        final String uri = uriInfo.getPath();
        SpanBuilder spanBuilder = tracer.spanBuilder(method + " " + uri).setSpanKind(SpanKind.SERVER);
        if (remoteContext) {
            spanBuilder.setParent(openTelemetry.getPropagators().getTextMapPropagator().extract(Context.current(), requestContext, new TextMapGetter<ContainerRequestContext>() {

                @Override
                public String get(ContainerRequestContext requestContext, String key) {
                    if (requestContext.getHeaders().containsKey(key)) {
                        return requestContext.getHeaders().get(key).get(0);
                    }
                    return null;
                }

                @Override
                public Iterable<String> keys(ContainerRequestContext requestContext) {
                    return requestContext.getHeaders().keySet();
                }
            }));
        } else {
            spanBuilder.setNoParent();
        }
        Span serverSpan = spanBuilder.startSpan();
        serverSpan.makeCurrent();
        serverSpan.setAttribute(SemanticAttributes.HTTP_METHOD, method);
        serverSpan.setAttribute(SemanticAttributes.HTTP_SCHEME, requestUri.getScheme());
        serverSpan.setAttribute(SemanticAttributes.HTTP_HOST, requestUri.getHost() + ":" + requestUri.getPort());
        serverSpan.setAttribute(SemanticAttributes.HTTP_TARGET, uri);
        requestContext.setProperty(SERVER_SPAN, serverSpan);
    }
}
Also used : SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) TextMapGetter(io.opentelemetry.context.propagation.TextMapGetter) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) URI(java.net.URI) Span(io.opentelemetry.api.trace.Span) UriInfo(javax.ws.rs.core.UriInfo)

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