cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r744104 - in /cxf/trunk: common/common/src/main/java/org/apache/cxf/common/util/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/ systests/src/test/java/org/apache/cxf...
Date Fri, 13 Feb 2009 12:56:46 GMT
Author: sergeyb
Date: Fri Feb 13 12:56:45 2009
New Revision: 744104

URL: http://svn.apache.org/viewvc?rev=744104&view=rev
Log:
JAXRS : client cglib proxies, xmlsource(to be optimized)

Added:
    cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/CglibProxyHelper.java
  (with props)
    cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/ProxyHelper.java   (with
props)
Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactory.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/WebClientTest.java
    cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java

Added: cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/CglibProxyHelper.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/CglibProxyHelper.java?rev=744104&view=auto
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/CglibProxyHelper.java
(added)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/CglibProxyHelper.java
Fri Feb 13 12:56:45 2009
@@ -0,0 +1,77 @@
+/**
+ * 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.common.util;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+
+import net.sf.cglib.proxy.Enhancer;
+import net.sf.cglib.proxy.MethodInterceptor;
+import net.sf.cglib.proxy.MethodProxy;
+
+
+/**
+ * 
+ */
+class CglibProxyHelper extends ProxyHelper {
+    CglibProxyHelper() throws Exception {
+        Class.forName("net.sf.cglib.proxy.Proxy");
+        Class.forName("net.sf.cglib.proxy.MethodInterceptor");
+        Class.forName("net.sf.cglib.proxy.MethodProxy");
+    }
+    
+    @Override
+    protected Object getProxyInternal(ClassLoader loader, Class[] interfaces, 
+                                      final java.lang.reflect.InvocationHandler h) {
+        
+        Class superClass = null;
+        List<Class> theInterfaces = new ArrayList<Class>();
+        
+        for (Class c : interfaces) {
+            if (!c.isInterface()) {
+                if (superClass != null) {
+                    throw new IllegalArgumentException("Only a single supreclass is supported");
+                }
+                superClass = c; 
+            } else {
+                theInterfaces.add(c);
+            }
+        }
+        if (superClass != null) {
+            Enhancer enhancer = new Enhancer();
+            enhancer.setSuperclass(superClass);
+            enhancer.setInterfaces(theInterfaces.toArray(new Class[]{}));
+            enhancer.setCallback(new MethodInterceptor() {
+
+                public Object intercept(Object obj, Method method, Object[] args, MethodProxy
proxy) 
+                    throws Throwable {
+                    return h.invoke(obj, method, args);
+                }
+                
+            });
+            return enhancer.create();
+        } else {
+            return super.getProxyInternal(loader, interfaces, h);
+        }
+    }
+    
+    
+}

Propchange: cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/CglibProxyHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/CglibProxyHelper.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/ProxyHelper.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/ProxyHelper.java?rev=744104&view=auto
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/ProxyHelper.java (added)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/ProxyHelper.java Fri
Feb 13 12:56:45 2009
@@ -0,0 +1,52 @@
+/**
+ * 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.common.util;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Proxy;
+
+/**
+ * 
+ */
+public class ProxyHelper {
+    static final ProxyHelper HELPER;
+    static {
+        ProxyHelper theHelper = null;
+        try {
+            theHelper = new CglibProxyHelper();
+        } catch (Exception ex) {
+            theHelper = new ProxyHelper();
+        }
+        HELPER = theHelper;
+    }
+    
+    
+    protected ProxyHelper() {
+    }
+    
+    protected Object getProxyInternal(ClassLoader loader, Class[] interfaces, InvocationHandler
handler) {
+        return Proxy.newProxyInstance(loader, interfaces, handler);
+    }
+    
+    
+    public static Object getProxy(ClassLoader loader, Class[] interfaces, InvocationHandler
handler) {
+        return HELPER.getProxyInternal(loader, interfaces, handler);
+    }
+}

Propchange: cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/ProxyHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/ProxyHelper.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java?rev=744104&r1=744103&r2=744104&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
Fri Feb 13 12:56:45 2009
@@ -211,7 +211,11 @@
     
     protected void resetBaseAddress(URI uri) {
         baseURI = uri;
-        currentBuilder = new UriBuilderImpl(baseURI);
+        resetCurrentBuilder(uri);
+    }
+    
+    protected void resetCurrentBuilder(URI uri) {
+        currentBuilder = new UriBuilderImpl(uri);
     }
     
     protected ResponseBuilder setResponseBuilder(HttpURLConnection conn) throws IOException
{

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactory.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactory.java?rev=744104&r1=744103&r2=744104&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactory.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/JAXRSClientFactory.java
Fri Feb 13 12:56:45 2009
@@ -18,12 +18,12 @@
  */
 package org.apache.cxf.jaxrs.client;
 
-import java.lang.reflect.Proxy;
 import java.net.URI;
 
 import javax.ws.rs.Path;
 import javax.ws.rs.core.MediaType;
 
+import org.apache.cxf.common.util.ProxyHelper;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
 import org.apache.cxf.jaxrs.utils.AnnotationUtils;
 import org.apache.cxf.jaxrs.utils.ResourceUtils;
@@ -72,7 +72,7 @@
     static <T> T create(URI baseURI, URI currentURI, Class<T> cls, boolean root,
boolean inheritHeaders) {
         ClassResourceInfo classResourceInfo = ResourceUtils.createClassResourceInfo(cls,
cls, root, true);
         
-        return cls.cast(Proxy.newProxyInstance(cls.getClassLoader(),
+        return cls.cast(ProxyHelper.getProxy(cls.getClassLoader(),
                         new Class[]{cls, Client.class},
                         new ClientProxyImpl(baseURI, currentURI, classResourceInfo, inheritHeaders)));
     }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java?rev=744104&r1=744103&r2=744104&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java Fri
Feb 13 12:56:45 2009
@@ -164,8 +164,15 @@
         return this;
     }
     
-    public WebClient to(String newAddress) {
-        resetBaseAddress(URI.create(newAddress));
+    public WebClient to(String newAddress, boolean forward) {
+        if (forward) {
+            if (!newAddress.startsWith(getBaseURI().toString())) {
+                throw new IllegalArgumentException("Base address can not be preserved");
+            }
+            resetCurrentBuilder(URI.create(newAddress));
+        } else {
+            resetBaseAddress(URI.create(newAddress));
+        }
         return this;
     }
     

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/WebClientTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/WebClientTest.java?rev=744104&r1=744103&r2=744104&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/WebClientTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/WebClientTest.java
Fri Feb 13 12:56:45 2009
@@ -38,12 +38,26 @@
         WebClient wc = new WebClient("http://foo");
         assertEquals(URI.create("http://foo"), wc.getBaseURI());
         assertEquals(URI.create("http://foo"), wc.getCurrentURI());
-        wc.to("http://bar");
+        wc.to("http://bar", false);
         assertEquals(URI.create("http://bar"), wc.getBaseURI());
         assertEquals(URI.create("http://bar"), wc.getCurrentURI());
     }
     
     @Test 
+    public void testForward() {
+        WebClient wc = new WebClient("http://foo");
+        wc.to("http://foo/bar", true);
+        assertEquals(URI.create("http://foo"), wc.getBaseURI());
+        assertEquals(URI.create("http://foo/bar"), wc.getCurrentURI());
+    }
+    
+    @Test(expected = IllegalArgumentException.class) 
+    public void testWrongForward() {
+        WebClient wc = new WebClient("http://foo");
+        wc.to("http://bar", true);
+    }
+    
+    @Test 
     public void testBaseCurrentPathAfterChange() {
         WebClient wc = new WebClient(URI.create("http://foo"));
         wc.path("bar").path("baz").matrix("m1", "m1value").query("q1", "q1value");

Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=744104&r1=744103&r2=744104&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Fri Feb 13 12:56:45 2009
@@ -33,7 +33,10 @@
 import org.apache.commons.httpclient.methods.RequestEntity;
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.io.CachedOutputStream;
+import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
+import org.apache.cxf.jaxrs.client.WebClient;
 import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+
 import org.junit.BeforeClass;
 import org.junit.Test;
 
@@ -206,6 +209,15 @@
     }
     
     @Test
+    public void testGetBook123WebClient() throws Exception {
+        BookStore bs = JAXRSClientFactory.create("http://localhost:9080", BookStore.class);
+        // just to verify the interface call goes through CGLIB proxy too
+        assertEquals("http://localhost:9080", WebClient.client(bs).getBaseURI().toString());
+        Book b = bs.getBook("123");
+        assertEquals(b.getId(), 123);
+    }
+    
+    @Test
     public void testGetBook123() throws Exception {
         getAndCompareAsStrings("http://localhost:9080/bookstore/books/123",
                                "resources/expected_get_book123.txt",



Mime
View raw message