From fed2c3ae725c9566b5902ceca22b30ac2593c635 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Wed, 10 Jan 2024 16:56:38 -0600 Subject: [PATCH] Adding client examples --- embedded/client/pom.xml | 37 +++++++++++ .../examples/ClientWithDynamicConnection.java | 63 +++++++++++++++++++ .../main/resources/jetty-logging.properties | 2 + embedded/pom.xml | 1 + 4 files changed, 103 insertions(+) create mode 100644 embedded/client/pom.xml create mode 100644 embedded/client/src/main/java/examples/ClientWithDynamicConnection.java create mode 100644 embedded/client/src/main/resources/jetty-logging.properties diff --git a/embedded/client/pom.xml b/embedded/client/pom.xml new file mode 100644 index 0000000..b501516 --- /dev/null +++ b/embedded/client/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + + org.eclipse.jetty.examples.embedded + jetty-embedded-examples + 10.0.x + + client + 10.0.x + jar + Jetty Examples :: Jetty 9.4.x :: Embedded :: Client + + + + org.eclipse.jetty + jetty-client + ${jetty.version} + + + org.eclipse.jetty.http2 + http2-client + ${jetty.version} + + + org.eclipse.jetty.http2 + http2-http-client-transport + ${jetty.version} + + + org.eclipse.jetty + jetty-slf4j-impl + ${jetty.version} + + + + diff --git a/embedded/client/src/main/java/examples/ClientWithDynamicConnection.java b/embedded/client/src/main/java/examples/ClientWithDynamicConnection.java new file mode 100644 index 0000000..b32cf93 --- /dev/null +++ b/embedded/client/src/main/java/examples/ClientWithDynamicConnection.java @@ -0,0 +1,63 @@ +// +// ======================================================================== +// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License v. 2.0 which is available at +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// ======================================================================== +// + +package examples; + +import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.client.api.ContentResponse; +import org.eclipse.jetty.client.dynamic.HttpClientTransportDynamic; +import org.eclipse.jetty.client.http.HttpClientConnectionFactory; +import org.eclipse.jetty.http2.client.HTTP2Client; +import org.eclipse.jetty.http2.client.http.ClientConnectionFactoryOverHTTP2; +import org.eclipse.jetty.io.ClientConnectionFactory; +import org.eclipse.jetty.io.ClientConnector; +import org.eclipse.jetty.util.ssl.SslContextFactory; + +/** + * Example of using a high level HttpClient to connect to a server that + * supports both HTTP/2 and HTTP/1.1, using TLSv1.3 only. + */ +public class ClientWithDynamicConnection +{ + public static void main(String[] args) throws Exception + { + SslContextFactory.Client sslContextFactory = new SslContextFactory.Client(); + sslContextFactory.setIncludeProtocols("TLSv1.3"); + ClientConnector clientConnector = new ClientConnector(); + clientConnector.setSslContextFactory(sslContextFactory); + + ClientConnectionFactory.Info h1 = HttpClientConnectionFactory.HTTP11; + HTTP2Client http2Client = new HTTP2Client(clientConnector); + ClientConnectionFactory.Info h2 = new ClientConnectionFactoryOverHTTP2.HTTP2(http2Client); + HttpClientTransportDynamic dynamicTransport = new HttpClientTransportDynamic(clientConnector, h1, h2); + + HttpClient httpClient = new HttpClient(dynamicTransport); + try + { + httpClient.start(); + // To see the SslContextFactory configuration, dump the client + System.out.printf("Dump of client: %s%n", httpClient.dump()); + ContentResponse res = httpClient.GET("https://api.github.com/zen"); + System.out.printf("response status: %d%n", res.getStatus()); + res.getHeaders().forEach((field) -> + { + System.out.printf("response header [%s]: %s%n", field.getName(), field.getValue()); + }); + System.out.printf("response body: %s%n", res.getContentAsString()); + } + finally + { + httpClient.stop(); + } + } +} diff --git a/embedded/client/src/main/resources/jetty-logging.properties b/embedded/client/src/main/resources/jetty-logging.properties new file mode 100644 index 0000000..047e1b0 --- /dev/null +++ b/embedded/client/src/main/resources/jetty-logging.properties @@ -0,0 +1,2 @@ +examples.LEVEL=DEBUG +org.eclipse.jetty.LEVEL=INFO \ No newline at end of file diff --git a/embedded/pom.xml b/embedded/pom.xml index 695a4f9..8d84827 100644 --- a/embedded/pom.xml +++ b/embedded/pom.xml @@ -13,6 +13,7 @@ Jetty Examples :: Jetty 9.4.x :: Embedded + client client-certificates compressed-encoding connectors