Search in sources :

Example 1 with HttpUrlConnectorProvider

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

the class ClientBufferingDisabledTest method testDisableBufferingWithFixedLengthViaMethod.

/**
     * Test that buffering can be disabled with {@link HttpURLConnection}. By default, the
     * {@code HttpURLConnection} buffers the output entity in order to calculate the
     * Content-length request attribute. This cause problems for large entities.
     * <p>
     * This test uses {@link HttpUrlConnectorProvider#useFixedLengthStreaming()} to enable
     * fix length streaming on {@code HttpURLConnection}.
     */
@Test
public void testDisableBufferingWithFixedLengthViaMethod() {
    postLatch = new CountDownLatch(1);
    // This IS sends out 10 chunks and waits whether they were received on the server. This tests
    // whether the buffering is disabled.
    InputStream is = getInputStream();
    final HttpUrlConnectorProvider connectorProvider = new HttpUrlConnectorProvider().useFixedLengthStreaming();
    ClientConfig clientConfig = new ClientConfig().connectorProvider(connectorProvider);
    Client client = ClientBuilder.newClient(clientConfig);
    final Response response = client.target(getBaseUri()).path("resource").request().header(HttpHeaders.CONTENT_LENGTH, LENGTH).post(Entity.entity(is, MediaType.APPLICATION_OCTET_STREAM));
    Assert.assertEquals(200, response.getStatus());
    final long count = response.readEntity(long.class);
    Assert.assertEquals("Unexpected content length received.", LENGTH, count);
}
Also used : Response(javax.ws.rs.core.Response) HttpUrlConnectorProvider(org.glassfish.jersey.client.HttpUrlConnectorProvider) InputStream(java.io.InputStream) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 2 with HttpUrlConnectorProvider

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

the class ClientBufferingDisabledTest method testDisableBufferingWithFixedLengthViaProperty.

/**
     * Test that buffering can be disabled with {@link HttpURLConnection}. By default, the
     * {@code HttpURLConnection} buffers the output entity in order to calculate the
     * Content-length request attribute. This cause problems for large entities.
     * <p>
     * This test uses {@link HttpUrlConnectorProvider#USE_FIXED_LENGTH_STREAMING} to enable
     * fix length streaming on {@code HttpURLConnection}.
     */
@Test
public void testDisableBufferingWithFixedLengthViaProperty() {
    postLatch = new CountDownLatch(1);
    // This IS sends out 10 chunks and waits whether they were received on the server. This tests
    // whether the buffering is disabled.
    InputStream is = getInputStream();
    final HttpUrlConnectorProvider connectorProvider = new HttpUrlConnectorProvider();
    ClientConfig clientConfig = new ClientConfig().connectorProvider(connectorProvider);
    clientConfig.property(HttpUrlConnectorProvider.USE_FIXED_LENGTH_STREAMING, true);
    Client client = ClientBuilder.newClient(clientConfig);
    final Response response = client.target(getBaseUri()).path("resource").request().header(HttpHeaders.CONTENT_LENGTH, LENGTH).post(Entity.entity(is, MediaType.APPLICATION_OCTET_STREAM));
    Assert.assertEquals(200, response.getStatus());
    final long count = response.readEntity(long.class);
    Assert.assertEquals("Unexpected content length received.", LENGTH, count);
}
Also used : Response(javax.ws.rs.core.Response) HttpUrlConnectorProvider(org.glassfish.jersey.client.HttpUrlConnectorProvider) InputStream(java.io.InputStream) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 3 with HttpUrlConnectorProvider

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

the class ClientBufferingDisabledTest method testDisableBufferingWithChunkEncoding.

/**
     * Test that buffering can be disabled with {@link HttpURLConnection}. By default, the
     * {@code HttpURLConnection} buffers the output entity in order to calculate the
     * Content-length request attribute. This cause problems for large entities.
     * <p>
     * In Jersey 1.x chunk encoding with {@code HttpURLConnection} was causing bugs
     * which occurred from time to time. This looks to be a case also in Jersey 2.x. This test
     * has failed unpredictably on some machines. Therefore it is disabled now.
     * </p>
     */
@Test
@Ignore("fails unpredictable (see javadoc)")
public void testDisableBufferingWithChunkEncoding() {
    postLatch = new CountDownLatch(1);
    // This IS sends out 10 chunks and waits whether they were received on the server. This tests
    // whether the buffering is disabled.
    InputStream is = getInputStream();
    final HttpUrlConnectorProvider connectorProvider = new HttpUrlConnectorProvider().chunkSize(CHUNK);
    ClientConfig clientConfig = new ClientConfig().connectorProvider(connectorProvider);
    Client client = ClientBuilder.newClient(clientConfig);
    final Response response = client.target(getBaseUri()).path("resource").request().post(Entity.entity(is, MediaType.APPLICATION_OCTET_STREAM));
    Assert.assertEquals(200, response.getStatus());
    final long count = response.readEntity(long.class);
    Assert.assertEquals("Unexpected content length received.", LENGTH, count);
}
Also used : Response(javax.ws.rs.core.Response) HttpUrlConnectorProvider(org.glassfish.jersey.client.HttpUrlConnectorProvider) InputStream(java.io.InputStream) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) Ignore(org.junit.Ignore) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 4 with HttpUrlConnectorProvider

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

the class SslHttpUrlConnectorTest method testSSLWithCustomSocketFactory.

/**
     * Test to see that the correct Http status is returned.
     *
     * @throws Exception in case of a test failure.
     */
@Test
public void testSSLWithCustomSocketFactory() throws Exception {
    final SSLContext sslContext = getSslContext();
    final CustomSSLSocketFactory socketFactory = new CustomSSLSocketFactory(sslContext);
    final ClientConfig cc = new ClientConfig().connectorProvider(new HttpUrlConnectorProvider().connectionFactory(new HttpUrlConnectorProvider.ConnectionFactory() {

        @Override
        public HttpURLConnection getConnection(final URL url) throws IOException {
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            connection.setSSLSocketFactory(socketFactory);
            return connection;
        }
    }));
    final Client client = ClientBuilder.newBuilder().withConfig(cc).sslContext(sslContext).register(HttpAuthenticationFeature.basic("user", "password")).register(LoggingFeature.class).build();
    final Response response = client.target(Server.BASE_URI).path("/").request().get();
    assertEquals(200, response.getStatus());
    assertTrue(socketFactory.isVisited());
}
Also used : Response(javax.ws.rs.core.Response) HttpUrlConnectorProvider(org.glassfish.jersey.client.HttpUrlConnectorProvider) LoggingFeature(org.glassfish.jersey.logging.LoggingFeature) SSLContext(javax.net.ssl.SSLContext) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 5 with HttpUrlConnectorProvider

use of org.glassfish.jersey.client.HttpUrlConnectorProvider in project openstack4j by ContainX.

the class ClientFactory method addProxy.

private static void addProxy(ClientConfig cc, Config config) {
    if (config.getProxy() != null) {
        HttpUrlConnectorProvider cp = new HttpUrlConnectorProvider();
        cc.connectorProvider(cp);
        final Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress(config.getProxy().getRawHost(), config.getProxy().getPort()));
        cp.connectionFactory(new ConnectionFactory() {

            @Override
            public HttpURLConnection getConnection(URL url) throws IOException {
                return (HttpURLConnection) url.openConnection(proxy);
            }
        });
    }
}
Also used : Proxy(java.net.Proxy) ConnectionFactory(org.glassfish.jersey.client.HttpUrlConnectorProvider.ConnectionFactory) HttpURLConnection(java.net.HttpURLConnection) HttpUrlConnectorProvider(org.glassfish.jersey.client.HttpUrlConnectorProvider) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

HttpUrlConnectorProvider (org.glassfish.jersey.client.HttpUrlConnectorProvider)6 Client (javax.ws.rs.client.Client)4 Response (javax.ws.rs.core.Response)4 ClientConfig (org.glassfish.jersey.client.ClientConfig)4 Test (org.junit.Test)4 InputStream (java.io.InputStream)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 JerseyTest (org.glassfish.jersey.test.JerseyTest)3 URL (java.net.URL)2 LoggingFeature (org.glassfish.jersey.logging.LoggingFeature)2 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 InetSocketAddress (java.net.InetSocketAddress)1 Proxy (java.net.Proxy)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1 SSLContext (javax.net.ssl.SSLContext)1 ConnectionFactory (org.glassfish.jersey.client.HttpUrlConnectorProvider.ConnectionFactory)1 Ignore (org.junit.Ignore)1