Compare commits
9 Commits
v2.0.6-cxf
...
v2.0.8-cxf
Author | SHA1 | Date | |
---|---|---|---|
8c9cbecd53 | |||
81198de0fb | |||
803d49e53e | |||
adad70d884 | |||
d29ccbef79 | |||
26c37ab737 | |||
4de7968e11 | |||
378563560b | |||
f84279a148 |
2
pom.xml
2
pom.xml
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>com.inteligr8</groupId>
|
||||
<artifactId>common-rest-client</artifactId>
|
||||
<version>2.0.6-cxf</version>
|
||||
<version>2.0.8-cxf</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>ReST API Client for Java</name>
|
||||
|
@@ -24,6 +24,8 @@ import org.apache.cxf.BusFactory;
|
||||
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
|
||||
import org.apache.cxf.jaxrs.client.WebClient;
|
||||
import org.apache.cxf.jaxrs.impl.RuntimeDelegateImpl;
|
||||
import org.apache.cxf.transport.http.HTTPConduit;
|
||||
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -129,6 +131,17 @@ public class ClientCxfImpl extends Client {
|
||||
// so we need to create a CXF client
|
||||
WebClient client = WebClient.create(this.getConfig().getBaseUrl(), providersAndFilters);
|
||||
|
||||
if (this.getConfig().getConnectTimeoutInMillis() != null || this.getConfig().getResponseTimeoutInMillis() != null) {
|
||||
HTTPConduit conduit = client.getConfiguration().getHttpConduit();
|
||||
HTTPClientPolicy policy = conduit.getClient();
|
||||
if (policy == null)
|
||||
conduit.setClient(policy = new HTTPClientPolicy());
|
||||
if (this.getConfig().getConnectTimeoutInMillis() != null)
|
||||
policy.setConnectionTimeout(this.getConfig().getConnectTimeoutInMillis());
|
||||
if (this.getConfig().getResponseTimeoutInMillis() != null)
|
||||
policy.setReceiveTimeout(this.getConfig().getResponseTimeoutInMillis());
|
||||
}
|
||||
|
||||
if (!this.getConfig().isDefaultBusEnabled()) {
|
||||
// Some applications (like ACS) add interceptors to the default bus
|
||||
// those interceptors may treat all messages as SOAP messages (like ACS), resulting in ClassCastExceptions
|
||||
|
@@ -14,6 +14,8 @@
|
||||
*/
|
||||
package com.inteligr8.rs;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.ws.rs.client.ClientBuilder;
|
||||
import javax.ws.rs.client.WebTarget;
|
||||
|
||||
@@ -83,6 +85,11 @@ public abstract class Client {
|
||||
.register(provider)
|
||||
.register(new LoggingFilter());
|
||||
|
||||
if (this.getConfig().getConnectTimeoutInMillis() != null)
|
||||
clientBuilder.connectTimeout(this.getConfig().getConnectTimeoutInMillis(), TimeUnit.MILLISECONDS);
|
||||
if (this.getConfig().getResponseTimeoutInMillis() != null)
|
||||
clientBuilder.readTimeout(this.getConfig().getResponseTimeoutInMillis(), TimeUnit.MILLISECONDS);
|
||||
|
||||
if (authFilter == null)
|
||||
authFilter = this.getConfig().createAuthorizationFilter();
|
||||
if (authFilter != null)
|
||||
|
@@ -120,6 +120,16 @@ public interface ClientConfiguration {
|
||||
|
||||
|
||||
|
||||
default Integer getConnectTimeoutInMillis() {
|
||||
return null;
|
||||
}
|
||||
|
||||
default Integer getResponseTimeoutInMillis() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return true to enable Jackson UNWRAP_ROOT_VALUE feature; false otherwise.
|
||||
*/
|
||||
|
@@ -48,6 +48,7 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
||||
private String accessToken;
|
||||
private long expiration;
|
||||
private String refreshToken;
|
||||
private Long refreshTokenExpiration;
|
||||
|
||||
/**
|
||||
* This constructor creates an OAuth-based authorization filter using the
|
||||
@@ -99,8 +100,29 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
||||
*/
|
||||
@Override
|
||||
public void filter(ClientRequestContext requestContext) {
|
||||
if (this.accessToken == null || System.currentTimeMillis() > this.expiration)
|
||||
if (this.accessToken == null) {
|
||||
this.requestToken();
|
||||
} else if (System.currentTimeMillis() >= this.expiration) {
|
||||
this.logger.trace("Access token expired; retrieving new one with refresh token");
|
||||
|
||||
if (this.refreshTokenExpiration != null && System.currentTimeMillis() >= this.refreshTokenExpiration.longValue()) {
|
||||
this.logger.debug("Refresh token expired; performing full authentication");
|
||||
this.refreshToken = null;
|
||||
this.requestToken();
|
||||
} else {
|
||||
try {
|
||||
this.requestToken();
|
||||
} catch (WebApplicationException wae) {
|
||||
if (wae.getResponse().getStatusInfo().getFamily() == Family.CLIENT_ERROR) {
|
||||
this.logger.debug("Received OAuth response {} using refresh token; performing full authentication", wae.getResponse().getStatus());
|
||||
this.refreshToken = null;
|
||||
this.requestToken();
|
||||
} else {
|
||||
throw wae;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION, "Bearer " + this.accessToken);
|
||||
}
|
||||
@@ -140,6 +162,8 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
||||
.build();
|
||||
WebTarget target = client.target(this.tokenUrl);
|
||||
|
||||
long requestSendTime = System.currentTimeMillis();
|
||||
|
||||
Response response = target.request().post(entity);
|
||||
|
||||
this.logger.debug("Received OAuth response: {}", response.getStatus());
|
||||
@@ -160,8 +184,10 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
||||
}
|
||||
|
||||
this.accessToken = (String)responseMap.get("access_token");
|
||||
this.expiration = System.currentTimeMillis() + ((Number)responseMap.get("expires_in")).longValue() * 1000L;
|
||||
this.expiration = requestSendTime + ((Number)responseMap.get("expires_in")).longValue() * 1000L;
|
||||
this.refreshToken = (String)responseMap.get("refresh_token");
|
||||
if (responseMap.containsKey("refresh_token_expires_in"))
|
||||
this.refreshTokenExpiration = requestSendTime + ((Number)responseMap.get("refresh_token_expires_in")).longValue() * 1000L;
|
||||
}
|
||||
|
||||
protected Form createRefreshForm() {
|
||||
|
Reference in New Issue
Block a user