Skip to content

Commit

Permalink
Allow for setting connector provider via properties (#5345)
Browse files Browse the repository at this point in the history
* Allow for setting connector provider via properties

Signed-off-by: jansupol <[email protected]>
  • Loading branch information
jansupol authored Jun 15, 2023
1 parent 08a85c7 commit 635d16d
Show file tree
Hide file tree
Showing 17 changed files with 561 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 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
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.helidon.connector;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.HttpUrlConnectorProvider;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import java.io.IOException;

// The Helidon jar has META-INF set
// Test of override
class MetaInfOverrideTest extends JerseyTest {

@Path("/origin")
public static class UserAgentServer {
@GET
public String get(@Context HttpHeaders headers) {
return headers.getHeaderString(HttpHeaders.USER_AGENT);
}
}

@Override
protected Application configure() {
return new ResourceConfig(UserAgentServer.class);
}

@Test
void defaultMetaInfTest() {
try (Response r = target("origin").request().get()) {
Assertions.assertEquals(200, r.getStatus());
Assertions.assertTrue(r.readEntity(String.class).contains("Helidon"));
}
}

@Test
void overrideMetaInfTest() {
ClientConfig config = new ClientConfig();
config.connectorProvider(new HttpUrlConnectorProvider());
try (Response r = ClientBuilder.newClient(config).target(target("origin").getUri()).request().get()) {
Assertions.assertEquals(200, r.getStatus());
r.bufferEntity();
System.out.println(r.readEntity(String.class));
Assertions.assertTrue(r.readEntity(String.class).contains("HttpUrlConnection"));
}
}

@Test
void overrideMetaInfByOtherConfigPropertyTest() {
ClientConfig config = new ClientConfig();
config.property(ClientProperties.CONNECTOR_PROVIDER, "org.glassfish.jersey.client.HttpUrlConnectorProvider");
try (Response r = ClientBuilder.newClient(config).target(target("origin").getUri()).request().get()) {
Assertions.assertEquals(200, r.getStatus());
r.bufferEntity();
System.out.println(r.readEntity(String.class));
Assertions.assertTrue(r.readEntity(String.class).contains("HttpUrlConnection"));
}
}

@Test
void overrideMetaInfByThePropertyTest() {
try (Response r = ClientBuilder.newBuilder()
.property(ClientProperties.CONNECTOR_PROVIDER, "org.glassfish.jersey.client.HttpUrlConnectorProvider")
.build()
.target(target("origin").getUri()).request().get()) {
Assertions.assertEquals(200, r.getStatus());
r.bufferEntity();
System.out.println(r.readEntity(String.class));
Assertions.assertTrue(r.readEntity(String.class).contains("HttpUrlConnection"));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 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
Expand All @@ -24,6 +24,8 @@
import org.glassfish.jersey.internal.util.PropertiesHelper;
import org.glassfish.jersey.internal.util.PropertyAlias;

import javax.ws.rs.client.ClientBuilder;

/**
* Jersey client implementation configuration properties.
*
Expand Down Expand Up @@ -444,7 +446,7 @@ public final class ClientProperties {
EXPECT_100_CONTINUE_THRESHOLD_SIZE = "jersey.config.client.request.expect.100.continue.threshold.size";

/**
* Default threshold size (64kb) after which which Expect:100-Continue header would be applied before
* Default threshold size (64kb) after which Expect:100-Continue header would be applied before
* the main request.
*
* @since 2.32
Expand All @@ -463,6 +465,22 @@ public final class ClientProperties {
*/
public static final String QUERY_PARAM_STYLE = "jersey.config.client.uri.query.param.style";

/**
* Sets the {@link org.glassfish.jersey.client.spi.ConnectorProvider} class. Overrides the value from META-INF/services.
*
* <p>
* The value MUST be an instance of {@code String}.
* </p>
* <p>
* The property is recognized by {@link ClientBuilder}.
* </p>
* <p>
* The name of the configuration property is <tt>{@value}</tt>.
* </p>
* @since 2.40
*/
public static final String CONNECTOR_PROVIDER = "jersey.config.client.connector.provider";

private ClientProperties() {
// prevents instantiation
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 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
Expand All @@ -16,6 +16,7 @@

package org.glassfish.jersey.client;

import java.security.AccessController;
import java.security.KeyStore;
import java.util.Collections;
import java.util.LinkedList;
Expand All @@ -32,9 +33,13 @@
import javax.net.ssl.SSLContext;

import org.glassfish.jersey.SslConfigurator;
import org.glassfish.jersey.client.innate.inject.NonInjectionManager;
import org.glassfish.jersey.client.internal.LocalizationMessages;
import org.glassfish.jersey.client.spi.ClientBuilderListener;
import org.glassfish.jersey.client.spi.ConnectorProvider;
import org.glassfish.jersey.internal.ServiceFinder;
import org.glassfish.jersey.internal.config.ExternalPropertiesConfigurationFactory;
import org.glassfish.jersey.internal.util.ReflectionHelper;
import org.glassfish.jersey.internal.util.collection.UnsafeValue;
import org.glassfish.jersey.internal.util.collection.Values;
import org.glassfish.jersey.model.internal.RankedComparator;
Expand Down Expand Up @@ -186,6 +191,9 @@ public ClientBuilder readTimeout(long timeout, TimeUnit unit) {

@Override
public JerseyClient build() {
ExternalPropertiesConfigurationFactory.configure(this.config);
setConnectorFromProperties();

if (sslContext != null) {
return new JerseyClient(config, sslContext, hostnameVerifier, null);
} else if (sslConfigurator != null) {
Expand All @@ -204,6 +212,20 @@ public SSLContext get() {
}
}

private void setConnectorFromProperties() {
final Object connectorClass = config.getProperty(ClientProperties.CONNECTOR_PROVIDER);
if (connectorClass != null) {
if (String.class.isInstance(connectorClass)) {
Class<? extends ConnectorProvider> clazz
= AccessController.doPrivileged(ReflectionHelper.classForNamePA((String) connectorClass));
final ConnectorProvider connectorProvider = new NonInjectionManager().justCreate(clazz);
config.connectorProvider(connectorProvider);
} else {
throw new IllegalArgumentException();
}
}
}

@Override
public ClientConfig getConfiguration() {
return config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,31 @@ public <T> T create(Class<T> createMe) {
}

ClassBindings<T> classBindings = classBindings(createMe);
return classBindings.create();
return classBindings.create(true);
}

private <T> T justCreate(Class<T> createMe) {
@Override
public <T> T createAndInitialize(Class<T> createMe) {
checkShutdown();

if (InjectionManager.class.equals(createMe)) {
return (T) this;
}
if (RequestScope.class.equals(createMe)) {
if (!isRequestScope) {
isRequestScope = true;
return (T) new NonInjectionRequestScope();
} else {
throw new IllegalStateException(LocalizationMessages.NONINJECT_REQUESTSCOPE_CREATED());
}
}

ClassBindings<T> classBindings = classBindings(createMe);
T t = classBindings.create(false);
return t != null ? t : justCreate(createMe);
}

public <T> T justCreate(Class<T> createMe) {
T result = null;
try {
Constructor<T> mostArgConstructor = findConstructor(createMe);
Expand Down Expand Up @@ -492,11 +513,6 @@ private void checkUnique(List<?> list) {
}
}

@Override
public <T> T createAndInitialize(Class<T> createMe) {
return justCreate(createMe);
}

@Override
public void inject(Object injectMe) {
Method postConstruct = getAnnotatedMethod(injectMe, PostConstruct.class);
Expand Down Expand Up @@ -750,7 +766,7 @@ private X _create(SupplierInstanceBinding<X> binding) {
return t;
}

X create() {
X create(boolean throwWhenNoBinding) {
_checkUnique();
if (!instanceBindings.isEmpty()) {
return _getInstance(instanceBindings.get(0));
Expand All @@ -762,15 +778,19 @@ X create() {
return _create(supplierClassBindings.get(0));
}

throw new IllegalStateException(LocalizationMessages.NONINJECT_NO_BINDING(type));
if (throwWhenNoBinding) {
throw new IllegalStateException(LocalizationMessages.NONINJECT_NO_BINDING(type));
} else {
return null;
}
}

protected X getInstance() {
X instance = instances.getInstance(type, instancesQualifiers);
if (instance != null) {
return instance;
}
return create();
return create(true);
}

List<X> allInstances() {
Expand Down Expand Up @@ -931,7 +951,7 @@ protected T _createAndStore(ClassBinding<T> binding) {

@SuppressWarnings("unchecked")
@Override
T create() {
T create(boolean throwWhenNoBinding) {
if (ParameterizedType.class.isInstance(type)) {
ParameterizedType pt = (ParameterizedType) type;
if (Provider.class.equals(pt.getRawType())) {
Expand All @@ -955,7 +975,7 @@ public Object get() {
};
}
}
return super.create();
return super.create(throwWhenNoBinding);
}
}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
org.glassfish.jersey.logging.LoggingFeatureAutoDiscoverable
org.glassfish.jersey.internal.config.ExternalPropertiesAutoDiscoverable
org.glassfish.jersey.logging.LoggingFeatureAutoDiscoverable
Loading

0 comments on commit 635d16d

Please sign in to comment.