Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2025 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 Down Expand Up @@ -100,9 +100,19 @@
*/
public class ApacheConnectorProvider implements ConnectorProvider {

private final Config config;

public ApacheConnectorProvider() {
config = config();
}

private ApacheConnectorProvider(Config config) {
this.config = config;
}

@Override
public Connector getConnector(final Client client, final Configuration runtimeConfig) {
return new ApacheConnector(client, runtimeConfig);
return new ApacheConnector(client, runtimeConfig, config);
}

/**
Expand Down Expand Up @@ -159,4 +169,23 @@ private static ApacheConnector getConnector(final Configurable<?> component) {
throw new IllegalArgumentException(LocalizationMessages.EXPECTED_CONNECTOR_PROVIDER_NOT_USED());
}
}

/**
* Instantiate a builder allowing to configure the ApacheConnectorProvider.
* @return a new {@link Config} instance.
*/
public static Config config() {
return new Config();
}

public static final class Config extends ApacheConnectorConfiguration<Config> {

private Config() {
}

public ApacheConnectorProvider build() {
return new ApacheConnectorProvider(this);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ public Connector getConnector(final Client client, final Configuration configura
protected Connector createHttpUrlConnector(Client client, ConnectionFactory connectionFactory,
int chunkSize, boolean fixLengthStreaming,
boolean setMethodWorkaround) {
config.connectionFactory(connectionFactory)
.chunkSize(chunkSize)
.useFixedLengthStreaming(fixLengthStreaming)
.useSetMethodWorkaround(setMethodWorkaround);
return new HttpUrlConnector(client, client.getConfiguration(), config);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.glassfish.jersey.client.ClientRequest;
import org.glassfish.jersey.client.RequestEntityProcessing;
import org.glassfish.jersey.client.innate.http.SSLParamConfigurator;
import org.glassfish.jersey.client.internal.LocalizationMessages;
import org.glassfish.jersey.internal.PropertiesResolver;

import javax.net.ssl.SSLContext;
Expand All @@ -39,6 +40,7 @@
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.logging.Logger;

/**
* Configuration object to use for configuring the client connectors and HTTP request processing.
Expand All @@ -47,7 +49,11 @@
* @param <E> the connector configuration subtype.
*/
public class ConnectorConfiguration<E extends ConnectorConfiguration<E>> {

protected static final Logger LOGGER = Logger.getLogger(ConnectorConfiguration.class.getName());

protected final NullableRef<Integer> connectTimeout = NullableRef.empty();
protected final NullableRef<Integer> chunkSize = NullableRef.empty();
protected final NullableRef<Boolean> expect100Continue = NullableRef.empty();
protected final NullableRef<Long> expect100continueThreshold = NullableRef.empty();
protected final NullableRef<Boolean> followRedirects = NullableRef.empty();
Expand Down Expand Up @@ -261,6 +267,24 @@ protected E self() {
return (E) this;
}

protected E chunkSize(Map<String, Object> properties) {
chunkSize.ifEmptySet(ClientProperties.DEFAULT_CHUNK_SIZE);
int computedChunkSize = ClientProperties.getValue(properties,
_prefixed(ClientProperties.CHUNKED_ENCODING_SIZE), chunkSize.get(), Integer.class);
if (computedChunkSize < 0) {
LOGGER.warning(LocalizationMessages.NEGATIVE_CHUNK_SIZE(computedChunkSize, chunkSize.get()));
} else {
chunkSize.set(computedChunkSize);
}

return self();
}

protected String _prefixed(String property) {
return prefix.ifPresentOrElse("") + property;
}


/**
* <p>
* A reference to a value. The reference can be empty, but unlike the {@code Optional}, once a value is set,
Expand Down Expand Up @@ -445,6 +469,7 @@ protected interface Read<CC extends ConnectorConfiguration<CC> & Read<CC>>
*/
public default <X extends ConnectorConfiguration<?>> void setNonEmpty(X other) {
me().connectTimeout.setNonEmpty(other.connectTimeout);
me().chunkSize.setNonEmpty(other.chunkSize);
me().expect100Continue.setNonEmpty(other.expect100Continue);
me().expect100continueThreshold.setNonEmpty(other.expect100continueThreshold);
me().followRedirects.setNonEmpty(other.followRedirects);
Expand Down Expand Up @@ -552,6 +577,10 @@ public default CC copyFromRequest(ClientRequest request) {
return requestConfiguration;
}

default int chunkSize() {
return me().chunkSize.get();
}

@Override
default String getSniHostNameProperty(Configuration configuration) {
Object property = configuration.getProperty(prefixed(ClientProperties.SNI_HOST_NAME));
Expand Down Expand Up @@ -649,6 +678,10 @@ public default Optional<ClientProxy> proxy(ClientRequest request, URI requestUri
return proxy;
}

public default Optional<ClientProxy> proxy(Configuration configuration) {
return ClientProxy.proxyFromConfiguration(new PrefixedConfiguration(me().prefix.get(), configuration));
}

/**
* Update {@link #readTimeout(int) read timeout} based on the HTTP request properties.
*
Expand Down Expand Up @@ -712,6 +745,23 @@ public default SSLContext sslContext(Client client, ClientRequest request) {
return supplier == null ? client.getSslContext() : supplier.get();
}

/**
* Get {@link SSLContext} either from the {@link ClientProperties#SSL_CONTEXT_SUPPLIER}, or from this configuration,
* or from the {@link Client#getSslContext()} in this order.
*
* @param client the client used to get the {@link SSLContext}.
* @return the {@link SSLContext}.
*/
public default SSLContext sslContext(Client client) {
@SuppressWarnings("unchecked")
Supplier<SSLContext> supplier =
(Supplier<SSLContext>) client.getConfiguration().getProperty(prefixed(ClientProperties.SSL_CONTEXT_SUPPLIER));
if (supplier == null) {
supplier = me().sslContextSupplier.get();
}
return supplier == null ? client.getSslContext() : supplier.get();
}

public default String prefixed(String propertyName) {
return me().prefix.get() + propertyName;
}
Expand Down Expand Up @@ -832,7 +882,7 @@ protected static class PrefixedConfiguration implements Configuration {
private final String prefix;
private final Configuration inner;

private PrefixedConfiguration(String prefix, Configuration inner) {
protected PrefixedConfiguration(String prefix, Configuration inner) {
this.prefix = prefix;
this.inner = inner;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ private ClientResponse _apply(final ClientRequest request) throws IOException {
if (requestConfiguration.useFixedLengthStreaming.get() && length > 0) {
uc.setFixedLengthStreamingMode(length);
} else if (entityProcessing == RequestEntityProcessing.CHUNKED) {
uc.setChunkedStreamingMode(requestConfiguration.chunkSize.get());
uc.setChunkedStreamingMode(requestConfiguration.chunkSize());
}
}
uc.setDoOutput(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,16 @@ public abstract class HttpUrlConnectorConfiguration<C extends HttpUrlConnectorCo
new DefaultConnectionFactory();

protected NullableRef<HttpUrlConnectorProvider.ConnectionFactory> connectionFactory = NullableRef.empty();
protected Ref<Integer> chunkSize = NullableRef.empty();
/* package */ Ref<Boolean> isRestrictedHeaderPropertySet = NullableRef.empty();
protected Ref<Boolean> useFixedLengthStreaming = NullableRef.empty();
protected Ref<Boolean> useSetMethodWorkaround = NullableRef.empty();

protected void preInit(Map<String, Object> properties) {
connectionFactory.ifEmptySet(DEFAULT_CONNECTION_FACTORY);
((NullableRef<Integer>) chunkSize).ifEmptySet(ClientProperties.DEFAULT_CHUNK_SIZE);
((NullableRef<Boolean>) useFixedLengthStreaming).ifEmptySet(Boolean.FALSE);
((NullableRef<Boolean>) useSetMethodWorkaround).ifEmptySet(Boolean.FALSE);

int computedChunkSize = ClientProperties.getValue(properties,
_prefixed(ClientProperties.CHUNKED_ENCODING_SIZE), chunkSize.get(), Integer.class);
if (computedChunkSize < 0) {
LOGGER.warning(LocalizationMessages.NEGATIVE_CHUNK_SIZE(computedChunkSize, chunkSize.get()));
} else {
chunkSize.set(computedChunkSize);
}
chunkSize(properties);

useFixedLengthStreaming(ClientProperties.getValue(properties,
_prefixed(HttpUrlConnectorProvider.USE_FIXED_LENGTH_STREAMING),
Expand All @@ -74,10 +66,6 @@ protected void preInit(Map<String, Object> properties) {
useSetMethodWorkaround.get(), Boolean.class));
}

private String _prefixed(String property) {
return prefix.ifPresentOrElse("") + property;
}

/**
* Set a custom {@link java.net.HttpURLConnection} factory.
*
Expand Down Expand Up @@ -218,6 +206,22 @@ ReadWrite fromClient(Configuration configuration) {
ReadWrite clientConfiguration = copyFromClient(configuration);
clientConfiguration.preInit(configuration.getProperties());

int computedChunkSize = ClientProperties.getValue(configuration.getProperties(),
prefixed(ClientProperties.CHUNKED_ENCODING_SIZE), clientConfiguration.chunkSize.get(), Integer.class);
if (computedChunkSize < 0) {
LOGGER.warning(LocalizationMessages.NEGATIVE_CHUNK_SIZE(computedChunkSize, clientConfiguration.chunkSize.get()));
} else {
clientConfiguration.chunkSize.set(computedChunkSize);
}

useFixedLengthStreaming(ClientProperties.getValue(configuration.getProperties(),
prefixed(HttpUrlConnectorProvider.USE_FIXED_LENGTH_STREAMING),
clientConfiguration.useFixedLengthStreaming.get(), Boolean.class));
useSetMethodWorkaround(ClientProperties.getValue(configuration.getProperties(),
prefixed(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND),
clientConfiguration.useSetMethodWorkaround.get(), Boolean.class));


// check if sun.net.http.allowRestrictedHeaders system property has been set and log the result
// the property is being cached in the HttpURLConnection, so this is only informative - there might
// already be some connection(s), that existed before the property was set/changed.
Expand Down
Loading