cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject [2/3] cxf git commit: Adding HttpUrlConnection reflection test which is activated with Java8 or higher
Date Fri, 21 Aug 2015 15:43:54 GMT
Adding HttpUrlConnection reflection test which is activated with Java8 or higher


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/56f4859e
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/56f4859e
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/56f4859e

Branch: refs/heads/3.0.x-fixes
Commit: 56f4859e1ad7690a920b9ab256886a8079d09694
Parents: 7316ee1
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Fri Aug 21 11:45:46 2015 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Fri Aug 21 16:43:34 2015 +0100

----------------------------------------------------------------------
 .../org/apache/cxf/common/util/ClassHelper.java |   4 +
 .../http/URLConnectionHTTPConduit.java          |  11 +-
 .../apache/cxf/systest/jaxrs/BookServer.java    |  67 ++++++++++++
 .../jaxrs/JAXRSClientServerBookTest.java        | 108 ++++++-------------
 4 files changed, 112 insertions(+), 78 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/56f4859e/core/src/main/java/org/apache/cxf/common/util/ClassHelper.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/common/util/ClassHelper.java b/core/src/main/java/org/apache/cxf/common/util/ClassHelper.java
index 68ea3b3..461c84a 100644
--- a/core/src/main/java/org/apache/cxf/common/util/ClassHelper.java
+++ b/core/src/main/java/org/apache/cxf/common/util/ClassHelper.java
@@ -85,5 +85,9 @@ public class ClassHelper {
             return HELPER.getRealClassInternal(o);
         }
     }
+    public static double getJavaVersion() {
+        String version = System.getProperty("java.version");
+        return Double.parseDouble(version.substring(0, 3));    
+    }
     
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/56f4859e/rt/transports/http/src/main/java/org/apache/cxf/transport/http/URLConnectionHTTPConduit.java
----------------------------------------------------------------------
diff --git a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/URLConnectionHTTPConduit.java
b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/URLConnectionHTTPConduit.java
index 1c55ac4..954db7a 100644
--- a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/URLConnectionHTTPConduit.java
+++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/URLConnectionHTTPConduit.java
@@ -49,11 +49,12 @@ import org.apache.cxf.ws.addressing.EndpointReferenceType;
  * 
  */
 public class URLConnectionHTTPConduit extends HTTPConduit {
-    public static final String HTTPURL_CONNECTION_METHOD_HACK = "httpurlconnection.method.hack";
+    public static final String HTTPURL_CONNECTION_METHOD_REFLECTION = "use.httpurlconnection.method.reflection";
 
-    private static final boolean DEFAULT_USE_HACK;
+    private static final boolean DEFAULT_USE_REFLECTION;
     static {
-        DEFAULT_USE_HACK = Boolean.valueOf(SystemPropertyAction.getProperty(HTTPURL_CONNECTION_METHOD_HACK,
"false"));
+        DEFAULT_USE_REFLECTION = 
+            Boolean.valueOf(SystemPropertyAction.getProperty(HTTPURL_CONNECTION_METHOD_REFLECTION,
"false"));
     }
     
     /**
@@ -137,8 +138,8 @@ public class URLConnectionHTTPConduit extends HTTPConduit {
         try {
             connection.setRequestMethod(httpRequestMethod);
         } catch (java.net.ProtocolException ex) {
-            Object o = message.getContextualProperty(HTTPURL_CONNECTION_METHOD_HACK);
-            boolean b = DEFAULT_USE_HACK;
+            Object o = message.getContextualProperty(HTTPURL_CONNECTION_METHOD_REFLECTION);
+            boolean b = DEFAULT_USE_REFLECTION;
             if (o != null) {
                 b = MessageUtils.isTrue(o);
             }

http://git-wip-us.apache.org/repos/asf/cxf/blob/56f4859e/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
index f89ea95..4ef1294 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
@@ -26,6 +26,9 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.ws.rs.client.ClientRequestContext;
+import javax.ws.rs.client.ClientResponseContext;
+import javax.ws.rs.client.ClientResponseFilter;
 import javax.ws.rs.container.ContainerRequestContext;
 import javax.ws.rs.container.ContainerRequestFilter;
 import javax.ws.rs.container.ContainerResponseContext;
@@ -37,9 +40,12 @@ import javax.ws.rs.ext.ExceptionMapper;
 import org.apache.cxf.Bus;
 import org.apache.cxf.BusFactory;
 import org.apache.cxf.common.util.PropertyUtils;
+import org.apache.cxf.helpers.CastUtils;
+import org.apache.cxf.interceptor.Fault;
 import org.apache.cxf.interceptor.Interceptor;
 import org.apache.cxf.interceptor.LoggingInInterceptor;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.client.ResponseExceptionMapper;
 import org.apache.cxf.jaxrs.ext.search.QueryContextProvider;
 import org.apache.cxf.jaxrs.ext.search.SearchBean;
 import org.apache.cxf.jaxrs.ext.search.SearchContextProvider;
@@ -49,6 +55,9 @@ import org.apache.cxf.jaxrs.provider.BinaryDataProvider;
 import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
 import org.apache.cxf.jaxrs.provider.StreamingResponseProvider;
 import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.AbstractPhaseInterceptor;
+import org.apache.cxf.phase.Phase;
+import org.apache.cxf.systest.jaxrs.BookStore.BookNotReturnedException;
 import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
     
 public class BookServer extends AbstractBusTestServerBase {
@@ -189,4 +198,62 @@ public class BookServer extends AbstractBusTestServerBase {
     public static class BlockedException extends RuntimeException {
         
     }
+    
+    public static class ReplaceContentTypeInterceptor extends AbstractPhaseInterceptor<Message>
{
+        public ReplaceContentTypeInterceptor() {
+            super(Phase.READ);
+        }
+
+        public void handleMessage(Message message) throws Fault {
+            Map<String, List<String>> headers = 
+                CastUtils.cast((Map<?, ?>)message.get(Message.PROTOCOL_HEADERS));
+            headers.put(Message.CONTENT_TYPE, Collections.singletonList("text/plain"));
+        }
+    }
+
+    public static class ReplaceStatusInterceptor extends AbstractPhaseInterceptor<Message>
{
+        public ReplaceStatusInterceptor() {
+            super(Phase.READ);
+        }
+
+        public void handleMessage(Message message) throws Fault {
+            message.getExchange().put(Message.RESPONSE_CODE, 200);
+        }
+    }
+    
+    public static class NotReturnedExceptionMapper implements ResponseExceptionMapper<BookNotReturnedException>
{
+
+        public BookNotReturnedException fromResponse(Response r) {
+            String status = r.getHeaderString("Status");
+            if ("notReturned".equals(status)) { 
+                return new BookNotReturnedException(status);
+            } else {
+                return null;
+            }
+        }
+        
+    }
+    
+    public static class NotFoundExceptionMapper implements ResponseExceptionMapper<BookNotFoundFault>
{
+
+        public BookNotFoundFault fromResponse(Response r) {
+            String status = r.getHeaderString("Status");
+            if ("notFound".equals(status)) { 
+                return new BookNotFoundFault(status);
+            } else {
+                return null;
+            }
+        }
+        
+    }
+    public static class TestResponseFilter implements ClientResponseFilter {
+
+        @Override
+        public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext)
+            throws IOException {
+            // TODO Auto-generated method stub
+            
+        }
+        
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/56f4859e/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
index 9c4487a..003fc9a 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
@@ -40,9 +40,6 @@ import javax.ws.rs.NotAcceptableException;
 import javax.ws.rs.ProcessingException;
 import javax.ws.rs.ServerErrorException;
 import javax.ws.rs.WebApplicationException;
-import javax.ws.rs.client.ClientRequestContext;
-import javax.ws.rs.client.ClientResponseContext;
-import javax.ws.rs.client.ClientResponseFilter;
 import javax.ws.rs.client.ResponseProcessingException;
 import javax.ws.rs.core.Form;
 import javax.ws.rs.core.GenericEntity;
@@ -62,22 +59,17 @@ import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.commons.httpclient.methods.PutMethod;
 import org.apache.commons.httpclient.methods.RequestEntity;
-import org.apache.cxf.helpers.CastUtils;
+import org.apache.cxf.common.util.ClassHelper;
 import org.apache.cxf.helpers.IOUtils;
-import org.apache.cxf.interceptor.Fault;
 import org.apache.cxf.interceptor.LoggingInInterceptor;
 import org.apache.cxf.io.CachedOutputStream;
 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
 import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
-import org.apache.cxf.jaxrs.client.ResponseExceptionMapper;
 import org.apache.cxf.jaxrs.client.WebClient;
 import org.apache.cxf.jaxrs.ext.xml.XMLSource;
 import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
 import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
 import org.apache.cxf.jaxrs.provider.XSLTJaxbProvider;
-import org.apache.cxf.message.Message;
-import org.apache.cxf.phase.AbstractPhaseInterceptor;
-import org.apache.cxf.phase.Phase;
 import org.apache.cxf.systest.jaxrs.BookStore.BookInfo;
 import org.apache.cxf.systest.jaxrs.BookStore.BookInfoInterface;
 import org.apache.cxf.systest.jaxrs.BookStore.BookNotReturnedException;
@@ -100,6 +92,32 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
     }
     
     @Test
+    public void testRetrieveBookCustomMethodReflection() throws Exception {
+        double version = ClassHelper.getJavaVersion();
+        if (version < 1.8) {
+            return;
+        }
+        try {
+            doRetrieveBook(false);
+            fail("HTTPUrlConnection does not support custom verbs without the reflection");
+        } catch (ProcessingException ex) {
+            // continue
+        }
+        Book book = doRetrieveBook(true);
+        assertEquals("Retrieve", book.getName());
+    }
+    
+    private Book doRetrieveBook(boolean useReflection) {
+        String address = "http://localhost:" + PORT + "/bookstore/retrieve";
+        WebClient wc = WebClient.create(address);
+        wc.type("application/xml").accept("application/xml");
+        if (useReflection) {
+            WebClient.getConfig(wc).getRequestContext().put("use.httpurlconnection.method.reflection",
true);
+        }
+        return wc.invoke("RETRIEVE", new Book("Retrieve", 123L), Book.class);
+    }
+    
+    @Test
     public void testBlockAndTrowException() throws Exception {
         String address = "http://localhost:" + PORT + "/bookstore/blockAndThrowException";
         WebClient wc = WebClient.create(address);
@@ -133,7 +151,7 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
     public void testNonExistent() throws Exception {
         String address = "http://localhostt/bookstore";
         WebClient wc = WebClient.create(address, 
-                                        Collections.singletonList(new TestResponseFilter()));
+                                        Collections.singletonList(new BookServer.TestResponseFilter()));
         try {
             wc.get();
             fail();
@@ -773,8 +791,8 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
     @Test
     public void testBookWithMultipleExceptions() throws Exception {
         List<Object> providers = new LinkedList<Object>();
-        providers.add(new NotReturnedExceptionMapper());
-        providers.add(new NotFoundExceptionMapper());
+        providers.add(new BookServer.NotReturnedExceptionMapper());
+        providers.add(new BookServer.NotFoundExceptionMapper());
         BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, 
                                                     BookStore.class,
                                                     providers);
@@ -808,8 +826,8 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
     @Test
     public void testBookWithMultipleExceptions2() throws Exception {
         List<Object> providers = new LinkedList<Object>();
-        providers.add(new NotReturnedExceptionMapper());
-        providers.add(NotFoundExceptionMapper.class);
+        providers.add(new BookServer.NotReturnedExceptionMapper());
+        providers.add(BookServer.NotFoundExceptionMapper.class);
         BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, 
                                                     BookStore.class,
                                                     providers);
@@ -1553,7 +1571,7 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
         WebClient wc = 
             WebClient.create("http://localhost:" + PORT + "/bookstore/books/check/malformedmt/123");
         wc.accept(MediaType.TEXT_PLAIN);
-        WebClient.getConfig(wc).getInInterceptors().add(new ReplaceContentTypeInterceptor());
+        WebClient.getConfig(wc).getInInterceptors().add(new BookServer.ReplaceContentTypeInterceptor());
         assertTrue(wc.get(Boolean.class));
     }
     
@@ -1770,7 +1788,7 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
     
     private void doTestEmptyResponse(String mt) {
         WebClient wc = WebClient.create("http://localhost:" + PORT + "/bookstore/emptybook");
-        WebClient.getConfig(wc).getInInterceptors().add(new ReplaceStatusInterceptor());
+        WebClient.getConfig(wc).getInInterceptors().add(new BookServer.ReplaceStatusInterceptor());
         wc.accept(mt);
         wc.get(Book.class);
     }
@@ -1778,7 +1796,7 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
     @Test(expected = ResponseProcessingException.class)
     public void testEmptyResponseProxy() {
         BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class);
-        WebClient.getConfig(store).getInInterceptors().add(new ReplaceStatusInterceptor());
+        WebClient.getConfig(store).getInInterceptors().add(new BookServer.ReplaceStatusInterceptor());
         store.getEmptyBook();
     }
     
@@ -2715,61 +2733,5 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
         return str;
     }
 
-    public static class ReplaceContentTypeInterceptor extends AbstractPhaseInterceptor<Message>
{
-        public ReplaceContentTypeInterceptor() {
-            super(Phase.READ);
-        }
-
-        public void handleMessage(Message message) throws Fault {
-            Map<String, List<String>> headers = 
-                CastUtils.cast((Map<?, ?>)message.get(Message.PROTOCOL_HEADERS));
-            headers.put(Message.CONTENT_TYPE, Collections.singletonList("text/plain"));
-        }
-    }
-
-    public static class ReplaceStatusInterceptor extends AbstractPhaseInterceptor<Message>
{
-        public ReplaceStatusInterceptor() {
-            super(Phase.READ);
-        }
-
-        public void handleMessage(Message message) throws Fault {
-            message.getExchange().put(Message.RESPONSE_CODE, 200);
-        }
-    }
-    
-    public static class NotReturnedExceptionMapper implements ResponseExceptionMapper<BookNotReturnedException>
{
-
-        public BookNotReturnedException fromResponse(Response r) {
-            String status = r.getHeaderString("Status");
-            if ("notReturned".equals(status)) { 
-                return new BookNotReturnedException(status);
-            } else {
-                return null;
-            }
-        }
-        
-    }
     
-    public static class NotFoundExceptionMapper implements ResponseExceptionMapper<BookNotFoundFault>
{
-
-        public BookNotFoundFault fromResponse(Response r) {
-            String status = r.getHeaderString("Status");
-            if ("notFound".equals(status)) { 
-                return new BookNotFoundFault(status);
-            } else {
-                return null;
-            }
-        }
-        
-    }
-    public static class TestResponseFilter implements ClientResponseFilter {
-
-        @Override
-        public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext)
-            throws IOException {
-            // TODO Auto-generated method stub
-            
-        }
-        
-    }
 }


Mime
View raw message