Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
28b2478a08 | |||
dc63abc272 | |||
dd5d0f504a | |||
34d03a91e5 | |||
2584b8d668 | |||
a756b05f6c | |||
853b4e66cf | |||
f2032df0f3 | |||
8d3f4559bd | |||
3f476bba5c | |||
6292dde23b | |||
659f71c72c | |||
52f77dddbb |
14
pom.xml
14
pom.xml
@@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.inteligr8</groupId>
|
||||
<artifactId>common-rest-api</artifactId>
|
||||
<version>1.1-SNAPSHOT</version>
|
||||
<version>1.1.2</version>
|
||||
<name>ReST API Client for Java</name>
|
||||
|
||||
<properties>
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
<junit.version>5.7.2</junit.version>
|
||||
<spring.version>5.2.14.RELEASE</spring.version>
|
||||
<jersey.version>2.35</jersey.version>
|
||||
<jersey.version>2.34</jersey.version>
|
||||
<cxf.version>3.3.2</cxf.version>
|
||||
</properties>
|
||||
|
||||
@@ -34,16 +34,6 @@
|
||||
<artifactId>jackson-jaxrs-json-provider</artifactId>
|
||||
<version>2.12.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
<version>2.12.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.media</groupId>
|
||||
<artifactId>jersey-media-multipart</artifactId>
|
||||
<version>${jersey.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
|
@@ -3,10 +3,7 @@ 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.
|
||||
@@ -15,21 +12,13 @@ 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() {
|
||||
synchronized (this.sync) {
|
||||
if (this.client == null)
|
||||
this.client = this.buildClient((AuthorizationFilter)null);
|
||||
}
|
||||
|
||||
return this.client;
|
||||
return this.getClient(null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,48 +26,22 @@ 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 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());
|
||||
.register(new JacksonJaxbJsonProvider());
|
||||
|
||||
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());
|
||||
return this.getTarget(null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,12 +49,8 @@ public abstract class Client {
|
||||
* @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());
|
||||
}
|
||||
return this.getClient(authFilter)
|
||||
.target(this.getConfig().getBaseUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -98,24 +98,6 @@ public interface ClientConfiguration {
|
||||
default String getOAuthPassword() {
|
||||
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
|
||||
|
@@ -66,15 +66,11 @@ public abstract class ClientCxfImpl extends Client implements InitializingBean {
|
||||
public WebClient getCxfClient(AuthorizationFilter authFilter) {
|
||||
List<Object> providersAndFilters = new LinkedList<Object>();
|
||||
providersAndFilters.add(new JacksonJaxbJsonProvider());
|
||||
providersAndFilters.add(new CxfLoggingFilter());
|
||||
providersAndFilters.add(new CxfMultipartProvider());
|
||||
|
||||
if (authFilter == null)
|
||||
authFilter = this.getConfig().createAuthorizationFilter();
|
||||
if (authFilter != null)
|
||||
providersAndFilters.add(authFilter);
|
||||
|
||||
this.addProvidersAndFilters(providersAndFilters);
|
||||
|
||||
// we can't use JAXRSClientFactory with a JAXRS client (duh!)
|
||||
// so we need to create a CXF client
|
||||
@@ -90,10 +86,6 @@ public abstract class ClientCxfImpl extends Client implements InitializingBean {
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
public void addProvidersAndFilters(List<Object> providersAndFilters) {
|
||||
// for extension purposes
|
||||
}
|
||||
|
||||
/**
|
||||
* This method retrieves a JAX-RS implementation of the specified API with
|
||||
|
@@ -1,12 +1,10 @@
|
||||
package com.inteligr8.rs;
|
||||
|
||||
import javax.ws.rs.client.ClientBuilder;
|
||||
import javax.ws.rs.ext.RuntimeDelegate;
|
||||
|
||||
import org.glassfish.jersey.client.ClientProperties;
|
||||
import org.glassfish.jersey.client.proxy.WebResourceFactory;
|
||||
import org.glassfish.jersey.internal.RuntimeDelegateImpl;
|
||||
import org.glassfish.jersey.media.multipart.MultiPartFeature;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
@@ -48,14 +46,20 @@ public abstract class ClientJerseyImpl extends Client implements InitializingBea
|
||||
this.logger.info("API Base URL: " + this.getConfig().getBaseUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param authFilter A post-configuration authorization filter.
|
||||
* @return A JAX-RS client.
|
||||
*/
|
||||
@Override
|
||||
public void buildClient(ClientBuilder clientBuilder) {
|
||||
clientBuilder.register(MultiPartFeature.class);
|
||||
|
||||
public javax.ws.rs.client.Client getClient(AuthorizationFilter authFilter) {
|
||||
javax.ws.rs.client.Client client = super.getClient(authFilter);
|
||||
|
||||
if (!this.getConfig().isPutBodyRequired()) {
|
||||
// allow PUT operations without body data
|
||||
clientBuilder.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
|
||||
client.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
|
||||
}
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -1,30 +0,0 @@
|
||||
package com.inteligr8.rs;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.ws.rs.client.ClientRequestContext;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
|
||||
import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public class CxfLoggingFilter extends LoggingFilter {
|
||||
|
||||
@Override
|
||||
protected void logUnhandledRequest(ClientRequestContext requestContext, Logger logger) throws IOException {
|
||||
if (MediaType.MULTIPART_FORM_DATA_TYPE.equals(requestContext.getMediaType())) {
|
||||
if (requestContext.getEntity() instanceof MultipartBody) {
|
||||
List<String> attIds = new LinkedList<>();
|
||||
for (Attachment att : ((MultipartBody)requestContext.getEntity()).getAllAttachments())
|
||||
attIds.add(att.getContentId());
|
||||
logger.trace("request: {} {}: {}", requestContext.getMethod(), requestContext.getUri(), attIds);
|
||||
} else {
|
||||
logger.trace("request: {} {}: failed to output form", requestContext.getMethod(), requestContext.getUri());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1,29 +0,0 @@
|
||||
package com.inteligr8.rs;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
|
||||
import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
|
||||
import org.apache.cxf.jaxrs.provider.MultipartProvider;
|
||||
|
||||
@Consumes(MediaType.MULTIPART_FORM_DATA)
|
||||
@Produces(MediaType.MULTIPART_FORM_DATA)
|
||||
@Provider
|
||||
public class CxfMultipartProvider extends MultipartProvider {
|
||||
|
||||
@Override
|
||||
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
|
||||
return MultipartBody.class.isAssignableFrom(type) || this.isReadable(type, genericType, annotations, mediaType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
|
||||
return MultipartBody.class.isAssignableFrom(type) || this.isWriteable(type, genericType, annotations, mediaType);
|
||||
}
|
||||
|
||||
}
|
@@ -1,65 +0,0 @@
|
||||
package com.inteligr8.rs;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.ws.rs.client.ClientRequestContext;
|
||||
import javax.ws.rs.client.ClientRequestFilter;
|
||||
import javax.ws.rs.client.ClientResponseContext;
|
||||
import javax.ws.rs.client.ClientResponseFilter;
|
||||
import javax.ws.rs.core.Form;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class LoggingFilter implements ClientRequestFilter, ClientResponseFilter {
|
||||
|
||||
private final Logger loggerRequest = LoggerFactory.getLogger("jaxrs.request");
|
||||
private final Logger loggerResponse = LoggerFactory.getLogger("jaxrs.response");
|
||||
|
||||
protected final ObjectMapper om = new ObjectMapper().setSerializationInclusion(Include.NON_NULL);
|
||||
|
||||
@Override
|
||||
public void filter(ClientRequestContext requestContext) throws IOException {
|
||||
if (this.loggerRequest.isTraceEnabled())
|
||||
this.logAnyRequest(requestContext, this.loggerRequest);
|
||||
}
|
||||
|
||||
protected void logAnyRequest(ClientRequestContext requestContext, Logger logger) throws IOException {
|
||||
if (MediaType.APPLICATION_JSON_TYPE.equals(requestContext.getMediaType())) {
|
||||
logger.trace("request: {} {}: {}", requestContext.getMethod(), requestContext.getUri(),
|
||||
this.om.writeValueAsString(requestContext.getEntity()));
|
||||
} else if (MediaType.APPLICATION_FORM_URLENCODED_TYPE.equals(requestContext.getMediaType())) {
|
||||
if (requestContext.getEntity() instanceof Form) {
|
||||
logger.trace("request: {} {}: {}", requestContext.getMethod(), requestContext.getUri(),
|
||||
((Form)requestContext.getEntity()).asMap());
|
||||
} else {
|
||||
this.loggerRequest.trace("request: {} {}: failed to output form", requestContext.getMethod(), requestContext.getUri());
|
||||
}
|
||||
} else {
|
||||
this.logUnhandledRequest(requestContext, logger);
|
||||
}
|
||||
}
|
||||
|
||||
protected void logUnhandledRequest(ClientRequestContext requestContext, Logger logger) throws IOException {
|
||||
if (requestContext.getMediaType() != null) {
|
||||
logger.trace("request '{}': {} {}", requestContext.getMediaType(), requestContext.getMethod(), requestContext.getUri());
|
||||
} else {
|
||||
logger.trace("request: {} {}", requestContext.getMethod(), requestContext.getUri());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
|
||||
if (this.loggerResponse.isTraceEnabled()) {
|
||||
this.loggerResponse.trace("response: {} ", this.om.writeValueAsString(responseContext.getStatus()));
|
||||
// WARN body is stream, which would need to be replaced after read
|
||||
this.loggerResponse.warn("response: NOT YET SUPPORTED");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -101,7 +101,6 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
||||
form.param("client_secret", this.clientSecret);
|
||||
if (this.scope != null)
|
||||
form.param("scope", this.scope);
|
||||
this.extendRefreshTokenForm(form);
|
||||
|
||||
Entity<Form> entity = Entity.form(form);
|
||||
|
||||
@@ -119,7 +118,6 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
||||
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);
|
||||
}
|
||||
|
||||
protected Form createRefreshForm() {
|
||||
@@ -128,11 +126,5 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
||||
}
|
||||
|
||||
protected abstract Form createForm();
|
||||
|
||||
protected void extendRefreshTokenForm(Form form) {
|
||||
}
|
||||
|
||||
protected void extendRefreshTokenResponse(Map<String, Object> response) {
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user