use of org.glassfish.jersey.client.ClientConfig in project jersey by jersey.
the class EventOutputTest method testGrizzlyConnectorWithEventSource.
@Test
public void testGrizzlyConnectorWithEventSource() throws InterruptedException {
ClientConfig clientConfig = new ClientConfig();
clientConfig.property(ClientProperties.CONNECT_TIMEOUT, 15000);
clientConfig.property(ClientProperties.READ_TIMEOUT, 0);
clientConfig.property(ClientProperties.ASYNC_THREADPOOL_SIZE, 8);
clientConfig.connectorProvider(new GrizzlyConnectorProvider());
Client client = ClientBuilder.newBuilder().withConfig(clientConfig).build();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<String> eventData = new AtomicReference<String>();
final AtomicInteger counter = new AtomicInteger(0);
WebTarget single = client.target(getBaseUri()).path("test/single");
EventSource es = EventSource.target(single).build();
es.register(new EventListener() {
@Override
public void onEvent(InboundEvent inboundEvent) {
final int i = counter.incrementAndGet();
if (i == 1) {
eventData.set(inboundEvent.readData());
}
latch.countDown();
}
});
boolean latchTimedOut;
boolean closeTimedOut;
try {
es.open();
latchTimedOut = latch.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
} finally {
closeTimedOut = es.close(5, TimeUnit.SECONDS);
}
assertEquals("Unexpected event count", 1, counter.get());
assertEquals("Unexpected event data", "single", eventData.get());
assertTrue("Event latch has timed out", latchTimedOut);
assertTrue("EventSource.close() has timed out", closeTimedOut);
}
use of org.glassfish.jersey.client.ClientConfig in project jersey by jersey.
the class EventOutputTest method testReadCommentsOnlySseEvents.
/**
* Reproducer for JERSEY-2912: Sending and receiving comments-only events.
*
* @throws Exception
*/
@Test
public void testReadCommentsOnlySseEvents() throws Exception {
ClientConfig clientConfig = new ClientConfig();
clientConfig.property(ClientProperties.CONNECT_TIMEOUT, 15000);
clientConfig.property(ClientProperties.READ_TIMEOUT, 0);
clientConfig.property(ClientProperties.ASYNC_THREADPOOL_SIZE, 8);
clientConfig.connectorProvider(new GrizzlyConnectorProvider());
Client client = ClientBuilder.newBuilder().withConfig(clientConfig).build();
final CountDownLatch latch = new CountDownLatch(2);
final Queue<String> eventComments = new ArrayBlockingQueue<>(2);
WebTarget single = client.target(getBaseUri()).path("test/comments-only");
EventSource es = EventSource.target(single).build();
es.register(new EventListener() {
@Override
public void onEvent(InboundEvent inboundEvent) {
eventComments.add(inboundEvent.getComment());
latch.countDown();
}
});
boolean latchTimedOut;
boolean closeTimedOut;
try {
es.open();
latchTimedOut = latch.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
} finally {
closeTimedOut = es.close(5, TimeUnit.SECONDS);
}
assertEquals("Unexpected event count", 2, eventComments.size());
for (int i = 1; i <= 2; i++) {
assertEquals("Unexpected comment data on event #" + i, "No comment #" + i, eventComments.poll());
}
assertTrue("Event latch has timed out", latchTimedOut);
assertTrue("EventSource.close() has timed out", closeTimedOut);
}
use of org.glassfish.jersey.client.ClientConfig in project openstack4j by ContainX.
the class ClientFactory method buildClientFromConfig.
private static Client buildClientFromConfig(Config config) {
ClientConfig clientConfig = new ClientConfig();
if (config.getProxy() != null) {
addProxy(clientConfig, config);
}
ClientBuilder cb = ClientBuilder.newBuilder().withConfig(clientConfig).property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, "true").register(JacksonFeature.class).register(RESOLVER).register(new RequestFilter());
if (config.getSslContext() != null)
cb.sslContext(config.getSslContext());
else if (config.isIgnoreSSLVerification())
cb.sslContext(UntrustedSSL.getSSLContext());
if (config.getHostNameVerifier() != null)
cb.hostnameVerifier(config.getHostNameVerifier());
else if (config.isIgnoreSSLVerification())
cb.hostnameVerifier(UntrustedSSL.getHostnameVerifier());
if (config.getReadTimeout() > 0)
cb.property(ClientProperties.READ_TIMEOUT, config.getReadTimeout());
if (config.getConnectTimeout() > 0)
cb.property(ClientProperties.CONNECT_TIMEOUT, config.getConnectTimeout());
return cb.build();
}
use of org.glassfish.jersey.client.ClientConfig in project storm by apache.
the class OpenTsdbClient method init.
private void init() {
final ApacheConnectorProvider apacheConnectorProvider = new ApacheConnectorProvider();
final ClientConfig clientConfig = new ClientConfig().connectorProvider(apacheConnectorProvider);
// transfer encoding should be set as jersey sets it on by default.
clientConfig.property(ClientProperties.REQUEST_ENTITY_PROCESSING, enableChunkedEncoding ? RequestEntityProcessing.CHUNKED : RequestEntityProcessing.BUFFERED);
client = ClientBuilder.newClient(clientConfig);
target = client.target(urlString).path(PUT_PATH);
if (sync) {
// need to add an empty string else it is nto added as query param.
target = target.queryParam("sync", "").queryParam("sync_timeout", syncTimeout);
}
if (responseType != ResponseType.None) {
// need to add an empty string else it is nto added as query param.
target = target.queryParam(responseType.value, "");
}
LOG.info("target uri [{}]", target.getUri());
}
use of org.glassfish.jersey.client.ClientConfig in project jersey by jersey.
the class UnderlyingHttpClientAccessTest method testHttpClientInstanceAccess.
/**
* Verifier of JERSEY-2424 fix.
*/
@Test
public void testHttpClientInstanceAccess() {
final Client client = ClientBuilder.newClient(new ClientConfig().connectorProvider(new ApacheConnectorProvider()));
final HttpClient hcOnClient = ApacheConnectorProvider.getHttpClient(client);
// important: the web target instance in this test must be only created AFTER the client has been pre-initialized
// (see org.glassfish.jersey.client.Initializable.preInitialize method). This is here achieved by calling the
// connector provider's static getHttpClient method above.
final WebTarget target = client.target("http://localhost/");
final HttpClient hcOnTarget = ApacheConnectorProvider.getHttpClient(target);
assertNotNull("HTTP client instance set on JerseyClient should not be null.", hcOnClient);
assertNotNull("HTTP client instance set on JerseyWebTarget should not be null.", hcOnTarget);
assertSame("HTTP client instance set on JerseyClient should be the same instance as the one set on JerseyWebTarget" + "(provided the target instance has not been further configured).", hcOnClient, hcOnTarget);
}
Aggregations