cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1389275 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/
Date Mon, 24 Sep 2012 10:29:59 GMT
Author: sergeyb
Date: Mon Sep 24 10:29:59 2012
New Revision: 1389275

URL: http://svn.apache.org/viewvc?rev=1389275&view=rev
Log:
Initial attempt at bringing the sync and async web client code closer

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/ClientProxyImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java

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=1389275&r1=1389274&r2=1389275&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
Mon Sep 24 10:29:59 2012
@@ -576,6 +576,14 @@ public abstract class AbstractClient imp
         return newRequestURI;
     }
     
+    protected void doRunInterceptorChain(Message m) {
+        try {
+            m.getInterceptorChain().doIntercept(m);
+        } catch (Exception ex) {
+            m.setContent(Exception.class, ex);
+        }
+    }
+    
     @SuppressWarnings("unchecked")
     public Object[] invoke(BindingOperationInfo oi, Object[] params, Map<String, Object>
context,
                            Exchange exchange) throws Exception {

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java?rev=1389275&r1=1389274&r2=1389275&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
Mon Sep 24 10:29:59 2012
@@ -520,11 +520,7 @@ public class ClientProxyImpl extends Abs
         reqContext.put("BODY_INDEX", bodyIndex);
         
         // execute chain    
-        try {
-            outMessage.getInterceptorChain().doIntercept(outMessage);
-        } catch (Exception ex) {
-            outMessage.setContent(Exception.class, ex);
-        }
+        doRunInterceptorChain(outMessage);
         
         Object[] results = preProcessResult(outMessage);
         if (results != null && results.length == 1) {

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=1389275&r1=1389274&r2=1389275&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 Mon
Sep 24 10:29:59 2012
@@ -747,20 +747,7 @@ public class WebClient extends AbstractC
                                 Class<?> responseClass, 
                                 Type outGenericType) {
         
-        MultivaluedMap<String, String> headers = getHeaders();
-        boolean contentTypeNotSet = headers.getFirst(HttpHeaders.CONTENT_TYPE) == null;
-        if (contentTypeNotSet) {
-            String ct = "*/*";
-            if (body != null) { 
-                ct = body instanceof Form ? MediaType.APPLICATION_FORM_URLENCODED 
-                                          : MediaType.APPLICATION_XML;
-            }
-            headers.putSingle(HttpHeaders.CONTENT_TYPE, ct);
-        }
-        if (responseClass != null && responseClass != Response.class 
-            && headers.getFirst(HttpHeaders.ACCEPT) == null) {
-            headers.putSingle(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML_TYPE.toString());
-        }
+        MultivaluedMap<String, String> headers = prepareHeaders(responseClass, body);
         resetResponse();
         Response r = doChainedInvocation(httpMethod, headers, body, requestClass, inGenericType,

                                          responseClass, outGenericType, null, null);
@@ -796,8 +783,29 @@ public class WebClient extends AbstractC
     protected <T> Future<T> doInvokeAsync(String httpMethod, 
                                           Object body, 
                                           Class<?> requestClass,
-                                          Type inGenericType,
+                                          Type inType,
                                           InvocationCallback<T> callback) {
+        
+        Type outType = getCallbackType(callback);
+        Class<?> respClass = outType instanceof Class ? (Class<?>) outType :
null;
+        
+        MultivaluedMap<String, String> headers = prepareHeaders(respClass, body);
+        resetResponse();
+
+        Message m = finalizeMessage(httpMethod, headers, body, requestClass, inType, 
+                                    respClass, outType, null, null);
+        
+        m.getExchange().setSynchronous(false);
+        JaxrsClientCallback<T> cb = new JaxrsClientCallback<T>(callback, respClass,
outType);
+        m.getExchange().put(JaxrsClientCallback.class, cb);
+        
+        doRunInterceptorChain(m);
+        
+        return cb.createFuture();
+    }
+
+    
+    private MultivaluedMap<String, String> prepareHeaders(Class<?> responseClass,
Object body) {
         MultivaluedMap<String, String> headers = getHeaders();
         boolean contentTypeNotSet = headers.getFirst(HttpHeaders.CONTENT_TYPE) == null;
         if (contentTypeNotSet) {
@@ -808,45 +816,14 @@ public class WebClient extends AbstractC
             }
             headers.putSingle(HttpHeaders.CONTENT_TYPE, ct);
         }
-        Type outGenericType = getCallbackType(callback);
-        Class<?> responseClass = outGenericType instanceof Class ? (Class<?>)
outGenericType : null;
+        
         if (responseClass != null && responseClass != Response.class 
             && headers.getFirst(HttpHeaders.ACCEPT) == null) {
             headers.putSingle(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML_TYPE.toString());
         }
-        resetResponse();
-        URI uri = getCurrentURI();
-        Exchange exchange = null;
-        Map<String, Object> invContext = null;
-        
-        Message m = createMessage(body, httpMethod, headers, uri, exchange, 
-                invContext, false);
-        
-        m.getExchange().setSynchronous(false);
-        
-        Map<String, Object> reqContext = getRequestContext(m);
-        reqContext.put(Message.HTTP_REQUEST_METHOD, httpMethod);
-        reqContext.put(REQUEST_CLASS, requestClass);
-        reqContext.put(REQUEST_TYPE, inGenericType);
-        reqContext.put(RESPONSE_CLASS, responseClass);
-        reqContext.put(RESPONSE_TYPE, outGenericType);
-        
-        if (body != null) {
-            m.getInterceptorChain().add(new BodyWriter());
-        }
-        setPlainOperationNameProperty(m, httpMethod + ":" + uri.toString());
-        
-        JaxrsClientCallback<T> cb = new JaxrsClientCallback<T>(callback, responseClass,
outGenericType);
-        m.getExchange().put(JaxrsClientCallback.class, cb);
-        try {
-            m.getInterceptorChain().doIntercept(m);
-        } catch (Exception ex) {
-            m.setContent(Exception.class, ex);
-        }
-        
-        return cb.createFuture();
+        return headers;
     }
-
+    
     private void handleAsyncResponse(Message message) {
         JaxrsClientCallback<?> cb = message.getExchange().get(JaxrsClientCallback.class);
         Response r = handleResponse(message.getExchange().getOutMessage(),
@@ -859,11 +836,11 @@ public class WebClient extends AbstractC
             cb.handleResponse(message, new Object[] {r.getEntity()});
         }
     }
-    public void handleAsyncFault(Message message) {
+    private void handleAsyncFault(Message message) {
     }
 
 
-    
+    //TODO: retry invocation will not work in case of async request failures for the moment
     @Override
     protected Object retryInvoke(URI newRequestURI, 
                                  MultivaluedMap<String, String> headers,
@@ -885,12 +862,29 @@ public class WebClient extends AbstractC
                                            MultivaluedMap<String, String> headers,

                                            Object body, 
                                            Class<?> requestClass,
-                                           Type inGenericType,
-                                           Class<?> responseClass, 
-                                           Type outGenericType,
+                                           Type inType,
+                                           Class<?> respClass, 
+                                           Type outType,
                                            Exchange exchange,
                                            Map<String, Object> invContext) {
     //CHECKSTYLE:ON    
+        Message m = finalizeMessage(httpMethod, headers, body, requestClass, inType, 
+                                    respClass, outType, exchange, invContext);
+        doRunInterceptorChain(m);
+        return doResponse(m, respClass, outType);
+    }
+    
+    //CHECKSTYLE:OFF
+    private Message finalizeMessage(String httpMethod, 
+                                   MultivaluedMap<String, String> headers, 
+                                   Object body, 
+                                   Class<?> requestClass,
+                                   Type inGenericType,
+                                   Class<?> responseClass, 
+                                   Type outGenericType,
+                                   Exchange exchange,
+                                   Map<String, Object> invContext) {
+   //CHECKSTYLE:ON    
         URI uri = getCurrentURI();
         Message m = createMessage(body, httpMethod, headers, uri, exchange, 
                 invContext, false);
@@ -906,14 +900,9 @@ public class WebClient extends AbstractC
             m.getInterceptorChain().add(new BodyWriter());
         }
         setPlainOperationNameProperty(m, httpMethod + ":" + uri.toString());
-        
-        try {
-            m.getInterceptorChain().doIntercept(m);
-        } catch (Exception ex) {
-            m.setContent(Exception.class, ex);
-        }
-        return doResponse(m, responseClass, outGenericType);
+        return m;
     }
+    
     protected Response doResponse(Message m, 
                                   Class<?> responseClass, 
                                   Type outGenericType) {

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java?rev=1389275&r1=1389274&r2=1389275&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
Mon Sep 24 10:29:59 2012
@@ -33,6 +33,7 @@ import javax.ws.rs.client.ClientResponse
 import javax.ws.rs.client.ClientResponseFilter;
 import javax.ws.rs.client.InvocationCallback;
 import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.ext.ReaderInterceptor;
 import javax.ws.rs.ext.ReaderInterceptorContext;
@@ -58,56 +59,58 @@ public class JAXRS20ClientServerBookTest
     @Test
     public void testGetBook() {
         String address = "http://localhost:" + PORT + "/bookstore/bookheaders/simple";
-        doTestBook(address);
+        doTestGetBook(address);
     }
     
     @Test
     public void testGetBookAsync() throws Exception {
         String address = "http://localhost:" + PORT + "/bookstore/bookheaders/simple";
-        doTestBookAsync(address, false);
+        doTestGetBookAsync(address, false);
+    }
+    
+    @Test
+    public void testGetBookAsyncResponse() throws Exception {
+        String address = "http://localhost:" + PORT + "/bookstore/bookheaders/simple";
+        doTestGetBookAsyncResponse(address, false);
     }
     
     @Test
     public void testGetBookAsyncInvoker() throws Exception {
         String address = "http://localhost:" + PORT + "/bookstore/bookheaders/simple";
-        doTestBookAsync(address, true);
+        doTestGetBookAsync(address, true);
     }
     
     @Test
     public void testGetBookWrongPath() {
         String address = "http://localhost:" + PORT + "/wrongpath";
-        doTestBook(address);
+        doTestGetBook(address);
     }
     @Test
     public void testGetBookWrongPathAsync() throws Exception {
         String address = "http://localhost:" + PORT + "/wrongpath";
-        doTestBookAsync(address, false);
+        doTestGetBookAsync(address, false);
     }
     
-    private void doTestBook(String address) {
-        List<Object> providers = new ArrayList<Object>();
-        providers.add(new ClientHeaderRequestFilter());
-        providers.add(new ClientHeaderResponseFilter());
-        WebClient wc = WebClient.create(address, providers);
-        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000L);
+    private void doTestGetBook(String address) {
+        WebClient wc = createWebClient(address);
         Book book = wc.get(Book.class);
         assertEquals(124L, book.getId());
-        Response response = wc.getResponse();
-        assertEquals("OK", response.getHeaderString("Response"));
-        assertEquals("custom", response.getHeaderString("Custom"));
-        assertEquals("simple", response.getHeaderString("Simple"));
-        assertEquals("serverWrite", response.getHeaderString("ServerWriterInterceptor"));
-        assertEquals("http://localhost/redirect", response.getHeaderString(HttpHeaders.LOCATION));
+        validateResponse(wc);
     }
     
-    private void doTestBookAsync(String address, boolean asyncInvoker) 
-        throws InterruptedException, ExecutionException {
-        
+    private WebClient createWebClient(String address) {
         List<Object> providers = new ArrayList<Object>();
         providers.add(new ClientHeaderRequestFilter());
         providers.add(new ClientHeaderResponseFilter());
         WebClient wc = WebClient.create(address, providers);
         WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000L);
+        return wc;
+    }
+    
+    private void doTestGetBookAsync(String address, boolean asyncInvoker) 
+        throws InterruptedException, ExecutionException {
+        
+        WebClient wc = createWebClient(address);
         
         final Holder<Book> holder = new Holder<Book>();
         final InvocationCallback<Book> callback = new InvocationCallback<Book>()
{
@@ -122,6 +125,31 @@ public class JAXRS20ClientServerBookTest
         Book book = future.get();
         assertSame(book, holder.value);
         assertEquals(124L, book.getId());
+        validateResponse(wc);   
+    }
+    
+    private void doTestGetBookAsyncResponse(String address, boolean asyncInvoker) 
+        throws InterruptedException, ExecutionException {
+        
+        WebClient wc = createWebClient(address);
+        wc.accept(MediaType.APPLICATION_XML_TYPE);
+        
+        final Holder<Response> holder = new Holder<Response>();
+        final InvocationCallback<Response> callback = new InvocationCallback<Response>()
{
+            public void completed(Response response) {
+                holder.value = response;
+            }
+            public void failed(ClientException error) {
+            }
+        };
+        
+        Future<Response> future = asyncInvoker ? wc.async().get(callback) : wc.get(callback);
+        Book book = future.get().readEntity(Book.class);
+        assertEquals(124L, book.getId());
+        validateResponse(wc);   
+    }
+    
+    private void validateResponse(WebClient wc) {
         Response response = wc.getResponse();
         assertEquals("OK", response.getHeaderString("Response"));
         assertEquals("custom", response.getHeaderString("Custom"));
@@ -130,6 +158,8 @@ public class JAXRS20ClientServerBookTest
         assertEquals("http://localhost/redirect", response.getHeaderString(HttpHeaders.LOCATION));
     }
     
+    
+    
     @Test
     public void testClientFiltersLocalResponse() {
         String address = "http://localhost:" + PORT + "/bookstores";
@@ -157,15 +187,12 @@ public class JAXRS20ClientServerBookTest
         WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000L);
         Book book = wc.post(new Book("Book", 126L), Book.class);
         assertEquals(124L, book.getId());
+        validateResponse(wc);
+        
         Response response = wc.getResponse();
-        assertEquals("OK", response.getHeaderString("Response"));
-        assertEquals("custom", response.getHeaderString("Custom"));
-        assertEquals("simple", response.getHeaderString("Simple"));
         assertEquals("serverRead", response.getHeaderString("ServerReaderInterceptor"));
-        assertEquals("serverWrite", response.getHeaderString("ServerWriterInterceptor"));
         assertEquals("clientWrite", response.getHeaderString("ClientWriterInterceptor"));
         assertEquals("clientRead", response.getHeaderString("ClientReaderInterceptor"));
-        assertEquals("http://localhost/redirect", response.getHeaderString(HttpHeaders.LOCATION));
     }
     
     private static class ClientCacheRequestFilter implements ClientRequestFilter {



Mime
View raw message