org.glassfish.jersey.client.ClientConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jaxrs-ri Show documentation
Show all versions of jaxrs-ri Show documentation
A bundle project producing JAX-RS RI bundles. The primary artifact is an "all-in-one" OSGi-fied JAX-RS RI bundle
(jaxrs-ri.jar).
Attached to that are two compressed JAX-RS RI archives. The first archive (jaxrs-ri.zip) consists of binary RI bits and
contains the API jar (under "api" directory), RI libraries (under "lib" directory) as well as all external
RI dependencies (under "ext" directory). The secondary archive (jaxrs-ri-src.zip) contains buildable JAX-RS RI source
bundle and contains the API jar (under "api" directory), RI sources (under "src" directory) as well as all external
RI dependencies (under "ext" directory). The second archive also contains "build.xml" ANT script that builds the RI
sources. To build the JAX-RS RI simply unzip the archive, cd to the created jaxrs-ri directory and invoke "ant" from
the command line.
/*
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018 Payara Foundation and/or its affiliates.
*
* 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.client;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import jakarta.ws.rs.RuntimeType;
import jakarta.ws.rs.core.Configurable;
import jakarta.ws.rs.core.Configuration;
import jakarta.ws.rs.core.Feature;
import org.glassfish.jersey.CommonProperties;
import org.glassfish.jersey.ExtendedConfig;
import org.glassfish.jersey.client.internal.LocalizationMessages;
import org.glassfish.jersey.client.innate.inject.NonInjectionManager;
import org.glassfish.jersey.client.internal.inject.ParameterUpdaterConfigurator;
import org.glassfish.jersey.client.spi.Connector;
import org.glassfish.jersey.client.spi.ConnectorProvider;
import org.glassfish.jersey.internal.AutoDiscoverableConfigurator;
import org.glassfish.jersey.internal.BootstrapBag;
import org.glassfish.jersey.internal.BootstrapConfigurator;
import org.glassfish.jersey.internal.ContextResolverFactory;
import org.glassfish.jersey.internal.ExceptionMapperFactory;
import org.glassfish.jersey.internal.FeatureConfigurator;
import org.glassfish.jersey.internal.JaxrsProviders;
import org.glassfish.jersey.internal.ServiceFinder;
import org.glassfish.jersey.internal.inject.Bindings;
import org.glassfish.jersey.internal.inject.InjectionManager;
import org.glassfish.jersey.internal.inject.Injections;
import org.glassfish.jersey.internal.inject.ProviderBinder;
import org.glassfish.jersey.internal.spi.AutoDiscoverable;
import org.glassfish.jersey.internal.util.collection.LazyValue;
import org.glassfish.jersey.internal.util.collection.Value;
import org.glassfish.jersey.internal.util.collection.Values;
import org.glassfish.jersey.model.internal.CommonConfig;
import org.glassfish.jersey.model.internal.ComponentBag;
import org.glassfish.jersey.model.internal.ManagedObjectsFinalizer;
import org.glassfish.jersey.process.internal.RequestScope;
import org.glassfish.jersey.internal.inject.ParamConverterConfigurator;
import org.glassfish.jersey.spi.ComponentProvider;
/**
* Jersey externalized implementation of client-side JAX-RS {@link jakarta.ws.rs.core.Configurable
* configurable} contract.
*
* @author Marek Potociar
* @author Martin Matula
* @author Libor Kramolis
* @author Gaurav Gupta ([email protected])
*/
public class ClientConfig implements Configurable, ExtendedConfig {
/**
* Internal configuration state.
*/
private State state;
private static class RuntimeConfigConfigurator implements BootstrapConfigurator {
private final State runtimeConfig;
private RuntimeConfigConfigurator(State runtimeConfig) {
this.runtimeConfig = runtimeConfig;
}
@Override
public void init(InjectionManager injectionManager, BootstrapBag bootstrapBag) {
bootstrapBag.setConfiguration(runtimeConfig);
injectionManager.register(Bindings.service(runtimeConfig).to(Configuration.class));
}
}
/**
* Default encapsulation of the internal configuration state.
*/
private static class State implements Configurable, ExtendedConfig {
/**
* Strategy that returns the same state instance.
*/
private static final StateChangeStrategy IDENTITY = state -> state;
/**
* Strategy that returns a copy of the state instance.
*/
private static final StateChangeStrategy COPY_ON_CHANGE = State::copy;
private volatile StateChangeStrategy strategy;
private final CommonConfig commonConfig;
private final JerseyClient client;
private volatile ConnectorProvider connectorProvider;
private volatile ExecutorService executorService;
private volatile ScheduledExecutorService scheduledExecutorService;
private final LazyValue runtime = Values.lazy((Value) this::initRuntime);
/**
* Configuration state change strategy.
*/
private interface StateChangeStrategy {
/**
* Invoked whenever a mutator method is called in the given configuration
* state.
*
* @param state configuration state to be mutated.
* @return state instance that will be mutated and returned from the
* invoked configuration state mutator method.
*/
State onChange(final State state);
}
/**
* Default configuration state constructor with {@link StateChangeStrategy "identity"}
* state change strategy.
*
* @param client bound parent Jersey client.
*/
State(final JerseyClient client) {
this.strategy = IDENTITY;
this.commonConfig = new CommonConfig(RuntimeType.CLIENT, ComponentBag.EXCLUDE_EMPTY);
this.client = client;
final Iterator iterator = ServiceFinder.find(ConnectorProvider.class).iterator();
if (iterator.hasNext()) {
this.connectorProvider = iterator.next();
} else {
this.connectorProvider = new HttpUrlConnectorProvider();
}
}
/**
* Copy the original configuration state while using the default state change
* strategy.
*
* @param client new Jersey client parent for the state.
* @param original configuration strategy to be copied.
*/
private State(final JerseyClient client, final State original) {
this.strategy = IDENTITY;
this.client = client;
this.commonConfig = new CommonConfig(original.commonConfig);
this.connectorProvider = original.connectorProvider;
this.executorService = original.executorService;
this.scheduledExecutorService = original.scheduledExecutorService;
}
/**
* Create a copy of the configuration state within the same parent Jersey
* client instance scope.
*
* @return configuration state copy.
*/
State copy() {
return new State(this.client, this);
}
/**
* Create a copy of the configuration state in a scope of the given
* parent Jersey client instance.
*
* @param client parent Jersey client instance.
* @return configuration state copy.
*/
State copy(final JerseyClient client) {
return new State(client, this);
}
void markAsShared() {
strategy = COPY_ON_CHANGE;
}
State preInitialize() {
final State state = strategy.onChange(this);
state.strategy = COPY_ON_CHANGE;
state.runtime.get().preInitialize();
return state;
}
@Override
public State property(final String name, final Object value) {
final State state = strategy.onChange(this);
state.commonConfig.property(name, value);
return state;
}
public State loadFrom(final Configuration config) {
final State state = strategy.onChange(this);
state.commonConfig.loadFrom(config);
return state;
}
@Override
public State register(final Class> providerClass) {
final State state = strategy.onChange(this);
state.commonConfig.register(providerClass);
return state;
}
@Override
public State register(final Object provider) {
final State state = strategy.onChange(this);
state.commonConfig.register(provider);
return state;
}
@Override
public State register(final Class> providerClass, final int bindingPriority) {
final State state = strategy.onChange(this);
state.commonConfig.register(providerClass, bindingPriority);
return state;
}
@Override
public State register(final Class> providerClass, final Class>... contracts) {
final State state = strategy.onChange(this);
state.commonConfig.register(providerClass, contracts);
return state;
}
@Override
public State register(final Class> providerClass, final Map, Integer> contracts) {
final State state = strategy.onChange(this);
state.commonConfig.register(providerClass, contracts);
return state;
}
@Override
public State register(final Object provider, final int bindingPriority) {
final State state = strategy.onChange(this);
state.commonConfig.register(provider, bindingPriority);
return state;
}
@Override
public State register(final Object provider, final Class>... contracts) {
final State state = strategy.onChange(this);
state.commonConfig.register(provider, contracts);
return state;
}
@Override
public State register(final Object provider, final Map, Integer> contracts) {
final State state = strategy.onChange(this);
state.commonConfig.register(provider, contracts);
return state;
}
State connectorProvider(final ConnectorProvider provider) {
if (provider == null) {
throw new NullPointerException(LocalizationMessages.NULL_CONNECTOR_PROVIDER());
}
final State state = strategy.onChange(this);
state.connectorProvider = provider;
return state;
}
State executorService(final ExecutorService executorService) {
if (executorService == null) {
throw new NullPointerException(LocalizationMessages.NULL_EXECUTOR_SERVICE());
}
final State state = strategy.onChange(this);
state.executorService = executorService;
return state;
}
State scheduledExecutorService(final ScheduledExecutorService scheduledExecutorService) {
if (scheduledExecutorService == null) {
throw new NullPointerException(LocalizationMessages.NULL_SCHEDULED_EXECUTOR_SERVICE());
}
final State state = strategy.onChange(this);
state.scheduledExecutorService = scheduledExecutorService;
return state;
}
Connector getConnector() {
// Get the connector only if the runtime has been initialized.
return (runtime.isInitialized()) ? runtime.get().getConnector() : null;
}
ConnectorProvider getConnectorProvider() {
return connectorProvider;
}
ExecutorService getExecutorService() {
return executorService;
}
ScheduledExecutorService getScheduledExecutorService() {
return scheduledExecutorService;
}
JerseyClient getClient() {
return client;
}
@Override
public State getConfiguration() {
return this;
}
@Override
public RuntimeType getRuntimeType() {
return commonConfig.getConfiguration().getRuntimeType();
}
@Override
public Map getProperties() {
return commonConfig.getConfiguration().getProperties();
}
@Override
public Object getProperty(final String name) {
return commonConfig.getConfiguration().getProperty(name);
}
@Override
public Collection getPropertyNames() {
return commonConfig.getConfiguration().getPropertyNames();
}
@Override
public boolean isProperty(final String name) {
return commonConfig.getConfiguration().isProperty(name);
}
@Override
public boolean isEnabled(final Feature feature) {
return commonConfig.getConfiguration().isEnabled(feature);
}
@Override
public boolean isEnabled(final Class extends Feature> featureClass) {
return commonConfig.getConfiguration().isEnabled(featureClass);
}
@Override
public boolean isRegistered(final Object component) {
return commonConfig.getConfiguration().isRegistered(component);
}
@Override
public boolean isRegistered(final Class> componentClass) {
return commonConfig.getConfiguration().isRegistered(componentClass);
}
@Override
public Map, Integer> getContracts(final Class> componentClass) {
return commonConfig.getConfiguration().getContracts(componentClass);
}
@Override
public Set> getClasses() {
return commonConfig.getConfiguration().getClasses();
}
@Override
public Set