Compare commits

...

44 Commits

Author SHA1 Message Date
f10fd39e97 v2.0.6 pom 2023-06-12 16:10:25 -04:00
eaed628f00 Merge branch 'develop' into stable 2023-06-12 16:08:46 -04:00
44e82b3a6b fixed build issue without jersey/cxf 2023-06-12 16:05:37 -04:00
ab920aedd7 added OAuth filter logging 2023-06-12 16:02:33 -04:00
ab6b0d8da7 refacter OAuth filter error handling 2023-06-12 16:02:19 -04:00
c25988fc8f removing errand enum enablement 2023-05-30 18:23:39 -04:00
cd278bcc81 v2.0.4 pom 2023-05-30 18:00:33 -04:00
b64c408029 Merge branch 'develop' into stable 2023-05-30 18:00:06 -04:00
9a9a958913 allowing client ext; not just config ext 2023-05-30 17:58:38 -04:00
37e8b63179 added default enum deserialization 2023-05-30 17:54:29 -04:00
255a9c8e85 enabling more extensible configuration 2023-05-30 17:53:10 -04:00
ec6e7cc15d v2.0.3 pom 2023-05-30 12:44:41 -04:00
2451c634b7 Merge branch 'develop' into stable 2023-05-30 12:44:21 -04:00
bcb7d4a7ed adding JavaTimeModule 2023-05-30 12:43:50 -04:00
ed00346e71 v2.0.2 pom 2023-05-29 10:15:38 -04:00
c001576b65 Merge branch 'develop' into stable 2023-05-29 10:11:16 -04:00
6cb7afd9f1 Merge branch 'develop' into stable 2022-10-02 17:56:01 -04:00
5df5388bd2 Merge branch 'develop' into stable 2022-10-02 17:52:21 -04:00
5cda622082 v2.0.1 poms 2022-10-01 21:26:18 -04:00
e1191abaaa Merge branch 'develop' into stable 2022-10-01 21:25:26 -04:00
67ea4b72b3 POM release to v2.0.0 2022-10-01 00:53:17 -04:00
a574154c20 Merge branch 'develop' into stable 2022-10-01 00:52:53 -04:00
b1b19d8cb0 Merge branch 'develop' into stable 2022-05-02 18:33:27 -04:00
160766e0ef v1.1.6 pom 2022-04-07 23:36:39 -04:00
e167ac43cc Merge branch 'develop' into stable 2022-04-07 23:36:09 -04:00
a28fc13161 v1.1.5 pom 2022-03-08 13:32:45 -05:00
52c7e3cf6d Merge branch 'develop' into stable 2022-03-08 13:29:28 -05:00
bd7413bb48 v1.1.4 pom 2022-02-02 13:34:16 -05:00
75c518801f Merge branch 'develop' into stable 2022-02-02 13:33:47 -05:00
b3bc04467b v1.1.3 pom 2021-12-21 17:13:26 -05:00
88c8657a34 Merge branch 'develop' into stable 2021-12-21 17:12:58 -05:00
28b2478a08 v1.1.2 pom 2021-12-02 16:25:05 -05:00
dc63abc272 Merge branch 'develop' into stable 2021-12-02 16:20:39 -05:00
dd5d0f504a Merge branch 'develop' into stable 2021-10-27 14:54:54 -04:00
34d03a91e5 Merge branch 'develop' into stable 2021-09-03 12:42:25 -04:00
2584b8d668 v1.1.1 pom 2021-09-03 11:38:52 -04:00
a756b05f6c Merge branch 'develop' into stable 2021-09-03 11:38:22 -04:00
853b4e66cf Merge branch 'develop' into stable 2021-09-01 14:43:43 -04:00
f2032df0f3 Merge branch 'develop' into stable 2021-08-31 13:22:01 -04:00
8d3f4559bd v1.0.2 pom 2021-07-22 15:23:15 -04:00
3f476bba5c Merge branch 'develop' into stable 2021-07-22 15:22:49 -04:00
6292dde23b v1.0.1 pom 2021-07-07 11:07:04 -04:00
659f71c72c Merge branch 'develop' into stable 2021-07-07 11:06:35 -04:00
52f77dddbb v1.0.0 pom 2021-06-16 15:59:04 -04:00
9 changed files with 194 additions and 35 deletions

View File

@@ -6,7 +6,7 @@
<groupId>com.inteligr8</groupId>
<artifactId>common-rest-client</artifactId>
<version>2.0-SNAPSHOT</version>
<version>2.0.6</version>
<packaging>jar</packaging>
<name>ReST API Client for Java</name>
@@ -49,6 +49,11 @@
</properties>
<dependencies>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>

View File

@@ -18,9 +18,10 @@ import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
/**
* A class that provides pre-configured JAX-RS Client &amp; WebTarget objects.
@@ -66,8 +67,13 @@ public abstract class Client {
* @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();
ObjectMapper om = new ObjectMapper();
om.registerModules(new JavaTimeModule());
this.getConfig().configureJacksonMapper(om);
JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider(om, JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS);
this.getConfig().configureJacksonProvider(provider);
if (this.getConfig().isWrapRootValueEnabled())
provider.enable(SerializationFeature.WRAP_ROOT_VALUE);
if (this.getConfig().isUnwrapRootValueEnabled())
@@ -82,16 +88,17 @@ public abstract class Client {
if (authFilter != null)
clientBuilder.register(authFilter);
this.buildClient(clientBuilder);
this.getConfig().configureClient(clientBuilder);
return clientBuilder.build();
}
/**
* @param clientBuilder A client builder
*/
public void buildClient(ClientBuilder clientBuilder) {
// for extension purposes
}
/**
* @param clientBuilder A client builder
*/
public void buildClient(ClientBuilder clientBuilder) {
// for extension purposes
}
/**
* @return A pre-configured JAX-RS target (client w/ base URL) with configured authorization.

View File

@@ -16,6 +16,11 @@ package com.inteligr8.rs;
import java.net.URI;
import javax.ws.rs.client.ClientBuilder;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
/**
* This interface defines the configurable parameters of the clients; primarily
* their default authentication and authorization.
@@ -128,6 +133,18 @@ public interface ClientConfiguration {
default boolean isWrapRootValueEnabled() {
return false;
}
/**
* @param mapper A Jackson object mapper to configure.
*/
default void configureJacksonMapper(ObjectMapper mapper) {
}
/**
* @param provider A Jackson JAX-RS provider to configure.
*/
default void configureJacksonProvider(JacksonJaxbJsonProvider provider) {
}
@@ -169,5 +186,13 @@ public interface ClientConfiguration {
return null;
}
}
/**
* A Jackson provider, logging filter, and authentication filter are already registered.
*
* @param clientBuilder A JAX-RS client builder to configure.
*/
default void configureClient(ClientBuilder clientBuilder) {
}
}

View File

@@ -0,0 +1,83 @@
/*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.inteligr8.rs;
import javax.annotation.PostConstruct;
import javax.ws.rs.client.ClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* A class that provides pre-configured JAX-RS Client &amp; WebTarget objects
* for Jersey.
*
* @author brian@inteligr8.com
*/
@Component("client.jaxrs")
public class ClientImpl extends Client {
private final Logger logger = LoggerFactory.getLogger(ClientImpl.class);
private ClientConfiguration config;
/**
* This constructor is for Spring or POJO use.
* @param config The client configuration.
*/
@Autowired
public ClientImpl(ClientConfiguration config) {
this.config = config;
}
/**
* This method registers the Jersey library as the default provider for the
* JAX-RS specification.
*/
@PostConstruct
public void register() {
this.logger.info("API Base URL: {}", this.getConfig().getBaseUrl());
}
/**
* @param clientBuilder A client builder.
*/
@Override
public void buildClient(ClientBuilder clientBuilder) {
}
/**
* @return The client configuration.
*/
public ClientConfiguration getConfig() {
return this.config;
}
/**
* 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.
*/
@Override
public <T> T getApi(AuthorizationFilter authFilter, Class<T> apiClass) {
throw new UnsupportedOperationException();
}
}

View File

@@ -58,7 +58,7 @@ public class LoggingFilter implements ClientRequestFilter, ClientResponseFilter
logger.trace("request: {} {}: {}", requestContext.getMethod(), requestContext.getUri(),
((Form)requestContext.getEntity()).asMap());
} else {
this.loggerRequest.trace("request: {} {}: failed to output form", requestContext.getMethod(), requestContext.getUri());
logger.trace("request: {} {}: failed to output form", requestContext.getMethod(), requestContext.getUri());
}
} else {
this.logUnhandledRequest(requestContext, logger);

View File

@@ -74,11 +74,15 @@ public class OAuthAuthorizationCodeAuthorizationFilter extends OAuthAuthorizatio
@Override
protected Form createForm() {
Form form = new Form().param("grant_type", "authorization_code")
.param("code", this.code);
Form form = new Form().param("grant_type", "authorization_code");
if (this.redirectUri != null)
form.param("redirect_uri", this.redirectUri.toString());
return form;
}
@Override
protected void extendFormSensitive(Form form) {
form.param("code", this.code);
}
}

View File

@@ -17,12 +17,18 @@ package com.inteligr8.rs;
import java.util.Map;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status.Family;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
@@ -33,6 +39,8 @@ import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
*/
public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final String tokenUrl;
private final String clientId;
private final String clientSecret;
@@ -111,29 +119,49 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
}
form.param("client_id", this.clientId);
if (this.clientSecret != null)
form.param("client_secret", this.clientSecret);
if (this.scope != null)
form.param("scope", this.scope);
this.extendRefreshTokenForm(form);
this.logger.trace("Sending OAuth request: {}", form);
if (this.refreshToken != null) {
this.extendRefreshFormSensitive(form);
} else {
this.extendFormSensitive(form);
}
if (this.clientSecret != null)
form.param("client_secret", this.clientSecret);
Entity<Form> entity = Entity.form(form);
WebTarget target = ClientBuilder.newBuilder()
.register(new JacksonJaxbJsonProvider())
.build()
.target(this.tokenUrl);
Client client = ClientBuilder.newBuilder()
.register(new JacksonJaxbJsonProvider())
.build();
WebTarget target = client.target(this.tokenUrl);
@SuppressWarnings("unchecked")
Map<String, Object> response = target.request().post(entity, Map.class);
Response response = target.request().post(entity);
if (response.containsKey("error"))
throw new WebApplicationException((String)response.get("error"), 400);
this.logger.debug("Received OAuth response: {}", response.getStatus());
@SuppressWarnings("unchecked")
Map<String, Object> responseMap = response.readEntity(Map.class);
this.logger.trace("Received OAuth response: {}", responseMap);
this.accessToken = (String)response.get("access_token");
this.expiration = System.currentTimeMillis() + ((Number)response.get("expires_in")).longValue() * 1000L;
this.refreshToken = (String)response.get("refresh_token");
this.extendRefreshTokenResponse(response);
if (response.getStatusInfo().getFamily() != Family.SUCCESSFUL) {
String code = (String) responseMap.get("error");
if (code != null) {
String description = (String) responseMap.get("error_description");
throw new WebApplicationException(code + ": " + description, response.getStatus());
} else {
throw new WebApplicationException(response);
}
}
this.accessToken = (String)responseMap.get("access_token");
this.expiration = System.currentTimeMillis() + ((Number)responseMap.get("expires_in")).longValue() * 1000L;
this.refreshToken = (String)responseMap.get("refresh_token");
}
protected Form createRefreshForm() {
@@ -143,10 +171,9 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
protected abstract Form createForm();
protected void extendRefreshTokenForm(Form form) {
}
protected void extendRefreshTokenResponse(Map<String, Object> response) {
protected void extendRefreshFormSensitive(Form form) {
}
protected abstract void extendFormSensitive(Form form);
}

View File

@@ -37,5 +37,9 @@ public class OAuthClientCredentialAuthorizationFilter extends OAuthAuthorization
protected Form createForm() {
return new Form().param("grant_type", "client_credentials");
}
@Override
protected void extendFormSensitive(Form form) {
}
}

View File

@@ -53,8 +53,12 @@ public class OAuthPasswordGrantAuthorizationFilter extends OAuthAuthorizationFil
@Override
protected Form createForm() {
return new Form().param("grant_type", "password")
.param("username", this.username)
.param("password", this.password);
.param("username", this.username);
}
@Override
protected void extendFormSensitive(Form form) {
form.param("password", this.password);
}
}