From 72490f96a68419201f155de2fd8929c427703e61 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Tue, 14 May 2019 15:53:02 -0700 Subject: [PATCH 01/12] Republish the fixed apache extensions as google-http-client-apache-v2 --- google-http-client-apache-v2/pom.xml | 115 ++++++++++ .../http/apache/v2/ApacheHttpRequest.java | 67 ++++++ .../http/apache/v2/ApacheHttpResponse.java | 120 +++++++++++ .../http/apache/v2/ApacheHttpTransport.java | 202 ++++++++++++++++++ .../client/http/apache/v2/ContentEntity.java | 70 ++++++ .../http/apache/v2/HttpExtensionMethod.java | 42 ++++ .../client/http/apache/v2/package-info.java | 23 ++ .../apache/v2/ApacheHttpTransportTest.java | 178 +++++++++++++++ pom.xml | 3 +- 9 files changed, 818 insertions(+), 2 deletions(-) create mode 100644 google-http-client-apache-v2/pom.xml create mode 100644 google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpRequest.java create mode 100644 google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpResponse.java create mode 100644 google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpTransport.java create mode 100644 google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ContentEntity.java create mode 100644 google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/HttpExtensionMethod.java create mode 100644 google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/package-info.java create mode 100644 google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java diff --git a/google-http-client-apache-v2/pom.xml b/google-http-client-apache-v2/pom.xml new file mode 100644 index 000000000..839f58846 --- /dev/null +++ b/google-http-client-apache-v2/pom.xml @@ -0,0 +1,115 @@ + + 4.0.0 + + com.google.http-client + google-http-client-parent + 1.29.2-SNAPSHOT0 + ../pom.xml + + google-http-client-apache-v2 + 1.29.2-SNAPSHOT + Apache HTTP transport v2 for the Google HTTP Client Library for Java. + + + + + maven-javadoc-plugin + + + http://download.oracle.com/javase/7/docs/api/ + https://jar-download.com/artifacts/org.codehaus.jackson/jackson-core-asl/${project.jackson-core-asl.version}/documentation + + ${project.name} ${project.version} + ${project.artifactId} ${project.version} + + + + maven-source-plugin + + + source-jar + compile + + jar + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 1.5 + + + add-test-source + generate-test-sources + + add-test-source + + + + target/generated-test-sources + + + + + + + maven-jar-plugin + + + ${project.build.outputDirectory}/META-INF/MANIFEST.MF + + com.google.api.client.http.apache.v2 + + + + + + org.apache.felix + maven-bundle-plugin + 2.5.4 + + + bundle-manifest + process-classes + + manifest + + + + + + + + + com.google.http-client + google-http-client + + + com.google.http-client + google-http-client-test + test + + + junit + junit + test + + + com.google.guava + guava + test + + + org.apache.httpcomponents + httpclient + + + org.mockito + mockito-all + test + + + diff --git a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpRequest.java b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpRequest.java new file mode 100644 index 000000000..4542a3383 --- /dev/null +++ b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpRequest.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2010 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.v2; + +import com.google.api.client.http.LowLevelHttpRequest; +import com.google.api.client.http.LowLevelHttpResponse; +import com.google.api.client.util.Preconditions; +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; + +/** + * @author Yaniv Inbar + */ +final class ApacheHttpRequest extends LowLevelHttpRequest { + private final HttpClient httpClient; + + 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 + public void addHeader(String name, String value) { + request.addHeader(name, value); + } + + @Override + public void setTimeout(int connectTimeout, int readTimeout) throws IOException { + requestConfig.setConnectionRequestTimeout(connectTimeout) + .setSocketTimeout(readTimeout); + } + + @Override + public LowLevelHttpResponse execute() throws IOException { + if (getStreamingContent() != null) { + Preconditions.checkArgument(request instanceof HttpEntityEnclosingRequest, + "Apache HTTP client does not support %s requests with content.", + request.getRequestLine().getMethod()); + ContentEntity entity = new ContentEntity(getContentLength(), getStreamingContent()); + entity.setContentEncoding(getContentEncoding()); + 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-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpResponse.java b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpResponse.java new file mode 100644 index 000000000..ecb6cbc9e --- /dev/null +++ b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpResponse.java @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2010 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.v2; + +import com.google.api.client.http.LowLevelHttpResponse; +import java.io.IOException; +import java.io.InputStream; +import org.apache.http.Header; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.StatusLine; +import org.apache.http.client.methods.HttpRequestBase; + +final class ApacheHttpResponse extends LowLevelHttpResponse { + + private final HttpRequestBase request; + private final HttpResponse response; + private final Header[] allHeaders; + + ApacheHttpResponse(HttpRequestBase request, HttpResponse response) { + this.request = request; + this.response = response; + allHeaders = response.getAllHeaders(); + } + + @Override + public int getStatusCode() { + StatusLine statusLine = response.getStatusLine(); + return statusLine == null ? 0 : statusLine.getStatusCode(); + } + + @Override + public InputStream getContent() throws IOException { + HttpEntity entity = response.getEntity(); + return entity == null ? null : entity.getContent(); + } + + @Override + public String getContentEncoding() { + HttpEntity entity = response.getEntity(); + if (entity != null) { + Header contentEncodingHeader = entity.getContentEncoding(); + if (contentEncodingHeader != null) { + return contentEncodingHeader.getValue(); + } + } + return null; + } + + @Override + public long getContentLength() { + HttpEntity entity = response.getEntity(); + return entity == null ? -1 : entity.getContentLength(); + } + + @Override + public String getContentType() { + HttpEntity entity = response.getEntity(); + if (entity != null) { + Header contentTypeHeader = entity.getContentType(); + if (contentTypeHeader != null) { + return contentTypeHeader.getValue(); + } + } + return null; + } + + @Override + public String getReasonPhrase() { + StatusLine statusLine = response.getStatusLine(); + return statusLine == null ? null : statusLine.getReasonPhrase(); + } + + @Override + public String getStatusLine() { + StatusLine statusLine = response.getStatusLine(); + return statusLine == null ? null : statusLine.toString(); + } + + public String getHeaderValue(String name) { + return response.getLastHeader(name).getValue(); + } + + @Override + public int getHeaderCount() { + return allHeaders.length; + } + + @Override + public String getHeaderName(int index) { + return allHeaders[index].getName(); + } + + @Override + public String getHeaderValue(int index) { + return allHeaders[index].getValue(); + } + + /** + * Aborts execution of the request. + * + * @since 1.30 + */ + @Override + public void disconnect() { + request.abort(); + } +} diff --git a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpTransport.java b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpTransport.java new file mode 100644 index 000000000..eabff4c5e --- /dev/null +++ b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpTransport.java @@ -0,0 +1,202 @@ +/* + * Copyright (c) 2010 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.v2; + +import com.google.api.client.http.HttpMethods; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.util.Preconditions; +import com.google.api.client.util.SecurityUtils; +import com.google.api.client.util.SslUtils; +import java.io.IOException; +import java.io.InputStream; +import java.net.ProxySelector; +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.client.HttpClient; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpHead; +import org.apache.http.client.methods.HttpOptions; +import org.apache.http.client.methods.HttpPatch; +import org.apache.http.client.methods.HttpPost; +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.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. + * + *

+ * Implementation is thread-safe, as long as any parameter modification to the + * {@link #getHttpClient() Apache HTTP Client} is only done at initialization time. For maximum + * efficiency, applications should use a single globally-shared instance of the HTTP transport. + *

+ * + *

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

+ * + * @since 1.30 + * @author Yaniv Inbar + */ +public final class ApacheHttpTransport extends HttpTransport { + + /** Apache HTTP client. */ + private final HttpClient httpClient; + + /** + * Constructor that uses {@link #newDefaultHttpClient()} for the Apache HTTP client. + * + * @since 1.30 + */ + public ApacheHttpTransport() { + this(newDefaultHttpClient()); + } + + /** + * Constructor that allows an alternative Apache HTTP client to be used. + * + *

+ * 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

+ * + * + * @param httpClient Apache HTTP client to use + * + * @since 1.30 + */ + public ApacheHttpTransport(HttpClient httpClient) { + this.httpClient = httpClient; + } + + /** + * Creates a new instance of the Apache HTTP client that is used by the + * {@link #ApacheHttpTransport()} constructor. + * + *

+ * Settings: + *

+ * + * + * @return new instance of the Apache HTTP client + * @since 1.30 + */ + 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 + public boolean supportsMethod(String method) { + return true; + } + + @Override + protected ApacheHttpRequest buildRequest(String method, String url) { + HttpRequestBase requestBase; + if (method.equals(HttpMethods.DELETE)) { + requestBase = new HttpDelete(url); + } else if (method.equals(HttpMethods.GET)) { + requestBase = new HttpGet(url); + } else if (method.equals(HttpMethods.HEAD)) { + requestBase = new HttpHead(url); + } else if (method.equals(HttpMethods.PATCH)) { + requestBase = new HttpPatch(url); + } else if (method.equals(HttpMethods.POST)) { + requestBase = new HttpPost(url); + } else if (method.equals(HttpMethods.PUT)) { + requestBase = new HttpPut(url); + } else if (method.equals(HttpMethods.TRACE)) { + requestBase = new HttpTrace(url); + } else if (method.equals(HttpMethods.OPTIONS)) { + requestBase = new HttpOptions(url); + } else { + requestBase = new HttpExtensionMethod(method, url); + } + return new ApacheHttpRequest(httpClient, requestBase); + } + + /** + * Shuts down the connection manager and releases allocated resources. This includes closing all + * connections, whether they are currently used or not. + * + * @since 1.30 + */ + @Override + public void shutdown() throws IOException { + if (httpClient instanceof CloseableHttpClient) { + ((CloseableHttpClient) httpClient).close(); + } + } + + /** + * Returns the Apache HTTP client. + * + * @since 1.30 + */ + public HttpClient getHttpClient() { + return httpClient; + } +} diff --git a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ContentEntity.java b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ContentEntity.java new file mode 100644 index 000000000..4a45801fb --- /dev/null +++ b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ContentEntity.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2010 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.v2; + +import com.google.api.client.util.Preconditions; +import com.google.api.client.util.StreamingContent; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import org.apache.http.entity.AbstractHttpEntity; + +/** + * @author Yaniv Inbar + */ +final class ContentEntity extends AbstractHttpEntity { + + /** Content length or less than zero if not known. */ + private final long contentLength; + + /** Streaming content. */ + private final StreamingContent streamingContent; + + /** + * @param contentLength content length or less than zero if not known + * @param streamingContent streaming content + */ + ContentEntity(long contentLength, StreamingContent streamingContent) { + this.contentLength = contentLength; + this.streamingContent = Preconditions.checkNotNull(streamingContent); + } + + @Override + public InputStream getContent() { + throw new UnsupportedOperationException(); + } + + @Override + public long getContentLength() { + return contentLength; + } + + @Override + public boolean isRepeatable() { + return false; + } + + @Override + public boolean isStreaming() { + return true; + } + + @Override + public void writeTo(OutputStream out) throws IOException { + if (contentLength != 0) { + streamingContent.writeTo(out); + } + } +} diff --git a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/HttpExtensionMethod.java b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/HttpExtensionMethod.java new file mode 100644 index 000000000..4143a3be0 --- /dev/null +++ b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/HttpExtensionMethod.java @@ -0,0 +1,42 @@ +/* + * 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.v2; + +import com.google.api.client.util.Preconditions; +import java.net.URI; +import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; + +/** + * HTTP extension method. + * + * @author Yaniv Inbar + */ +final class HttpExtensionMethod extends HttpEntityEnclosingRequestBase { + + /** Request method name. */ + private final String methodName; + + /** + * @param methodName request method name + * @param uri URI + */ + public HttpExtensionMethod(String methodName, String uri) { + this.methodName = Preconditions.checkNotNull(methodName); + setURI(URI.create(uri)); + } + + @Override + public String getMethod() { + return methodName; + } +} diff --git a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/package-info.java b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/package-info.java new file mode 100644 index 000000000..e45d3e648 --- /dev/null +++ b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/package-info.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2010 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. + */ + +/** + * HTTP Transport library for Google API's based on Apache HTTP Client version 4.5+ + * + * @since 1.30 + * @author Yaniv Inbar + */ + +package com.google.api.client.http.apache.v2; + diff --git a/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java b/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java new file mode 100644 index 000000000..386a5aa82 --- /dev/null +++ b/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2011 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.v2; + +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 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.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 { + + @Test + public void testApacheHttpTransport() { + ApacheHttpTransport transport = new ApacheHttpTransport(); + checkHttpTransport(transport); + } + + @Test + public void testApacheHttpTransportWithParam() { + ApacheHttpTransport transport = new ApacheHttpTransport(HttpClients.custom().build()); + checkHttpTransport(transport); + } + + @Test + public void testNewDefaultHttpClient() { + 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); + when(mockClient.execute(any(HttpUriRequest.class))).thenReturn(mockResponse); + + ApacheHttpTransport transport = new ApacheHttpTransport(mockClient); + + // Test GET. + subtestUnsupportedRequestsWithContent( + transport.buildRequest("GET", "http://www.test.url"), "GET"); + // Test DELETE. + subtestUnsupportedRequestsWithContent( + transport.buildRequest("DELETE", "http://www.test.url"), "DELETE"); + // Test HEAD. + subtestUnsupportedRequestsWithContent( + transport.buildRequest("HEAD", "http://www.test.url"), "HEAD"); + + // Test PATCH. + execute(transport.buildRequest("PATCH", "http://www.test.url")); + // Test PUT. + execute(transport.buildRequest("PUT", "http://www.test.url")); + // Test POST. + execute(transport.buildRequest("POST", "http://www.test.url")); + // Test PATCH. + execute(transport.buildRequest("PATCH", "http://www.test.url")); + } + + private void subtestUnsupportedRequestsWithContent(ApacheHttpRequest request, String method) + throws Exception { + try { + execute(request); + fail("expected " + IllegalArgumentException.class); + } catch (IllegalArgumentException e) { + // expected + assertEquals(e.getMessage(), + "Apache HTTP client does not support " + method + " requests with content."); + } + } + + private void execute(ApacheHttpRequest request) throws Exception { + byte[] bytes = StringUtils.getBytesUtf8("abc"); + request.setStreamingContent(new ByteArrayStreamingContent(bytes)); + request.setContentType("text/html"); + request.setContentLength(bytes.length); + request.execute(); + } + + @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()); + } + + @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/pom.xml b/pom.xml index 6776e672b..e4574529c 100644 --- a/pom.xml +++ b/pom.xml @@ -57,8 +57,7 @@ google-http-client-assembly google-http-client-appengine google-http-client-android - google-http-client-apache - google-http-client-apache-legacy + google-http-client-apache-v2 google-http-client-protobuf google-http-client-gson google-http-client-jackson From 8a21514a3bf8ab54f140d1dcb65631871821b025 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Tue, 14 May 2019 15:59:21 -0700 Subject: [PATCH 02/12] Fix parent version --- google-http-client-apache-v2/pom.xml | 2 +- versions.txt | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/google-http-client-apache-v2/pom.xml b/google-http-client-apache-v2/pom.xml index 839f58846..afcf421b1 100644 --- a/google-http-client-apache-v2/pom.xml +++ b/google-http-client-apache-v2/pom.xml @@ -4,7 +4,7 @@ com.google.http-client google-http-client-parent - 1.29.2-SNAPSHOT0 + 1.29.2-SNAPSHOT ../pom.xml google-http-client-apache-v2 diff --git a/versions.txt b/versions.txt index f90ff8086..d3e97b814 100644 --- a/versions.txt +++ b/versions.txt @@ -6,8 +6,7 @@ google-http-client-bom:1.29.1:1.29.2-SNAPSHOT google-http-client-parent:1.29.1:1.29.2-SNAPSHOT google-http-client-android:1.29.1:1.29.2-SNAPSHOT google-http-client-android-test:1.29.1:1.29.2-SNAPSHOT -google-http-client-apache:2.1.1:2.1.2-SNAPSHOT -google-http-client-apache-legacy:1.29.1:1.29.2-SNAPSHOT +google-http-client-apache-v2:1.29.1:1.29.2-SNAPSHOT google-http-client-appengine:1.29.1:1.29.2-SNAPSHOT google-http-client-assembly:1.29.1:1.29.2-SNAPSHOT google-http-client-findbugs:1.29.1:1.29.2-SNAPSHOT From f690a40c91bd0ba6a280be0f97a00df59ea57cf7 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Tue, 14 May 2019 15:59:52 -0700 Subject: [PATCH 03/12] Remove google-http-client-apache and google-http-client-apache-legacy --- google-http-client-apache-legacy/pom.xml | 116 ------ .../client/http/apache/ApacheHttpRequest.java | 66 --- .../http/apache/ApacheHttpResponse.java | 120 ------ .../http/apache/ApacheHttpTransport.java | 387 ------------------ .../api/client/http/apache/ContentEntity.java | 68 --- .../http/apache/HttpExtensionMethod.java | 42 -- .../apache/SSLSocketFactoryExtension.java | 59 --- .../api/client/http/apache/package-info.java | 21 - .../http/apache/ApacheHttpTransportTest.java | 120 ------ google-http-client-apache/pom.xml | 115 ------ .../http/apache/ApacheHttpTransportTest.java | 120 ------ 11 files changed, 1234 deletions(-) delete mode 100644 google-http-client-apache-legacy/pom.xml delete mode 100644 google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/ApacheHttpRequest.java delete mode 100644 google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/ApacheHttpResponse.java delete mode 100644 google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/ApacheHttpTransport.java delete mode 100644 google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/ContentEntity.java delete mode 100644 google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/HttpExtensionMethod.java delete mode 100644 google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/SSLSocketFactoryExtension.java delete mode 100644 google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/package-info.java delete mode 100644 google-http-client-apache-legacy/src/test/java/com/google/api/client/http/apache/ApacheHttpTransportTest.java delete mode 100644 google-http-client-apache/pom.xml delete mode 100644 google-http-client-apache/src/test/java/com/google/api/client/http/apache/ApacheHttpTransportTest.java diff --git a/google-http-client-apache-legacy/pom.xml b/google-http-client-apache-legacy/pom.xml deleted file mode 100644 index 26e0d01c6..000000000 --- a/google-http-client-apache-legacy/pom.xml +++ /dev/null @@ -1,116 +0,0 @@ - - 4.0.0 - - com.google.http-client - google-http-client-parent - 1.29.2-SNAPSHOT - ../pom.xml - - google-http-client-apache - 1.29.2-SNAPSHOT - Legacy Apache HTTP transport for the Google HTTP Client Library for Java. - - - - - maven-javadoc-plugin - - - https://download.oracle.com/javase/7/docs/api/ - https://jar-download.com/javaDoc/org.codehaus.jackson/jackson-core-asl/${project.jackson-core-asl.version} - - ${project.name} ${project.version} - ${project.artifactId} ${project.version} - - - - maven-source-plugin - - - source-jar - compile - - jar - - - - - - org.codehaus.mojo - build-helper-maven-plugin - 1.5 - - - add-test-source - generate-test-sources - - add-test-source - - - - target/generated-test-sources - - - - - - - maven-jar-plugin - - - ${project.build.outputDirectory}/META-INF/MANIFEST.MF - - com.google.api.client.http.apache - - - - - - org.apache.felix - maven-bundle-plugin - 2.5.4 - - - bundle-manifest - process-classes - - manifest - - - - - - - - - com.google.http-client - google-http-client - - - com.google.http-client - google-http-client-test - test - - - junit - junit - test - - - com.google.guava - guava - test - - - org.apache.httpcomponents - httpclient - 4.2.6 - - - org.mockito - mockito-all - test - - - diff --git a/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/ApacheHttpRequest.java b/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/ApacheHttpRequest.java deleted file mode 100644 index 2d2f40189..000000000 --- a/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/ApacheHttpRequest.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2010 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 com.google.api.client.http.LowLevelHttpRequest; -import com.google.api.client.http.LowLevelHttpResponse; -import com.google.api.client.util.Preconditions; -import java.io.IOException; -import org.apache.http.HttpEntityEnclosingRequest; -import org.apache.http.client.HttpClient; -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 */ -final class ApacheHttpRequest extends LowLevelHttpRequest { - private final HttpClient httpClient; - - private final HttpRequestBase request; - - ApacheHttpRequest(HttpClient httpClient, HttpRequestBase request) { - this.httpClient = httpClient; - this.request = request; - } - - @Override - public void addHeader(String name, String value) { - request.addHeader(name, 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); - } - - @Override - public LowLevelHttpResponse execute() throws IOException { - if (getStreamingContent() != null) { - Preconditions.checkArgument( - request instanceof HttpEntityEnclosingRequest, - "Apache HTTP client does not support %s requests with content.", - request.getRequestLine().getMethod()); - ContentEntity entity = new ContentEntity(getContentLength(), getStreamingContent()); - entity.setContentEncoding(getContentEncoding()); - entity.setContentType(getContentType()); - ((HttpEntityEnclosingRequest) request).setEntity(entity); - } - return new ApacheHttpResponse(request, httpClient.execute(request)); - } -} diff --git a/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/ApacheHttpResponse.java b/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/ApacheHttpResponse.java deleted file mode 100644 index 5e8e427aa..000000000 --- a/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/ApacheHttpResponse.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (c) 2010 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 com.google.api.client.http.LowLevelHttpResponse; -import java.io.IOException; -import java.io.InputStream; -import org.apache.http.Header; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.StatusLine; -import org.apache.http.client.methods.HttpRequestBase; - -final class ApacheHttpResponse extends LowLevelHttpResponse { - - private final HttpRequestBase request; - private final HttpResponse response; - private final Header[] allHeaders; - - ApacheHttpResponse(HttpRequestBase request, HttpResponse response) { - this.request = request; - this.response = response; - allHeaders = response.getAllHeaders(); - } - - @Override - public int getStatusCode() { - StatusLine statusLine = response.getStatusLine(); - return statusLine == null ? 0 : statusLine.getStatusCode(); - } - - @Override - public InputStream getContent() throws IOException { - HttpEntity entity = response.getEntity(); - return entity == null ? null : entity.getContent(); - } - - @Override - public String getContentEncoding() { - HttpEntity entity = response.getEntity(); - if (entity != null) { - Header contentEncodingHeader = entity.getContentEncoding(); - if (contentEncodingHeader != null) { - return contentEncodingHeader.getValue(); - } - } - return null; - } - - @Override - public long getContentLength() { - HttpEntity entity = response.getEntity(); - return entity == null ? -1 : entity.getContentLength(); - } - - @Override - public String getContentType() { - HttpEntity entity = response.getEntity(); - if (entity != null) { - Header contentTypeHeader = entity.getContentType(); - if (contentTypeHeader != null) { - return contentTypeHeader.getValue(); - } - } - return null; - } - - @Override - public String getReasonPhrase() { - StatusLine statusLine = response.getStatusLine(); - return statusLine == null ? null : statusLine.getReasonPhrase(); - } - - @Override - public String getStatusLine() { - StatusLine statusLine = response.getStatusLine(); - return statusLine == null ? null : statusLine.toString(); - } - - @Override - public int getHeaderCount() { - return allHeaders.length; - } - - @Override - public String getHeaderName(int index) { - return allHeaders[index].getName(); - } - - public String getHeaderValue(String name) { - return response.getLastHeader(name).getValue(); - } - - @Override - public String getHeaderValue(int index) { - return allHeaders[index].getValue(); - } - - /** - * Aborts execution of the request. - * - * @since 1.4 - */ - @Override - public void disconnect() { - request.abort(); - } -} diff --git a/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/ApacheHttpTransport.java b/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/ApacheHttpTransport.java deleted file mode 100644 index 2dfda73fe..000000000 --- a/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/ApacheHttpTransport.java +++ /dev/null @@ -1,387 +0,0 @@ -/* - * Copyright (c) 2010 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 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; -import java.io.IOException; -import java.io.InputStream; -import java.net.ProxySelector; -import java.security.GeneralSecurityException; -import java.security.KeyStore; -import java.security.cert.CertificateFactory; -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; -import org.apache.http.client.methods.HttpHead; -import org.apache.http.client.methods.HttpOptions; -import org.apache.http.client.methods.HttpPost; -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; - -/** - * Thread-safe HTTP transport based on the Apache HTTP Client library. - * - *

Implementation is thread-safe, as long as any parameter modification to the {@link - * #getHttpClient() Apache HTTP Client} is only done at initialization time. For maximum efficiency, - * applications should use a single globally-shared instance of the HTTP transport. - * - *

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. - * - * @since 1.0 - * @author Yaniv Inbar - */ -public final class ApacheHttpTransport extends HttpTransport { - - /** Apache HTTP client. */ - private final HttpClient httpClient; - - /** - * Constructor that uses {@link #newDefaultHttpClient()} for the Apache HTTP client. - * - *

Use {@link Builder} to modify HTTP client options. - * - * @since 1.3 - */ - public ApacheHttpTransport() { - this(newDefaultHttpClient()); - } - - /** - * Constructor that allows an alternative Apache HTTP client to be used. - * - *

Note that a few settings are overridden: - * - *

    - *
  • 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()}. - *
- * - *

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); - } - - /** 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. - * - *

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

    - *
  • The client connection manager is set to {@link ThreadSafeClientConnManager}. - *
  • The socket buffer size is set to 8192 using {@link - * HttpConnectionParams#setSocketBufferSize}. - *
  • The retry mechanism is turned off by setting {@code new DefaultHttpRequestRetryHandler(0, - * false)}. - *
  • The route planner uses {@link ProxySelectorRoutePlanner} 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()); - } - - /** - * 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 - */ - 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; - } - - @Override - public boolean supportsMethod(String method) { - return true; - } - - @Override - protected ApacheHttpRequest buildRequest(String method, String url) { - HttpRequestBase requestBase; - if (method.equals(HttpMethods.DELETE)) { - requestBase = new HttpDelete(url); - } else if (method.equals(HttpMethods.GET)) { - requestBase = new HttpGet(url); - } else if (method.equals(HttpMethods.HEAD)) { - requestBase = new HttpHead(url); - } else if (method.equals(HttpMethods.POST)) { - requestBase = new HttpPost(url); - } else if (method.equals(HttpMethods.PUT)) { - requestBase = new HttpPut(url); - } else if (method.equals(HttpMethods.TRACE)) { - requestBase = new HttpTrace(url); - } else if (method.equals(HttpMethods.OPTIONS)) { - requestBase = new HttpOptions(url); - } else { - requestBase = new HttpExtensionMethod(method, url); - } - return new ApacheHttpRequest(httpClient, requestBase); - } - - /** - * Shuts down the connection manager and releases allocated resources. This includes closing all - * connections, whether they are currently used or not. - * - * @since 1.4 - */ - @Override - public void shutdown() { - httpClient.getConnectionManager().shutdown(); - } - - /** - * Returns the Apache HTTP client. - * - * @since 1.5 - */ - 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 final 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-legacy/src/main/java/com/google/api/client/http/apache/ContentEntity.java b/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/ContentEntity.java deleted file mode 100644 index 343b35c50..000000000 --- a/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/ContentEntity.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2010 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 com.google.api.client.util.Preconditions; -import com.google.api.client.util.StreamingContent; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import org.apache.http.entity.AbstractHttpEntity; - -/** @author Yaniv Inbar */ -final class ContentEntity extends AbstractHttpEntity { - - /** Content length or less than zero if not known. */ - private final long contentLength; - - /** Streaming content. */ - private final StreamingContent streamingContent; - - /** - * @param contentLength content length or less than zero if not known - * @param streamingContent streaming content - */ - ContentEntity(long contentLength, StreamingContent streamingContent) { - this.contentLength = contentLength; - this.streamingContent = Preconditions.checkNotNull(streamingContent); - } - - @Override - public InputStream getContent() { - throw new UnsupportedOperationException(); - } - - @Override - public long getContentLength() { - return contentLength; - } - - @Override - public boolean isRepeatable() { - return false; - } - - @Override - public boolean isStreaming() { - return true; - } - - @Override - public void writeTo(OutputStream out) throws IOException { - if (contentLength != 0) { - streamingContent.writeTo(out); - } - } -} diff --git a/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/HttpExtensionMethod.java b/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/HttpExtensionMethod.java deleted file mode 100644 index 598833de5..000000000 --- a/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/HttpExtensionMethod.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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 com.google.api.client.util.Preconditions; -import java.net.URI; -import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; - -/** - * HTTP extension method. - * - * @author Yaniv Inbar - */ -final class HttpExtensionMethod extends HttpEntityEnclosingRequestBase { - - /** Request method name. */ - private final String methodName; - - /** - * @param methodName request method name - * @param uri URI - */ - public HttpExtensionMethod(String methodName, String uri) { - this.methodName = Preconditions.checkNotNull(methodName); - setURI(URI.create(uri)); - } - - @Override - public String getMethod() { - return methodName; - } -} diff --git a/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/SSLSocketFactoryExtension.java b/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/SSLSocketFactoryExtension.java deleted file mode 100644 index 4b9a624f2..000000000 --- a/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/SSLSocketFactoryExtension.java +++ /dev/null @@ -1,59 +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.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 { - SSLSocket sslSocket = (SSLSocket) socketFactory.createSocket(socket, host, port, autoClose); - getHostnameVerifier().verify(host, sslSocket); - return sslSocket; - } -} diff --git a/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/package-info.java b/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/package-info.java deleted file mode 100644 index 0c2c23b4f..000000000 --- a/google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/package-info.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (c) 2010 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. - */ - -/** - * HTTP Transport library for Google API's based on Apache HTTP Client version 4. - * - * @since 1.0 - * @author Yaniv Inbar - */ -package com.google.api.client.http.apache; diff --git a/google-http-client-apache-legacy/src/test/java/com/google/api/client/http/apache/ApacheHttpTransportTest.java b/google-http-client-apache-legacy/src/test/java/com/google/api/client/http/apache/ApacheHttpTransportTest.java deleted file mode 100644 index 8176166db..000000000 --- a/google-http-client-apache-legacy/src/test/java/com/google/api/client/http/apache/ApacheHttpTransportTest.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (c) 2011 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 static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import com.google.api.client.util.ByteArrayStreamingContent; -import com.google.api.client.util.StringUtils; -import junit.framework.TestCase; -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; - -/** - * Tests {@link ApacheHttpTransport}. - * - * @author Yaniv Inbar - */ -public class ApacheHttpTransportTest extends TestCase { - - public void testApacheHttpTransport() { - ApacheHttpTransport transport = new ApacheHttpTransport(); - DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient(); - checkDefaultHttpClient(httpClient); - checkHttpClient(httpClient); - } - - public void testApacheHttpTransportWithParam() { - ApacheHttpTransport transport = new ApacheHttpTransport(new DefaultHttpClient()); - checkHttpClient(transport.getHttpClient()); - } - - public void testNewDefaultHttpClient() { - checkDefaultHttpClient(ApacheHttpTransport.newDefaultHttpClient()); - } - - public void testRequestsWithContent() throws Exception { - HttpClient mockClient = mock(HttpClient.class); - HttpResponse mockResponse = mock(HttpResponse.class); - when(mockClient.execute(any(HttpUriRequest.class))).thenReturn(mockResponse); - - ApacheHttpTransport transport = new ApacheHttpTransport(mockClient); - - // Test GET. - subtestUnsupportedRequestsWithContent( - transport.buildRequest("GET", "http://www.test.url"), "GET"); - // Test DELETE. - subtestUnsupportedRequestsWithContent( - transport.buildRequest("DELETE", "http://www.test.url"), "DELETE"); - // Test HEAD. - subtestUnsupportedRequestsWithContent( - transport.buildRequest("HEAD", "http://www.test.url"), "HEAD"); - - // Test PUT. - execute(transport.buildRequest("PUT", "http://www.test.url")); - // Test POST. - execute(transport.buildRequest("POST", "http://www.test.url")); - // Test PATCH. - execute(transport.buildRequest("PATCH", "http://www.test.url")); - } - - private void subtestUnsupportedRequestsWithContent(ApacheHttpRequest request, String method) - throws Exception { - try { - execute(request); - fail("expected " + IllegalArgumentException.class); - } catch (IllegalArgumentException e) { - // expected - assertEquals( - e.getMessage(), - "Apache HTTP client does not support " + method + " requests with content."); - } - } - - private void execute(ApacheHttpRequest request) throws Exception { - byte[] bytes = StringUtils.getBytesUtf8("abc"); - request.setStreamingContent(new ByteArrayStreamingContent(bytes)); - request.setContentType("text/html"); - request.setContentLength(bytes.length); - 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()); - } - - private void checkHttpClient(HttpClient client) { - HttpParams params = client.getParams(); - assertFalse(params.getBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true)); - assertEquals(HttpVersion.HTTP_1_1, HttpProtocolParams.getVersion(params)); - } -} diff --git a/google-http-client-apache/pom.xml b/google-http-client-apache/pom.xml deleted file mode 100644 index 126ae7cc9..000000000 --- a/google-http-client-apache/pom.xml +++ /dev/null @@ -1,115 +0,0 @@ - - 4.0.0 - - com.google.http-client - google-http-client-parent - 1.29.2-SNAPSHOT - ../pom.xml - - google-http-client-apache - 2.1.2-SNAPSHOT - Apache HTTP transport for the Google HTTP Client Library for Java. - - - - - maven-javadoc-plugin - - - https://download.oracle.com/javase/7/docs/api/ - https://jar-download.com/artifacts/org.codehaus.jackson/jackson-core-asl/${project.jackson-core-asl.version}/documentation - - ${project.name} ${project.version} - ${project.artifactId} ${project.version} - - - - maven-source-plugin - - - source-jar - compile - - jar - - - - - - org.codehaus.mojo - build-helper-maven-plugin - 1.5 - - - add-test-source - generate-test-sources - - add-test-source - - - - target/generated-test-sources - - - - - - - maven-jar-plugin - - - ${project.build.outputDirectory}/META-INF/MANIFEST.MF - - com.google.api.client.http.apache - - - - - - org.apache.felix - maven-bundle-plugin - 2.5.4 - - - bundle-manifest - process-classes - - manifest - - - - - - - - - com.google.http-client - google-http-client - - - com.google.http-client - google-http-client-test - test - - - junit - junit - test - - - com.google.guava - guava - test - - - org.apache.httpcomponents - httpclient - - - org.mockito - mockito-all - test - - - 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 deleted file mode 100644 index 8176166db..000000000 --- a/google-http-client-apache/src/test/java/com/google/api/client/http/apache/ApacheHttpTransportTest.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (c) 2011 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 static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import com.google.api.client.util.ByteArrayStreamingContent; -import com.google.api.client.util.StringUtils; -import junit.framework.TestCase; -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; - -/** - * Tests {@link ApacheHttpTransport}. - * - * @author Yaniv Inbar - */ -public class ApacheHttpTransportTest extends TestCase { - - public void testApacheHttpTransport() { - ApacheHttpTransport transport = new ApacheHttpTransport(); - DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient(); - checkDefaultHttpClient(httpClient); - checkHttpClient(httpClient); - } - - public void testApacheHttpTransportWithParam() { - ApacheHttpTransport transport = new ApacheHttpTransport(new DefaultHttpClient()); - checkHttpClient(transport.getHttpClient()); - } - - public void testNewDefaultHttpClient() { - checkDefaultHttpClient(ApacheHttpTransport.newDefaultHttpClient()); - } - - public void testRequestsWithContent() throws Exception { - HttpClient mockClient = mock(HttpClient.class); - HttpResponse mockResponse = mock(HttpResponse.class); - when(mockClient.execute(any(HttpUriRequest.class))).thenReturn(mockResponse); - - ApacheHttpTransport transport = new ApacheHttpTransport(mockClient); - - // Test GET. - subtestUnsupportedRequestsWithContent( - transport.buildRequest("GET", "http://www.test.url"), "GET"); - // Test DELETE. - subtestUnsupportedRequestsWithContent( - transport.buildRequest("DELETE", "http://www.test.url"), "DELETE"); - // Test HEAD. - subtestUnsupportedRequestsWithContent( - transport.buildRequest("HEAD", "http://www.test.url"), "HEAD"); - - // Test PUT. - execute(transport.buildRequest("PUT", "http://www.test.url")); - // Test POST. - execute(transport.buildRequest("POST", "http://www.test.url")); - // Test PATCH. - execute(transport.buildRequest("PATCH", "http://www.test.url")); - } - - private void subtestUnsupportedRequestsWithContent(ApacheHttpRequest request, String method) - throws Exception { - try { - execute(request); - fail("expected " + IllegalArgumentException.class); - } catch (IllegalArgumentException e) { - // expected - assertEquals( - e.getMessage(), - "Apache HTTP client does not support " + method + " requests with content."); - } - } - - private void execute(ApacheHttpRequest request) throws Exception { - byte[] bytes = StringUtils.getBytesUtf8("abc"); - request.setStreamingContent(new ByteArrayStreamingContent(bytes)); - request.setContentType("text/html"); - request.setContentLength(bytes.length); - 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()); - } - - private void checkHttpClient(HttpClient client) { - HttpParams params = client.getParams(); - assertFalse(params.getBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true)); - assertEquals(HttpVersion.HTTP_1_1, HttpProtocolParams.getVersion(params)); - } -} From f6401bef32eb825f968bd80ec4e425cade21be93 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Tue, 14 May 2019 16:06:50 -0700 Subject: [PATCH 04/12] Deprecate the public interfaces for the legacy apache extensions. Deprecates the package com.google.api.client.http.apache Deprecates the com.google.api.client.http.apache.AppacheHttpTransport class. --- .../com/google/api/client/http/apache/ApacheHttpTransport.java | 3 +++ .../java/com/google/api/client/http/apache/package-info.java | 2 ++ 2 files changed, 5 insertions(+) diff --git a/google-http-client/src/main/java/com/google/api/client/http/apache/ApacheHttpTransport.java b/google-http-client/src/main/java/com/google/api/client/http/apache/ApacheHttpTransport.java index 946f5e718..ce5e59d51 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/apache/ApacheHttpTransport.java +++ b/google-http-client/src/main/java/com/google/api/client/http/apache/ApacheHttpTransport.java @@ -74,7 +74,10 @@ * * @since 1.0 * @author Yaniv Inbar + * @deprecated Please use com.google.api.client.http.apache.v2.ApacheHttpTransport provided by + * the com.google.http-client:google-http-client-apache-v2 artifact. */ +@Deprecated public final class ApacheHttpTransport extends HttpTransport { /** Apache HTTP client. */ diff --git a/google-http-client/src/main/java/com/google/api/client/http/apache/package-info.java b/google-http-client/src/main/java/com/google/api/client/http/apache/package-info.java index 0c2c23b4f..93577e38b 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/apache/package-info.java +++ b/google-http-client/src/main/java/com/google/api/client/http/apache/package-info.java @@ -17,5 +17,7 @@ * * @since 1.0 * @author Yaniv Inbar + * @deprecated Please use com.google.api.client.http.apache.v2 provided by the + * com.google.http-client:google-http-client-apache-v2 artifact. */ package com.google.api.client.http.apache; From 32fa962cd63620bf481b0215c8b88bd3c9216720 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Tue, 14 May 2019 16:14:39 -0700 Subject: [PATCH 05/12] Clean up bom versions. Appengine artifact does not need to depend upon the apache artifact --- google-http-client-android/pom.xml | 4 ---- google-http-client-apache-v2/pom.xml | 2 +- google-http-client-bom/pom.xml | 4 ++-- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/google-http-client-android/pom.xml b/google-http-client-android/pom.xml index f28dc3d1e..0db928781 100644 --- a/google-http-client-android/pom.xml +++ b/google-http-client-android/pom.xml @@ -48,9 +48,5 @@ com.google.http-client google-http-client - - com.google.http-client - google-http-client-apache - diff --git a/google-http-client-apache-v2/pom.xml b/google-http-client-apache-v2/pom.xml index afcf421b1..fab345615 100644 --- a/google-http-client-apache-v2/pom.xml +++ b/google-http-client-apache-v2/pom.xml @@ -17,7 +17,7 @@ maven-javadoc-plugin - http://download.oracle.com/javase/7/docs/api/ + https://download.oracle.com/javase/7/docs/api/ https://jar-download.com/artifacts/org.codehaus.jackson/jackson-core-asl/${project.jackson-core-asl.version}/documentation ${project.name} ${project.version} diff --git a/google-http-client-bom/pom.xml b/google-http-client-bom/pom.xml index c4e73d148..9db27edc7 100644 --- a/google-http-client-bom/pom.xml +++ b/google-http-client-bom/pom.xml @@ -72,8 +72,8 @@ com.google.http-client - google-http-client-apache - 2.1.2-SNAPSHOT + google-http-client-apache-v2 + 1.29.2-SNAPSHOT com.google.http-client From 510814a75dc9094796e94ebc24b3c951d2512ff5 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Tue, 14 May 2019 16:20:29 -0700 Subject: [PATCH 06/12] Fix assembly references to apache artifact --- google-http-client-assembly/assembly.xml | 8 ++++---- google-http-client-assembly/pom.xml | 2 +- .../google-http-client-apache-v2.jar.properties | 1 + .../properties/google-http-client-apache.jar.properties | 1 - pom.xml | 3 +-- 5 files changed, 7 insertions(+), 8 deletions(-) create mode 100644 google-http-client-assembly/properties/google-http-client-apache-v2.jar.properties delete mode 100644 google-http-client-assembly/properties/google-http-client-apache.jar.properties diff --git a/google-http-client-assembly/assembly.xml b/google-http-client-assembly/assembly.xml index 80ee72de1..45478f40a 100644 --- a/google-http-client-assembly/assembly.xml +++ b/google-http-client-assembly/assembly.xml @@ -39,8 +39,8 @@ true - properties/google-http-client-apache.jar.properties - google-http-client-apache-${project.http-client-apache.version}.jar.properties + properties/google-http-client-apache-v2.jar.properties + google-http-client-apache-v2-${project.http-client.version}.jar.properties google-http-java-client/libs true @@ -110,8 +110,8 @@ google-http-java-client/dependencies - ../google-http-client-apache/target/site/dependencies.html - google-http-client-apache-dependencies.html + ../google-http-client-apache-v2/target/site/dependencies.html + google-http-client-apache-v2-dependencies.html google-http-java-client/dependencies diff --git a/google-http-client-assembly/pom.xml b/google-http-client-assembly/pom.xml index 4888ef25f..1279fa067 100644 --- a/google-http-client-assembly/pom.xml +++ b/google-http-client-assembly/pom.xml @@ -24,7 +24,7 @@ com.google.http-client - google-http-client-apache + google-http-client-apache-v2 com.google.http-client diff --git a/google-http-client-assembly/properties/google-http-client-apache-v2.jar.properties b/google-http-client-assembly/properties/google-http-client-apache-v2.jar.properties new file mode 100644 index 000000000..e1d1f0f97 --- /dev/null +++ b/google-http-client-assembly/properties/google-http-client-apache-v2.jar.properties @@ -0,0 +1 @@ +src=../libs-sources/google-http-client-apache-v2-${project.version}-sources.jar diff --git a/google-http-client-assembly/properties/google-http-client-apache.jar.properties b/google-http-client-assembly/properties/google-http-client-apache.jar.properties deleted file mode 100644 index 4046b3a73..000000000 --- a/google-http-client-assembly/properties/google-http-client-apache.jar.properties +++ /dev/null @@ -1 +0,0 @@ -src=../libs-sources/google-http-client-apache-${project.http-client-apache.version}-sources.jar diff --git a/pom.xml b/pom.xml index e4574529c..2c19bf6fb 100644 --- a/pom.xml +++ b/pom.xml @@ -175,7 +175,7 @@ com.google.http-client - google-http-client-apache + google-http-client-apache-v2 ${project.http-client-apache.version} @@ -553,7 +553,6 @@ - Internally, update the default features.json file --> 1.29.2-SNAPSHOT - 2.1.2-SNAPSHOT 1.9.71 UTF-8 3.0.2 From a7f8f74e86d9abebb826746d81d3aa45bc956ec4 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Tue, 14 May 2019 16:23:41 -0700 Subject: [PATCH 07/12] Fix main dependency management config --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 2c19bf6fb..b1d4d0bb6 100644 --- a/pom.xml +++ b/pom.xml @@ -176,7 +176,7 @@ com.google.http-client google-http-client-apache-v2 - ${project.http-client-apache.version} + ${project.http-client.version} com.google.http-client From 75380d421448db48459965de1aebeb62647c945a Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Wed, 15 May 2019 09:02:55 -0700 Subject: [PATCH 08/12] Fix license headers --- .../com/google/api/client/http/apache/v2/ApacheHttpRequest.java | 2 +- .../google/api/client/http/apache/v2/ApacheHttpResponse.java | 2 +- .../google/api/client/http/apache/v2/ApacheHttpTransport.java | 2 +- .../com/google/api/client/http/apache/v2/ContentEntity.java | 2 +- .../google/api/client/http/apache/v2/HttpExtensionMethod.java | 2 ++ .../java/com/google/api/client/http/apache/v2/package-info.java | 2 +- 6 files changed, 7 insertions(+), 5 deletions(-) diff --git a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpRequest.java b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpRequest.java index 4542a3383..df2741ede 100644 --- a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpRequest.java +++ b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Google Inc. + * Copyright 2019 Google LLC * * 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 diff --git a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpResponse.java b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpResponse.java index ecb6cbc9e..ccf9c5788 100644 --- a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpResponse.java +++ b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpResponse.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Google Inc. + * Copyright 2019 Google LLC * * 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 diff --git a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpTransport.java b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpTransport.java index eabff4c5e..c0966b3aa 100644 --- a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpTransport.java +++ b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpTransport.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Google Inc. + * Copyright 2019 Google LLC * * 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 diff --git a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ContentEntity.java b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ContentEntity.java index 4a45801fb..8fc11e6d8 100644 --- a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ContentEntity.java +++ b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ContentEntity.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Google Inc. + * Copyright 2019 Google LLC * * 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 diff --git a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/HttpExtensionMethod.java b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/HttpExtensionMethod.java index 4143a3be0..360baaecb 100644 --- a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/HttpExtensionMethod.java +++ b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/HttpExtensionMethod.java @@ -1,4 +1,6 @@ /* + * Copyright 2019 Google LLC + * * 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 * diff --git a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/package-info.java b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/package-info.java index e45d3e648..bc753838b 100644 --- a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/package-info.java +++ b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/package-info.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Google Inc. + * Copyright 2019 Google LLC * * 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 From ab778e1a65ff472b5d4d1df65d53b16b9e607b48 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Wed, 15 May 2019 09:07:39 -0700 Subject: [PATCH 09/12] Update javadoc from review --- .../google/api/client/http/apache/v2/ApacheHttpRequest.java | 1 + .../api/client/http/apache/v2/ApacheHttpTransport.java | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpRequest.java b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpRequest.java index df2741ede..5d9323dd6 100644 --- a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpRequest.java +++ b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpRequest.java @@ -36,6 +36,7 @@ final class ApacheHttpRequest extends LowLevelHttpRequest { ApacheHttpRequest(HttpClient httpClient, HttpRequestBase request) { this.httpClient = httpClient; this.request = request; + // disable redirects as google-http-client handles redirects this.requestConfig = RequestConfig.custom().setRedirectsEnabled(false); } diff --git a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpTransport.java b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpTransport.java index c0966b3aa..42f53d490 100644 --- a/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpTransport.java +++ b/google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpTransport.java @@ -82,8 +82,8 @@ public ApacheHttpTransport() { * Constructor that allows an alternative Apache HTTP client to be used. * *

- * Note that in the previous version, we tried overrode several settings, however, we are no - * longer able to do so. + * Note that in the previous version, we 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

@@ -179,7 +179,7 @@ protected ApacheHttpRequest buildRequest(String method, String url) { } /** - * Shuts down the connection manager and releases allocated resources. This includes closing all + * Shuts down the connection manager and releases allocated resources. This closes all * connections, whether they are currently used or not. * * @since 1.30 From 61187bf79527392c14f95a5f2fc4ee8eeb01f953 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Wed, 15 May 2019 09:12:39 -0700 Subject: [PATCH 10/12] Declare explicit exceptions thrown in tests --- .../client/http/apache/v2/ApacheHttpTransportTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java b/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java index 386a5aa82..c2fe53e88 100644 --- a/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java +++ b/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 Google Inc. + * Copyright 2019 Google LLC * * 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 @@ -81,7 +81,7 @@ private void checkHttpClient(HttpClient client) { } @Test - public void testRequestsWithContent() throws Exception { + public void testRequestsWithContent() throws IOException { HttpClient mockClient = mock(HttpClient.class); HttpResponse mockResponse = mock(HttpResponse.class); when(mockClient.execute(any(HttpUriRequest.class))).thenReturn(mockResponse); @@ -109,7 +109,7 @@ public void testRequestsWithContent() throws Exception { } private void subtestUnsupportedRequestsWithContent(ApacheHttpRequest request, String method) - throws Exception { + throws IOException { try { execute(request); fail("expected " + IllegalArgumentException.class); @@ -120,7 +120,7 @@ private void subtestUnsupportedRequestsWithContent(ApacheHttpRequest request, St } } - private void execute(ApacheHttpRequest request) throws Exception { + private void execute(ApacheHttpRequest request) throws IOException { byte[] bytes = StringUtils.getBytesUtf8("abc"); request.setStreamingContent(new ByteArrayStreamingContent(bytes)); request.setContentType("text/html"); From bc28d9f6bbccfbbc213c4395564a0ea3af9ce65a Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Wed, 15 May 2019 09:14:15 -0700 Subject: [PATCH 11/12] Rename test variables --- .../client/http/apache/v2/ApacheHttpTransportTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java b/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java index c2fe53e88..922a8c0ef 100644 --- a/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java +++ b/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java @@ -133,12 +133,12 @@ public void testRequestShouldNotFollowRedirects() throws IOException { final AtomicInteger requestsAttempted = new AtomicInteger(0); HttpRequestExecutor requestExecutor = new HttpRequestExecutor() { @Override - public HttpResponse execute(HttpRequest request, HttpClientConnection conn, + public HttpResponse execute(HttpRequest request, HttpClientConnection connection, HttpContext context) throws IOException, HttpException { - HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, 302, null); - resp.addHeader("location", "https://google.com/path"); + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 302, null); + response.addHeader("location", "https://google.com/path"); requestsAttempted.incrementAndGet(); - return resp; + return response; } }; HttpClient client = HttpClients.custom().setRequestExecutor(requestExecutor).build(); From 14493dece45a564b9803c1d6fa819c82228e48f2 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Wed, 15 May 2019 09:16:09 -0700 Subject: [PATCH 12/12] Replace StringUtils.getbytesUtf8() --- .../api/client/http/apache/v2/ApacheHttpTransportTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java b/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java index 922a8c0ef..dabd9bf3d 100644 --- a/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java +++ b/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java @@ -24,8 +24,8 @@ import com.google.api.client.http.LowLevelHttpResponse; import com.google.api.client.util.ByteArrayStreamingContent; -import com.google.api.client.util.StringUtils; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import org.apache.http.Header; @@ -121,7 +121,7 @@ private void subtestUnsupportedRequestsWithContent(ApacheHttpRequest request, St } private void execute(ApacheHttpRequest request) throws IOException { - byte[] bytes = StringUtils.getBytesUtf8("abc"); + byte[] bytes = "abc".getBytes(StandardCharsets.UTF_8); request.setStreamingContent(new ByteArrayStreamingContent(bytes)); request.setContentType("text/html"); request.setContentLength(bytes.length);