118 lines
3.4 KiB
Java
118 lines
3.4 KiB
Java
package com.inteligr8.rs;
|
|
|
|
import javax.ws.rs.client.ClientBuilder;
|
|
import javax.ws.rs.client.WebTarget;
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
|
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
|
|
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
|
|
|
|
/**
|
|
* A class that provides pre-configured JAX-RS Client & WebTarget objects.
|
|
*
|
|
* @author brian@inteligr8.com
|
|
*/
|
|
public abstract class Client {
|
|
|
|
private final Object sync = new Object();
|
|
private javax.ws.rs.client.Client client;
|
|
|
|
protected abstract ClientConfiguration getConfig();
|
|
|
|
/**
|
|
* @return A pre-configured JAX-RS client (no URL) with configured authorization.
|
|
*/
|
|
public final javax.ws.rs.client.Client getClient() {
|
|
synchronized (this.sync) {
|
|
if (this.client == null)
|
|
this.client = this.buildClient((AuthorizationFilter)null);
|
|
}
|
|
|
|
return this.client;
|
|
}
|
|
|
|
/**
|
|
* @param authFilter A dynamic authorization filter.
|
|
* @return A pre-configured JAX-RS client (no URL) with the specified authorization.
|
|
*/
|
|
public javax.ws.rs.client.Client getClient(AuthorizationFilter authFilter) {
|
|
if (authFilter == null) {
|
|
return this.getClient();
|
|
} else {
|
|
return this.buildClient(authFilter);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param authFilter A dynamic authorization filter.
|
|
* @return A pre-configured JAX-RS client (no URL) with the specified authorization.
|
|
*/
|
|
public final javax.ws.rs.client.Client buildClient(AuthorizationFilter authFilter) {
|
|
JacksonJsonProvider provider = new JacksonJaxbJsonProvider();
|
|
|
|
if (this.getConfig().isWrapRootValueEnabled())
|
|
provider.enable(SerializationFeature.WRAP_ROOT_VALUE);
|
|
if (this.getConfig().isUnwrapRootValueEnabled())
|
|
provider.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
|
|
|
|
ClientBuilder clientBuilder = ClientBuilder.newBuilder()
|
|
.register(provider)
|
|
.register(new LoggingFilter());
|
|
|
|
if (authFilter == null)
|
|
authFilter = this.getConfig().createAuthorizationFilter();
|
|
if (authFilter != null)
|
|
clientBuilder.register(authFilter);
|
|
this.buildClient(clientBuilder);
|
|
|
|
return clientBuilder.build();
|
|
}
|
|
|
|
public void buildClient(ClientBuilder clientBuilder) {
|
|
// for extension purposes
|
|
}
|
|
|
|
/**
|
|
* @return A pre-configured JAX-RS target (client w/ base URL) with configured authorization.
|
|
*/
|
|
public final WebTarget getTarget() {
|
|
return this.getClient()
|
|
.target(this.getConfig().getBaseUrl());
|
|
}
|
|
|
|
/**
|
|
* @param authFilter A dynamic authorization filter.
|
|
* @return A pre-configured JAX-RS target (client w/ base URL) with the specified authorization.
|
|
*/
|
|
public WebTarget getTarget(AuthorizationFilter authFilter) {
|
|
if (authFilter == null) {
|
|
return this.getTarget();
|
|
} else {
|
|
return this.getClient(authFilter)
|
|
.target(this.getConfig().getBaseUrl());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* This method retrieves a JAX-RS implementation of the specified API.
|
|
*
|
|
* @param apiClass A JAX-RS annotation API class.
|
|
* @return An instance of the API class.
|
|
*/
|
|
public final <T> T getApi(Class<T> apiClass) {
|
|
return this.getApi(null, apiClass);
|
|
}
|
|
|
|
/**
|
|
* This method retrieves a JAX-RS implementation of the specified API with
|
|
* the specified authorization.
|
|
*
|
|
* @param authFilter A dynamic authorization filter.
|
|
* @param apiClass A JAX-RS annotation API class.
|
|
* @return An instance of the API class.
|
|
*/
|
|
public abstract <T> T getApi(AuthorizationFilter authFilter, Class<T> apiClass);
|
|
|
|
}
|