wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r903921 - in /incubator/wink/trunk: wink-common/src/main/java/org/apache/wink/common/internal/registry/ContextAccessor.java wink-server/src/test/java/org/apache/wink/server/internal/jaxrs/UriInfoImplTest.java
Date Thu, 28 Jan 2010 00:40:54 GMT
Author: bluk
Date: Thu Jan 28 00:40:54 2010
New Revision: 903921

URL: http://svn.apache.org/viewvc?rev=903921&view=rev
Log:
Inject proxies for context objects if object not available

Thanks to bharath chinnadurai for reporting the
issue.

See [WINK-243]

Modified:
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ContextAccessor.java
    incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/jaxrs/UriInfoImplTest.java

Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ContextAccessor.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ContextAccessor.java?rev=903921&r1=903920&r2=903921&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ContextAccessor.java
(original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ContextAccessor.java
Thu Jan 28 00:40:54 2010
@@ -36,7 +36,6 @@
 
 public class ContextAccessor {
 
-
     public <T> T getContext(Class<T> contextClass, RuntimeContext runtimeContext)
{
 
         // try to get a context from a ContextResolver
@@ -73,7 +72,16 @@
         // return context directly
         if (runtimeContext != null) {
             try {
-                return runtimeContext.getAttribute(contextClass);
+                T instance = runtimeContext.getAttribute(contextClass);
+                if (instance != null) {
+                    return instance;
+                }
+                /*
+                 * if the instance isn't directly available from the
+                 * runtimecontext at this time, inject a proxy instead. This
+                 * allows context attributes to be added after a class is
+                 * instantiated
+                 */
             } catch (Exception e) {
                 if (e instanceof InvocationTargetException) {
                     Throwable ite = ((InvocationTargetException)e).getTargetException();

Modified: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/jaxrs/UriInfoImplTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/jaxrs/UriInfoImplTest.java?rev=903921&r1=903920&r2=903921&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/jaxrs/UriInfoImplTest.java
(original)
+++ incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/jaxrs/UriInfoImplTest.java
Thu Jan 28 00:40:54 2010
@@ -29,16 +29,19 @@
 import javax.ws.rs.core.PathSegment;
 import javax.ws.rs.core.UriInfo;
 
+import org.apache.wink.common.internal.runtime.RuntimeContextTLS;
 import org.apache.wink.server.internal.servlet.MockServletInvocationTest;
 import org.apache.wink.test.mock.MockRequestConstructor;
 import org.junit.Test;
 import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
 
 public class UriInfoImplTest extends MockServletInvocationTest {
 
     @Override
     protected Class<?>[] getClasses() {
-        return new Class[] {FooResource.class, TestResource.class};
+        return new Class[] {FooResource.class, TestResource.class,
+            ResourceForCustomContextAttr.class};
     }
 
     @Path("/te st/{id}")
@@ -236,6 +239,43 @@
         }
     }
 
+    @Path("customcontext")
+    public static class ResourceForCustomContextAttr {
+        private @Context
+        CustomServerContextAttribute mycontextattribute;
+
+        @Path("injectaftersub")
+        public SubResForCustomContextAttr getHeadersAgain(@Context UriInfo uriInfo) {
+            return new SubResForCustomContextAttr();
+        }
+    }
+
+    public static interface CustomServerContextAttribute {
+        public String doSomething();
+    }
+
+    public static class SubResForCustomContextAttr {
+        public SubResForCustomContextAttr() {
+            /*
+             * for test simplicity, going to change the server context here
+             */
+            RuntimeContextTLS.getRuntimeContext().setAttribute(CustomServerContextAttribute.class,
+                                                               new CustomServerContextAttribute()
{
+
+                                                                   public String doSomething()
{
+                                                                       return "Hello world!";
+                                                                   }
+                                                               });
+        }
+
+        @GET
+        public String getHeadersAgain(@Context UriInfo uriInfo) {
+            ResourceForCustomContextAttr r =
+                (ResourceForCustomContextAttr)uriInfo.getMatchedResources().get(1);
+            return r.mycontextattribute.doSomething();
+        }
+    }
+
     @Test
     public void testUriInfoMatchedResourcesAndURIs() throws Exception {
 
@@ -288,4 +328,20 @@
         invoke(servletRequest);
     }
 
+    @Test
+    public void testCustomContextAttrInjectionAfterInstantiation() throws Exception {
+        /*
+         * Order of operations could be that 1) a root resource is instantiated,
+         * 2) fields are injected normally but there is something missing on the
+         * context, 3) later, the developer adds the missing context attribute
+         */
+        MockHttpServletRequest servletRequest =
+            MockRequestConstructor.constructMockRequest("GET",
+                                                        "customcontext/injectaftersub",
+                                                        "text/plain");
+        MockHttpServletResponse response = invoke(servletRequest);
+        assertEquals(200, response.getStatus());
+        assertEquals("Hello world!", response.getContentAsString());
+    }
+
 }



Mime
View raw message