-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce google-http-client-apache-v5 (Apache Client/Core 5.x) (…
…#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
1 parent
afd6afc
commit 5d527dc
Showing
17 changed files
with
1,471 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
79 changes: 79 additions & 0 deletions
79
...nt-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5ContentEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
} |
78 changes: 78 additions & 0 deletions
78
...ient-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5HttpRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.