Search in sources :

Example 31 with ClientBuilder

use of javax.ws.rs.client.ClientBuilder in project vespa by vespa-engine.

the class JerseyJaxRsClientFactory method createClient.

/**
 * Contains some workarounds for HTTP/JAX-RS/Jersey issues. See:
 *   https://jersey.java.net/apidocs/latest/jersey/org/glassfish/jersey/client/ClientProperties.html#SUPPRESS_HTTP_COMPLIANCE_VALIDATION
 *   https://jersey.java.net/apidocs/latest/jersey/org/glassfish/jersey/client/HttpUrlConnectorProvider.html#SET_METHOD_WORKAROUND
 */
@Override
public <T> T createClient(final Class<T> apiClass, final HostName hostName, final int port, final String pathPrefix, String scheme) {
    final UriBuilder uriBuilder = UriBuilder.fromPath(pathPrefix).host(hostName.s()).port(port).scheme(scheme);
    ClientBuilder builder = ClientBuilder.newBuilder().property(ClientProperties.CONNECT_TIMEOUT, connectTimeoutMs).property(ClientProperties.READ_TIMEOUT, readTimeoutMs).property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, // Allow empty PUT. TODO: Fix API.
    true).property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, // Allow e.g. PATCH method.
    true).property(ClientProperties.FOLLOW_REDIRECTS, true);
    if (sslContext != null) {
        builder.sslContext(sslContext);
    }
    if (hostnameVerifier != null) {
        builder.hostnameVerifier(hostnameVerifier);
    }
    if (userAgent != null) {
        builder.register((ClientRequestFilter) context -> context.getHeaders().put(HttpHeaders.USER_AGENT, Collections.singletonList(userAgent)));
    }
    final WebTarget target = builder.build().target(uriBuilder);
    // TODO: Check if this fills up non-heap memory with loaded classes.
    return WebResourceFactory.newResource(apiClass, target);
}
Also used : ClientBuilder(javax.ws.rs.client.ClientBuilder) SSLContext(javax.net.ssl.SSLContext) HttpHeaders(javax.ws.rs.core.HttpHeaders) HttpUrlConnectorProvider(org.glassfish.jersey.client.HttpUrlConnectorProvider) HostName(com.yahoo.vespa.applicationmodel.HostName) WebResourceFactory(org.glassfish.jersey.client.proxy.WebResourceFactory) UriBuilder(javax.ws.rs.core.UriBuilder) ClientProperties(org.glassfish.jersey.client.ClientProperties) WebTarget(javax.ws.rs.client.WebTarget) HostnameVerifier(javax.net.ssl.HostnameVerifier) ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) Collections(java.util.Collections) WebTarget(javax.ws.rs.client.WebTarget) UriBuilder(javax.ws.rs.core.UriBuilder) ClientBuilder(javax.ws.rs.client.ClientBuilder)

Example 32 with ClientBuilder

use of javax.ws.rs.client.ClientBuilder in project wildfly by wildfly.

the class UndertowSSLv2HelloTestCase method testTwoWayElytronClientServerSupportsSSLv2Hello.

/**
 * Two way SSL - RESTEasy client sends SSLv2Hello message and server supports the protocol.
 * Handshake should succeed.
 */
@Test
public void testTwoWayElytronClientServerSupportsSSLv2Hello() throws Exception {
    configureSSLContext(SSLV2HELLO_CONTEXT);
    AuthenticationContext context = doPrivileged((PrivilegedAction<AuthenticationContext>) () -> {
        try {
            URL config = getClass().getResource("wildfly-config-sslv2hello.xml");
            return ElytronXmlParser.parseAuthenticationClientConfiguration(config.toURI()).create();
        } catch (Throwable t) {
            throw new InvalidAuthenticationConfigurationException(t);
        }
    });
    context.run(() -> {
        ClientBuilder clientBuilder = ClientBuilder.newBuilder().hostnameVerifier((s, sslSession) -> true);
        Client client = clientBuilder.build();
        Response response = client.target(String.valueOf(securedRootUrl)).request().get();
        Assert.assertEquals(200, response.getStatus());
    });
    restoreConfiguration();
}
Also used : Response(javax.ws.rs.core.Response) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) InvalidAuthenticationConfigurationException(org.wildfly.security.auth.client.InvalidAuthenticationConfigurationException) Client(javax.ws.rs.client.Client) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) URL(java.net.URL) ClientBuilder(javax.ws.rs.client.ClientBuilder) Test(org.junit.Test)

Example 33 with ClientBuilder

use of javax.ws.rs.client.ClientBuilder in project wildfly by wildfly.

the class UndertowSSLv2HelloTestCase method testTwoWayElytronClientNoSSLv2HelloSupport.

/**
 * Two way SSL - Server supports SSLv2Hello, but client does not support SSLv2Hello.
 * Handshake should succeed as they still share protocol TLSv1 in common.
 */
@Test
public void testTwoWayElytronClientNoSSLv2HelloSupport() throws Exception {
    configureSSLContext(SSLV2HELLO_CONTEXT);
    AuthenticationContext context = doPrivileged((PrivilegedAction<AuthenticationContext>) () -> {
        try {
            URL config = getClass().getResource("wildfly-config-no-sslv2hello.xml");
            return ElytronXmlParser.parseAuthenticationClientConfiguration(config.toURI()).create();
        } catch (Throwable t) {
            throw new InvalidAuthenticationConfigurationException(t);
        }
    });
    context.run(() -> {
        ClientBuilder clientBuilder = ClientBuilder.newBuilder().hostnameVerifier((s, sslSession) -> true);
        Client client = clientBuilder.build();
        Response response = client.target(String.valueOf(securedRootUrl)).request().get();
        Assert.assertEquals(200, response.getStatus());
    });
    restoreConfiguration();
}
Also used : Response(javax.ws.rs.core.Response) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) InvalidAuthenticationConfigurationException(org.wildfly.security.auth.client.InvalidAuthenticationConfigurationException) Client(javax.ws.rs.client.Client) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) URL(java.net.URL) ClientBuilder(javax.ws.rs.client.ClientBuilder) Test(org.junit.Test)

Example 34 with ClientBuilder

use of javax.ws.rs.client.ClientBuilder in project wildfly by wildfly.

the class UndertowTwoWaySslNeedClientAuthTestCase method testClientConfigProviderSSLContextIsSuccessfulWhenBasicSetOnRESTEasy.

/**
 * Test situation when credentials are set on RESTEeasy client, but truststore is part of SSLContext configured for Elytron client.
 * Test that Elytron SSLContext will be used successfully.
 */
@Test
public void testClientConfigProviderSSLContextIsSuccessfulWhenBasicSetOnRESTEasy() {
    AuthenticationContext context = doPrivileged((PrivilegedAction<AuthenticationContext>) () -> {
        try {
            URL config = getClass().getResource("wildfly-config-correct-truststore.xml");
            return ElytronXmlParser.parseAuthenticationClientConfiguration(config.toURI()).create();
        } catch (Throwable t) {
            throw new InvalidAuthenticationConfigurationException(t);
        }
    });
    context.run(() -> {
        ClientBuilder resteasyClientBuilder = ClientBuilder.newBuilder();
        resteasyClientBuilder.hostnameVerifier((s, sslSession) -> true);
        Client client = resteasyClientBuilder.build();
        client.register(HttpAuthorization.basic("randomName", "randomPass"));
        Response response = client.target(String.valueOf(securedRootUrl)).request().get();
        Assert.assertEquals(200, response.getStatus());
    });
}
Also used : Response(javax.ws.rs.core.Response) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) InvalidAuthenticationConfigurationException(org.wildfly.security.auth.client.InvalidAuthenticationConfigurationException) AuthenticationContextConfigurationClient(org.wildfly.security.auth.client.AuthenticationContextConfigurationClient) Client(javax.ws.rs.client.Client) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) HttpClient(org.apache.http.client.HttpClient) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) URL(java.net.URL) ClientBuilder(javax.ws.rs.client.ClientBuilder) Test(org.junit.Test)

Example 35 with ClientBuilder

use of javax.ws.rs.client.ClientBuilder in project wildfly by wildfly.

the class UndertowTwoWaySslNeedClientAuthTestCase method testClientConfigProviderSSLContextForDifferentHostWillNotWork.

/**
 * Test that RESTEasy client does choose SSLContext from Elytron client based on destination of the request.
 * In this case the truststore is set for different endpoint/server and so SSL handshake will fail.
 */
@Test(expected = ProcessingException.class)
public void testClientConfigProviderSSLContextForDifferentHostWillNotWork() {
    AuthenticationContext context = doPrivileged((PrivilegedAction<AuthenticationContext>) () -> {
        try {
            URL config = getClass().getResource("wildfly-config-correct-truststore-different-host.xml");
            return ElytronXmlParser.parseAuthenticationClientConfiguration(config.toURI()).create();
        } catch (Throwable t) {
            throw new InvalidAuthenticationConfigurationException(t);
        }
    });
    context.run(() -> {
        ClientBuilder resteasyClientBuilder = ClientBuilder.newBuilder().hostnameVerifier((s, sslSession) -> true);
        Client client = resteasyClientBuilder.build();
        Response response = client.target(String.valueOf(securedRootUrl)).request().get();
        Assert.assertEquals(200, response.getStatus());
    });
}
Also used : Response(javax.ws.rs.core.Response) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) InvalidAuthenticationConfigurationException(org.wildfly.security.auth.client.InvalidAuthenticationConfigurationException) AuthenticationContextConfigurationClient(org.wildfly.security.auth.client.AuthenticationContextConfigurationClient) Client(javax.ws.rs.client.Client) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) HttpClient(org.apache.http.client.HttpClient) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) URL(java.net.URL) ClientBuilder(javax.ws.rs.client.ClientBuilder) Test(org.junit.Test)

Aggregations

ClientBuilder (javax.ws.rs.client.ClientBuilder)57 Client (javax.ws.rs.client.Client)41 Response (javax.ws.rs.core.Response)26 Test (org.junit.Test)26 RunAsClient (org.jboss.arquillian.container.test.api.RunAsClient)24 AuthenticationContext (org.wildfly.security.auth.client.AuthenticationContext)24 URL (java.net.URL)20 SSLContext (javax.net.ssl.SSLContext)16 ClientConfig (org.glassfish.jersey.client.ClientConfig)15 AuthenticationConfiguration (org.wildfly.security.auth.client.AuthenticationConfiguration)13 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)11 InvalidAuthenticationConfigurationException (org.wildfly.security.auth.client.InvalidAuthenticationConfigurationException)11 WebTarget (javax.ws.rs.client.WebTarget)10 IOException (java.io.IOException)9 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)9 KeyStore (java.security.KeyStore)7 HttpClient (org.apache.http.client.HttpClient)7 AuthenticationContextConfigurationClient (org.wildfly.security.auth.client.AuthenticationContextConfigurationClient)6 MalformedURLException (java.net.MalformedURLException)5 HostnameVerifier (javax.net.ssl.HostnameVerifier)5