diff --git a/src/main/java/com/inteligr8/rs/Client.java b/src/main/java/com/inteligr8/rs/Client.java index 901b802..a607652 100644 --- a/src/main/java/com/inteligr8/rs/Client.java +++ b/src/main/java/com/inteligr8/rs/Client.java @@ -15,13 +15,21 @@ import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; */ 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() { - return this.getClient(null); + synchronized (this.sync) { + if (this.client == null) + this.client = this.buildClient(null); + } + + return this.client; } /** @@ -29,6 +37,18 @@ public abstract class Client { * @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 javax.ws.rs.client.Client buildClient(AuthorizationFilter authFilter) { JacksonJsonProvider provider = new JacksonJaxbJsonProvider(); if (this.getConfig().isWrapRootValueEnabled()) @@ -52,7 +72,8 @@ public abstract class Client { * @return A pre-configured JAX-RS target (client w/ base URL) with configured authorization. */ public final WebTarget getTarget() { - return this.getTarget(null); + return this.getClient() + .target(this.getConfig().getBaseUrl()); } /** @@ -60,8 +81,12 @@ public abstract class Client { * @return A pre-configured JAX-RS target (client w/ base URL) with the specified authorization. */ public WebTarget getTarget(AuthorizationFilter authFilter) { - return this.getClient(authFilter) - .target(this.getConfig().getBaseUrl()); + if (authFilter == null) { + return this.getTarget(); + } else { + return this.getClient(authFilter) + .target(this.getConfig().getBaseUrl()); + } } /**