Search in sources :

Example 86 with ClientConfig

use of org.glassfish.jersey.client.ClientConfig in project jersey by jersey.

the class HttpMethodTest method createPoolingClient.

protected Client createPoolingClient() {
    ClientConfig cc = new ClientConfig();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(100);
    connectionManager.setDefaultMaxPerRoute(100);
    cc.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
    cc.connectorProvider(new ApacheConnectorProvider());
    return ClientBuilder.newClient(cc);
}
Also used : ClientConfig(org.glassfish.jersey.client.ClientConfig) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 87 with ClientConfig

use of org.glassfish.jersey.client.ClientConfig in project jersey by jersey.

the class GZIPContentEncodingTest method testPostChunked.

@Test
public void testPostChunked() {
    ClientConfig cc = new ClientConfig(GZipEncoder.class);
    cc.property(ClientProperties.CHUNKED_ENCODING_SIZE, 1024);
    cc.connectorProvider(new ApacheConnectorProvider());
    Client client = ClientBuilder.newClient(cc);
    WebTarget r = client.target(getBaseUri());
    byte[] content = new byte[1024 * 1024];
    assertTrue(Arrays.equals(content, r.request().post(Entity.entity(content, MediaType.APPLICATION_OCTET_STREAM_TYPE)).readEntity(byte[].class)));
    Response cr = r.request().post(Entity.text("POST"));
    assertTrue(cr.hasEntity());
    cr.close();
}
Also used : Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 88 with ClientConfig

use of org.glassfish.jersey.client.ClientConfig in project jersey by jersey.

the class GZIPContentEncodingTest method testPost.

@Test
public void testPost() {
    ClientConfig cc = new ClientConfig(GZipEncoder.class);
    cc.connectorProvider(new ApacheConnectorProvider());
    Client client = ClientBuilder.newClient(cc);
    WebTarget r = client.target(getBaseUri());
    byte[] content = new byte[1024 * 1024];
    assertTrue(Arrays.equals(content, r.request().post(Entity.entity(content, MediaType.APPLICATION_OCTET_STREAM_TYPE)).readEntity(byte[].class)));
    Response cr = r.request().post(Entity.entity(content, MediaType.APPLICATION_OCTET_STREAM_TYPE));
    assertTrue(cr.hasEntity());
    cr.close();
}
Also used : Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 89 with ClientConfig

use of org.glassfish.jersey.client.ClientConfig in project jersey by jersey.

the class HelloWorldTest method testAsyncClientRequests.

@Test
public void testAsyncClientRequests() throws InterruptedException {
    HttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    ClientConfig cc = new ClientConfig();
    cc.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
    cc.connectorProvider(new ApacheConnectorProvider());
    Client client = ClientBuilder.newClient(cc);
    WebTarget target = client.target(getBaseUri());
    final int REQUESTS = 20;
    final CountDownLatch latch = new CountDownLatch(REQUESTS);
    final long tic = System.currentTimeMillis();
    final Map<Integer, String> results = new ConcurrentHashMap<Integer, String>();
    for (int i = 0; i < REQUESTS; i++) {
        final int id = i;
        target.path(ROOT_PATH).request().async().get(new InvocationCallback<Response>() {

            @Override
            public void completed(Response response) {
                try {
                    final String result = response.readEntity(String.class);
                    results.put(id, result);
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void failed(Throwable error) {
                Logger.getLogger(HelloWorldTest.class.getName()).log(Level.SEVERE, "Failed on throwable", error);
                results.put(id, "error: " + error.getMessage());
                latch.countDown();
            }
        });
    }
    assertTrue(latch.await(10 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS));
    final long toc = System.currentTimeMillis();
    Logger.getLogger(HelloWorldTest.class.getName()).info("Executed in: " + (toc - tic));
    StringBuilder resultInfo = new StringBuilder("Results:\n");
    for (int i = 0; i < REQUESTS; i++) {
        String result = results.get(i);
        resultInfo.append(i).append(": ").append(result).append('\n');
    }
    Logger.getLogger(HelloWorldTest.class.getName()).info(resultInfo.toString());
    for (int i = 0; i < REQUESTS; i++) {
        String result = results.get(i);
        assertEquals(HelloWorldResource.CLICHED_MESSAGE, result);
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Response(javax.ws.rs.core.Response) HttpResponse(org.apache.http.HttpResponse) WebTarget(javax.ws.rs.client.WebTarget) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 90 with ClientConfig

use of org.glassfish.jersey.client.ClientConfig in project jersey by jersey.

the class AuthTest method testAuthGet.

@Test
public void testAuthGet() {
    CredentialsProvider credentialsProvider = new org.apache.http.impl.client.BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("name", "password"));
    ClientConfig cc = new ClientConfig();
    cc.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider);
    cc.connectorProvider(new ApacheConnectorProvider());
    Client client = ClientBuilder.newClient(cc);
    WebTarget r = client.target(getBaseUri()).path("test");
    assertEquals("GET", r.request().get(String.class));
}
Also used : CredentialsProvider(org.apache.http.client.CredentialsProvider) WebTarget(javax.ws.rs.client.WebTarget) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Aggregations

ClientConfig (org.glassfish.jersey.client.ClientConfig)169 Client (javax.ws.rs.client.Client)129 Test (org.junit.Test)85 Response (javax.ws.rs.core.Response)66 JerseyTest (org.glassfish.jersey.test.JerseyTest)52 WebTarget (javax.ws.rs.client.WebTarget)48 ClientBuilder (javax.ws.rs.client.ClientBuilder)17 SSLContext (javax.net.ssl.SSLContext)12 Invocation (javax.ws.rs.client.Invocation)12 ClientResponse (org.glassfish.jersey.client.ClientResponse)12 ApacheConnectorProvider (org.glassfish.jersey.apache.connector.ApacheConnectorProvider)10 IOException (java.io.IOException)9 IntegrationTest (com.hortonworks.streamline.common.test.IntegrationTest)8 URI (java.net.URI)7 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)7 CredentialsProvider (org.apache.http.client.CredentialsProvider)7 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 JerseyClient (org.glassfish.jersey.client.JerseyClient)6 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)5