cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r644075 - in /incubator/cxf/trunk/rt/frontend/jaxrs: ./ src/main/java/org/apache/cxf/jaxrs/ src/main/java/org/apache/cxf/jaxrs/model/ src/test/java/org/apache/cxf/jaxrs/ src/test/java/org/apache/cxf/jaxrs/model/
Date Wed, 02 Apr 2008 21:22:30 GMT
Author: dkulp
Date: Wed Apr  2 14:22:29 2008
New Revision: 644075

URL: http://svn.apache.org/viewvc?rev=644075&view=rev
Log:
[CXF-1498] Patch from Mickael Goujon applied.
Support injection of servlet things

Modified:
    incubator/cxf/trunk/rt/frontend/jaxrs/pom.xml
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ClassResourceInfo.java
    incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSUtilsTest.java
    incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/ClassResourceInfoTest.java

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/pom.xml
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/pom.xml?rev=644075&r1=644074&r2=644075&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/pom.xml (original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/pom.xml Wed Apr  2 14:22:29 2008
@@ -68,6 +68,11 @@
             <version>0.6</version>
         </dependency>
         <dependency>
+            <groupId>org.apache.cxf</groupId>
+            <artifactId>cxf-rt-transports-http</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
             <groupId>jaxen</groupId>
             <artifactId>jaxen</artifactId>
         </dependency>

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java?rev=644075&r1=644074&r2=644075&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
(original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
Wed Apr  2 14:22:29 2008
@@ -62,6 +62,9 @@
             JAXRSUtils.injectHttpContextValues(resourceObject, 
                                                ori, 
                                                exchange.getInMessage());
+            JAXRSUtils.injectServletResourceValues(resourceObject, 
+                                               ori, 
+                                               exchange.getInMessage());
         }
 
         List<Object> params = null;

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java?rev=644075&r1=644074&r2=644075&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java
(original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java
Wed Apr  2 14:22:29 2008
@@ -40,6 +40,9 @@
 import java.util.SortedMap;
 import java.util.TreeMap;
 
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import javax.ws.rs.ConsumeMime;
 import javax.ws.rs.HeaderParam;
 import javax.ws.rs.HttpMethod;
@@ -70,6 +73,7 @@
 import org.apache.cxf.jaxrs.provider.SecurityContextImpl;
 import org.apache.cxf.jaxrs.provider.UriInfoImpl;
 import org.apache.cxf.message.Message;
+import org.apache.cxf.transport.http.AbstractHTTPDestination;
 
 public final class JAXRSUtils {
 
@@ -390,6 +394,21 @@
         return null;
     }
 
+    public static Object createServletResourceValue(Message m, Class<?> clazz) {
+                
+        if (HttpServletRequest.class.isAssignableFrom(clazz)) {
+            return (HttpServletRequest) m.get(AbstractHTTPDestination.HTTP_REQUEST);
+        }
+        if (HttpServletResponse.class.isAssignableFrom(clazz)) {
+            return (HttpServletResponse) m.get(AbstractHTTPDestination.HTTP_RESPONSE);
+        }
+        if (ServletContext.class.isAssignableFrom(clazz)) {
+            return (ServletContext) m.get(AbstractHTTPDestination.HTTP_CONTEXT);
+        }
+        
+        return null;
+    }
+
     private static Object readFromUriParam(PathParam uriParamAnnotation,
                                            Class<?> parameter,
                                            Type parameterType,
@@ -594,6 +613,21 @@
         }
     }
     
+    public static void injectServletResourceValues(Object o,
+                                               OperationResourceInfo ori,
+                                               Message m) {
+        
+        for (Field f : ori.getClassResourceInfo().getResources()) {
+            Object value = createServletResourceValue(m, f.getType());
+            f.setAccessible(true);
+            try {
+                f.set(o, value);
+            } catch (IllegalAccessException ex) {
+                // ignore
+            }
+        }
+    }
+
     private static <K, V> MultivaluedMap<K, V> cloneMap(MultivaluedMap<K,
V> map1) {
         
         MultivaluedMap<K, V> map2 = new MetadataMap<K, V>();

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ClassResourceInfo.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ClassResourceInfo.java?rev=644075&r1=644074&r2=644075&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ClassResourceInfo.java
(original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ClassResourceInfo.java
Wed Apr  2 14:22:29 2008
@@ -24,6 +24,7 @@
 import java.util.Collections;
 import java.util.List;
 
+import javax.annotation.Resource;
 import javax.ws.rs.core.Context;
 
 import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
@@ -37,6 +38,7 @@
     private ResourceProvider resourceProvider;
     private List<ClassResourceInfo> subClassResourceInfo = new ArrayList<ClassResourceInfo>();
     private List<Field> httpContexts;
+    private List<Field> resources;
 
     public ClassResourceInfo(Class<?> theResourceClass) {
         this(theResourceClass, false);
@@ -46,6 +48,7 @@
         resourceClass = theResourceClass;
         root = theRoot;
         initHttpContexts();
+        initResources();
     }
 
     public boolean isRoot() {
@@ -106,11 +109,41 @@
             }
         }
     }
+
+    private void initResources() {
+        if (resourceClass == null || !root) {
+            return;
+        }
+        resources = new ArrayList<Field>();
+        Field[] fields = resourceClass.getDeclaredFields();
+        
+        for (Field f : fields) {
+            Resource resource = f.getAnnotation(Resource.class);
+            if (resource != null) {
+                resources.add(f);               
+            }
+        }
+    }
+
     
     
-    @SuppressWarnings("unchecked")
     public List<Field> getHttpContexts() {
-        return httpContexts == null ? Collections.EMPTY_LIST 
-               : Collections.unmodifiableList(httpContexts);
+        List<Field> ret;
+        if (httpContexts != null) {
+            ret = Collections.unmodifiableList(httpContexts);
+        } else {
+            ret = Collections.emptyList();
+        }
+        return ret;
+    }
+    
+    public List<Field> getResources() {
+        List<Field> ret;
+        if (resources != null) {
+            ret = Collections.unmodifiableList(resources);
+        } else {
+            ret = Collections.emptyList();
+        }
+        return ret;
     }                               
 }

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSUtilsTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSUtilsTest.java?rev=644075&r1=644074&r2=644075&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSUtilsTest.java
(original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSUtilsTest.java
Wed Apr  2 14:22:29 2008
@@ -24,6 +24,10 @@
 import java.util.HashMap;
 import java.util.List;
 
+import javax.annotation.Resource;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import javax.ws.rs.ConsumeMime;
 import javax.ws.rs.HeaderParam;
 import javax.ws.rs.MatrixParam;
@@ -47,11 +51,16 @@
 import org.apache.cxf.jaxrs.provider.UriInfoImpl;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.message.MessageImpl;
+import org.apache.cxf.transport.http.AbstractHTTPDestination;
+
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
 
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.replay;
+
 public class JAXRSUtilsTest extends Assert {
     
     public class Customer {
@@ -60,6 +69,9 @@
         @Context private HttpHeaders headers;
         @Context private Request request;
         @Context private SecurityContext sContext;
+        @Resource private HttpServletRequest servletRequest;
+        @Resource private HttpServletResponse servletResponse;
+        @Resource private ServletContext servletContext;
         
         public UriInfo getUriInfo() {
             return uriInfo;
@@ -77,6 +89,18 @@
             return sContext;
         }
         
+        public HttpServletRequest getServletRequest() {
+            return servletRequest;
+        }
+        
+        public HttpServletResponse getServletResponse() {
+            return servletResponse;
+        }
+        
+        public ServletContext getServletContext() {
+            return servletContext;
+        }
+
         @ProduceMime("text/xml")
         @ConsumeMime("text/xml")
         public void test() {
@@ -582,6 +606,34 @@
         assertSame(HttpHeadersImpl.class, c.getHeaders().getClass());
         assertSame(RequestImpl.class, c.getRequest().getClass());
         assertSame(SecurityContextImpl.class, c.getSecurityContext().getClass());
+        
+    }
+
+    @Test
+    public void testServletResourceFields() throws Exception {
+        
+        ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
+        OperationResourceInfo ori = new OperationResourceInfo(null, cri);
+        
+        Customer c = new Customer();
+        
+        // Creating mocks for the servlet request, response and context
+        HttpServletRequest request = createMock(HttpServletRequest.class);
+        HttpServletResponse response = createMock(HttpServletResponse.class);
+        ServletContext context = createMock(ServletContext.class);
+        replay(request);
+        replay(response);
+        replay(context);
+        
+        Message m = new MessageImpl();
+        m.put(AbstractHTTPDestination.HTTP_REQUEST, request);
+        m.put(AbstractHTTPDestination.HTTP_RESPONSE, response);
+        m.put(AbstractHTTPDestination.HTTP_CONTEXT, context);
+        
+        JAXRSUtils.injectServletResourceValues(c, ori, m);
+        assertSame(request.getClass(), c.getServletRequest().getClass());
+        assertSame(response.getClass(), c.getServletResponse().getClass());
+        assertSame(context.getClass(), c.getServletContext().getClass());
         
     }
 }

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/ClassResourceInfoTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/ClassResourceInfoTest.java?rev=644075&r1=644074&r2=644075&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/ClassResourceInfoTest.java
(original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/ClassResourceInfoTest.java
Wed Apr  2 14:22:29 2008
@@ -20,8 +20,14 @@
 package org.apache.cxf.jaxrs.model;
 
 import java.lang.reflect.Field;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
+import javax.annotation.Resource;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.UriInfo;
@@ -34,6 +40,9 @@
     private static class TestClass {
         @Context UriInfo u;
         @Context HttpHeaders h;
+        @Resource HttpServletRequest req;
+        @Resource HttpServletResponse res;
+        @Resource ServletContext c;
         int i;
     }
     
@@ -51,6 +60,24 @@
                    || fields.get(1).getType() == UriInfo.class)
                    && (fields.get(0).getType() == HttpHeaders.class
                    || fields.get(1).getType() == HttpHeaders.class));
+    }
+
+    @Test
+    public void testGetResources() {
+        ClassResourceInfo c = new ClassResourceInfo(TestClass.class);
+        List<Field> fields = c.getResources();
+        assertEquals("Only root classes should check these fields", 0, fields.size());
+        c = new ClassResourceInfo(TestClass.class, true);
+        fields = c.getResources();
         
+        Set<Class<?>> clses = new HashSet<Class<?>>(); 
+        for (Field f : fields) {
+            clses.add(f.getType());
+        }
+        assertEquals("3 resources fields available", 3, fields.size());
+        assertTrue("Wrong fields selected",
+                   clses.contains(HttpServletRequest.class)
+                   && clses.contains(HttpServletResponse.class)
+                   && clses.contains(ServletContext.class)); 
     }
 }



Mime
View raw message