Merge branch 'develop' into develop-jersey

This commit is contained in:
Brian Long 2023-06-12 16:07:35 -04:00
commit fa96a8cfb8
8 changed files with 151 additions and 28 deletions

View File

@ -50,6 +50,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

@ -72,9 +72,6 @@ public abstract class Client {
this.getConfig().configureJacksonMapper(om);
JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider(om, JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS);
provider.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);
provider.disable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE);
this.getConfig().configureJacksonProvider(provider);
if (this.getConfig().isWrapRootValueEnabled())
@ -91,7 +88,6 @@ public abstract class Client {
if (authFilter != null)
clientBuilder.register(authFilter);
this.buildClient(clientBuilder);
this.getConfig().configureClient(clientBuilder);
return clientBuilder.build();

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