Java Code Examples for org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder#build()

The following examples show how to use org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder#build() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProxyFactory.java    From robozonky with Apache License 2.0 6 votes vote down vote up
public static ResteasyClient newResteasyClient() {
    LOGGER.debug("Creating RESTEasy client.");
    final Settings settings = Settings.INSTANCE;
    final long socketTimeout = settings.getSocketTimeout()
        .toMillis();
    LOGGER.debug("Set socket timeout to {} ms.", socketTimeout);
    final long connectionTimeout = settings.getConnectionTimeout()
        .toMillis();
    LOGGER.debug("Set connection timeout to {} ms.", connectionTimeout);
    final ResteasyClientBuilder builder = ((ResteasyClientBuilder) ClientBuilder.newBuilder())
        .useAsyncHttpEngine()
        .readTimeout(socketTimeout, TimeUnit.MILLISECONDS)
        .connectTimeout(connectionTimeout, TimeUnit.MILLISECONDS);
    /*
     * setup HTTP proxy when required (see
     * http://docs.jboss.org/resteasy/docs/4.0.0.Final/userguide/html/RESTEasy_Client_Framework.html#http_proxy)
     */
    settings.getHttpsProxyHostname()
        .ifPresent(host -> {
            final int port = settings.getHttpsProxyPort();
            builder.property("org.jboss.resteasy.jaxrs.client.proxy.host", host)
                .property("org.jboss.resteasy.jaxrs.client.proxy.port", port);
            LOGGER.debug("Set HTTP proxy to {}:{}.", host, port);
        });
    return builder.build();
}
 
Example 2
Source File: POSTServiceBean.java    From OpenIoE with Apache License 2.0 5 votes vote down vote up
@Override
public String post(String server, String path, LinkedHashMap<String, Object> parameters) throws InvalidResponseException {
	String json =  null;
	Client client = null;
	try {
		ResteasyClientBuilder clientBuilder = new ResteasyClientBuilder()
				.establishConnectionTimeout(100, TimeUnit.SECONDS)
				.socketTimeout(100, TimeUnit.SECONDS);
		clientBuilder = clientBuilder.connectionPoolSize(1);
		client = clientBuilder.build();
		WebTarget webResource = client.target(server);
		webResource = webResource.path(path);
		Builder builder = webResource.request(MediaType.APPLICATION_JSON);
		builder.accept(MediaType.APPLICATION_JSON);
		Response response = builder.post(Entity.json(parameters));
		if (response.getStatus() == Status.OK.getStatusCode()) {
			json = response.readEntity(String.class);
		} else {
			throw new InvalidResponseException("Response code is not 200.");
		}
	} catch (Exception ex) {
		throw new InvalidResponseException("POST error when calling server: " + server + " path: " + path + ". " + ex.getMessage());
	} finally {
		if(client != null) {
			client.close();
		}
	}
	return json;
}
 
Example 3
Source File: RestInterfaceFactory.java    From EDDI with Apache License 2.0 5 votes vote down vote up
private ResteasyClient getResteasyClient(String targetServerUri) {
    ResteasyClient client = clients.get(targetServerUri);
    if (client == null) {

        JettyClientEngine engine = new JettyClientEngine(httpClient);
        ResteasyClientBuilder clientBuilder = (ResteasyClientBuilder) ClientBuilder.newBuilder();
        clientBuilder.httpEngine(engine);

        client = clientBuilder.build();
        clients.put(targetServerUri, client);
    }

    return client;
}
 
Example 4
Source File: RestClientFactoryImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Client create(final RestClientConfiguration configuration) {
  checkNotNull(configuration);

  try (TcclBlock tccl = TcclBlock.begin(ResteasyClientBuilder.class)) {
    HttpContext httpContext = new BasicHttpContext();
    if (configuration.getUseTrustStore()) {
        httpContext.setAttribute(SSLContextSelector.USE_TRUST_STORE, true);
    }
    HttpClient client;
    if (configuration.getHttpClient() != null) {
      client = checkNotNull(configuration.getHttpClient().get());
    }
    else {
      client = httpClient.get();
    }
    ClientHttpEngine httpEngine = new ApacheHttpClient4Engine(client, httpContext);

    ResteasyClientBuilder builder = new ResteasyClientBuilder().httpEngine(httpEngine);

    if (configuration.getCustomizer() != null) {
      configuration.getCustomizer().apply(builder);
    }

    return builder.build();
  }
}
 
Example 5
Source File: KeycloakTestingClient.java    From keycloak with Apache License 2.0 5 votes vote down vote up
KeycloakTestingClient(String serverUrl, ResteasyClient resteasyClient) {
    if (resteasyClient != null) {
        client = resteasyClient;
    } else {
        ResteasyClientBuilder resteasyClientBuilder = new ResteasyClientBuilder();
        resteasyClientBuilder.connectionPoolSize(10);
        if (serverUrl.startsWith("https")) {
            // Disable PKIX path validation errors when running tests using SSL
            resteasyClientBuilder.disableTrustManager().hostnameVerification(ResteasyClientBuilder.HostnameVerificationPolicy.ANY);
        }
        resteasyClientBuilder.httpEngine(AdminClientUtil.getCustomClientHttpEngine(resteasyClientBuilder, 10));
        client = resteasyClientBuilder.build();
    }
    target = client.target(serverUrl);
}
 
Example 6
Source File: ImpersonationTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
protected Cookie testSuccessfulImpersonation(String admin, String adminRealm) {
    ResteasyClientBuilder resteasyClientBuilder = new ResteasyClientBuilder();
    resteasyClientBuilder.connectionPoolSize(10);
    resteasyClientBuilder.httpEngine(AdminClientUtil.getCustomClientHttpEngine(resteasyClientBuilder, 10));
    ResteasyClient resteasyClient = resteasyClientBuilder.build();

    // Login adminClient
    try (Keycloak client = login(admin, adminRealm, resteasyClient)) {
        // Impersonate
        return impersonate(client, admin, adminRealm);
    }
}
 
Example 7
Source File: DigdagClient.java    From digdag with Apache License 2.0 4 votes vote down vote up
private DigdagClient(Builder builder)
{
    if (builder.host == null) {
        throw new IllegalArgumentException("host is not set");
    }

    if (builder.ssl) {
        if (builder.port < 0) {
            this.endpoint = "https://" + builder.host;
        }
        else {
            this.endpoint = "https://" + builder.host + ":" + builder.port;
        }
    }
    else {
        if (builder.port < 0) {
            this.endpoint = "http://" + builder.host;
        }
        else {
            this.endpoint = "http://" + builder.host + ":" + builder.port;
        }
    }

    final Map<String, String> baseHeaders = builder.baseHeaders;
    final Function<Map<String, String>, Map<String, String>> headerBuilder = builder.headerBuilder;

    if (headerBuilder != null) {
        this.headers = () -> new MultivaluedHashMap<>(headerBuilder.apply(new HashMap<>(baseHeaders)));
    }
    else {
        final MultivaluedMap<String, Object> staticHeaders = new MultivaluedHashMap<>(baseHeaders);
        this.headers = () -> staticHeaders;
    }

    ObjectMapper mapper = objectMapper();

    ResteasyClientBuilder clientBuilder = new ResteasyClientBuilder()
            .register(AcceptEncodingGZIPFilter.class)
            .register(GZIPDecodingInterceptor.class)
            .register(GZIPEncodingInterceptor.class)
            .register(new UserAgentFilter("DigdagClient/" + buildVersion()))
            .register(new JacksonJsonProvider(mapper));

    // TODO: support proxy user/pass
    if (builder.proxyHost != null) {
        if (builder.proxyPort == null) {
            clientBuilder.defaultProxy(builder.proxyHost);
        } else {
            if (builder.proxyScheme == null) {
                clientBuilder.defaultProxy(builder.proxyHost, builder.proxyPort);
            } else {
                clientBuilder.defaultProxy(builder.proxyHost, builder.proxyPort, builder.proxyScheme);
            }
        }
    }

    if (builder.disableCertValidation) {
        clientBuilder.disableTrustManager();
    }

    this.client = clientBuilder.build();

    this.cf = new ConfigFactory(mapper);

    this.disableDirectDownload = builder.disableDirectDownload;

}