Skip to content

Commit

Permalink
feat: Jetty HttpClient implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa committed Jun 3, 2022
1 parent c4a3291 commit 9e8ef20
Show file tree
Hide file tree
Showing 53 changed files with 3,082 additions and 104 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/e2e-httpclient-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
fail-fast: false
matrix:
kubernetes: [v1.24.0,v1.23.3, v1.12.10]
httpclient: [jdk]
httpclient: [jdk,jetty]
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down Expand Up @@ -67,7 +67,7 @@ jobs:
fail-fast: false
matrix:
openshift: [v3.11.0, v3.10.0]
httpclient: [jdk]
httpclient: [jdk,jetty]
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-snapshots.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ jobs:
gpg-private-key: ${{ secrets.SIGNINGKEY }}
gpg-passphrase: SIGNINGPASSWORD
- name: Build and release Java 11 modules
run: ./mvnw ${MAVEN_ARGS} ${RELEASE_MAVEN_ARGS} -pl "httpclient-jdk" clean deploy
run: ./mvnw ${MAVEN_ARGS} ${RELEASE_MAVEN_ARGS} -pl "httpclient-jdk" -pl "httpclient-jetty" clean deploy
2 changes: 1 addition & 1 deletion httpclient-jdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

<artifactId>kubernetes-httpclient-jdk</artifactId>
<packaging>jar</packaging>
<name>Fabric8 :: Kubernetes :: JDK HttpClient</name>
<name>Fabric8 :: Kubernetes :: HttpClient :: JDK</name>

<properties>
<maven.compiler.release>11</maven.compiler.release>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public Builder preferHttp11() {
}

@Override
public Builder tlsVersions(TlsVersion[] tlsVersions) {
public Builder tlsVersions(TlsVersion... tlsVersions) {
this.tlsVersions = tlsVersions;
return this;
}
Expand All @@ -192,7 +192,6 @@ public JdkHttpClientBuilderImpl copy(java.net.http.HttpClient httpClient) {
copy.readTimeout = this.readTimeout;
copy.sslContext = this.sslContext;
copy.interceptors = new LinkedHashMap<>(this.interceptors);
copy.followRedirects = this.followRedirects;
copy.proxyAddress = this.proxyAddress;
copy.proxyAuthorization = this.proxyAuthorization;
copy.tlsVersions = this.tlsVersions;
Expand All @@ -202,4 +201,4 @@ public JdkHttpClientBuilderImpl copy(java.net.http.HttpClient httpClient) {
return copy;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
Expand Down Expand Up @@ -132,6 +133,11 @@ public List<String> headers(String key) {
return response.headers().allValues(key);
}

@Override
public Map<String, List<String>> headers() {
return response.headers().map();
}

@Override
public int code() {
return response.statusCode();
Expand Down Expand Up @@ -334,13 +340,7 @@ public CompletableFuture<WebSocketResponse> internalBuildAsync(JdkWebSocketImpl.
// use a responseholder to convey both the exception and the websocket
CompletableFuture<WebSocketResponse> response = new CompletableFuture<>();

URI uri = request.uri();
if (uri.getScheme().startsWith("http")) {
// the jdk logic expects a ws uri
// after the https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8245245 it just does the reverse of this
// to convert back to http(s) ...
uri = URI.create("ws" + uri.toString().substring(4));
}
URI uri = WebSocket.toWebSocketUri(request.uri());
newBuilder.buildAsync(uri, new JdkWebSocketImpl.ListenerAdapter(listener, queueSize)).whenComplete((w, t) -> {
if (t instanceof CompletionException && t.getCause() != null) {
t = t.getCause();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Flow.Subscriber;

class JdkHttpRequestImpl implements HttpRequest {
import static io.fabric8.kubernetes.client.http.StandardHttpHeaders.CONTENT_TYPE;

private static final String CONTENT_TYPE = "Content-Type";
class JdkHttpRequestImpl implements HttpRequest {

public static class BuilderImpl implements Builder {

Expand Down Expand Up @@ -77,6 +78,7 @@ public Builder method(String method, String contentType, String body) {
@Override
public Builder post(String contentType, byte[] writeValueAsBytes) {
this.bodyString = null;
// TODO: Is this incomplete?
this.builder.setHeader(CONTENT_TYPE, contentType).POST(BodyPublishers.ofByteArray(writeValueAsBytes));
return this;
}
Expand Down Expand Up @@ -141,6 +143,11 @@ public List<String> headers(String key) {
return request.headers().allValues(key);
}

@Override
public Map<String, List<String>> headers() {
return request.headers().map();
}

@Override
public URI uri() {
return request.uri();
Expand Down
191 changes: 191 additions & 0 deletions httpclient-jetty/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2015 Red Hat, 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.
-->
<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>
<artifactId>kubernetes-client-project</artifactId>
<groupId>io.fabric8</groupId>
<version>6.0-SNAPSHOT</version>
</parent>

<artifactId>kubernetes-httpclient-jetty</artifactId>
<packaging>jar</packaging>
<name>Fabric8 :: Kubernetes :: HttpClient :: Jetty</name>

<properties>
<maven.compiler.release>11</maven.compiler.release>
<osgi.require-capability>
osgi.extender;
filter:="(osgi.extender=osgi.serviceloader.registrar)",
</osgi.require-capability>
<osgi.import>
!android.util*,
*,
</osgi.import>
<osgi.export>
io.fabric8.kubernetes.client.jetty*;-noimport:=true,
</osgi.export>
<osgi.private>
</osgi.private>
</properties>

<dependencies>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.http2</groupId>
<artifactId>http2-http-client-transport</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-jetty-client</artifactId>
</dependency>

<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client-api</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>mockwebserver</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- We cleanup system properties an env vars, so that we can test in a predictable env -->
<environmentVariables>
<KUBERNETES_MASTER />
<KUBERNETES_API_VERSION />
<KUBERNETES_TRUST_CERTIFICATES />
<KUBERNETES_CERTS_CA_FILE />
<KUBERNETES_CERTS_CA_DATA />
<KUBERNETES_CERTS_CLIENT_FILE />
<KUBERNETES_CERTS_CLIENT_DATA />
<KUBERNETES_CERTS_CLIENT_KEY_FILE />
<KUBERNETES_CERTS_CLIENT_KEY_DATA />
<KUBERNETES_CERTS_CLIENT_KEY_ALGO />
<KUBERNETES_CERTS_CLIENT_KEY_PASSPHRASE />
<KUBERNETES_AUTH_BASIC_USERNAME />
<KUBERNETES_AUTH_BASIC_PASSWORD />
<KUBERNETES_AUTH_TRYKUBECONFIG />
<KUBERNETES_AUTH_TRYSERVICEACCOUNT />
<KUBERNETES_AUTH_TOKEN />
<KUBERNETES_WATCH_RECONNECTINTERVAL />
<KUBERNETES_WATCH_RECONNECTLIMIT />
<KUBERNETES_REQUEST_TIMEOUT />
<KUBERNETES_NAMESPACE />
<KUBERNETES_TLS_VERSIONS>TLSv1.2,TLSv1.1,TLSv1</KUBERNETES_TLS_VERSIONS>
</environmentVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<classpathScope>test</classpathScope>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>report-aggregate</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>${maven.bundle.plugin.version}</version>
<executions>
<execution>
<id>bundle</id>
<phase>package</phase>
<goals>
<goal>bundle</goal>
</goals>
<configuration>
<instructions>
<Bundle-Name>${project.name}</Bundle-Name>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Export-Package>${osgi.export}</Export-Package>
<Import-Package>${osgi.import}</Import-Package>
<DynamicImport-Package>${osgi.dynamic.import}</DynamicImport-Package>
<Require-Capability>${osgi.require-capability}</Require-Capability>
<Provide-Capability>${osgi.provide-capability}</Provide-Capability>
<Private-Package>${osgi.private}</Private-Package>
<Require-Bundle>${osgi.bundles}</Require-Bundle>
<Bundle-Activator>${osgi.activator}</Bundle-Activator>
<Export-Service>${osgi.export.service}</Export-Service>
<Include-Resource>
/META-INF/services/io.fabric8.kubernetes.client.http.HttpClient$Factory=target/classes/META-INF/services/io.fabric8.kubernetes.client.http.HttpClient$Factory,
</Include-Resource>
</instructions>
<classifier>bundle</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright (C) 2015 Red Hat, 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 io.fabric8.kubernetes.client.jetty;

import io.fabric8.kubernetes.client.http.HttpClient;
import io.fabric8.kubernetes.client.http.Interceptor;

import java.time.Duration;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@SuppressWarnings("unchecked")
public abstract class DerivedJettyHttpClientBuilder<T extends HttpClient.DerivedClientBuilder> implements HttpClient.DerivedClientBuilder {

final JettyHttpClientFactory factory;
Duration readTimeout = Duration.ZERO;
Duration writeTimeout = Duration.ZERO;
final Map<String, Interceptor> interceptors;

DerivedJettyHttpClientBuilder(JettyHttpClientFactory factory) {
this.factory = factory;
interceptors = new LinkedHashMap<>();
}

@Override
public final T readTimeout(long readTimeout, TimeUnit unit) {
this.readTimeout = Duration.ofNanos(unit.toNanos(readTimeout));
return (T) this;
}

@Override
public T writeTimeout(long writeTimeout, TimeUnit unit) {
this.writeTimeout = Duration.ofNanos(unit.toNanos(writeTimeout));
return (T) this;
}

@Override
public T forStreaming() {
// NO OP
return (T) this;
}

@Override
public T authenticatorNone() {
// NO OP
return (T) this;
}

@Override
public T addOrReplaceInterceptor(String name, Interceptor interceptor) {
interceptors.put(name, interceptor);
return (T) this;
}
}

0 comments on commit 9e8ef20

Please sign in to comment.