Merge branch 'develop' into develop-jersey
This commit is contained in:
commit
fa96a8cfb8
5
pom.xml
5
pom.xml
@ -50,6 +50,11 @@
|
|||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.annotation</groupId>
|
||||||
|
<artifactId>javax.annotation-api</artifactId>
|
||||||
|
<version>1.3.2</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-context</artifactId>
|
<artifactId>spring-context</artifactId>
|
||||||
|
@ -72,9 +72,6 @@ public abstract class Client {
|
|||||||
this.getConfig().configureJacksonMapper(om);
|
this.getConfig().configureJacksonMapper(om);
|
||||||
|
|
||||||
JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider(om, JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS);
|
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);
|
this.getConfig().configureJacksonProvider(provider);
|
||||||
|
|
||||||
if (this.getConfig().isWrapRootValueEnabled())
|
if (this.getConfig().isWrapRootValueEnabled())
|
||||||
@ -91,7 +88,6 @@ public abstract class Client {
|
|||||||
if (authFilter != null)
|
if (authFilter != null)
|
||||||
clientBuilder.register(authFilter);
|
clientBuilder.register(authFilter);
|
||||||
this.buildClient(clientBuilder);
|
this.buildClient(clientBuilder);
|
||||||
|
|
||||||
this.getConfig().configureClient(clientBuilder);
|
this.getConfig().configureClient(clientBuilder);
|
||||||
|
|
||||||
return clientBuilder.build();
|
return clientBuilder.build();
|
||||||
|
83
src/main/java/com/inteligr8/rs/ClientImpl.java
Normal file
83
src/main/java/com/inteligr8/rs/ClientImpl.java
Normal 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 & 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -58,7 +58,7 @@ public class LoggingFilter implements ClientRequestFilter, ClientResponseFilter
|
|||||||
logger.trace("request: {} {}: {}", requestContext.getMethod(), requestContext.getUri(),
|
logger.trace("request: {} {}: {}", requestContext.getMethod(), requestContext.getUri(),
|
||||||
((Form)requestContext.getEntity()).asMap());
|
((Form)requestContext.getEntity()).asMap());
|
||||||
} else {
|
} 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 {
|
} else {
|
||||||
this.logUnhandledRequest(requestContext, logger);
|
this.logUnhandledRequest(requestContext, logger);
|
||||||
|
@ -74,11 +74,15 @@ public class OAuthAuthorizationCodeAuthorizationFilter extends OAuthAuthorizatio
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Form createForm() {
|
protected Form createForm() {
|
||||||
Form form = new Form().param("grant_type", "authorization_code")
|
Form form = new Form().param("grant_type", "authorization_code");
|
||||||
.param("code", this.code);
|
|
||||||
if (this.redirectUri != null)
|
if (this.redirectUri != null)
|
||||||
form.param("redirect_uri", this.redirectUri.toString());
|
form.param("redirect_uri", this.redirectUri.toString());
|
||||||
return form;
|
return form;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void extendFormSensitive(Form form) {
|
||||||
|
form.param("code", this.code);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,12 +17,18 @@ package com.inteligr8.rs;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.ws.rs.WebApplicationException;
|
import javax.ws.rs.WebApplicationException;
|
||||||
|
import javax.ws.rs.client.Client;
|
||||||
import javax.ws.rs.client.ClientBuilder;
|
import javax.ws.rs.client.ClientBuilder;
|
||||||
import javax.ws.rs.client.ClientRequestContext;
|
import javax.ws.rs.client.ClientRequestContext;
|
||||||
import javax.ws.rs.client.Entity;
|
import javax.ws.rs.client.Entity;
|
||||||
import javax.ws.rs.client.WebTarget;
|
import javax.ws.rs.client.WebTarget;
|
||||||
import javax.ws.rs.core.Form;
|
import javax.ws.rs.core.Form;
|
||||||
import javax.ws.rs.core.HttpHeaders;
|
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;
|
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
|
||||||
|
|
||||||
@ -33,6 +39,8 @@ import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
|
|||||||
*/
|
*/
|
||||||
public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
private final String tokenUrl;
|
private final String tokenUrl;
|
||||||
private final String clientId;
|
private final String clientId;
|
||||||
private final String clientSecret;
|
private final String clientSecret;
|
||||||
@ -111,29 +119,49 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
form.param("client_id", this.clientId);
|
form.param("client_id", this.clientId);
|
||||||
if (this.clientSecret != null)
|
|
||||||
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);
|
|
||||||
|
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);
|
Entity<Form> entity = Entity.form(form);
|
||||||
|
|
||||||
WebTarget target = ClientBuilder.newBuilder()
|
Client client = ClientBuilder.newBuilder()
|
||||||
.register(new JacksonJaxbJsonProvider())
|
.register(new JacksonJaxbJsonProvider())
|
||||||
.build()
|
.build();
|
||||||
.target(this.tokenUrl);
|
WebTarget target = client.target(this.tokenUrl);
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
Response response = target.request().post(entity);
|
||||||
Map<String, Object> response = target.request().post(entity, Map.class);
|
|
||||||
|
|
||||||
if (response.containsKey("error"))
|
this.logger.debug("Received OAuth response: {}", response.getStatus());
|
||||||
throw new WebApplicationException((String)response.get("error"), 400);
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Map<String, Object> responseMap = response.readEntity(Map.class);
|
||||||
|
|
||||||
|
this.logger.trace("Received OAuth response: {}", responseMap);
|
||||||
|
|
||||||
this.accessToken = (String)response.get("access_token");
|
if (response.getStatusInfo().getFamily() != Family.SUCCESSFUL) {
|
||||||
this.expiration = System.currentTimeMillis() + ((Number)response.get("expires_in")).longValue() * 1000L;
|
String code = (String) responseMap.get("error");
|
||||||
this.refreshToken = (String)response.get("refresh_token");
|
if (code != null) {
|
||||||
this.extendRefreshTokenResponse(response);
|
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() {
|
protected Form createRefreshForm() {
|
||||||
@ -143,10 +171,9 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
|||||||
|
|
||||||
protected abstract Form createForm();
|
protected abstract Form createForm();
|
||||||
|
|
||||||
protected void extendRefreshTokenForm(Form form) {
|
protected void extendRefreshFormSensitive(Form form) {
|
||||||
}
|
|
||||||
|
|
||||||
protected void extendRefreshTokenResponse(Map<String, Object> response) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected abstract void extendFormSensitive(Form form);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -37,5 +37,9 @@ public class OAuthClientCredentialAuthorizationFilter extends OAuthAuthorization
|
|||||||
protected Form createForm() {
|
protected Form createForm() {
|
||||||
return new Form().param("grant_type", "client_credentials");
|
return new Form().param("grant_type", "client_credentials");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void extendFormSensitive(Form form) {
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -53,8 +53,12 @@ public class OAuthPasswordGrantAuthorizationFilter extends OAuthAuthorizationFil
|
|||||||
@Override
|
@Override
|
||||||
protected Form createForm() {
|
protected Form createForm() {
|
||||||
return new Form().param("grant_type", "password")
|
return new Form().param("grant_type", "password")
|
||||||
.param("username", this.username)
|
.param("username", this.username);
|
||||||
.param("password", this.password);
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void extendFormSensitive(Form form) {
|
||||||
|
form.param("password", this.password);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user