From 6978806a7e42bb87064c1786eaee73cfa19ccb3a Mon Sep 17 00:00:00 2001 From: stianst Date: Fri, 21 Feb 2020 12:19:03 +0100 Subject: [PATCH] KEYCLOAK-12285 Add support for RestEasy 4 to admin client --- integration/admin-client/pom.xml | 32 +++++++++++++++++++ .../admin/client/ClientBuilderWrapper.java | 28 ++++++++++++++++ .../org/keycloak/admin/client/Keycloak.java | 13 +++++--- .../admin/client/token/TokenManager.java | 5 +-- 4 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 integration/admin-client/src/main/java/org/keycloak/admin/client/ClientBuilderWrapper.java diff --git a/integration/admin-client/pom.xml b/integration/admin-client/pom.xml index 23d2d23e89..4163d27ffa 100755 --- a/integration/admin-client/pom.xml +++ b/integration/admin-client/pom.xml @@ -30,18 +30,50 @@ Keycloak Admin REST Client + + 3.9.1.Final + + org.keycloak keycloak-core + + + * + * + + + + + org.keycloak + keycloak-common + + + * + * + + + + + org.jboss.resteasy + resteasy-client + ${resteasy.versions} org.jboss.resteasy resteasy-multipart-provider + ${resteasy.versions} org.jboss.resteasy resteasy-jackson2-provider + ${resteasy.versions} + + + org.jboss.resteasy + resteasy-jaxb-provider + ${resteasy.versions} diff --git a/integration/admin-client/src/main/java/org/keycloak/admin/client/ClientBuilderWrapper.java b/integration/admin-client/src/main/java/org/keycloak/admin/client/ClientBuilderWrapper.java new file mode 100644 index 0000000000..77497c4668 --- /dev/null +++ b/integration/admin-client/src/main/java/org/keycloak/admin/client/ClientBuilderWrapper.java @@ -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); + } + } + +} diff --git a/integration/admin-client/src/main/java/org/keycloak/admin/client/Keycloak.java b/integration/admin-client/src/main/java/org/keycloak/admin/client/Keycloak.java index 3372e057d0..0a59ae3e72 100755 --- a/integration/admin-client/src/main/java/org/keycloak/admin/client/Keycloak.java +++ b/integration/admin-client/src/main/java/org/keycloak/admin/client/Keycloak.java @@ -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 proxy(Class 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; } } diff --git a/integration/admin-client/src/main/java/org/keycloak/admin/client/token/TokenManager.java b/integration/admin-client/src/main/java/org/keycloak/admin/client/token/TokenManager.java index 5246c99fd4..bd231c89db 100644 --- a/integration/admin-client/src/main/java/org/keycloak/admin/client/token/TokenManager.java +++ b/integration/admin-client/src/main/java/org/keycloak/admin/client/token/TokenManager.java @@ -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())); }