Skip to content

Commit

Permalink
feat: Introduce google-http-client-apache-v5 (Apache Client/Core 5.x) (
Browse files Browse the repository at this point in the history
…#1960)

Adds a new module `google-http-client-apache-v5`, which is based in Apache Client/Core 5.x

The implementation was meant to keep the same functionality prescribed by the tests of `google-http-client-apache-v2`.

### Before merging
 - [ ] Remove _convenience_ integration tests from unit tests file
  • Loading branch information
diegomarquezp authored Aug 21, 2024
1 parent afd6afc commit 5d527dc
Show file tree
Hide file tree
Showing 17 changed files with 1,471 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-java7.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
# dailymotion-simple-cmdline-sample and google-http-client-assembly depend on
# google-http-client-jackson2
mvn --batch-mode --show-version -ntp test \
--projects '!google-http-client-jackson2,!google-http-client-appengine,!samples/dailymotion-simple-cmdline-sample,!google-http-client-assembly' \
--projects '!google-http-client-jackson2,!google-http-client-appengine,!samples/dailymotion-simple-cmdline-sample,!google-http-client-assembly,!google-http-client-apache-v5' \
-Dclirr.skip=true -Denforcer.skip=true -Dmaven.javadoc.skip=true \
-Dgcloud.download.skip=true -T 1C \
-Dproject.surefire.version=2.22.2 \
Expand Down
11 changes: 9 additions & 2 deletions docs/http-transport.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ There are three built-in low-level HTTP transports:

1. [`NetHttpTransport`][net-http-transport]: based on [`HttpURLConnection`][http-url-connection]
that is found in all Java SDKs, and thus usually the simplest choice.
1. [`Apache5HttpTransport`][apache-http-transport]: based on the popular
[Apache 5.x HttpClient][apache5-http-client] that allows for more customization.
1. [`ApacheHttpTransport`][apache-http-transport]: based on the popular
[Apache HttpClient][apache-http-client] that allows for more customization.
[Apache 4.x HttpClient][apache-http-client] that allows for more customization. Note that this transport implementation
relies on [Apache 4.x HttpCore][apache-http-core] which has reached end of life. It is recommended to use
[`Apache5HttpTransport`][apache-http-transport] instead.
1. [`UrlFetchTransport`][url-fetch-transport]: based on the [URL Fetch Java API][url-fetch] in the
Google App Engine SDK.

Expand Down Expand Up @@ -124,7 +128,10 @@ HttpRequestFactory requestFactory = transport.createRequestFactory(new MyInitial
[net-http-transport]: https://googleapis.dev/java/google-http-client/latest/index.html?com/google/api/client/http/javanet/NetHttpTransport.html
[http-url-connection]: http://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html
[apache-http-transport]: https://googleapis.dev/java/google-http-client/latest/index.html?com/google/api/client/http/apache/v2/ApacheHttpTransport.html
[apache-http-client]: http://hc.apache.org/httpcomponents-client-ga/index.html
[apache5-http-transport]: https://github.com/googleapis/google-http-java-client/blob/de8743587d1415e8a6046096ac1fc0a5e81490c3/google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5HttpTransport.java
[apache-http-client]: https://hc.apache.org/httpcomponents-client-4.5.x/index.html
[apache-http-core]: https://hc.apache.org/httpcomponents-core-4.4.x/index.html
[apache5-http-client]: https://hc.apache.org/httpcomponents-client-5.3.x/index.html
[url-fetch-transport]: https://googleapis.dev/java/google-http-client/latest/index.html?com/google/api/client/extensions/appengine/http/UrlFetchTransport.html
[url-fetch]: https://cloud.google.com/appengine/docs/java/javadoc/com/google/appengine/api/urlfetch/package-summary
[logger]: https://docs.oracle.com/javase/7/docs/api/java/util/logging/Logger.html
Expand Down
116 changes: 116 additions & 0 deletions google-http-client-apache-v5/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-parent</artifactId>
<version>1.44.3-SNAPSHOT</version><!-- {x-version-update:google-http-client-parent:current} -->
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>google-http-client-apache-v5</artifactId>
<version>1.44.3-SNAPSHOT</version><!-- {x-version-update:google-http-client-apache-v5:current} -->
<name>Apache HTTP transport v5 for the Google HTTP Client Library for Java.</name>

<build>
<plugins>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<links>
<link>https://download.oracle.com/javase/7/docs/api/</link>
</links>
<doctitle>${project.name} ${project.version}</doctitle>
<windowtitle>${project.artifactId} ${project.version}</windowtitle>
</configuration>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-test-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
<manifestEntries>
<Automatic-Module-Name>com.google.api.client.http.apache.v5</Automatic-Module-Name>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>5.1.9</version>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2024 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
*
* 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.v5;

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.hc.core5.http.io.entity.AbstractHttpEntity;

/**
* Translation class to make google-http-client entity conform with Apache 5.x {@link
* AbstractHttpEntity}
*/
final class Apache5ContentEntity 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
*/
Apache5ContentEntity(
long contentLength,
StreamingContent streamingContent,
String contentType,
String contentEncoding) {
super(contentType, contentEncoding, contentLength == -1);
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);
}
}

@Override
public void close() throws IOException {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2024 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
*
* 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.v5;

import com.google.api.client.http.LowLevelHttpRequest;
import com.google.api.client.http.LowLevelHttpResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.routing.RoutingSupport;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.util.Timeout;

public final class Apache5HttpRequest extends LowLevelHttpRequest {

private final HttpUriRequestBase request;

private final RequestConfig.Builder requestConfig;

private final HttpClient httpClient;

Apache5HttpRequest(HttpClient httpClient, HttpUriRequestBase request) {
this.httpClient = httpClient;
this.request = request;
// disable redirects as google-http-client handles redirects
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
.setConnectTimeout(Timeout.of(connectTimeout, TimeUnit.MILLISECONDS))
// ResponseTimeout behaves the same as 4.x's SocketTimeout
.setResponseTimeout(Timeout.of(readTimeout, TimeUnit.MILLISECONDS));
}

@Override
public LowLevelHttpResponse execute() throws IOException {
if (getStreamingContent() != null) {
Apache5ContentEntity entity =
new Apache5ContentEntity(
getContentLength(), getStreamingContent(), getContentType(), getContentEncoding());
request.setEntity(entity);
}
request.setConfig(requestConfig.build());
HttpHost target;
try {
target = RoutingSupport.determineHost(request);
} catch (HttpException e) {
throw new ClientProtocolException("The request's host is invalid.", e);
}
// we use a null context so the client creates the default one internally
ClassicHttpResponse httpResponse = httpClient.executeOpen(target, request, null);
return new Apache5HttpResponse(request, httpResponse);
}
}
Loading

0 comments on commit 5d527dc

Please sign in to comment.