All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.resteasy.client.jaxrs.internal.ClientConfiguration Maven / Gradle / Ivy

There is a newer version: 7.0.0.Alpha4
Show newest version
package org.jboss.resteasy.client.jaxrs.internal;

import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

import jakarta.ws.rs.RuntimeType;
import jakarta.ws.rs.client.ClientRequestFilter;
import jakarta.ws.rs.client.ClientResponseFilter;
import jakarta.ws.rs.client.RxInvoker;
import jakarta.ws.rs.client.RxInvokerProvider;
import jakarta.ws.rs.container.DynamicFeature;
import jakarta.ws.rs.core.Configurable;
import jakarta.ws.rs.core.Configuration;
import jakarta.ws.rs.core.Feature;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.ext.ContextResolver;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.MessageBodyReader;
import jakarta.ws.rs.ext.MessageBodyWriter;
import jakarta.ws.rs.ext.ParamConverter;
import jakarta.ws.rs.ext.Providers;
import jakarta.ws.rs.ext.ReaderInterceptor;
import jakarta.ws.rs.ext.RuntimeDelegate.HeaderDelegate;
import jakarta.ws.rs.ext.WriterInterceptor;

import org.jboss.resteasy.core.ThreadLocalResteasyProviderFactory;
import org.jboss.resteasy.resteasy_jaxrs.i18n.LogMessages;
import org.jboss.resteasy.spi.HeaderValueProcessor;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.jboss.resteasy.spi.interception.JaxrsInterceptorRegistry;

/**
 * @author Bill Burke
 * @version $Revision: 1 $
 */
public class ClientConfiguration implements Configuration, Configurable, Providers, HeaderValueProcessor {
    protected ResteasyProviderFactory providerFactory;

    public ClientConfiguration(final ResteasyProviderFactory factory) {
        if (factory instanceof ThreadLocalResteasyProviderFactory) {
            this.providerFactory = new LocalResteasyProviderFactory(
                    ((ThreadLocalResteasyProviderFactory) factory).getDelegate());
        } else {
            this.providerFactory = new LocalResteasyProviderFactory(factory);
        }
    }

    public ClientConfiguration(final ClientConfiguration parent) {
        this(parent.getProviderFactory());
        setProperties(parent.getProperties());
    }

    public void setProperties(Map newProps) {
        if (newProps != null && !newProps.isEmpty()) {
            providerFactory.setProperties(newProps);
        }
    }

    protected ResteasyProviderFactory getProviderFactory() {
        return providerFactory;
    }

    public Map getMutableProperties() {
        return providerFactory.getMutableProperties();
    }

    /**
     * Convert an object to a header string. First try StringConverter, then HeaderDelegate, then object.toString()
     *
     * @param object header object
     * @return header string
     */
    public String toHeaderString(Object object) {
        if (object instanceof String)
            return (String) object;
        return providerFactory.toHeaderString(object);
    }

    public  MessageBodyWriter getMessageBodyWriter(Class type, Type genericType, Annotation[] annotations,
            MediaType mediaType) {
        MessageBodyWriter writer = providerFactory.getClientMessageBodyWriter(type, genericType, annotations, mediaType);
        if (writer != null)
            LogMessages.LOGGER.debugf("MessageBodyWriter: %s", writer.getClass().getName());
        return writer;
    }

    public  MessageBodyReader getMessageBodyReader(Class type, Type genericType, Annotation[] annotations,
            MediaType mediaType) {
        MessageBodyReader reader = providerFactory.getClientMessageBodyReader(type, genericType, annotations, mediaType);
        if (reader != null)
            LogMessages.LOGGER.debugf("MessageBodyReader: %s", reader.getClass().getName());
        return reader;
    }

    public WriterInterceptor[] getWriterInterceptors(Class declaring, AccessibleObject target) {
        JaxrsInterceptorRegistry writerInterceptors = providerFactory.getClientWriterInterceptorRegistry();
        return writerInterceptors == null ? null : writerInterceptors.postMatch(declaring, target);
    }

    public ReaderInterceptor[] getReaderInterceptors(Class declaring, AccessibleObject target) {
        JaxrsInterceptorRegistry readerInterceptors = providerFactory.getClientReaderInterceptorRegistry();
        return readerInterceptors == null ? null : readerInterceptors.postMatch(declaring, target);
    }

    public ClientRequestFilter[] getRequestFilters(Class declaring, AccessibleObject target) {
        JaxrsInterceptorRegistry requestFilters = providerFactory.getClientRequestFilterRegistry();
        return requestFilters == null ? null : requestFilters.postMatch(declaring, target);
    }

    public ClientResponseFilter[] getResponseFilters(Class declaring, AccessibleObject target) {
        JaxrsInterceptorRegistry filters = providerFactory.getClientResponseFilters();
        return filters == null ? null : filters.postMatch(declaring, target);
    }

    public Set getDynamicFeatures() {
        return providerFactory.getClientDynamicFeatures();
    }

    public ParamConverter getParamConverter(Class clazz, Type genericType, Annotation[] annotations) {
        return providerFactory.getParamConverter(clazz, genericType, annotations);
    }

    public String toString(Object object) {
        return providerFactory.toString(object, object.getClass(), null, null);
    }

    public String toString(Object object, Type type, Annotation[] annotations) {
        return providerFactory.toString(object, object.getClass(), type, annotations);
    }

    // interface implementation

    // Providers

    @Override
    public  ExceptionMapper getExceptionMapper(Class type) {
        return providerFactory.getExceptionMapper(type);
    }

    @Override
    public  ContextResolver getContextResolver(Class contextType, MediaType mediaType) {
        return providerFactory.getContextResolver(contextType, mediaType);
    }

    // Configuration

    @Override
    public Map getProperties() {
        return providerFactory.getProperties();
    }

    @Override
    public Object getProperty(String name) {
        return providerFactory.getProperty(name);
    }

    @Override
    public Set> getClasses() {
        return providerFactory.getClasses();
    }

    @Override
    public Set getInstances() {
        return providerFactory.getInstances();
    }

    @Override
    public ClientConfiguration register(Class providerClass) {
        providerFactory.register(providerClass);
        return this;
    }

    @Override
    public ClientConfiguration register(Object provider) {
        providerFactory.register(provider);
        return this;
    }

    @Override
    public ClientConfiguration register(Class providerClass, int priority) {
        providerFactory.register(providerClass, priority);
        return this;
    }

    @Override
    public ClientConfiguration register(Object provider, int Priority) {
        providerFactory.register(provider, Priority);
        return this;
    }

    @Override
    public ClientConfiguration property(String name, Object value) {
        providerFactory.property(name, value);
        return this;
    }

    @Override
    public Configuration getConfiguration() {
        return this;
    }

    @Override
    public ClientConfiguration register(Class componentClass, Class... contracts) {
        providerFactory.register(componentClass, contracts);
        return this;
    }

    @Override
    public ClientConfiguration register(Class componentClass, Map, Integer> contracts) {
        providerFactory.register(componentClass, contracts);
        return this;
    }

    @Override
    public ClientConfiguration register(Object component, Class... contracts) {
        providerFactory.register(component, contracts);
        return this;
    }

    @Override
    public ClientConfiguration register(Object component, Map, Integer> contracts) {
        providerFactory.register(component, contracts);
        return this;
    }

    @Override
    public RuntimeType getRuntimeType() {
        return RuntimeType.CLIENT;
    }

    @Override
    public Collection getPropertyNames() {
        return providerFactory.getProperties().keySet();
    }

    @Override
    public boolean isEnabled(Feature feature) {
        return providerFactory.isEnabled(feature);
    }

    @Override
    public boolean isEnabled(Class featureClass) {
        return providerFactory.isEnabled(featureClass);
    }

    @Override
    public boolean isRegistered(Object component) {
        return providerFactory.isRegistered(component);
    }

    @Override
    public boolean isRegistered(Class componentClass) {
        return providerFactory.isRegistered(componentClass);
    }

    @Override
    public Map, Integer> getContracts(Class componentClass) {
        return providerFactory.getContracts(componentClass);
    }

    public > RxInvokerProvider getRxInvokerProvider(Class clazz) {
        return providerFactory.getRxInvokerProvider(clazz);
    }

    public RxInvokerProvider getRxInvokerProviderFromReactiveClass(Class clazz) {
        return providerFactory.getRxInvokerProviderFromReactiveClass(clazz);
    }

    public boolean isReactive(Class clazz) {
        return providerFactory.isReactive(clazz);
    }

    public void addHeaderDelegate(Class clazz, HeaderDelegate header) {
        providerFactory.addHeaderDelegate(clazz, header);
    }
}