Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
b3bc04467b | |||
88c8657a34 | |||
2c4f4f7285 | |||
28b2478a08 | |||
dc63abc272 | |||
23c76c4bcf | |||
dd5d0f504a | |||
eaa55fa48e |
7
pom.xml
7
pom.xml
@@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.inteligr8</groupId>
|
<groupId>com.inteligr8</groupId>
|
||||||
<artifactId>common-rest-api</artifactId>
|
<artifactId>common-rest-api</artifactId>
|
||||||
<version>1.1.1</version>
|
<version>1.1.3</version>
|
||||||
<name>ReST API Client for Java</name>
|
<name>ReST API Client for Java</name>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
@@ -34,6 +34,11 @@
|
|||||||
<artifactId>jackson-jaxrs-json-provider</artifactId>
|
<artifactId>jackson-jaxrs-json-provider</artifactId>
|
||||||
<version>2.12.2</version>
|
<version>2.12.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||||
|
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||||
|
<version>2.12.2</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
<artifactId>slf4j-api</artifactId>
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
@@ -1,7 +1,5 @@
|
|||||||
package com.inteligr8.rs;
|
package com.inteligr8.rs;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
|
|
||||||
import javax.ws.rs.client.ClientRequestContext;
|
import javax.ws.rs.client.ClientRequestContext;
|
||||||
import javax.ws.rs.core.HttpHeaders;
|
import javax.ws.rs.core.HttpHeaders;
|
||||||
|
|
||||||
@@ -30,7 +28,6 @@ public class BearerTokenAuthorizationFilter implements AuthorizationFilter {
|
|||||||
* This method applies the 'Authorization' header to the {@link ClientRequestContext}.
|
* This method applies the 'Authorization' header to the {@link ClientRequestContext}.
|
||||||
*
|
*
|
||||||
* @param requestContext A request context.
|
* @param requestContext A request context.
|
||||||
* @throws UnsupportedEncodingException The 'utf-8' encoding is not supported.
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void filter(ClientRequestContext requestContext) {
|
public void filter(ClientRequestContext requestContext) {
|
||||||
|
@@ -3,7 +3,10 @@ package com.inteligr8.rs;
|
|||||||
import javax.ws.rs.client.ClientBuilder;
|
import javax.ws.rs.client.ClientBuilder;
|
||||||
import javax.ws.rs.client.WebTarget;
|
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.JacksonJaxbJsonProvider;
|
||||||
|
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class that provides pre-configured JAX-RS Client & WebTarget objects.
|
* A class that provides pre-configured JAX-RS Client & WebTarget objects.
|
||||||
@@ -26,8 +29,15 @@ public abstract class Client {
|
|||||||
* @return A pre-configured JAX-RS client (no URL) with the specified authorization.
|
* @return A pre-configured JAX-RS client (no URL) with the specified authorization.
|
||||||
*/
|
*/
|
||||||
public javax.ws.rs.client.Client getClient(AuthorizationFilter authFilter) {
|
public javax.ws.rs.client.Client getClient(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()
|
ClientBuilder clientBuilder = ClientBuilder.newBuilder()
|
||||||
.register(new JacksonJaxbJsonProvider());
|
.register(provider);
|
||||||
|
|
||||||
if (authFilter == null)
|
if (authFilter == null)
|
||||||
authFilter = this.getConfig().createAuthorizationFilter();
|
authFilter = this.getConfig().createAuthorizationFilter();
|
||||||
|
@@ -29,6 +29,20 @@ public interface ClientConfiguration {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The client ID for Client Enforcement authentication.
|
||||||
|
*/
|
||||||
|
default String getClientId() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The corresponding client secret for the client ID in Client Enforcement authentication.
|
||||||
|
*/
|
||||||
|
default String getClientSecret() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return The token for BEARER authorization.
|
* @return The token for BEARER authorization.
|
||||||
*/
|
*/
|
||||||
@@ -47,14 +61,14 @@ public interface ClientConfiguration {
|
|||||||
* @return The client ID provided by the OAuth IdP administrator.
|
* @return The client ID provided by the OAuth IdP administrator.
|
||||||
*/
|
*/
|
||||||
default String getOAuthClientId() {
|
default String getOAuthClientId() {
|
||||||
return null;
|
return this.getClientId();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return The corresponding client secret for the client ID provided by the OAuth IdP administrator.
|
* @return The corresponding client secret for the client ID provided by the OAuth IdP administrator.
|
||||||
*/
|
*/
|
||||||
default String getOAuthClientSecret() {
|
default String getOAuthClientSecret() {
|
||||||
return null;
|
return this.getClientSecret();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -84,6 +98,24 @@ public interface ClientConfiguration {
|
|||||||
default String getOAuthPassword() {
|
default String getOAuthPassword() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return true to enable Jackson UNWRAP_ROOT_VALUE feature; false otherwise.
|
||||||
|
*/
|
||||||
|
default boolean isUnwrapRootValueEnabled() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return true to enable Jackson WRAP_ROOT_VALUE feature; false otherwise.
|
||||||
|
*/
|
||||||
|
default boolean isWrapRootValueEnabled() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method creates an authorization filter based on the configuration
|
* This method creates an authorization filter based on the configuration
|
||||||
@@ -115,6 +147,8 @@ public interface ClientConfiguration {
|
|||||||
return new OAuthClientCredentialAuthorizationFilter(this.getOAuthTokenUrl(),
|
return new OAuthClientCredentialAuthorizationFilter(this.getOAuthTokenUrl(),
|
||||||
this.getOAuthClientId(), this.getOAuthClientSecret());
|
this.getOAuthClientId(), this.getOAuthClientSecret());
|
||||||
}
|
}
|
||||||
|
} else if (this.getClientId() != null) {
|
||||||
|
return new ClientEnforcementAuthorizationFilter(this.getClientId(), this.getClientSecret());
|
||||||
} else if (this.getBasicAuthUsername() != null) {
|
} else if (this.getBasicAuthUsername() != null) {
|
||||||
return new BasicAuthorizationFilter(this.getBasicAuthUsername(), this.getBasicAuthPassword());
|
return new BasicAuthorizationFilter(this.getBasicAuthUsername(), this.getBasicAuthPassword());
|
||||||
} else {
|
} else {
|
||||||
|
@@ -13,7 +13,7 @@ public interface ClientCxfConfiguration extends ClientConfiguration {
|
|||||||
* wreck havoc on your implementation. This method allows you to
|
* wreck havoc on your implementation. This method allows you to
|
||||||
* explicitly by-pass the default bus.
|
* explicitly by-pass the default bus.
|
||||||
*
|
*
|
||||||
* @see https://cxf.apache.org/docs/bus-configuration.html
|
* See https://cxf.apache.org/docs/bus-configuration.html.
|
||||||
*
|
*
|
||||||
* @return true to use the default bus; false otherwise.
|
* @return true to use the default bus; false otherwise.
|
||||||
*/
|
*/
|
||||||
|
@@ -0,0 +1,49 @@
|
|||||||
|
package com.inteligr8.rs;
|
||||||
|
|
||||||
|
import javax.ws.rs.client.ClientRequestContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is the base for implementations of client authorization similar
|
||||||
|
* to OAuth-based flows.
|
||||||
|
*
|
||||||
|
* @author brian@inteligr8.com
|
||||||
|
*/
|
||||||
|
public class ClientEnforcementAuthorizationFilter implements AuthorizationFilter {
|
||||||
|
|
||||||
|
private final String clientId;
|
||||||
|
private final String clientSecret;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This constructor creates a client authorization filter using a client ID
|
||||||
|
* registered with the endpoint.
|
||||||
|
*
|
||||||
|
* @param clientId An endpoint provided client ID.
|
||||||
|
*/
|
||||||
|
public ClientEnforcementAuthorizationFilter(String clientId) {
|
||||||
|
this(clientId, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This constructor creates a client authorization filter using a client ID
|
||||||
|
* registered with the endpoint, and the corresponding client secret.
|
||||||
|
*
|
||||||
|
* @param clientId An endpoint provided client ID.
|
||||||
|
* @param clientSecret A secret corresponding to the client ID.
|
||||||
|
*/
|
||||||
|
public ClientEnforcementAuthorizationFilter(String clientId, String clientSecret) {
|
||||||
|
this.clientId = clientId;
|
||||||
|
this.clientSecret = clientSecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method applies the client headers to the {@link ClientRequestContext}.
|
||||||
|
*
|
||||||
|
* @param requestContext A request context.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void filter(ClientRequestContext requestContext) {
|
||||||
|
requestContext.getHeaders().add("client_id", this.clientId);
|
||||||
|
requestContext.getHeaders().add("client_secret", this.clientSecret);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -26,7 +26,11 @@ public abstract class ClientJerseyImpl extends Client implements InitializingBea
|
|||||||
public void afterPropertiesSet() {
|
public void afterPropertiesSet() {
|
||||||
this.register();
|
this.register();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method registers the Jersey library as the default provider for the
|
||||||
|
* JAX-RS specification.
|
||||||
|
*/
|
||||||
public void register() {
|
public void register() {
|
||||||
if (RuntimeDelegate.getInstance() == null) {
|
if (RuntimeDelegate.getInstance() == null) {
|
||||||
this.logger.info("Setting JAX-RS runtime delegate to the Jersey library");
|
this.logger.info("Setting JAX-RS runtime delegate to the Jersey library");
|
||||||
|
@@ -27,14 +27,42 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
|||||||
private long expiration;
|
private long expiration;
|
||||||
private String refreshToken;
|
private String refreshToken;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This constructor creates an OAuth-based authorization filter using the
|
||||||
|
* OAuth identity provider token URL and a client ID registered with the
|
||||||
|
* same OAuth identity provider.
|
||||||
|
*
|
||||||
|
* @param tokenUrl An OAuth identity provider token URL.
|
||||||
|
* @param clientId An OAuth identity provider client ID.
|
||||||
|
*/
|
||||||
public OAuthAuthorizationFilter(String tokenUrl, String clientId) {
|
public OAuthAuthorizationFilter(String tokenUrl, String clientId) {
|
||||||
this(tokenUrl, clientId, null);
|
this(tokenUrl, clientId, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This constructor creates an OAuth-based authorization filter using the
|
||||||
|
* OAuth identity provider token URL, client ID registered with the
|
||||||
|
* same OAuth identity provider, and the corresponding client secret.
|
||||||
|
*
|
||||||
|
* @param tokenUrl An OAuth identity provider token URL.
|
||||||
|
* @param clientId An OAuth identity provider client ID.
|
||||||
|
* @param clientSecret A secret corresponding to the client ID.
|
||||||
|
*/
|
||||||
public OAuthAuthorizationFilter(String tokenUrl, String clientId, String clientSecret) {
|
public OAuthAuthorizationFilter(String tokenUrl, String clientId, String clientSecret) {
|
||||||
this(tokenUrl, clientId, clientSecret, null);
|
this(tokenUrl, clientId, clientSecret, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This constructor creates an OAuth-based authorization filter using the
|
||||||
|
* OAuth identity provider token URL, client ID registered with the
|
||||||
|
* same OAuth identity provider, the corresponding client secret, and OAuth
|
||||||
|
* scope.
|
||||||
|
*
|
||||||
|
* @param tokenUrl An OAuth identity provider token URL.
|
||||||
|
* @param clientId An OAuth identity provider client ID.
|
||||||
|
* @param clientSecret A secret corresponding to the client ID.
|
||||||
|
* @param scope An OAuth scope.
|
||||||
|
*/
|
||||||
public OAuthAuthorizationFilter(String tokenUrl, String clientId, String clientSecret, String scope) {
|
public OAuthAuthorizationFilter(String tokenUrl, String clientId, String clientSecret, String scope) {
|
||||||
this.tokenUrl = tokenUrl;
|
this.tokenUrl = tokenUrl;
|
||||||
this.clientId = clientId;
|
this.clientId = clientId;
|
||||||
|
Reference in New Issue
Block a user