KEYCLOAK-12285 Add support for RestEasy 4 to admin client

This commit is contained in:
stianst 2020-02-21 12:19:03 +01:00 committed by Stian Thorgersen
parent dfb67c3aa4
commit 6978806a7e
4 changed files with 71 additions and 7 deletions

View file

@ -30,18 +30,50 @@
<name>Keycloak Admin REST Client</name>
<description/>
<properties>
<resteasy.versions>3.9.1.Final</resteasy.versions>
</properties>
<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-common</artifactId>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>${resteasy.versions}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-multipart-provider</artifactId>
<version>${resteasy.versions}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>${resteasy.versions}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>${resteasy.versions}</version>
</dependency>
</dependencies>

View file

@ -0,0 +1,28 @@
package org.keycloak.admin.client;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
public class ClientBuilderWrapper {
static Class clazz;
static {
try {
clazz = Class.forName("org.jboss.resteasy.client.jaxrs.internal.ResteasyClientBuilderImpl");
} catch (ClassNotFoundException e) {
try {
clazz = Class.forName("org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder");
} catch (ClassNotFoundException ex) {
throw new RuntimeException("RestEasy 3 or 4 not found on classpath");
}
}
}
public static ResteasyClientBuilder create() {
try {
return (ResteasyClientBuilder) clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View file

@ -30,6 +30,7 @@ import org.keycloak.admin.client.token.TokenManager;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.ws.rs.client.Client;
import java.net.URI;
@ -50,7 +51,8 @@ public class Keycloak implements AutoCloseable {
private final TokenManager tokenManager;
private final String authToken;
private final ResteasyWebTarget target;
private final ResteasyClient client;
private final Client client;
private boolean closed = false;
Keycloak(String serverUrl, String realm, String username, String password, String clientId, String clientSecret, String grantType, ResteasyClient resteasyClient, String authtoken) {
config = new Config(serverUrl, realm, username, password, clientId, clientSecret, grantType);
@ -58,7 +60,7 @@ public class Keycloak implements AutoCloseable {
authToken = authtoken;
tokenManager = authtoken == null ? new TokenManager(config, client) : null;
target = client.target(config.getServerUrl());
target = (ResteasyWebTarget) client.target(config.getServerUrl());
target.register(newAuthFilter());
}
@ -67,7 +69,7 @@ public class Keycloak implements AutoCloseable {
}
private static ResteasyClient newRestEasyClient(ResteasyJackson2Provider customJacksonProvider, SSLContext sslContext, boolean disableTrustManager) {
ResteasyClientBuilder clientBuilder = new ResteasyClientBuilder()
ResteasyClientBuilder clientBuilder = ClientBuilderWrapper.create()
.sslContext(sslContext)
.connectionPoolSize(10);
@ -141,7 +143,7 @@ public class Keycloak implements AutoCloseable {
* @return
*/
public <T> T proxy(Class<T> proxyClass, URI absoluteURI) {
return client.target(absoluteURI).register(newAuthFilter()).proxy(proxyClass);
return ((ResteasyWebTarget) client.target(absoluteURI)).register(newAuthFilter()).proxy(proxyClass);
}
/**
@ -149,6 +151,7 @@ public class Keycloak implements AutoCloseable {
*/
@Override
public void close() {
closed = true;
client.close();
}
@ -156,6 +159,6 @@ public class Keycloak implements AutoCloseable {
* @return true if the underlying client is closed.
*/
public boolean isClosed() {
return client.isClosed();
return closed;
}
}

View file

@ -25,6 +25,7 @@ import org.keycloak.common.util.Time;
import org.keycloak.representations.AccessTokenResponse;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.client.Client;
import javax.ws.rs.core.Form;
import static org.keycloak.OAuth2Constants.CLIENT_CREDENTIALS;
@ -46,9 +47,9 @@ public class TokenManager {
private final TokenService tokenService;
private final String accessTokenGrantType;
public TokenManager(Config config, ResteasyClient client) {
public TokenManager(Config config, Client client) {
this.config = config;
ResteasyWebTarget target = client.target(config.getServerUrl());
ResteasyWebTarget target = (ResteasyWebTarget) client.target(config.getServerUrl());
if (!config.isPublicClient()) {
target.register(new BasicAuthFilter(config.getClientId(), config.getClientSecret()));
}