Search in sources :

Example 11 with ClientRequestContext

use of javax.ws.rs.client.ClientRequestContext in project instrumentation-java by census-instrumentation.

the class JaxrsClientFilterTest method testRequestFilter.

@Test
public void testRequestFilter() throws Exception {
    URI uri = URI.create("https://mydomain/myresource");
    ClientRequestContext requestContext = mock(ClientRequestContext.class);
    when(requestContext.getUri()).thenReturn(uri);
    filter.filter(requestContext);
    verify(requestContext).getUri();
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) URI(java.net.URI) Test(org.junit.Test)

Example 12 with ClientRequestContext

use of javax.ws.rs.client.ClientRequestContext in project cxf by apache.

the class ClientRequestFilterInterceptor method handleMessage.

public void handleMessage(Message outMessage) throws Fault {
    ClientProviderFactory pf = ClientProviderFactory.getInstance(outMessage);
    if (pf == null) {
        return;
    }
    // create an empty proxy output stream that the filter can interact with
    // and save a reference for later
    ProxyOutputStream pos = new ProxyOutputStream();
    outMessage.setContent(OutputStream.class, pos);
    outMessage.setContent(ProxyOutputStream.class, pos);
    List<ProviderInfo<ClientRequestFilter>> filters = pf.getClientRequestFilters();
    if (!filters.isEmpty()) {
        final Exchange exchange = outMessage.getExchange();
        final ClientRequestContext context = new ClientRequestContextImpl(outMessage, false);
        for (ProviderInfo<ClientRequestFilter> filter : filters) {
            InjectionUtils.injectContexts(filter.getProvider(), filter, outMessage);
            try {
                filter.getProvider().filter(context);
                @SuppressWarnings("unchecked") Map<String, List<Object>> headers = CastUtils.cast((Map<String, List<Object>>) outMessage.get(Message.PROTOCOL_HEADERS));
                HttpUtils.convertHeaderValuesToString(headers, false);
                Response response = outMessage.getExchange().get(Response.class);
                if (response != null) {
                    outMessage.getInterceptorChain().abort();
                    Message inMessage = new MessageImpl();
                    inMessage.setExchange(exchange);
                    inMessage.put(Message.RESPONSE_CODE, response.getStatus());
                    inMessage.put(Message.PROTOCOL_HEADERS, response.getMetadata());
                    exchange.setInMessage(inMessage);
                    MessageObserver observer = exchange.get(MessageObserver.class);
                    observer.onMessage(inMessage);
                    return;
                }
            } catch (IOException ex) {
                throw new ProcessingException(ex);
            }
        }
    }
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) MessageObserver(org.apache.cxf.transport.MessageObserver) Message(org.apache.cxf.message.Message) IOException(java.io.IOException) ClientProviderFactory(org.apache.cxf.jaxrs.client.ClientProviderFactory) Exchange(org.apache.cxf.message.Exchange) Response(javax.ws.rs.core.Response) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) ProxyOutputStream(org.apache.cxf.transport.http.ProxyOutputStream) List(java.util.List) MessageImpl(org.apache.cxf.message.MessageImpl) ProcessingException(javax.ws.rs.ProcessingException)

Example 13 with ClientRequestContext

use of javax.ws.rs.client.ClientRequestContext in project cxf by apache.

the class JAXRS20ClientServerBookTest method testGetSetEntityStream.

@Test
public void testGetSetEntityStream() {
    String address = "http://localhost:" + PORT + "/bookstore/entityecho";
    String entity = "BOOKSTORE";
    Client client = ClientBuilder.newClient();
    client.register(new ClientRequestFilter() {

        @Override
        public void filter(ClientRequestContext context) throws IOException {
            context.setEntityStream(new ReplacingOutputStream(context.getEntityStream(), 'X', 'O'));
        }
    });
    WebTarget target = client.target(address);
    Response response = target.request().post(Entity.entity(entity.replace('O', 'X'), "text/plain"));
    assertEquals(entity, response.readEntity(String.class));
}
Also used : ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) ClientRequestContext(javax.ws.rs.client.ClientRequestContext) Response(javax.ws.rs.core.Response) IOException(java.io.IOException) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 14 with ClientRequestContext

use of javax.ws.rs.client.ClientRequestContext in project cxf by apache.

the class SpecExamplesTest method interceptorDefaultTest.

@Test
public void interceptorDefaultTest() {
    URI uri = URI.create("https://www.example.com/foo?param=value&pet=dog");
    String method = "POST";
    MessageSigner messageSigner = new MessageSigner(keyProvider, "Test", Collections.singletonList("Date"));
    CreateSignatureInterceptor interceptor = new CreateSignatureInterceptor();
    interceptor.setMessageSigner(messageSigner);
    Map<String, List<String>> headers = createMockHeaders();
    MultivaluedMap<String, Object> requestHeaders = new MultivaluedHashMap<>();
    MultivaluedMap<String, String> requestStringHeaders = new MultivaluedHashMap<>();
    headers.forEach((header, value) -> {
        requestHeaders.add(header, value.get(0));
        requestStringHeaders.add(header, value.get(0));
    });
    final Message message = new MessageImpl();
    doInPhaseInterceptor(message, () -> {
        ClientRequestContext requestContext = getClientRequestContextMock(uri, method, requestHeaders);
        interceptor.filter(requestContext);
        String signatureHeader = (String) requestHeaders.get("Signature").get(0);
        requestStringHeaders.add("Signature", signatureHeader);
        String expectedHeader = "keyId=\"Test\",algorithm=\"rsa-sha256\"," + "signature=\"SjWJWbWN7i0wzBvtPl8rbASWz5xQW6mcJmn+ibttBqtifLN7Sazz" + "6m79cNfwwb8DMJ5cou1s7uEGKKCs+FLEEaDV5lp7q25WqS+lavg7T8hc0GppauB" + "6hbgEKTwblDHYGEtbGmtdHgVCk9SuS13F0hZ8FD0k/5OxEPXe5WozsbM=\"";
        assertEquals(signatureHeader.replaceAll("headers=\"date\",", ""), expectedHeader);
        // Verify that the request signature can be verified by the filter
        MessageVerifier messageVerifier = new MessageVerifier(keyId -> publicKey);
        messageVerifier.setAddDefaultRequiredHeaders(false);
        messageVerifier.setSecurityProvider(new MockSecurityProvider());
        messageVerifier.setAlgorithmProvider(new MockAlgorithmProvider());
        VerifySignatureFilter verifySignatureFilter = new VerifySignatureFilter();
        verifySignatureFilter.setMessageVerifier(messageVerifier);
        ContainerRequestContext containerRequestContext = getContainerRequestContextMock(uri, method, requestStringHeaders);
        verifySignatureFilter.filter(containerRequestContext);
    });
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) MockAlgorithmProvider(org.apache.cxf.rs.security.httpsignature.provider.MockAlgorithmProvider) Message(org.apache.cxf.message.Message) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) VerifySignatureFilter(org.apache.cxf.rs.security.httpsignature.filters.VerifySignatureFilter) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) CreateSignatureInterceptor(org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor) MockSecurityProvider(org.apache.cxf.rs.security.httpsignature.provider.MockSecurityProvider) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 15 with ClientRequestContext

use of javax.ws.rs.client.ClientRequestContext in project cxf by apache.

the class SpecExamplesTest method interceptorAllHeadersTest.

@Test
public void interceptorAllHeadersTest() {
    URI uri = URI.create("https://www.example.com/foo?param=value&pet=dog");
    String method = "POST";
    MessageSigner messageSigner = new MessageSigner(keyProvider, "Test", Arrays.asList("(request-target)", "host", "date", "content-type", "digest", "content-length"));
    CreateSignatureInterceptor interceptor = new CreateSignatureInterceptor();
    interceptor.setMessageSigner(messageSigner);
    Map<String, List<String>> headers = createMockHeaders();
    MultivaluedMap<String, Object> requestHeaders = new MultivaluedHashMap<>();
    MultivaluedMap<String, String> requestStringHeaders = new MultivaluedHashMap<>();
    headers.forEach((header, value) -> {
        requestHeaders.add(header, value.get(0));
        requestStringHeaders.add(header, value.get(0));
    });
    final Message message = new MessageImpl();
    doInPhaseInterceptor(message, () -> {
        ClientRequestContext requestContext = getClientRequestContextMock(uri, method, requestHeaders);
        interceptor.filter(requestContext);
        String signatureHeader = (String) requestHeaders.get("Signature").get(0);
        requestStringHeaders.add("Signature", signatureHeader);
        String expectedHeader = "keyId=\"Test\",algorithm=\"rsa-sha256\"," + "headers=\"(request-target) host date content-type digest content-length\"," + "signature=\"vSdrb+dS3EceC9bcwHSo4MlyKS59iFIrhgYkz8+oVLEEzmYZZvRs" + "8rgOp+63LEM3v+MFHB32NfpB2bEKBIvB1q52LaEUHFv120V01IL+TAD48XaERZF" + "ukWgHoBTLMhYS2Gb51gWxpeIq8knRmPnYePbF5MOkR0Zkly4zKH7s1dE=\"";
        assertEquals(signatureHeader, expectedHeader);
        // Verify that the request signature can be verified by the filter
        MessageVerifier messageVerifier = new MessageVerifier(keyId -> publicKey);
        messageVerifier.setAddDefaultRequiredHeaders(false);
        messageVerifier.setSecurityProvider(new MockSecurityProvider());
        messageVerifier.setAlgorithmProvider(new MockAlgorithmProvider());
        VerifySignatureFilter verifySignatureFilter = new VerifySignatureFilter();
        verifySignatureFilter.setMessageVerifier(messageVerifier);
        ContainerRequestContext containerRequestContext = getContainerRequestContextMock(uri, method, requestStringHeaders);
        InputStream stream = new ByteArrayInputStream("{\"hello\": \"world\"}".getBytes(StandardCharsets.UTF_8));
        Mockito.when(containerRequestContext.getEntityStream()).thenReturn(stream);
        verifySignatureFilter.filter(containerRequestContext);
    });
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) MockAlgorithmProvider(org.apache.cxf.rs.security.httpsignature.provider.MockAlgorithmProvider) Message(org.apache.cxf.message.Message) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) VerifySignatureFilter(org.apache.cxf.rs.security.httpsignature.filters.VerifySignatureFilter) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) CreateSignatureInterceptor(org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor) MockSecurityProvider(org.apache.cxf.rs.security.httpsignature.provider.MockSecurityProvider) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Aggregations

ClientRequestContext (javax.ws.rs.client.ClientRequestContext)30 Test (org.junit.Test)15 ClientResponseContext (javax.ws.rs.client.ClientResponseContext)11 Response (javax.ws.rs.core.Response)10 IOException (java.io.IOException)9 ClientRequestFilter (javax.ws.rs.client.ClientRequestFilter)8 ClientResponseFilter (javax.ws.rs.client.ClientResponseFilter)7 WebTarget (javax.ws.rs.client.WebTarget)7 Test (org.junit.jupiter.api.Test)7 URI (java.net.URI)6 Client (javax.ws.rs.client.Client)4 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)4 Message (org.apache.cxf.message.Message)4 MessageImpl (org.apache.cxf.message.MessageImpl)4 JerseyTest (org.glassfish.jersey.test.JerseyTest)4 ProcessingException (javax.ws.rs.ProcessingException)3 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)3 CreateSignatureInterceptor (org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor)3 VerifySignatureFilter (org.apache.cxf.rs.security.httpsignature.filters.VerifySignatureFilter)3 MockAlgorithmProvider (org.apache.cxf.rs.security.httpsignature.provider.MockAlgorithmProvider)3