This commit is contained in:
stianst 2020-02-26 16:42:17 +01:00 committed by Stian Thorgersen
parent 6978806a7e
commit 7b1b1cd35f
2 changed files with 16 additions and 22 deletions

View file

@ -1,6 +1,7 @@
package org.keycloak.admin.client;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import javax.net.ssl.SSLContext;
import javax.ws.rs.client.ClientBuilder;
public class ClientBuilderWrapper {
@ -17,9 +18,15 @@ public class ClientBuilderWrapper {
}
}
public static ResteasyClientBuilder create() {
public static ClientBuilder create(SSLContext sslContext, boolean disableTrustManager) {
try {
return (ResteasyClientBuilder) clazz.newInstance();
Object o = clazz.newInstance();
clazz.getMethod("sslContext", SSLContext.class).invoke(o, sslContext);
clazz.getMethod("connectionPoolSize", int.class).invoke(o, 10);
if (disableTrustManager) {
clazz.getMethod("disableTrustManager").invoke(o);
}
return (ClientBuilder) o;
} catch (Exception e) {
throw new RuntimeException(e);
}

View file

@ -14,11 +14,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.admin.client;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider;
import org.keycloak.admin.client.resource.BearerAuthFilter;
@ -27,19 +24,16 @@ import org.keycloak.admin.client.resource.RealmsResource;
import org.keycloak.admin.client.resource.ServerInfoResource;
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 javax.ws.rs.client.ClientBuilder;
import java.net.URI;
import static org.keycloak.OAuth2Constants.PASSWORD;
/**
* Provides a Keycloak client. By default, this implementation uses a {@link ResteasyClient RESTEasy client} with the
* default {@link ResteasyClientBuilder} settings. To customize the underling client, use a {@link KeycloakBuilder} to
* create a Keycloak client.
* Provides a Keycloak client. By default, this implementation uses a the default RestEasy client builder settings.
* To customize the underling client, use a {@link KeycloakBuilder} to create a Keycloak client.
*
* To read Responses, you can use {@link CreatedResponseUtil} for objects created
*
@ -54,7 +48,7 @@ public class Keycloak implements AutoCloseable {
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) {
Keycloak(String serverUrl, String realm, String username, String password, String clientId, String clientSecret, String grantType, Client resteasyClient, String authtoken) {
config = new Config(serverUrl, realm, username, password, clientId, clientSecret, grantType);
client = resteasyClient != null ? resteasyClient : newRestEasyClient(null, null, false);
authToken = authtoken;
@ -68,15 +62,8 @@ public class Keycloak implements AutoCloseable {
return authToken != null ? new BearerAuthFilter(authToken) : new BearerAuthFilter(tokenManager);
}
private static ResteasyClient newRestEasyClient(ResteasyJackson2Provider customJacksonProvider, SSLContext sslContext, boolean disableTrustManager) {
ResteasyClientBuilder clientBuilder = ClientBuilderWrapper.create()
.sslContext(sslContext)
.connectionPoolSize(10);
if (disableTrustManager) {
// Disable PKIX path validation errors when running tests using SSL
clientBuilder.disableTrustManager().hostnameVerification(ResteasyClientBuilder.HostnameVerificationPolicy.ANY);
}
private static Client newRestEasyClient(ResteasyJackson2Provider customJacksonProvider, SSLContext sslContext, boolean disableTrustManager) {
ClientBuilder clientBuilder = ClientBuilderWrapper.create(sslContext, disableTrustManager);
if (customJacksonProvider != null) {
clientBuilder.register(customJacksonProvider, 100);