Search in sources :

Example 11 with ClientRequestFilter

use of javax.ws.rs.client.ClientRequestFilter in project perry by ca-cwds.

the class SecureClientFactory method createSecureClient.

public static Client createSecureClient() {
    Client client = ClientBuilder.newClient();
    client.register((ClientRequestFilter) requestContext -> requestContext.getHeaders().add("Authorization", PerrySubject.getToken()));
    return client;
}
Also used : ClientBuilder(javax.ws.rs.client.ClientBuilder) PerrySubject(gov.ca.cwds.security.realm.PerrySubject) Client(javax.ws.rs.client.Client) ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) Client(javax.ws.rs.client.Client)

Example 12 with ClientRequestFilter

use of javax.ws.rs.client.ClientRequestFilter 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 13 with ClientRequestFilter

use of javax.ws.rs.client.ClientRequestFilter in project Payara by payara.

the class RemoteEJBContext method getClientBuilder.

private ClientBuilder getClientBuilder() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    ClientBuilder clientBuilder = ClientBuilder.newBuilder();
    if (environment.containsKey(JAXRS_CLIENT_CONNECT_TIMEOUT)) {
        clientBuilder.connectTimeout(getLong(environment.get(JAXRS_CLIENT_CONNECT_TIMEOUT)), MICROSECONDS);
    }
    if (environment.containsKey(JAXRS_CLIENT_EXECUTOR_SERVICE)) {
        clientBuilder.executorService(getInstance(environment.get(JAXRS_CLIENT_EXECUTOR_SERVICE), ExecutorService.class));
    }
    if (environment.containsKey(JAXRS_CLIENT_HOSTNAME_VERIFIER)) {
        clientBuilder.hostnameVerifier(getInstance(environment.get(JAXRS_CLIENT_HOSTNAME_VERIFIER), HostnameVerifier.class));
    }
    if (environment.containsKey(JAXRS_CLIENT_KEY_STORE)) {
        clientBuilder.keyStore(getInstance(environment.get(JAXRS_CLIENT_KEY_STORE), KeyStore.class), getPassword(environment.containsKey("keyStorePassword") ? environment.get("keyStorePassword") : environment.get(JAXRS_CLIENT_KEY_STORE_PASSOWRD)));
    }
    if (environment.containsKey(JAXRS_CLIENT_READ_TIMEOUT)) {
        clientBuilder.readTimeout(getLong(environment.get(JAXRS_CLIENT_READ_TIMEOUT)), MICROSECONDS);
    }
    if (environment.containsKey(JAXRS_CLIENT_SCHEDULED_EXECUTOR_SERVICE)) {
        clientBuilder.scheduledExecutorService(getInstance(environment.get(JAXRS_CLIENT_SCHEDULED_EXECUTOR_SERVICE), ScheduledExecutorService.class));
    }
    if (environment.containsKey(JAXRS_CLIENT_SSL_CONTEXT)) {
        clientBuilder.sslContext(getInstance(environment.get(JAXRS_CLIENT_SSL_CONTEXT), SSLContext.class));
    }
    if (environment.containsKey(JAXRS_CLIENT_TRUST_STORE)) {
        clientBuilder.trustStore(getInstance(environment.get(JAXRS_CLIENT_TRUST_STORE), KeyStore.class));
    }
    if (environment.containsKey(JAXRS_CLIENT_CONFIG)) {
        clientBuilder.withConfig(getInstance(environment.get(JAXRS_CLIENT_CONFIG), Configuration.class));
    }
    if (environment.containsKey(JAXRS_CLIENT_REQUEST_FILTER)) {
        Object value = environment.get(JAXRS_CLIENT_REQUEST_FILTER);
        if (value instanceof Class && (ClientRequestFilter.class.isAssignableFrom((Class) value))) {
            clientBuilder.register((Class<ClientRequestFilter>) value);
        } else if (value instanceof ClientRequestFilter) {
            clientBuilder.register((ClientRequestFilter) value);
        }
    }
    if (environment.containsKey(JAXRS_CLIENT_RESPONSE_FILTER)) {
        Object value = environment.get(JAXRS_CLIENT_RESPONSE_FILTER);
        if (value instanceof Class && (ClientResponseFilter.class.isAssignableFrom((Class) value))) {
            clientBuilder.register((Class<ClientResponseFilter>) value);
        } else if (value instanceof ClientResponseFilter) {
            clientBuilder.register((ClientResponseFilter) value);
        }
    }
    String providerPrincipal = (String) environment.get(PROVIDER_PRINCIPAL);
    String providerCredentials = (String) environment.get(PROVIDER_CREDENTIALS);
    if (providerPrincipal != null && providerCredentials != null) {
        String authType = (String) environment.get(PROVIDER_AUTH_TYPE);
        if (null == authType || BASIC_AUTH.equals(authType)) {
            clientBuilder.register(HttpAuthenticationFeature.basic(providerPrincipal, providerCredentials));
        } else if (DIGEST_AUTH.equals(authType)) {
            clientBuilder.register(HttpAuthenticationFeature.digest(providerPrincipal, providerCredentials));
        }
    }
    return clientBuilder;
}
Also used : ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Configuration(javax.ws.rs.core.Configuration) ClientResponseFilter(javax.ws.rs.client.ClientResponseFilter) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) ClientBuilder(javax.ws.rs.client.ClientBuilder) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 14 with ClientRequestFilter

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

the class JAXRS20ClientServerBookTest method testGetSetEntityStreamLambda.

@Test
public void testGetSetEntityStreamLambda() {
    String address = "http://localhost:" + PORT + "/bookstore/entityecho";
    String entity = "BOOKSTORE";
    Client client = ClientBuilder.newClient();
    client.register((ClientRequestFilter) context -> {
        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 : Arrays(java.util.Arrays) Assert.assertNotSame(org.junit.Assert.assertNotSame) JAXRSUtils(org.apache.cxf.jaxrs.utils.JAXRSUtils) ParamConverter(javax.ws.rs.ext.ParamConverter) Future(java.util.concurrent.Future) MediaType(javax.ws.rs.core.MediaType) ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) AbstractBusClientServerTestBase(org.apache.cxf.testutil.common.AbstractBusClientServerTestBase) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) ClientRequestContext(javax.ws.rs.client.ClientRequestContext) Context(javax.ws.rs.core.Context) GenericEntity(javax.ws.rs.core.GenericEntity) Invocation(javax.ws.rs.client.Invocation) Entity(javax.ws.rs.client.Entity) JacksonJaxbJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider) BookInfo(org.apache.cxf.systest.jaxrs.BookStore.BookInfo) GenericType(javax.ws.rs.core.GenericType) Priority(javax.annotation.Priority) List(java.util.List) HttpHeaders(javax.ws.rs.core.HttpHeaders) WriterInterceptorContext(javax.ws.rs.ext.WriterInterceptorContext) Response(javax.ws.rs.core.Response) Type(java.lang.reflect.Type) Assert.assertFalse(org.junit.Assert.assertFalse) Annotation(java.lang.annotation.Annotation) WebApplicationException(javax.ws.rs.WebApplicationException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) QName(javax.xml.namespace.QName) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) Feature(javax.ws.rs.core.Feature) BeforeClass(org.junit.BeforeClass) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Client(javax.ws.rs.client.Client) ReaderInterceptorContext(javax.ws.rs.ext.ReaderInterceptorContext) ClientResponseFilter(javax.ws.rs.client.ClientResponseFilter) ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) Assert.assertSame(org.junit.Assert.assertSame) ClientBuilder(javax.ws.rs.client.ClientBuilder) FeatureContext(javax.ws.rs.core.FeatureContext) InvocationCallback(javax.ws.rs.client.InvocationCallback) WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Before(org.junit.Before) WebClient(org.apache.cxf.jaxrs.client.WebClient) Assert.assertNotNull(org.junit.Assert.assertNotNull) JAXBElement(javax.xml.bind.JAXBElement) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) UnknownHostException(java.net.UnknownHostException) JAXRSClientFactory(org.apache.cxf.jaxrs.client.JAXRSClientFactory) ExecutionException(java.util.concurrent.ExecutionException) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) URLEncoder(java.net.URLEncoder) Assert.assertNull(org.junit.Assert.assertNull) ClientResponseContext(javax.ws.rs.client.ClientResponseContext) ResourceContext(javax.ws.rs.container.ResourceContext) Holder(javax.xml.ws.Holder) WebTarget(javax.ws.rs.client.WebTarget) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 15 with ClientRequestFilter

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

the class JAXRS20ClientServerBookTest method testClientFiltersLocalResponseLambdas.

@Test
public void testClientFiltersLocalResponseLambdas() {
    String address = "http://localhost:" + PORT + "/bookstores";
    WebClient wc = WebClient.create(address, Arrays.asList((ClientRequestFilter) ctx -> {
        ctx.abortWith(Response.status(201).entity(ctx.getEntity()).type(MediaType.TEXT_XML_TYPE).build());
    }, (ClientResponseFilter) (reqContext, respContext) -> {
        MultivaluedMap<String, String> headers = respContext.getHeaders();
        headers.putSingle(HttpHeaders.LOCATION, "http://localhost/redirect");
    }));
    Book theBook = new Book("Echo", 123L);
    Response r = wc.post(theBook);
    assertEquals(201, r.getStatus());
    assertEquals("http://localhost/redirect", r.getHeaderString(HttpHeaders.LOCATION));
    Book responseBook = r.readEntity(Book.class);
    assertSame(theBook, responseBook);
}
Also used : ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) Response(javax.ws.rs.core.Response) ClientResponseFilter(javax.ws.rs.client.ClientResponseFilter) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Aggregations

ClientRequestFilter (javax.ws.rs.client.ClientRequestFilter)16 Client (javax.ws.rs.client.Client)9 ClientRequestContext (javax.ws.rs.client.ClientRequestContext)9 Response (javax.ws.rs.core.Response)9 Test (org.junit.Test)8 IOException (java.io.IOException)7 WebTarget (javax.ws.rs.client.WebTarget)7 ClientBuilder (javax.ws.rs.client.ClientBuilder)4 ClientResponseFilter (javax.ws.rs.client.ClientResponseFilter)4 Invocation (javax.ws.rs.client.Invocation)3 WebClient (org.apache.cxf.jaxrs.client.WebClient)3 Collections (java.util.Collections)2 List (java.util.List)2 ExecutorService (java.util.concurrent.ExecutorService)2 HostnameVerifier (javax.net.ssl.HostnameVerifier)2 SSLContext (javax.net.ssl.SSLContext)2 ClientResponseContext (javax.ws.rs.client.ClientResponseContext)2 Entity (javax.ws.rs.client.Entity)2 HttpHeaders (javax.ws.rs.core.HttpHeaders)2 ITHttpAsyncClient (brave.test.http.ITHttpAsyncClient)1