Skip to content

Commit

Permalink
Adding client examples
Browse files Browse the repository at this point in the history
  • Loading branch information
joakime committed Jan 10, 2024
1 parent cc114cf commit fed2c3a
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
37 changes: 37 additions & 0 deletions embedded/client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.jetty.examples.embedded</groupId>
<artifactId>jetty-embedded-examples</artifactId>
<version>10.0.x</version>
</parent>
<artifactId>client</artifactId>
<version>10.0.x</version>
<packaging>jar</packaging>
<name>Jetty Examples :: Jetty 9.4.x :: Embedded :: Client</name>

<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.http2</groupId>
<artifactId>http2-client</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.http2</groupId>
<artifactId>http2-http-client-transport</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-slf4j-impl</artifactId>
<version>${jetty.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
2 changes: 2 additions & 0 deletions embedded/client/src/main/resources/jetty-logging.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
examples.LEVEL=DEBUG
org.eclipse.jetty.LEVEL=INFO
1 change: 1 addition & 0 deletions embedded/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<name>Jetty Examples :: Jetty 9.4.x :: Embedded</name>

<modules>
<module>client</module>
<module>client-certificates</module>
<module>compressed-encoding</module>
<module>connectors</module>
Expand Down

0 comments on commit fed2c3a

Please sign in to comment.