From d63dc1b5155255fb3122456b3d6058769386d668 Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Fri, 24 Mar 2023 08:36:20 +0100 Subject: [PATCH] HTTP/2 for JNH connector Signed-off-by: Maxim Nesen --- .../JavaNetHttpClientProperties.java | 12 ++++++++++- .../jnh/connector/JavaNetHttpConnector.java | 11 +++++++--- docs/src/main/docbook/appendix-properties.xml | 20 ++++++++++++++++++- docs/src/main/docbook/jersey.ent | 3 ++- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/connectors/jnh-connector/src/main/java/org/glassfish/jersey/jnh/connector/JavaNetHttpClientProperties.java b/connectors/jnh-connector/src/main/java/org/glassfish/jersey/jnh/connector/JavaNetHttpClientProperties.java index 3a48d2c69a7..55dfcf9651c 100644 --- a/connectors/jnh-connector/src/main/java/org/glassfish/jersey/jnh/connector/JavaNetHttpClientProperties.java +++ b/connectors/jnh-connector/src/main/java/org/glassfish/jersey/jnh/connector/JavaNetHttpClientProperties.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -75,6 +75,16 @@ public class JavaNetHttpClientProperties { public static final String DISABLE_COOKIES = "jersey.config.jnh.client.disableCookies"; + /** + * HTTP version - if null or instance of HttpClient.Version.HTTP_1_1 the version will be set to HTTP_1_1 + * if version is HttpClient.Version.HTTP_2 the client will attempt to perform each request using HTTP_2 protocol + * but if not supported by server, the protocol will be still HTTP_1_1 + * + * @since 3.1.2 + */ + public static final String HTTP_VERSION = + "jersey.config.jnh.client.httpVersion"; + /** * Prevent this class from instantiation. diff --git a/connectors/jnh-connector/src/main/java/org/glassfish/jersey/jnh/connector/JavaNetHttpConnector.java b/connectors/jnh-connector/src/main/java/org/glassfish/jersey/jnh/connector/JavaNetHttpConnector.java index 7530c4ec93d..997d1cf5fcb 100644 --- a/connectors/jnh-connector/src/main/java/org/glassfish/jersey/jnh/connector/JavaNetHttpConnector.java +++ b/connectors/jnh-connector/src/main/java/org/glassfish/jersey/jnh/connector/JavaNetHttpConnector.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -82,8 +82,10 @@ public class JavaNetHttpConnector implements Connector { * @param configuration the configuration properties for this connector */ public JavaNetHttpConnector(final Client client, final Configuration configuration) { - HttpClient.Builder httpClientBuilder = HttpClient.newBuilder(); - httpClientBuilder.version(HttpClient.Version.HTTP_1_1); + final HttpClient.Builder httpClientBuilder = HttpClient.newBuilder(); + final HttpClient.Version version = + getPropertyOrNull(configuration, JavaNetHttpClientProperties.HTTP_VERSION, HttpClient.Version.class); + httpClientBuilder.version(version == null ? HttpClient.Version.HTTP_1_1 : version); SSLContext sslContext = client.getSslContext(); if (sslContext != null) { httpClientBuilder.sslContext(sslContext); @@ -220,6 +222,9 @@ private T getPropertyOrNull(final Configuration configuration, final String if (propertyObject == null) { return null; } + if (resultClass.isEnum() && propertyObject instanceof String) { + return (T) Enum.valueOf(resultClass.asSubclass(Enum.class), (String) propertyObject); + } if (!resultClass.isInstance(propertyObject)) { LOGGER.warning(LocalizationMessages.ERROR_INVALID_CLASS(propertyKey, resultClass.getName())); return null; diff --git a/docs/src/main/docbook/appendix-properties.xml b/docs/src/main/docbook/appendix-properties.xml index c979b320545..14c4de3d087 100644 --- a/docs/src/main/docbook/appendix-properties.xml +++ b/docs/src/main/docbook/appendix-properties.xml @@ -1,7 +1,7 @@