initial checkin
This commit is contained in:
commit
d7ac94c69d
11
.gitignore
vendored
Normal file
11
.gitignore
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
# Maven
|
||||
target
|
||||
pom.xml.versionsBackup
|
||||
|
||||
# Eclipse
|
||||
.project
|
||||
.classpath
|
||||
.settings
|
||||
|
||||
# Visual Studio Code
|
||||
.vscode
|
142
pom.xml
Normal file
142
pom.xml
Normal file
@ -0,0 +1,142 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.inteligr8</groupId>
|
||||
<artifactId>common-rest-api</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>ReST API Client for Java</name>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
|
||||
<junit.version>5.7.2</junit.version>
|
||||
<spring.version>5.2.14.RELEASE</spring.version>
|
||||
<jersey.version>2.34</jersey.version>
|
||||
<cxf.version>3.3.2</cxf.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.12.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.jaxrs</groupId>
|
||||
<artifactId>jackson-jaxrs-json-provider</artifactId>
|
||||
<version>2.12.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.29</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.ws.rs</groupId>
|
||||
<artifactId>jakarta.ws.rs-api</artifactId>
|
||||
<version>2.1.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.ext</groupId>
|
||||
<artifactId>jersey-proxy-client</artifactId>
|
||||
<version>${jersey.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-rs-client</artifactId>
|
||||
<version>${cxf.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.core</groupId>
|
||||
<artifactId>jersey-client</artifactId>
|
||||
<version>${jersey.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.inject</groupId>
|
||||
<artifactId>jersey-hk2</artifactId>
|
||||
<version>${jersey.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.media</groupId>
|
||||
<artifactId>jersey-media-json-jackson</artifactId>
|
||||
<version>${jersey.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.9</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.0.0-M5</version>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
<version>3.0.0-M5</version>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>io.repaint.maven</groupId>
|
||||
<artifactId>tiles-maven-plugin</artifactId>
|
||||
<version>2.21</version>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<filtering>true</filtering>
|
||||
<tiles>
|
||||
<tile>com.inteligr8:maven-public-deploy-tile:[1.0.0,2.0.0)</tile>
|
||||
</tiles>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>inteligr8-releases</id>
|
||||
<url>http://repos.inteligr8.com/nexus/repository/inteligr8-public</url>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
</project>
|
20
src/main/java/com/inteligr8/rs/AccessTokenRequestFilter.java
Normal file
20
src/main/java/com/inteligr8/rs/AccessTokenRequestFilter.java
Normal file
@ -0,0 +1,20 @@
|
||||
package com.inteligr8.rs;
|
||||
|
||||
import javax.ws.rs.client.ClientRequestContext;
|
||||
import javax.ws.rs.client.ClientRequestFilter;
|
||||
import javax.ws.rs.core.HttpHeaders;
|
||||
|
||||
public class AccessTokenRequestFilter implements ClientRequestFilter {
|
||||
|
||||
private final String token;
|
||||
|
||||
public AccessTokenRequestFilter(String token) {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void filter(ClientRequestContext requestContext) {
|
||||
requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION, "Bearer " + this.token);
|
||||
}
|
||||
|
||||
}
|
28
src/main/java/com/inteligr8/rs/BasicAuthRequestFilter.java
Normal file
28
src/main/java/com/inteligr8/rs/BasicAuthRequestFilter.java
Normal file
@ -0,0 +1,28 @@
|
||||
package com.inteligr8.rs;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Base64;
|
||||
|
||||
import javax.ws.rs.client.ClientRequestContext;
|
||||
import javax.ws.rs.client.ClientRequestFilter;
|
||||
import javax.ws.rs.core.HttpHeaders;
|
||||
|
||||
public class BasicAuthRequestFilter implements ClientRequestFilter {
|
||||
|
||||
private final String username;
|
||||
private final String password;
|
||||
|
||||
public BasicAuthRequestFilter(String username, String password) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void filter(ClientRequestContext requestContext) throws UnsupportedEncodingException {
|
||||
String userAndPass = this.username + ":" + this.password;
|
||||
String userAndPassEncoded = Base64.getEncoder().encodeToString(userAndPass.getBytes("utf-8"));
|
||||
|
||||
requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION, "Basic " + userAndPassEncoded);
|
||||
}
|
||||
|
||||
}
|
34
src/main/java/com/inteligr8/rs/Client.java
Normal file
34
src/main/java/com/inteligr8/rs/Client.java
Normal file
@ -0,0 +1,34 @@
|
||||
package com.inteligr8.rs;
|
||||
|
||||
import javax.ws.rs.client.ClientBuilder;
|
||||
import javax.ws.rs.client.ClientRequestFilter;
|
||||
import javax.ws.rs.client.WebTarget;
|
||||
|
||||
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
|
||||
|
||||
/**
|
||||
* Configured JAX-RS Client & WebTarget
|
||||
*/
|
||||
public abstract class Client {
|
||||
|
||||
protected abstract ClientConfiguration getConfig();
|
||||
|
||||
public javax.ws.rs.client.Client getClient() {
|
||||
ClientRequestFilter authFilter = this.getConfig().getAuthorizationFilter();
|
||||
|
||||
ClientBuilder clientBuilder = ClientBuilder.newBuilder()
|
||||
.register(new JacksonJaxbJsonProvider());
|
||||
if (authFilter != null)
|
||||
clientBuilder.register(authFilter);
|
||||
|
||||
return clientBuilder.build();
|
||||
}
|
||||
|
||||
public WebTarget getTarget() {
|
||||
return this.getClient()
|
||||
.target(this.getConfig().getBaseUrl());
|
||||
}
|
||||
|
||||
public abstract <T> T getApi(Class<T> apiClass);
|
||||
|
||||
}
|
54
src/main/java/com/inteligr8/rs/ClientConfiguration.java
Normal file
54
src/main/java/com/inteligr8/rs/ClientConfiguration.java
Normal file
@ -0,0 +1,54 @@
|
||||
package com.inteligr8.rs;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import javax.ws.rs.client.ClientRequestFilter;
|
||||
|
||||
public abstract class ClientConfiguration {
|
||||
|
||||
public abstract String getBaseUrl();
|
||||
|
||||
public abstract String getBasicAuthUsername();
|
||||
|
||||
public abstract String getBasicAuthPassword();
|
||||
|
||||
public abstract String getAccessToken();
|
||||
|
||||
public abstract String getOAuthTokenUrl();
|
||||
|
||||
public abstract String getOAuthClientId();
|
||||
|
||||
public abstract String getOAuthClientSecret();
|
||||
|
||||
public abstract String getOAuthAuthCode();
|
||||
|
||||
public abstract String getOAuthAuthRedirectUri();
|
||||
|
||||
public abstract String getOAuthUsername();
|
||||
|
||||
public abstract String getOAuthPassword();
|
||||
|
||||
public ClientRequestFilter getAuthorizationFilter() {
|
||||
if (this.getAccessToken() != null) {
|
||||
return new AccessTokenRequestFilter(this.getAccessToken());
|
||||
} else if (this.getOAuthTokenUrl() != null) {
|
||||
if (this.getOAuthAuthCode() != null) {
|
||||
return new OAuthAuthorizationCodeRequestFilter(this.getOAuthTokenUrl(),
|
||||
this.getOAuthClientId(), this.getOAuthClientSecret(),
|
||||
this.getOAuthAuthCode(), URI.create(this.getOAuthAuthRedirectUri()));
|
||||
} else if (this.getOAuthUsername() != null) {
|
||||
return new OAuthPasswordGrantRequestFilter(this.getOAuthTokenUrl(),
|
||||
this.getOAuthClientId(), this.getOAuthClientSecret(),
|
||||
this.getOAuthUsername(), this.getOAuthPassword());
|
||||
} else {
|
||||
return new OAuthClientCredentialRequestFilter(this.getOAuthTokenUrl(),
|
||||
this.getOAuthClientId(), this.getOAuthClientSecret());
|
||||
}
|
||||
} else if (this.getBasicAuthUsername() != null) {
|
||||
return new BasicAuthRequestFilter(this.getBasicAuthUsername(), this.getBasicAuthPassword());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
61
src/main/java/com/inteligr8/rs/ClientCxfImpl.java
Normal file
61
src/main/java/com/inteligr8/rs/ClientCxfImpl.java
Normal file
@ -0,0 +1,61 @@
|
||||
package com.inteligr8.rs;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.ws.rs.client.ClientRequestFilter;
|
||||
import javax.ws.rs.ext.RuntimeDelegate;
|
||||
|
||||
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
|
||||
import org.apache.cxf.jaxrs.client.WebClient;
|
||||
import org.apache.cxf.jaxrs.impl.RuntimeDelegateImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
|
||||
|
||||
/**
|
||||
* Configured JAX-RS Client & WebTarget & CXF WebClient for CXF
|
||||
*/
|
||||
public abstract class ClientCxfImpl extends Client implements InitializingBean {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(ClientCxfImpl.class);
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
if (RuntimeDelegate.getInstance() == null) {
|
||||
this.logger.info("Setting JAX-RS runtime delegate to the CXF library");
|
||||
RuntimeDelegate.setInstance(new RuntimeDelegateImpl());
|
||||
} else if (RuntimeDelegate.getInstance() instanceof RuntimeDelegateImpl) {
|
||||
this.logger.info("JAX-RS runtime delegate already the CXF library");
|
||||
} else {
|
||||
this.logger.warn("Setting JAX-RS runtime delegate to the CXF library; was: " + RuntimeDelegate.getInstance().getClass().getName());
|
||||
RuntimeDelegate.setInstance(new RuntimeDelegateImpl());
|
||||
}
|
||||
|
||||
if (this.logger.isInfoEnabled())
|
||||
this.logger.info("API Base URL: " + this.getConfig().getBaseUrl());
|
||||
}
|
||||
|
||||
public WebClient getCxfClient() {
|
||||
List<Object> providersAndFilters = new LinkedList<Object>();
|
||||
providersAndFilters.add(new JacksonJaxbJsonProvider());
|
||||
|
||||
ClientRequestFilter authFilter = this.getConfig().getAuthorizationFilter();
|
||||
if (authFilter != null)
|
||||
providersAndFilters.add(authFilter);
|
||||
|
||||
// we can't use JAXRSClientFactory with a JAXRS client (duh!)
|
||||
// so we need to create a CXF client
|
||||
WebClient client = WebClient.create(this.getConfig().getBaseUrl(), providersAndFilters);
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getApi(Class<T> apiClass) {
|
||||
return JAXRSClientFactory.fromClient(this.getCxfClient(), apiClass);
|
||||
}
|
||||
|
||||
}
|
39
src/main/java/com/inteligr8/rs/ClientJerseyImpl.java
Normal file
39
src/main/java/com/inteligr8/rs/ClientJerseyImpl.java
Normal file
@ -0,0 +1,39 @@
|
||||
package com.inteligr8.rs;
|
||||
|
||||
import javax.ws.rs.ext.RuntimeDelegate;
|
||||
|
||||
import org.glassfish.jersey.client.proxy.WebResourceFactory;
|
||||
import org.glassfish.jersey.internal.RuntimeDelegateImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
/**
|
||||
* Configured JAX-RS Client & WebTarget for Jersey
|
||||
*/
|
||||
public abstract class ClientJerseyImpl extends Client implements InitializingBean {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(ClientJerseyImpl.class);
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
if (RuntimeDelegate.getInstance() == null) {
|
||||
this.logger.info("Setting JAX-RS runtime delegate to the Jersey library");
|
||||
RuntimeDelegate.setInstance(new RuntimeDelegateImpl());
|
||||
} else if (RuntimeDelegate.getInstance() instanceof RuntimeDelegateImpl) {
|
||||
this.logger.info("JAX-RS runtime delegate already the Jersey library");
|
||||
} else {
|
||||
this.logger.warn("Setting JAX-RS runtime delegate to the Jersey library; was: " + RuntimeDelegate.getInstance().getClass().getName());
|
||||
RuntimeDelegate.setInstance(new RuntimeDelegateImpl());
|
||||
}
|
||||
|
||||
if (this.logger.isInfoEnabled())
|
||||
this.logger.info("API Base URL: " + this.getConfig().getBaseUrl());
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getApi(Class<T> apiClass) {
|
||||
return WebResourceFactory.newResource(apiClass, this.getTarget());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package com.inteligr8.rs;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import javax.ws.rs.core.Form;
|
||||
|
||||
public class OAuthAuthorizationCodeRequestFilter extends OAuthRequestFilter {
|
||||
|
||||
private final String code;
|
||||
private final URI redirectUri;
|
||||
|
||||
public OAuthAuthorizationCodeRequestFilter(String tokenUrl, String clientId, String code) {
|
||||
this(tokenUrl, clientId, null, code);
|
||||
}
|
||||
|
||||
public OAuthAuthorizationCodeRequestFilter(String tokenUrl, String clientId, String code, URI redirectUri) {
|
||||
this(tokenUrl, clientId, null, code, redirectUri);
|
||||
}
|
||||
|
||||
public OAuthAuthorizationCodeRequestFilter(String tokenUrl, String clientId, String clientSecret, String code) {
|
||||
this(tokenUrl, clientId, clientSecret, code, null);
|
||||
}
|
||||
|
||||
public OAuthAuthorizationCodeRequestFilter(String tokenUrl, String clientId, String clientSecret, String code, URI redirectUri) {
|
||||
super(tokenUrl, clientId, clientSecret);
|
||||
|
||||
this.code = code;
|
||||
this.redirectUri = redirectUri;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Form createForm() {
|
||||
Form form = new Form().param("grant_type", "authorization_code")
|
||||
.param("code", this.code);
|
||||
if (this.redirectUri != null)
|
||||
form.param("redirect_uri", this.redirectUri.toString());
|
||||
return form;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.inteligr8.rs;
|
||||
|
||||
import javax.ws.rs.core.Form;
|
||||
|
||||
public class OAuthClientCredentialRequestFilter extends OAuthRequestFilter {
|
||||
|
||||
public OAuthClientCredentialRequestFilter(String tokenUrl, String clientId, String clientSecret) {
|
||||
super(tokenUrl, clientId, clientSecret);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Form createForm() {
|
||||
return new Form().param("grant_type", "client_credentials");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.inteligr8.rs;
|
||||
|
||||
import javax.ws.rs.core.Form;
|
||||
|
||||
public class OAuthPasswordGrantRequestFilter extends OAuthRequestFilter {
|
||||
|
||||
private final String username;
|
||||
private final String password;
|
||||
|
||||
public OAuthPasswordGrantRequestFilter(String tokenUrl, String clientId, String username, String password) {
|
||||
this(tokenUrl, clientId, null, username, password);
|
||||
}
|
||||
|
||||
public OAuthPasswordGrantRequestFilter(String tokenUrl, String clientId, String clientSecret, String username, String password) {
|
||||
super(tokenUrl, clientId, clientSecret);
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Form createForm() {
|
||||
return new Form().param("grant_type", "password")
|
||||
.param("username", this.username)
|
||||
.param("password", this.password);
|
||||
}
|
||||
|
||||
}
|
85
src/main/java/com/inteligr8/rs/OAuthRequestFilter.java
Normal file
85
src/main/java/com/inteligr8/rs/OAuthRequestFilter.java
Normal file
@ -0,0 +1,85 @@
|
||||
package com.inteligr8.rs;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.client.ClientBuilder;
|
||||
import javax.ws.rs.client.ClientRequestContext;
|
||||
import javax.ws.rs.client.ClientRequestFilter;
|
||||
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 com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
|
||||
|
||||
public abstract class OAuthRequestFilter implements ClientRequestFilter {
|
||||
|
||||
private final String tokenUrl;
|
||||
private final String clientId;
|
||||
private final String clientSecret;
|
||||
private final String scope;
|
||||
private String accessToken;
|
||||
private long expiration;
|
||||
private String refreshToken;
|
||||
|
||||
public OAuthRequestFilter(String tokenUrl, String clientId) {
|
||||
this(tokenUrl, clientId, null);
|
||||
}
|
||||
|
||||
public OAuthRequestFilter(String tokenUrl, String clientId, String clientSecret) {
|
||||
this(tokenUrl, clientId, clientSecret, null);
|
||||
}
|
||||
|
||||
public OAuthRequestFilter(String tokenUrl, String clientId, String clientSecret, String scope) {
|
||||
this.tokenUrl = tokenUrl;
|
||||
this.clientId = clientId;
|
||||
this.clientSecret = clientSecret;
|
||||
this.scope = scope;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void filter(ClientRequestContext requestContext) {
|
||||
if (this.accessToken == null || System.currentTimeMillis() > this.expiration)
|
||||
this.requestToken();
|
||||
|
||||
requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION, "Bearer " + this.accessToken);
|
||||
}
|
||||
|
||||
private void requestToken() {
|
||||
Form form;
|
||||
|
||||
if (this.refreshToken != null) {
|
||||
form = new Form().param("grant_type", "refresh_token")
|
||||
.param("refresh_token", this.refreshToken);
|
||||
} else {
|
||||
form = this.createForm();
|
||||
}
|
||||
|
||||
form.param("clientId", this.clientId);
|
||||
if (this.clientSecret != null)
|
||||
form.param("clientSecret", this.clientSecret);
|
||||
if (this.scope != null)
|
||||
form.param("scope", this.scope);
|
||||
|
||||
Entity<Form> entity = Entity.form(form);
|
||||
|
||||
WebTarget target = ClientBuilder.newBuilder()
|
||||
.register(new JacksonJaxbJsonProvider())
|
||||
.build()
|
||||
.target(this.tokenUrl);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> response = target.request().post(entity, Map.class);
|
||||
|
||||
if (response.containsKey("error"))
|
||||
throw new WebApplicationException((String)response.get("error"), 400);
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
protected abstract Form createForm();
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user