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;
}
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);
}
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;
}
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));
}
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);
}
Aggregations