Skip to content

Commit

Permalink
[[RESTEASY-1668]:Use ResteasyProviderFactory.InjectedInstance(); Add @…
Browse files Browse the repository at this point in the history
…context injection test
  • Loading branch information
jimma authored and asoldano committed Jul 26, 2017
1 parent 72dca30 commit 6cb34e4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
Expand Up @@ -3,49 +3,35 @@
import org.jboss.resteasy.resteasy_jaxrs.i18n.Messages; import org.jboss.resteasy.resteasy_jaxrs.i18n.Messages;
import org.jboss.resteasy.specimpl.BuiltResponse; import org.jboss.resteasy.specimpl.BuiltResponse;
import org.jboss.resteasy.spi.ApplicationException; import org.jboss.resteasy.spi.ApplicationException;
import org.jboss.resteasy.spi.ConstructorInjector;
import org.jboss.resteasy.spi.Failure;
import org.jboss.resteasy.spi.HttpRequest; import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.HttpResponse; import org.jboss.resteasy.spi.HttpResponse;
import org.jboss.resteasy.spi.InjectorFactory; import org.jboss.resteasy.spi.InjectorFactory;
import org.jboss.resteasy.spi.InternalServerErrorException; import org.jboss.resteasy.spi.InternalServerErrorException;
import org.jboss.resteasy.spi.MethodInjector; import org.jboss.resteasy.spi.MethodInjector;
import org.jboss.resteasy.spi.Registry;
import org.jboss.resteasy.spi.ResourceFactory; import org.jboss.resteasy.spi.ResourceFactory;
import org.jboss.resteasy.spi.ResteasyProviderFactory; import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.jboss.resteasy.spi.ResteasyUriInfo; import org.jboss.resteasy.spi.ResteasyUriInfo;
import org.jboss.resteasy.spi.metadata.ResourceBuilder;
import org.jboss.resteasy.spi.metadata.ResourceClass;
import org.jboss.resteasy.spi.metadata.ResourceLocator; import org.jboss.resteasy.spi.metadata.ResourceLocator;
import org.jboss.resteasy.util.FindAnnotation;
import org.jboss.resteasy.util.GetRestful; import org.jboss.resteasy.util.GetRestful;
import org.jboss.resteasy.util.Types;


import javax.ws.rs.NotFoundException; import javax.ws.rs.NotFoundException;


import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Proxy;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;


/** /**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a> * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $ * @version $Revision: 1 $
*/ */
@SuppressWarnings("unchecked")
public class ResourceLocatorInvoker implements ResourceInvoker public class ResourceLocatorInvoker implements ResourceInvoker
{ {
protected InjectorFactory injector; protected InjectorFactory injector;
protected MethodInjector methodInjector; protected MethodInjector methodInjector;
protected ResourceFactory resource; protected ResourceFactory resource;
protected ResteasyProviderFactory providerFactory; protected ResteasyProviderFactory providerFactory;
protected ResourceLocator method; protected ResourceLocator method;
protected ConcurrentHashMap<Class, LocatorRegistry> cachedSubresources = new ConcurrentHashMap<Class, LocatorRegistry>(); protected ConcurrentHashMap<Class<?>, LocatorRegistry> cachedSubresources = new ConcurrentHashMap<Class<?>, LocatorRegistry>();


public ResourceLocatorInvoker(ResourceFactory resource, InjectorFactory injector, ResteasyProviderFactory providerFactory, ResourceLocator locator) public ResourceLocatorInvoker(ResourceFactory resource, InjectorFactory injector, ResteasyProviderFactory providerFactory, ResourceLocator locator)
{ {
Expand Down Expand Up @@ -83,9 +69,7 @@ protected Object createResource(HttpRequest request, HttpResponse response, Obje
Object subResource = method.getMethod().invoke(locator, args); Object subResource = method.getMethod().invoke(locator, args);
if (subResource instanceof Class) if (subResource instanceof Class)
{ {
Constructor<?> constructor = ((Class<?>) subResource).getConstructor(new Class[] {}); subResource = this.providerFactory.injectedInstance((Class<?>)subResource);
ConstructorInjector constructInjector = injector.createConstructor(constructor, this.providerFactory);
subResource = constructInjector.construct();
} }
return subResource; return subResource;


Expand All @@ -95,12 +79,6 @@ protected Object createResource(HttpRequest request, HttpResponse response, Obje
throw new InternalServerErrorException(e); throw new InternalServerErrorException(e);
} }
catch (InvocationTargetException e) catch (InvocationTargetException e)
{
throw new ApplicationException(e.getCause());
} catch (InstantiationException e) {
throw new ApplicationException(e.getCause());
}
catch (NoSuchMethodException e)
{ {
throw new ApplicationException(e.getCause()); throw new ApplicationException(e.getCause());
} }
Expand Down
Expand Up @@ -6,6 +6,11 @@
import javax.inject.Inject; import javax.inject.Inject;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.UriInfo;

import org.junit.Assert;
@ApplicationScoped @ApplicationScoped
public class ParameterSubResClassSub public class ParameterSubResClassSub
{ {
Expand All @@ -16,10 +21,14 @@ public class ParameterSubResClassSub
@Inject @Inject
RequestScopedObject requestScope; RequestScopedObject requestScope;


@Context UriInfo uriInfo;

@GET @GET
@Produces("text/plain") @Produces("text/plain")
public String get() public String get(@Context HttpHeaders headers)
{ {
Assert.assertEquals("Wrong path value from injected UriInfo", "/path/subclass", uriInfo.getPath());
Assert.assertNotNull("Connection header from injected HttpHeaders is null", headers.getHeaderString("Connection"));
return "resourceCounter:" + resourceCounter.incrementAndGet() + ",appscope:" + appScope.getCount() + ",requestScope:" + requestScope.getCount(); return "resourceCounter:" + resourceCounter.incrementAndGet() + ",appscope:" + appScope.getCount() + ",requestScope:" + requestScope.getCount();
} }
} }

0 comments on commit 6cb34e4

Please sign in to comment.