diff --git a/google-http-client-apache-legacy/pom.xml b/google-http-client-apache-legacy/pom.xml index 3914fc251..8c19415a0 100644 --- a/google-http-client-apache-legacy/pom.xml +++ b/google-http-client-apache-legacy/pom.xml @@ -7,9 +7,9 @@ 1.27.1-SNAPSHOT ../pom.xml - google-http-client-apache-legacy + google-http-client-apache 1.27.1-SNAPSHOT - Apache HTTP transport for the Google HTTP Client Library for Java. + Legacy Apache HTTP transport for the Google HTTP Client Library for Java. @@ -102,6 +102,11 @@ guava test + + org.apache.httpcomponents + httpclient + 4.2.6 + org.mockito mockito-all diff --git a/google-http-client-apache/pom.xml b/google-http-client-apache/pom.xml index 6a40174c5..bafdc0375 100644 --- a/google-http-client-apache/pom.xml +++ b/google-http-client-apache/pom.xml @@ -8,7 +8,7 @@ ../pom.xml google-http-client-apache - 1.27.1-SNAPSHOT + 2.0.1-SNAPSHOT Apache HTTP transport for the Google HTTP Client Library for Java. @@ -102,6 +102,10 @@ guava test + + org.apache.httpcomponents + httpclient + org.mockito mockito-all diff --git a/google-http-client-apache/src/main/java/com/google/api/client/http/apache/ApacheHttpRequest.java b/google-http-client-apache/src/main/java/com/google/api/client/http/apache/ApacheHttpRequest.java index b31b20594..f498b7150 100644 --- a/google-http-client-apache/src/main/java/com/google/api/client/http/apache/ApacheHttpRequest.java +++ b/google-http-client-apache/src/main/java/com/google/api/client/http/apache/ApacheHttpRequest.java @@ -20,10 +20,8 @@ import java.io.IOException; import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.client.HttpClient; +import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpRequestBase; -import org.apache.http.conn.params.ConnManagerParams; -import org.apache.http.params.HttpConnectionParams; -import org.apache.http.params.HttpParams; /** * @author Yaniv Inbar @@ -33,9 +31,12 @@ final class ApacheHttpRequest extends LowLevelHttpRequest { private final HttpRequestBase request; + private RequestConfig.Builder requestConfig; + ApacheHttpRequest(HttpClient httpClient, HttpRequestBase request) { this.httpClient = httpClient; this.request = request; + this.requestConfig = RequestConfig.custom().setRedirectsEnabled(false); } @Override @@ -45,10 +46,8 @@ public void addHeader(String name, String value) { @Override public void setTimeout(int connectTimeout, int readTimeout) throws IOException { - HttpParams params = request.getParams(); - ConnManagerParams.setTimeout(params, connectTimeout); - HttpConnectionParams.setConnectionTimeout(params, connectTimeout); - HttpConnectionParams.setSoTimeout(params, readTimeout); + requestConfig.setConnectionRequestTimeout(connectTimeout) + .setSocketTimeout(readTimeout); } @Override @@ -62,6 +61,7 @@ public LowLevelHttpResponse execute() throws IOException { entity.setContentType(getContentType()); ((HttpEntityEnclosingRequest) request).setEntity(entity); } + request.setConfig(requestConfig.build()); return new ApacheHttpResponse(request, httpClient.execute(request)); } } diff --git a/google-http-client-apache/src/main/java/com/google/api/client/http/apache/ApacheHttpTransport.java b/google-http-client-apache/src/main/java/com/google/api/client/http/apache/ApacheHttpTransport.java index 0b55a4ed0..ecc7e0c13 100644 --- a/google-http-client-apache/src/main/java/com/google/api/client/http/apache/ApacheHttpTransport.java +++ b/google-http-client-apache/src/main/java/com/google/api/client/http/apache/ApacheHttpTransport.java @@ -15,9 +15,7 @@ package com.google.api.client.http.apache; import com.google.api.client.http.HttpMethods; -import com.google.api.client.http.HttpRequest; import com.google.api.client.http.HttpTransport; -import com.google.api.client.util.Beta; import com.google.api.client.util.Preconditions; import com.google.api.client.util.SecurityUtils; import com.google.api.client.util.SslUtils; @@ -27,9 +25,8 @@ import java.security.GeneralSecurityException; import java.security.KeyStore; import java.security.cert.CertificateFactory; +import java.util.concurrent.TimeUnit; import javax.net.ssl.SSLContext; -import org.apache.http.HttpHost; -import org.apache.http.HttpVersion; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; @@ -40,24 +37,12 @@ import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.methods.HttpTrace; -import org.apache.http.client.params.ClientPNames; -import org.apache.http.conn.ClientConnectionManager; -import org.apache.http.conn.params.ConnManagerParams; -import org.apache.http.conn.params.ConnPerRouteBean; -import org.apache.http.conn.params.ConnRouteParams; -import org.apache.http.conn.scheme.PlainSocketFactory; -import org.apache.http.conn.scheme.Scheme; -import org.apache.http.conn.scheme.SchemeRegistry; -import org.apache.http.conn.ssl.SSLSocketFactory; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.impl.client.DefaultHttpRequestRetryHandler; -import org.apache.http.impl.conn.DefaultHttpRoutePlanner; -import org.apache.http.impl.conn.ProxySelectorRoutePlanner; -import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; -import org.apache.http.params.BasicHttpParams; -import org.apache.http.params.HttpConnectionParams; -import org.apache.http.params.HttpParams; -import org.apache.http.params.HttpProtocolParams; +import org.apache.http.config.SocketConfig; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.impl.conn.SystemDefaultRoutePlanner; /** * Thread-safe HTTP transport based on the Apache HTTP Client library. @@ -71,8 +56,7 @@ *

* Default settings are specified in {@link #newDefaultHttpClient()}. Use the * {@link #ApacheHttpTransport(HttpClient)} constructor to override the Apache HTTP Client used. - * Alternatively, use {@link #ApacheHttpTransport()} and change the {@link #getHttpClient()}. Please - * read the Apache HTTP * Client connection management tutorial for more complex configuration options. *

@@ -88,10 +72,6 @@ public final class ApacheHttpTransport extends HttpTransport { /** * Constructor that uses {@link #newDefaultHttpClient()} for the Apache HTTP client. * - *

- * Use {@link Builder} to modify HTTP client options. - *

- * * @since 1.3 */ public ApacheHttpTransport() { @@ -102,34 +82,23 @@ public ApacheHttpTransport() { * Constructor that allows an alternative Apache HTTP client to be used. * *

- * Note that a few settings are overridden: + * Note that in the previous version, we tried overrode several settings, however, we are no + * longer able to do so. *

+ * + *

If you choose to provide your own Apache HttpClient implementation, be sure that

*
    - *
  • HTTP version is set to 1.1 using {@link HttpProtocolParams#setVersion} with - * {@link HttpVersion#HTTP_1_1}.
  • - *
  • Redirects are disabled using {@link ClientPNames#HANDLE_REDIRECTS}.
  • - *
  • {@link ConnManagerParams#setTimeout} and {@link HttpConnectionParams#setConnectionTimeout} - * are set on each request based on {@link HttpRequest#getConnectTimeout()}.
  • - *
  • {@link HttpConnectionParams#setSoTimeout} is set on each request based on - * {@link HttpRequest#getReadTimeout()}.
  • + *
  • HTTP version is set to 1.1.
  • + *
  • Redirects are disabled (google-http-client handles redirects).
  • + *
  • Retries are disabled (google-http-client handles retries).
  • *
* - *

- * Use {@link Builder} for a more user-friendly way to modify the HTTP client options. - *

- * * @param httpClient Apache HTTP client to use * * @since 1.6 */ public ApacheHttpTransport(HttpClient httpClient) { this.httpClient = httpClient; - HttpParams params = httpClient.getParams(); - if (params == null) { - params = newDefaultHttpClient().getParams(); - } - HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); - params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false); } /** @@ -137,63 +106,46 @@ public ApacheHttpTransport(HttpClient httpClient) { * {@link #ApacheHttpTransport()} constructor. * *

- * Use this constructor if you want to customize the default Apache HTTP client. Settings: + * Settings: *

*
    - *
  • The client connection manager is set to {@link ThreadSafeClientConnManager}.
  • - *
  • The socket buffer size is set to 8192 using - * {@link HttpConnectionParams#setSocketBufferSize}.
  • - *
  • - *
  • The route planner uses {@link ProxySelectorRoutePlanner} with + *
  • The client connection manager is set to {@link PoolingHttpClientConnectionManager}.
  • + *
  • The socket buffer size is set to 8192 using {@link SocketConfig}.
  • + *
  • + *
  • The route planner uses {@link SystemDefaultRoutePlanner} with * {@link ProxySelector#getDefault()}, which uses the proxy settings from system * properties.
  • *
* * @return new instance of the Apache HTTP client - * @since 1.6 - */ - public static DefaultHttpClient newDefaultHttpClient() { - return newDefaultHttpClient( - SSLSocketFactory.getSocketFactory(), newDefaultHttpParams(), ProxySelector.getDefault()); - } - - /** Returns a new instance of the default HTTP parameters we use. */ - static HttpParams newDefaultHttpParams() { - HttpParams params = new BasicHttpParams(); - // Turn off stale checking. Our connections break all the time anyway, - // and it's not worth it to pay the penalty of checking every time. - HttpConnectionParams.setStaleCheckingEnabled(params, false); - HttpConnectionParams.setSocketBufferSize(params, 8192); - ConnManagerParams.setMaxTotalConnections(params, 200); - ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(20)); - return params; - } - - /** - * Creates a new instance of the Apache HTTP client that is used by the - * {@link #ApacheHttpTransport()} constructor. - * - * @param socketFactory SSL socket factory - * @param params HTTP parameters - * @param proxySelector HTTP proxy selector to use {@link ProxySelectorRoutePlanner} or - * {@code null} for {@link DefaultHttpRoutePlanner} - * @return new instance of the Apache HTTP client + * @since 2.0 */ - static DefaultHttpClient newDefaultHttpClient( - SSLSocketFactory socketFactory, HttpParams params, ProxySelector proxySelector) { - // See http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html - SchemeRegistry registry = new SchemeRegistry(); - registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); - registry.register(new Scheme("https", socketFactory, 443)); - ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(params, registry); - DefaultHttpClient defaultHttpClient = new DefaultHttpClient(connectionManager, params); - defaultHttpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); - if (proxySelector != null) { - defaultHttpClient.setRoutePlanner(new ProxySelectorRoutePlanner(registry, proxySelector)); - } - return defaultHttpClient; + public static HttpClient newDefaultHttpClient() { + // Set socket buffer sizes to 8192 + SocketConfig socketConfig = + SocketConfig.custom() + .setRcvBufSize(8192) + .setSndBufSize(8192) + .build(); + + PoolingHttpClientConnectionManager connectionManager = + new PoolingHttpClientConnectionManager(-1, TimeUnit.MILLISECONDS); + // Disable the stale connection check (previously configured in the HttpConnectionParams + connectionManager.setValidateAfterInactivity(-1); + + return HttpClientBuilder.create() + .useSystemProperties() + .setSSLSocketFactory(SSLConnectionSocketFactory.getSocketFactory()) + .setDefaultSocketConfig(socketConfig) + .setMaxConnTotal(200) + .setMaxConnPerRoute(20) + .setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault())) + .setConnectionManager(connectionManager) + .disableRedirectHandling() + .disableAutomaticRetries() + .build(); } @Override @@ -233,8 +185,10 @@ protected ApacheHttpRequest buildRequest(String method, String url) { * @since 1.4 */ @Override - public void shutdown() { - httpClient.getConnectionManager().shutdown(); + public void shutdown() throws IOException { + if (httpClient instanceof CloseableHttpClient) { + ((CloseableHttpClient) httpClient).close(); + } } /** @@ -245,171 +199,4 @@ public void shutdown() { public HttpClient getHttpClient() { return httpClient; } - - /** - * Builder for {@link ApacheHttpTransport}. - * - *

- * Implementation is not thread-safe. - *

- * - * @since 1.13 - */ - public static final class Builder { - - /** SSL socket factory. */ - private SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory(); - - /** HTTP parameters. */ - private HttpParams params = newDefaultHttpParams(); - - /** - * HTTP proxy selector to use {@link ProxySelectorRoutePlanner} or {@code null} for - * {@link DefaultHttpRoutePlanner}. - */ - private ProxySelector proxySelector = ProxySelector.getDefault(); - - /** - * Sets the HTTP proxy to use {@link DefaultHttpRoutePlanner} or {@code null} to use - * {@link #setProxySelector(ProxySelector)} with {@link ProxySelector#getDefault()}. - * - *

- * By default it is {@code null}, which uses the proxy settings from system - * properties. - *

- * - *

- * For example: - *

- * - *
-       setProxy(new HttpHost("127.0.0.1", 8080))
-     * 
- */ - public Builder setProxy(HttpHost proxy) { - ConnRouteParams.setDefaultProxy(params, proxy); - if (proxy != null) { - proxySelector = null; - } - return this; - } - - /** - * Sets the HTTP proxy selector to use {@link ProxySelectorRoutePlanner} or {@code null} for - * {@link DefaultHttpRoutePlanner}. - * - *

- * By default it is {@link ProxySelector#getDefault()} which uses the proxy settings from system - * properties. - *

- */ - public Builder setProxySelector(ProxySelector proxySelector) { - this.proxySelector = proxySelector; - if (proxySelector != null) { - ConnRouteParams.setDefaultProxy(params, null); - } - return this; - } - /** - * Sets the SSL socket factory based on root certificates in a Java KeyStore. - * - *

- * Example usage: - *

- * - *
-    trustCertificatesFromJavaKeyStore(new FileInputStream("certs.jks"), "password");
-     * 
- * - * @param keyStoreStream input stream to the key store (closed at the end of this method in a - * finally block) - * @param storePass password protecting the key store file - * @since 1.14 - */ - public Builder trustCertificatesFromJavaKeyStore(InputStream keyStoreStream, String storePass) - throws GeneralSecurityException, IOException { - KeyStore trustStore = SecurityUtils.getJavaKeyStore(); - SecurityUtils.loadKeyStore(trustStore, keyStoreStream, storePass); - return trustCertificates(trustStore); - } - - /** - * Sets the SSL socket factory based root certificates generated from the specified stream using - * {@link CertificateFactory#generateCertificates(InputStream)}. - * - *

- * Example usage: - *

- * - *
-    trustCertificatesFromStream(new FileInputStream("certs.pem"));
-     * 
- * - * @param certificateStream certificate stream - * @since 1.14 - */ - public Builder trustCertificatesFromStream(InputStream certificateStream) - throws GeneralSecurityException, IOException { - KeyStore trustStore = SecurityUtils.getJavaKeyStore(); - trustStore.load(null, null); - SecurityUtils.loadKeyStoreFromCertificates( - trustStore, SecurityUtils.getX509CertificateFactory(), certificateStream); - return trustCertificates(trustStore); - } - - /** - * Sets the SSL socket factory based on a root certificate trust store. - * - * @param trustStore certificate trust store (use for example {@link SecurityUtils#loadKeyStore} - * or {@link SecurityUtils#loadKeyStoreFromCertificates}) - * - * @since 1.14 - */ - public Builder trustCertificates(KeyStore trustStore) throws GeneralSecurityException { - SSLContext sslContext = SslUtils.getTlsSslContext(); - SslUtils.initSslContext(sslContext, trustStore, SslUtils.getPkixTrustManagerFactory()); - return setSocketFactory(new SSLSocketFactoryExtension(sslContext)); - } - - /** - * {@link Beta}
- * Disables validating server SSL certificates by setting the SSL socket factory using - * {@link SslUtils#trustAllSSLContext()} for the SSL context and - * {@link SSLSocketFactory#ALLOW_ALL_HOSTNAME_VERIFIER} for the host name verifier. - * - *

- * Be careful! Disabling certificate validation is dangerous and should only be done in testing - * environments. - *

- */ - @Beta - public Builder doNotValidateCertificate() throws GeneralSecurityException { - socketFactory = new SSLSocketFactoryExtension(SslUtils.trustAllSSLContext()); - socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); - return this; - } - - /** Sets the SSL socket factory ({@link SSLSocketFactory#getSocketFactory()} by default). */ - public Builder setSocketFactory(SSLSocketFactory socketFactory) { - this.socketFactory = Preconditions.checkNotNull(socketFactory); - return this; - } - - /** Returns the SSL socket factory ({@link SSLSocketFactory#getSocketFactory()} by default). */ - public SSLSocketFactory getSSLSocketFactory() { - return socketFactory; - } - - /** Returns the HTTP parameters. */ - public HttpParams getHttpParams() { - return params; - } - - /** Returns a new instance of {@link ApacheHttpTransport} based on the options. */ - public ApacheHttpTransport build() { - return new ApacheHttpTransport(newDefaultHttpClient(socketFactory, params, proxySelector)); - } - } } diff --git a/google-http-client-apache/src/main/java/com/google/api/client/http/apache/SSLSocketFactoryExtension.java b/google-http-client-apache/src/main/java/com/google/api/client/http/apache/SSLSocketFactoryExtension.java deleted file mode 100644 index 3d507371b..000000000 --- a/google-http-client-apache/src/main/java/com/google/api/client/http/apache/SSLSocketFactoryExtension.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2013 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ - -package com.google.api.client.http.apache; - -import java.io.IOException; -import java.net.Socket; -import java.net.UnknownHostException; -import java.security.KeyManagementException; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.UnrecoverableKeyException; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLSocket; -import org.apache.http.conn.ssl.SSLSocketFactory; - -/** - * Implementation of SSL socket factory that extends Apache's implementation to provide - * functionality missing from the Android SDK that is available in Apache HTTP Client. - * - * @author Yaniv Inbar - */ -final class SSLSocketFactoryExtension extends SSLSocketFactory { - - /** Wrapped Java SSL socket factory. */ - private final javax.net.ssl.SSLSocketFactory socketFactory; - - /** - * @param sslContext SSL context - */ - SSLSocketFactoryExtension(SSLContext sslContext) throws KeyManagementException, - UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException { - super((KeyStore) null); - socketFactory = sslContext.getSocketFactory(); - } - - @Override - public Socket createSocket() throws IOException { - return socketFactory.createSocket(); - } - - @Override - public Socket createSocket(Socket socket, String host, int port, boolean autoClose) - throws IOException, UnknownHostException { - SSLSocket sslSocket = (SSLSocket) socketFactory.createSocket(socket, host, port, autoClose); - getHostnameVerifier().verify(host, sslSocket); - return sslSocket; - } -} diff --git a/google-http-client-apache/src/test/java/com/google/api/client/http/apache/ApacheHttpTransportTest.java b/google-http-client-apache/src/test/java/com/google/api/client/http/apache/ApacheHttpTransportTest.java index 07f6c29c7..ef46084fe 100644 --- a/google-http-client-apache/src/test/java/com/google/api/client/http/apache/ApacheHttpTransportTest.java +++ b/google-http-client-apache/src/test/java/com/google/api/client/http/apache/ApacheHttpTransportTest.java @@ -14,48 +14,73 @@ package com.google.api.client.http.apache; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import com.google.api.client.http.LowLevelHttpResponse; import com.google.api.client.util.ByteArrayStreamingContent; import com.google.api.client.util.StringUtils; -import junit.framework.TestCase; +import java.io.IOException; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.http.Header; +import org.apache.http.HttpClientConnection; +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpResponse; import org.apache.http.HttpVersion; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpUriRequest; -import org.apache.http.client.params.ClientPNames; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.impl.client.DefaultHttpRequestRetryHandler; -import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; -import org.apache.http.params.CoreConnectionPNames; -import org.apache.http.params.HttpParams; -import org.apache.http.params.HttpProtocolParams; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicHttpResponse; +import org.apache.http.protocol.HttpContext; +import org.apache.http.protocol.HttpRequestExecutor; +import org.junit.Test; /** * Tests {@link ApacheHttpTransport}. * * @author Yaniv Inbar */ -public class ApacheHttpTransportTest extends TestCase { +public class ApacheHttpTransportTest { + @Test public void testApacheHttpTransport() { ApacheHttpTransport transport = new ApacheHttpTransport(); - DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient(); - checkDefaultHttpClient(httpClient); - checkHttpClient(httpClient); + checkHttpTransport(transport); } + @Test public void testApacheHttpTransportWithParam() { - ApacheHttpTransport transport = new ApacheHttpTransport(new DefaultHttpClient()); - checkHttpClient(transport.getHttpClient()); + ApacheHttpTransport transport = new ApacheHttpTransport(HttpClients.custom().build()); + checkHttpTransport(transport); } + @Test public void testNewDefaultHttpClient() { - checkDefaultHttpClient(ApacheHttpTransport.newDefaultHttpClient()); + HttpClient client = ApacheHttpTransport.newDefaultHttpClient(); + checkHttpClient(client); } + private void checkHttpTransport(ApacheHttpTransport transport) { + assertNotNull(transport); + HttpClient client = transport.getHttpClient(); + checkHttpClient(client); + } + + private void checkHttpClient(HttpClient client) { + assertNotNull(client); + // TODO(chingor): Is it possible to test this effectively? The newer HttpClient implementations + // are read-only and we're testing that we built the client with the right configuration + } + + @Test public void testRequestsWithContent() throws Exception { HttpClient mockClient = mock(HttpClient.class); HttpResponse mockResponse = mock(HttpResponse.class); @@ -103,19 +128,51 @@ private void execute(ApacheHttpRequest request) throws Exception { request.execute(); } - private void checkDefaultHttpClient(DefaultHttpClient client) { - HttpParams params = client.getParams(); - assertTrue(client.getConnectionManager() instanceof ThreadSafeClientConnManager); - assertEquals(8192, params.getIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, -1)); - DefaultHttpRequestRetryHandler retryHandler = - (DefaultHttpRequestRetryHandler) client.getHttpRequestRetryHandler(); - assertEquals(0, retryHandler.getRetryCount()); - assertFalse(retryHandler.isRequestSentRetryEnabled()); + @Test + public void testRequestShouldNotFollowRedirects() throws IOException { + final AtomicInteger requestsAttempted = new AtomicInteger(0); + HttpRequestExecutor requestExecutor = new HttpRequestExecutor() { + @Override + public HttpResponse execute(HttpRequest request, HttpClientConnection conn, + HttpContext context) throws IOException, HttpException { + HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, 302, null); + resp.addHeader("location", "https://google.com/path"); + requestsAttempted.incrementAndGet(); + return resp; + } + }; + HttpClient client = HttpClients.custom().setRequestExecutor(requestExecutor).build(); + ApacheHttpTransport transport = new ApacheHttpTransport(client); + ApacheHttpRequest request = transport.buildRequest("GET", "https://google.com"); + LowLevelHttpResponse response = request.execute(); + assertEquals(1, requestsAttempted.get()); + assertEquals(302, response.getStatusCode()); } - private void checkHttpClient(HttpClient client) { - HttpParams params = client.getParams(); - assertFalse(params.getBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true)); - assertEquals(HttpVersion.HTTP_1_1, HttpProtocolParams.getVersion(params)); + @Test + public void testRequestCanSetHeaders() { + final AtomicBoolean interceptorCalled = new AtomicBoolean(false); + HttpClient client = HttpClients.custom().addInterceptorFirst(new HttpRequestInterceptor() { + @Override + public void process(HttpRequest request, HttpContext context) + throws HttpException, IOException { + Header header = request.getFirstHeader("foo"); + assertNotNull("Should have found header", header); + assertEquals("bar", header.getValue()); + interceptorCalled.set(true); + throw new IOException("cancelling request"); + } + }).build(); + + ApacheHttpTransport transport = new ApacheHttpTransport(client); + ApacheHttpRequest request = transport.buildRequest("GET", "https://google.com"); + request.addHeader("foo", "bar"); + try { + LowLevelHttpResponse response = request.execute(); + fail("should not actually make the request"); + } catch (IOException exception) { + assertEquals("cancelling request", exception.getMessage()); + } + assertTrue("Expected to have called our test interceptor", interceptorCalled.get()); } } diff --git a/google-http-client-bom/pom.xml b/google-http-client-bom/pom.xml index 381a72528..fbf36651d 100644 --- a/google-http-client-bom/pom.xml +++ b/google-http-client-bom/pom.xml @@ -73,7 +73,7 @@ com.google.http-client google-http-client-apache - 1.27.1-SNAPSHOT + 2.0.1-SNAPSHOT com.google.http-client diff --git a/google-http-client/pom.xml b/google-http-client/pom.xml index b036147b7..8243c83d0 100644 --- a/google-http-client/pom.xml +++ b/google-http-client/pom.xml @@ -165,14 +165,9 @@ mockito-all test - - org.apache.httpcomponents - httpclient - commons-codec commons-codec - provided com.google.j2objc diff --git a/pom.xml b/pom.xml index 8ec73aa68..84c905f42 100644 --- a/pom.xml +++ b/pom.xml @@ -178,22 +178,22 @@ com.google.http-client google-http-client - ${project.version} + ${project.http-client.version} com.google.http-client google-http-client-apache - ${project.version} + ${project.http-client.version} com.google.http-client google-http-client-appengine - ${project.version} + ${project.http-client.version} com.google.http-client google-http-client-android - ${project.version} + ${project.http-client.version} com.google.http-client @@ -203,37 +203,37 @@ com.google.http-client google-http-client-gson - ${project.version} + ${project.http-client.version} com.google.http-client google-http-client-jackson - ${project.version} + ${project.http-client.version} com.google.http-client google-http-client-jackson2 - ${project.version} + ${project.http-client.version} com.google.http-client google-http-client-jdo - ${project.version} + ${project.http-client.version} com.google.http-client google-http-client-xml - ${project.version} + ${project.http-client.version} com.google.http-client google-http-client-findbugs - ${project.version} + ${project.http-client.version} com.google.http-client google-http-client-test - ${project.version} + ${project.http-client.version} org.mockito @@ -509,7 +509,7 @@ com.google.http-client google-http-client-findbugs - ${project.version} + ${project.http-client.version}
@@ -564,6 +564,7 @@ - google-api-java-client/google-api-client-assembly/android-properties (make the filenames match the version here) - Internally, update the default features.json file --> + 1.27.1-SNAPSHOT 1.9.64 UTF-8 3.0.2 diff --git a/versions.txt b/versions.txt index ba1abcf45..dab7c58cc 100644 --- a/versions.txt +++ b/versions.txt @@ -6,7 +6,7 @@ google-http-client-bom:1.27.0:1.27.1-SNAPSHOT google-http-client-parent:1.27.0:1.27.1-SNAPSHOT google-http-client-android:1.27.0:1.27.1-SNAPSHOT google-http-client-android-test:1.27.0:1.27.1-SNAPSHOT -google-http-client-apache:1.27.0:1.27.1-SNAPSHOT +google-http-client-apache:2.0.0:2.0.1-SNAPSHOT google-http-client-apache-legacy:1.27.0:1.27.1-SNAPSHOT google-http-client-appengine:1.27.0:1.27.1-SNAPSHOT google-http-client-assembly:1.27.0:1.27.1-SNAPSHOT