Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
a28fc13161 | |||
52c7e3cf6d | |||
b4d7d2a154 | |||
95730a9440 | |||
bd7413bb48 | |||
75c518801f | |||
556909f2e9 | |||
6fd42a8daf | |||
aea7a0d035 | |||
d3f10e4c00 |
4
pom.xml
4
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.3</version>
|
<version>1.1.5</version>
|
||||||
<name>ReST API Client for Java</name>
|
<name>ReST API Client for Java</name>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
<junit.version>5.7.2</junit.version>
|
<junit.version>5.7.2</junit.version>
|
||||||
<spring.version>5.2.14.RELEASE</spring.version>
|
<spring.version>5.2.14.RELEASE</spring.version>
|
||||||
<jersey.version>2.34</jersey.version>
|
<jersey.version>2.35</jersey.version>
|
||||||
<cxf.version>3.3.2</cxf.version>
|
<cxf.version>3.3.2</cxf.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
@@ -15,13 +15,21 @@ import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
|
|||||||
*/
|
*/
|
||||||
public abstract class Client {
|
public abstract class Client {
|
||||||
|
|
||||||
|
private final Object sync = new Object();
|
||||||
|
private javax.ws.rs.client.Client client;
|
||||||
|
|
||||||
protected abstract ClientConfiguration getConfig();
|
protected abstract ClientConfiguration getConfig();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return A pre-configured JAX-RS client (no URL) with configured authorization.
|
* @return A pre-configured JAX-RS client (no URL) with configured authorization.
|
||||||
*/
|
*/
|
||||||
public final javax.ws.rs.client.Client getClient() {
|
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.
|
* @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) {
|
||||||
|
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();
|
JacksonJsonProvider provider = new JacksonJaxbJsonProvider();
|
||||||
|
|
||||||
if (this.getConfig().isWrapRootValueEnabled())
|
if (this.getConfig().isWrapRootValueEnabled())
|
||||||
@@ -37,7 +57,8 @@ public abstract class Client {
|
|||||||
provider.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
|
provider.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
|
||||||
|
|
||||||
ClientBuilder clientBuilder = ClientBuilder.newBuilder()
|
ClientBuilder clientBuilder = ClientBuilder.newBuilder()
|
||||||
.register(provider);
|
.register(provider)
|
||||||
|
.register(new LoggingFilter());
|
||||||
|
|
||||||
if (authFilter == null)
|
if (authFilter == null)
|
||||||
authFilter = this.getConfig().createAuthorizationFilter();
|
authFilter = this.getConfig().createAuthorizationFilter();
|
||||||
@@ -51,7 +72,8 @@ public abstract class Client {
|
|||||||
* @return A pre-configured JAX-RS target (client w/ base URL) with configured authorization.
|
* @return A pre-configured JAX-RS target (client w/ base URL) with configured authorization.
|
||||||
*/
|
*/
|
||||||
public final WebTarget getTarget() {
|
public final WebTarget getTarget() {
|
||||||
return this.getTarget(null);
|
return this.getClient()
|
||||||
|
.target(this.getConfig().getBaseUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -59,9 +81,13 @@ public abstract class Client {
|
|||||||
* @return A pre-configured JAX-RS target (client w/ base URL) with the specified authorization.
|
* @return A pre-configured JAX-RS target (client w/ base URL) with the specified authorization.
|
||||||
*/
|
*/
|
||||||
public WebTarget getTarget(AuthorizationFilter authFilter) {
|
public WebTarget getTarget(AuthorizationFilter authFilter) {
|
||||||
|
if (authFilter == null) {
|
||||||
|
return this.getTarget();
|
||||||
|
} else {
|
||||||
return this.getClient(authFilter)
|
return this.getClient(authFilter)
|
||||||
.target(this.getConfig().getBaseUrl());
|
.target(this.getConfig().getBaseUrl());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method retrieves a JAX-RS implementation of the specified API.
|
* This method retrieves a JAX-RS implementation of the specified API.
|
||||||
|
@@ -9,6 +9,7 @@ import org.apache.cxf.BusFactory;
|
|||||||
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
|
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
|
||||||
import org.apache.cxf.jaxrs.client.WebClient;
|
import org.apache.cxf.jaxrs.client.WebClient;
|
||||||
import org.apache.cxf.jaxrs.impl.RuntimeDelegateImpl;
|
import org.apache.cxf.jaxrs.impl.RuntimeDelegateImpl;
|
||||||
|
import org.apache.cxf.jaxrs.provider.MultipartProvider;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
@@ -66,6 +67,8 @@ public abstract class ClientCxfImpl extends Client implements InitializingBean {
|
|||||||
public WebClient getCxfClient(AuthorizationFilter authFilter) {
|
public WebClient getCxfClient(AuthorizationFilter authFilter) {
|
||||||
List<Object> providersAndFilters = new LinkedList<Object>();
|
List<Object> providersAndFilters = new LinkedList<Object>();
|
||||||
providersAndFilters.add(new JacksonJaxbJsonProvider());
|
providersAndFilters.add(new JacksonJaxbJsonProvider());
|
||||||
|
providersAndFilters.add(new CxfLoggingFilter());
|
||||||
|
providersAndFilters.add(new MultipartProvider());
|
||||||
|
|
||||||
if (authFilter == null)
|
if (authFilter == null)
|
||||||
authFilter = this.getConfig().createAuthorizationFilter();
|
authFilter = this.getConfig().createAuthorizationFilter();
|
||||||
|
30
src/main/java/com/inteligr8/rs/CxfLoggingFilter.java
Normal file
30
src/main/java/com/inteligr8/rs/CxfLoggingFilter.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
65
src/main/java/com/inteligr8/rs/LoggingFilter.java
Normal file
65
src/main/java/com/inteligr8/rs/LoggingFilter.java
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
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,6 +101,7 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
|||||||
form.param("client_secret", this.clientSecret);
|
form.param("client_secret", this.clientSecret);
|
||||||
if (this.scope != null)
|
if (this.scope != null)
|
||||||
form.param("scope", this.scope);
|
form.param("scope", this.scope);
|
||||||
|
this.extendRefreshTokenForm(form);
|
||||||
|
|
||||||
Entity<Form> entity = Entity.form(form);
|
Entity<Form> entity = Entity.form(form);
|
||||||
|
|
||||||
@@ -118,6 +119,7 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
|||||||
this.accessToken = (String)response.get("access_token");
|
this.accessToken = (String)response.get("access_token");
|
||||||
this.expiration = System.currentTimeMillis() + ((Number)response.get("expires_in")).longValue() * 1000L;
|
this.expiration = System.currentTimeMillis() + ((Number)response.get("expires_in")).longValue() * 1000L;
|
||||||
this.refreshToken = (String)response.get("refresh_token");
|
this.refreshToken = (String)response.get("refresh_token");
|
||||||
|
this.extendRefreshTokenResponse(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Form createRefreshForm() {
|
protected Form createRefreshForm() {
|
||||||
@@ -127,4 +129,10 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
|||||||
|
|
||||||
protected abstract Form createForm();
|
protected abstract Form createForm();
|
||||||
|
|
||||||
|
protected void extendRefreshTokenForm(Form form) {
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void extendRefreshTokenResponse(Map<String, Object> response) {
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user