Skip to content

Commit

Permalink
- New test for protocol upgrade without TLS
Browse files Browse the repository at this point in the history
- Fixes problem in connector when accessing request properties
- Renames test classes for clarity
  • Loading branch information
spericas committed Sep 15, 2023
1 parent 596f07c commit b272981
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@
import org.glassfish.jersey.client.spi.Connector;
import org.glassfish.jersey.internal.util.PropertiesHelper;

import static io.helidon.jersey.connector.HelidonProperties.PROTOCOL_CONFIG;
import static io.helidon.jersey.connector.HelidonProperties.PROTOCOL_PREFERENCE;
import static io.helidon.jersey.connector.HelidonProperties.DEFAULT_HEADERS;
import static io.helidon.jersey.connector.HelidonProperties.PROTOCOL_CONFIGS;
import static io.helidon.jersey.connector.HelidonProperties.PROTOCOL_ID;
import static io.helidon.jersey.connector.HelidonProperties.TLS;
import static org.glassfish.jersey.client.ClientProperties.CONNECT_TIMEOUT;
import static org.glassfish.jersey.client.ClientProperties.FOLLOW_REDIRECTS;
Expand All @@ -63,6 +64,7 @@ class HelidonConnector implements Connector {
static final Logger LOGGER = Logger.getLogger(HelidonConnector.class.getName());

private static final int DEFAULT_TIMEOUT = 10000;
private static final Map<String, String> EMPTY_MAP_LIST = Map.of("", "");

private static final String HELIDON_VERSION = "Helidon/" + Version.VERSION + " (java "
+ PropertiesHelper.getSystemProperty("java.runtime.version") + ")";
Expand Down Expand Up @@ -103,16 +105,16 @@ class HelidonConnector implements Connector {
builder.tls(getValue(properties, TLS, Tls.class));
hasTls = true;
}
if (properties.containsKey(PROTOCOL_PREFERENCE)) {
builder.addProtocolPreference(getValue(properties, PROTOCOL_PREFERENCE, List.of("")));
}
if (properties.containsKey(PROTOCOL_CONFIG)) {
if (properties.containsKey(PROTOCOL_CONFIGS)) {
List<? extends ProtocolConfig> protocolConfigs =
(List<? extends ProtocolConfig>) properties.get(PROTOCOL_CONFIG);
(List<? extends ProtocolConfig>) properties.get(PROTOCOL_CONFIGS);
if (protocolConfigs != null) {
builder.addProtocolConfigs(protocolConfigs);
}
}
if (properties.containsKey(DEFAULT_HEADERS)) {
builder.defaultHeadersMap(getValue(properties, DEFAULT_HEADERS, EMPTY_MAP_LIST));
}
webClient = builder.build();
}

Expand Down Expand Up @@ -157,11 +159,17 @@ private HttpClientRequest mapRequest(ClientRequest request) {
}

// request config
if (request.hasProperty(FOLLOW_REDIRECTS)) {
httpRequest.followRedirects(request.resolveProperty(FOLLOW_REDIRECTS, true));
Boolean followRedirects = request.resolveProperty(FOLLOW_REDIRECTS, Boolean.class);
if (followRedirects != null) {
httpRequest.followRedirects(followRedirects);
}
Duration readTimeout = request.resolveProperty(READ_TIMEOUT, Duration.class);
if (readTimeout != null) {
httpRequest.readTimeout(readTimeout);
}
if (request.hasProperty(READ_TIMEOUT)) {
httpRequest.readTimeout(Duration.ofMillis(request.resolveProperty(READ_TIMEOUT, DEFAULT_TIMEOUT)));
String protocolId = request.resolveProperty(PROTOCOL_ID, String.class);
if (protocolId != null) {
httpRequest.protocolId(protocolId);
}

// copy properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.helidon.jersey.connector;

import java.util.List;
import java.util.Map;

import io.helidon.common.tls.Tls;
import io.helidon.config.Config;
Expand Down Expand Up @@ -46,22 +47,29 @@ private HelidonProperties() {
*/
public static final String TLS = "jersey.connector.helidon.tls";

/**
* Property name to set a {@code List<String>} instance with a list of protocol preferences
* to be used by underlying {@link WebClient}.
* This property is settable on {@link jakarta.ws.rs.core.Configurable#property(String, Object)}.
*
* @see io.helidon.webclient.api.WebClientConfig.Builder#addProtocolPreference(List)
*/
public static final String PROTOCOL_PREFERENCE = "jersey.connector.helidon.protocolPreference";

/**
* Property name to set a {@code List<? extends ProtocolConfig>} instance with a list of
* protocol configs to be used by underlying {@link WebClient}.
* This property is settable on {@link jakarta.ws.rs.core.Configurable#property(String, Object)}.
*
* @see io.helidon.webclient.api.WebClientConfig.Builder#protocolConfigs(List)
*/
public static final String PROTOCOL_CONFIG = "jersey.connector.helidon.protocolConfig";
public static final String PROTOCOL_CONFIGS = "jersey.connector.helidon.protocolConfigs";

/**
* Property name to set a {@code Map<String, String>} instance with a list of
* default headers to be used by underlying {@link WebClient}.
* This property is settable on {@link jakarta.ws.rs.core.Configurable#property(String, Object)}.
*
* @see io.helidon.webclient.api.WebClientConfig.Builder#defaultHeadersMap(Map)
*/
public static final String DEFAULT_HEADERS = "jersey.connector.helidon.defaultHeaders";

/**
* Property name to set a protocol ID for each request. You can use this property
* to request an HTTP/2 upgrade from HTTP/1.1 by setting its value to {@code "h2"}.
*
* @see io.helidon.webclient.api.HttpClientRequest#protocolId(String)
*/
public static final String PROTOCOL_ID = "jersey.connector.helidon.protocolId";
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Arrays;

import io.helidon.http.Http;
import io.helidon.webclient.http2.Http2Client;
import io.helidon.webserver.http.HttpRules;
import io.helidon.webserver.http.ServerRequest;
import io.helidon.webserver.http.ServerResponse;
Expand All @@ -36,21 +37,26 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasKey;

class JerseyConnectorBase {
class ConnectorBase {

protected String baseURI;
protected Client client;
protected String protocolId;

@SetUpRoute
static void routing(HttpRules rules) {
rules.get("/basic/get", JerseyConnectorBase::basicGet)
.post("/basic/post", JerseyConnectorBase::basicPost)
.get("/basic/getquery", JerseyConnectorBase::basicGetQuery)
.get("/basic/headers", JerseyConnectorBase::basicHeaders);
rules.get("/basic/get", ConnectorBase::basicGet)
.post("/basic/post", ConnectorBase::basicPost)
.get("/basic/getquery", ConnectorBase::basicGetQuery)
.get("/basic/headers", ConnectorBase::basicHeaders);
}

private WebTarget target(String uri) {
return client.target(baseURI).path(uri);
WebTarget webTarget = client.target(baseURI).path(uri);
if (protocolId != null) {
webTarget.property(HelidonProperties.PROTOCOL_ID, Http2Client.PROTOCOL_ID);
}
return webTarget;
}

static void basicGet(ServerRequest request, ServerResponse response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
* WebClient to execute HTTP requests.
*/
@ServerTest
class JerseyConnectorHttp1Test extends JerseyConnectorBase {
class ConnectorHttp1Test extends ConnectorBase {

JerseyConnectorHttp1Test(WebServer webServer) {
ConnectorHttp1Test(WebServer webServer) {
baseURI = "http://localhost:" + webServer.port();
ClientConfig config = new ClientConfig();
config.connectorProvider(new HelidonConnectorProvider()); // use Helidon's provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@
import jakarta.ws.rs.client.ClientBuilder;
import org.glassfish.jersey.client.ClientConfig;

import static io.helidon.jersey.connector.HelidonProperties.TLS;

/**
* Tests HTTP/2 integration of Jakarta REST client with the Helidon connector that uses
* WebClient to execute HTTP requests. Uses ALPN extension for negotiation.
*/
@ServerTest
class JerseyConnectorHttp2AlpnTest extends JerseyConnectorBase {
class ConnectorHttp2AlpnTest extends ConnectorBase {

@SetUpServer
static void setUpServer(WebServerConfig.Builder serverBuilder) {
Expand All @@ -54,7 +52,7 @@ static void setUpServer(WebServerConfig.Builder serverBuilder) {
serverBuilder.addProtocol(Http2Config.create()); // h2 support ALPN
}

JerseyConnectorHttp2AlpnTest(WebServer server) {
ConnectorHttp2AlpnTest(WebServer server) {
int port = server.port("https");

Tls tls = Tls.builder()
Expand All @@ -65,7 +63,7 @@ static void setUpServer(WebServerConfig.Builder serverBuilder) {

ClientConfig config = new ClientConfig();
config.connectorProvider(new HelidonConnectorProvider()); // use Helidon's provider
config.property(TLS, tls);
config.property(HelidonProperties.TLS, tls);

client = ClientBuilder.newClient(config);
baseURI = "https://localhost:" + port;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import io.helidon.common.configurable.Resource;
import io.helidon.common.pki.Keys;
import io.helidon.common.tls.Tls;
import io.helidon.webclient.http2.Http2Client;
import io.helidon.webclient.http2.Http2ClientProtocolConfig;
import io.helidon.webserver.WebServer;
import io.helidon.webserver.WebServerConfig;
Expand All @@ -31,16 +30,12 @@
import jakarta.ws.rs.client.ClientBuilder;
import org.glassfish.jersey.client.ClientConfig;

import static io.helidon.jersey.connector.HelidonProperties.PROTOCOL_CONFIG;
import static io.helidon.jersey.connector.HelidonProperties.PROTOCOL_PREFERENCE;
import static io.helidon.jersey.connector.HelidonProperties.TLS;

/**
* Tests HTTP/2 integration of Jakarta REST client with the Helidon connector that uses
* WebClient to execute HTTP requests. Assumes prior knowledge.
*/
@ServerTest
class JerseyConnectorHttp2PriorTest extends JerseyConnectorBase {
class ConnectorHttp2PriorTest extends ConnectorBase {

@SetUpServer
static void setUpServer(WebServerConfig.Builder serverBuilder) {
Expand All @@ -59,7 +54,7 @@ static void setUpServer(WebServerConfig.Builder serverBuilder) {
serverBuilder.addProtocol(Http2Config.create());
}

JerseyConnectorHttp2PriorTest(WebServer server) {
ConnectorHttp2PriorTest(WebServer server) {
int port = server.port("https");

Tls tls = Tls.builder()
Expand All @@ -69,9 +64,8 @@ static void setUpServer(WebServerConfig.Builder serverBuilder) {

ClientConfig config = new ClientConfig();
config.connectorProvider(new HelidonConnectorProvider()); // use Helidon's provider
config.property(TLS, tls);
config.property(PROTOCOL_PREFERENCE, List.of(Http2Client.PROTOCOL_ID));
config.property(PROTOCOL_CONFIG, List.of(Http2ClientProtocolConfig.builder()
config.property(HelidonProperties.TLS, tls);
config.property(HelidonProperties.PROTOCOL_CONFIGS, List.of(Http2ClientProtocolConfig.builder()
.priorKnowledge(true)
.build()));
client = ClientBuilder.newClient(config);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* 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.helidon.jersey.connector;

import io.helidon.webclient.http2.Http2Client;
import io.helidon.webserver.WebServer;
import io.helidon.webserver.WebServerConfig;
import io.helidon.webserver.http2.Http2Config;
import io.helidon.webserver.testing.junit5.ServerTest;
import io.helidon.webserver.testing.junit5.SetUpServer;
import jakarta.ws.rs.client.ClientBuilder;
import org.glassfish.jersey.client.ClientConfig;

/**
* Tests HTTP/2 integration of Jakarta REST client with the Helidon connector that uses
* WebClient to execute HTTP requests. Upgrades connection from HTTP/1.1 to HTTP/2.
*/
@ServerTest
class ConnectorHttp2UpgradeTest extends ConnectorBase {

@SetUpServer
static void setUpServer(WebServerConfig.Builder serverBuilder) {
serverBuilder.addProtocol(Http2Config.create());
}

ConnectorHttp2UpgradeTest(WebServer server) {
int port = server.port();

ClientConfig config = new ClientConfig();
config.connectorProvider(new HelidonConnectorProvider()); // use Helidon's provider

client = ClientBuilder.newClient(config);
baseURI = "http://localhost:" + port;
protocolId = Http2Client.PROTOCOL_ID; // HTTP/2 negotiation from 1.1
}
}

0 comments on commit b272981

Please sign in to comment.