You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2011/03/14 18:31:20 UTC

svn commit: r1081488 - in /cxf/trunk: common/common/src/main/java/org/apache/cxf/common/util/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/ systests/jaxrs/src/test/resources/jaxrs_proxy...

Author: sergeyb
Date: Mon Mar 14 17:31:19 2011
New Revision: 1081488

URL: http://svn.apache.org/viewvc?rev=1081488&view=rev
Log:
[CXF-3400] Better handling of request-scoped beans

Added:
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/RequestScopeResourceFactory.java   (with props)
Modified:
    cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/SpringAopClassHelper.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerProxySpringBookTest.java
    cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_proxy/WEB-INF/beans.xml
    cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_proxy/WEB-INF/web.xml

Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/SpringAopClassHelper.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/SpringAopClassHelper.java?rev=1081488&r1=1081487&r2=1081488&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/SpringAopClassHelper.java (original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/SpringAopClassHelper.java Mon Mar 14 17:31:19 2011
@@ -19,8 +19,10 @@
 
 package org.apache.cxf.common.util;
 
+import org.springframework.aop.TargetSource;
 import org.springframework.aop.framework.Advised;
 import org.springframework.aop.support.AopUtils;
+import org.springframework.beans.factory.BeanCreationException;
 
 /**
  * 
@@ -56,12 +58,22 @@ class SpringAopClassHelper extends Class
         if (AopUtils.isAopProxy(o)) {
             Advised advised = (Advised)o;
             try {
-                Object target = advised.getTargetSource().getTarget();
+                TargetSource targetSource = advised.getTargetSource();
+                
+                Object target = null;
+                
+                try {
+                    target = targetSource.getTarget();
+                } catch (BeanCreationException ex) {
+                    // some scopes such as 'request' may not 
+                    // be active on the current thread yet
+                    return getRealClassFromClassInternal(targetSource.getTargetClass());
+                }
                 
                 if (target == null) {
                     Class targetClass = AopUtils.getTargetClass(o);
                     if (targetClass != null) {
-                        return targetClass;
+                        return getRealClassFromClassInternal(targetClass);
                     }
                 } else {
                     return getRealClassInternal(target); 

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java?rev=1081488&r1=1081487&r2=1081488&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java Mon Mar 14 17:31:19 2011
@@ -120,13 +120,14 @@ public class JAXRSInvoker extends Abstra
             pushOntoStack(ori, ClassHelper.getRealClass(resourceObject), exchange.getInMessage());
                     
             if (cri.isRoot()) {
-                JAXRSUtils.handleSetters(ori, resourceObject,
+                Object realResourceObject = ClassHelper.getRealObject(resourceObject);
+                JAXRSUtils.handleSetters(ori, realResourceObject,
                                          exchange.getInMessage());
     
-                InjectionUtils.injectContextFields(resourceObject,
+                InjectionUtils.injectContextFields(realResourceObject,
                                                    ori.getClassResourceInfo(),
                                                    exchange.getInMessage());
-                InjectionUtils.injectResourceFields(resourceObject,
+                InjectionUtils.injectResourceFields(realResourceObject,
                                                 ori.getClassResourceInfo(),
                                                 exchange.getInMessage());
             }

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerProxySpringBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerProxySpringBookTest.java?rev=1081488&r1=1081487&r2=1081488&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerProxySpringBookTest.java (original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerProxySpringBookTest.java Mon Mar 14 17:31:19 2011
@@ -26,7 +26,9 @@ import java.net.URLConnection;
 
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.io.CachedOutputStream;
+import org.apache.cxf.jaxrs.client.WebClient;
 import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+
 import org.junit.BeforeClass;
 import org.junit.Test;
 
@@ -36,7 +38,7 @@ public class JAXRSClientServerProxySprin
     @BeforeClass
     public static void startServers() throws Exception {
         assertTrue("server did not launch correctly", 
-                   launchServer(BookServerProxySpring.class));
+                   launchServer(BookServerProxySpring.class, true));
     }
     
     @Test
@@ -139,6 +141,17 @@ public class JAXRSClientServerProxySprin
         assertEquals(getStringFromInputStream(expected), getStringFromInputStream(in)); 
     }
 
+    @Test
+    public void testGetBookWithRequestScope() {
+        // the BookStore method which will handle this request depends on the injected HttpHeaders
+        WebClient wc = WebClient.create("http://localhost:" + PORT + "/test/request/bookstore/booksecho2");
+        wc.type("text/plain").accept("text/plain");
+        wc.header("CustomHeader", "custom-header");
+        String value = wc.post("CXF", String.class);
+        assertEquals("CXF", value);
+        assertEquals("custom-header", wc.getResponse().getMetadata().getFirst("CustomHeader"));
+    }
+    
     private String getStringFromInputStream(InputStream in) throws Exception {        
         CachedOutputStream bos = new CachedOutputStream();
         IOUtils.copy(in, bos);

Added: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/RequestScopeResourceFactory.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/RequestScopeResourceFactory.java?rev=1081488&view=auto
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/RequestScopeResourceFactory.java (added)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/RequestScopeResourceFactory.java Mon Mar 14 17:31:19 2011
@@ -0,0 +1,32 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.systest.jaxrs;
+
+import org.apache.cxf.jaxrs.spring.SpringResourceFactory;
+
+/**
+ * ApplicationContext representing a request-scope bean exposes it as a Spring singleton.
+ * However, in JAX-RS terms, this bean actually follows a kind-of per-request life-cycle
+ */
+public class RequestScopeResourceFactory extends SpringResourceFactory {
+    @Override
+    public boolean isSingleton() {
+        return false;
+    }
+}

Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/RequestScopeResourceFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/RequestScopeResourceFactory.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_proxy/WEB-INF/beans.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_proxy/WEB-INF/beans.xml?rev=1081488&r1=1081487&r2=1081488&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_proxy/WEB-INF/beans.xml (original)
+++ cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_proxy/WEB-INF/beans.xml Mon Mar 14 17:31:19 2011
@@ -39,6 +39,8 @@ http://cxf.apache.org/schemas/jaxrs.xsd"
   <import resource="classpath:META-INF/cxf/cxf.xml" />
   <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
 
+  <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
+
   <bean id="bookstore" class="org.apache.cxf.systest.jaxrs.BookStore"/>
   
   <bean id="bookstoreInterface" class="org.apache.cxf.systest.jaxrs.BookStoreWithInterface"/>
@@ -94,6 +96,18 @@ http://cxf.apache.org/schemas/jaxrs.xsd"
   </jaxrs:server>
 
 
+  <jaxrs:server id="requestScopeEndpoint" address="/request">
+     <jaxrs:serviceFactories>
+		<bean class="org.apache.cxf.systest.jaxrs.RequestScopeResourceFactory">
+             <property name="beanId" value="requestScopeBean"/>
+        </bean>
+     </jaxrs:serviceFactories>
+  </jaxrs:server>
+
+  <bean id="requestScopeBean" class="org.apache.cxf.systest.jaxrs.BookStore" scope="request">
+      <aop:scoped-proxy />
+  </bean>
+
   <bean id="sfactory1" class="org.apache.cxf.jaxrs.spring.SpringResourceFactory">  
       <property name="beanId" value="bookstore"/>
   </bean>

Modified: cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_proxy/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_proxy/WEB-INF/web.xml?rev=1081488&r1=1081487&r2=1081488&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_proxy/WEB-INF/web.xml (original)
+++ cxf/trunk/systests/jaxrs/src/test/resources/jaxrs_proxy/WEB-INF/web.xml Mon Mar 14 17:31:19 2011
@@ -33,6 +33,11 @@
 		</listener-class>
 	</listener>
 
+    <!-- makes request-scoped beans work -->
+	<listener>
+		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
+	</listener>
+
 	<servlet>
 		<servlet-name>CXFServlet</servlet-name>
 		<display-name>CXF Servlet</display-name>