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

org.jboss.resteasy.client.ProxyBuilder Maven / Gradle / Ivy

There is a newer version: 4.0.0.Beta5
Show newest version
package org.jboss.resteasy.client;

import org.jboss.resteasy.client.core.ClientProxy;
import org.jboss.resteasy.client.core.MethodInvoker;
import org.jboss.resteasy.client.core.SubResourceInvoker;
import org.jboss.resteasy.client.core.extractors.DefaultEntityExtractorFactory;
import org.jboss.resteasy.client.core.extractors.EntityExtractorFactory;
import org.jboss.resteasy.client.core.marshallers.ResteasyClientProxy;
import org.jboss.resteasy.spi.ProviderFactoryDelegate;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.jboss.resteasy.util.IsHttpMethod;

import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 *
 * @param 
 * @deprecated The Resteasy proxy facility in the client framework in resteasy-jaxrs is replaced by the
 * proxy facility extension in the JAX-RS 2.0 compliant resteasy-client module.
 *
 * @see org.jboss.resteasy.client.jaxrs.ResteasyClient
 * @see org.jboss.resteasy.client.jaxrs.ProxyBuilder
 * @see org.jboss.resteasy.client.jaxrs.ResteasyWebTarget
 */
@Deprecated
public class ProxyBuilder
{
   public static  ProxyBuilder build(Class iface, URI base)
   {
      return new ProxyBuilder(iface, base);
   }

   public static  ProxyBuilder build(Class iface, String base)
   {
      return new ProxyBuilder(iface, ProxyFactory.createUri(base));
   }

   private final Class iface;
   private final URI baseUri;
   private ClassLoader loader;
   private ClientExecutor executor;
   private ResteasyProviderFactory providerFactory;
   private EntityExtractorFactory extractorFactory;
   private Map requestAttributes;
   private MediaType serverConsumes;
   private MediaType serverProduces;

   private ProxyBuilder(final Class iface, final URI base)
   {
      this.iface = iface;
      this.baseUri = base;
      this.loader = iface.getClassLoader();
   }

   public ProxyBuilder classloader(ClassLoader cl)
   {
      this.loader = cl;
      return this;
   }

   public ProxyBuilder executor(ClientExecutor exec)
   {
      this.executor = exec;
      return this;
   }

   public ProxyBuilder providerFactory(ResteasyProviderFactory fact)
   {
      this.providerFactory = fact;
      return this;
   }

   public ProxyBuilder extractorFactory(EntityExtractorFactory fact)
   {
      this.extractorFactory = fact;
      return this;
   }

   /** shortcut for serverProduces(type).serverConsumes(type) */
   public ProxyBuilder serverMediaType(MediaType type)
   {
      this.serverProduces = type;
      this.serverConsumes = type;
      return this;
   }

   public ProxyBuilder serverProduces(MediaType type)
   {
      this.serverProduces = type;
      return this;
   }

   public ProxyBuilder serverConsumes(MediaType type)
   {
      this.serverConsumes = type;
      return this;
   }

   public ProxyBuilder requestAttributes(Map attrs)
   {
      this.requestAttributes = attrs;
      return this;
   }

   private static final Class[] cClassArgArray =
   {
      Class.class
   };

   public T now()
   {
      if (providerFactory instanceof ProviderFactoryDelegate)
         providerFactory = ((ProviderFactoryDelegate) providerFactory).getDelegate();

      if (executor == null)
         executor = ClientRequest.getDefaultExecutor();
      if (providerFactory == null)
         providerFactory = ResteasyProviderFactory.getInstance();
      if (extractorFactory == null)
         extractorFactory = new DefaultEntityExtractorFactory();
      if (requestAttributes == null)
         requestAttributes = Collections.emptyMap();

      final ProxyConfig config = new ProxyConfig(loader, executor, providerFactory, extractorFactory, requestAttributes, serverConsumes, serverProduces);
      return createProxy(iface, baseUri, config);
   }

   @SuppressWarnings("unchecked")
   public static  T createProxy(final Class iface, URI baseUri, final ProxyConfig config)
   {
      HashMap methodMap = new HashMap();
      for (Method method : iface.getMethods())
      {
         // ignore the as method to allow declaration in client interfaces
         if (!("as".equals(method.getName()) && Arrays.equals(method.getParameterTypes(), cClassArgArray)))
         {
            MethodInvoker invoker;
            Set httpMethods = IsHttpMethod.getHttpMethods(method);
            if ((httpMethods == null || httpMethods.size() == 0) && method.isAnnotationPresent(Path.class) && method.getReturnType().isInterface())
            {
               invoker = new SubResourceInvoker(baseUri, method, config);
            }
            else
            {
               invoker = ProxyFactory.createClientInvoker(iface, method, baseUri, config);
            }
            methodMap.put(method, invoker);
         }
      }

      Class[] intfs =
      {
         iface, ResteasyClientProxy.class
      };

      ClientProxy clientProxy = new ClientProxy(methodMap, baseUri, config);
      // this is done so that equals and hashCode work ok. Adding the proxy to a
      // Collection will cause equals and hashCode to be invoked. The Spring
      // infrastructure had some problems without this.
      clientProxy.setClazz(iface);

      return (T) Proxy.newProxyInstance(config.getLoader(), intfs, clientProxy);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy