cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1462164 - in /cxf/branches/2.7.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/spec/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/
Date Thu, 28 Mar 2013 15:43:54 GMT
Author: sergeyb
Date: Thu Mar 28 15:43:54 2013
New Revision: 1462164

URL: http://svn.apache.org/r1462164
Log:
Merged revisions 1462146 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1462146 | sergeyb | 2013-03-28 18:16:31 +0300 (Thu, 28 Mar 2013) | 1 line
  
  [CXF-4929] Inital updates to support client side filtering of types and annoytations, though
more clean up will be need later on after the full support for 2.0 client api is in
........

Modified:
    cxf/branches/2.7.x-fixes/   (props changed)
    cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
    cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
    cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
    cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientRequestContextImpl.java
    cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer20.java
    cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
    cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java

Propchange: cxf/branches/2.7.x-fixes/
------------------------------------------------------------------------------
  Merged /cxf/trunk:r1462146

Propchange: cxf/branches/2.7.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java?rev=1462164&r1=1462163&r2=1462164&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
(original)
+++ cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
Thu Mar 28 15:43:54 2013
@@ -974,10 +974,15 @@ public abstract class AbstractClient imp
             }
             
             Object body = objs.get(0);
-            
-            doWriteBody(outMessage, body, os);
+            Annotation[] customAnns = (Annotation[])outMessage.get(Annotation.class.getName());
+            Type t = outMessage.get(Type.class);
+            doWriteBody(outMessage, body, t, customAnns, os);
         }
         
-        protected abstract void doWriteBody(Message outMessage, Object body, OutputStream
os) throws Fault;
+        protected abstract void doWriteBody(Message outMessage, 
+                                            Object body,
+                                            Type bodyType,
+                                            Annotation[] customAnns,
+                                            OutputStream os) throws Fault;
     }
 }

Modified: cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java?rev=1462164&r1=1462163&r2=1462164&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
(original)
+++ cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
Thu Mar 28 15:43:54 2013
@@ -526,6 +526,9 @@ public class ClientProxyImpl extends Abs
         setPlainOperationNameProperty(outMessage, ori.getMethodToInvoke().getName());
         outMessage.getExchange().put(Method.class, ori.getMethodToInvoke());
         
+        outMessage.put(Annotation.class.getName(), 
+                       getMethodAnnotations(ori.getAnnotatedMethod(), bodyIndex));
+        
         if (body != null) {
             outMessage.put("BODY_INDEX", bodyIndex);
             outMessage.getInterceptorChain().add(new BodyWriter());
@@ -610,10 +613,18 @@ public class ClientProxyImpl extends Abs
         throw new ClientException(errorMsg.toString());
     }
     
+    protected static Annotation[] getMethodAnnotations(Method aMethod, int bodyIndex) {
+        return aMethod == null || bodyIndex == -1 ? new Annotation[0] 
+            : aMethod.getParameterAnnotations()[bodyIndex];
+    }
+    
     private class BodyWriter extends AbstractBodyWriter {
         
-        protected void doWriteBody(Message outMessage, Object body, 
-                                 OutputStream os) throws Fault {
+        protected void doWriteBody(Message outMessage, 
+                                   Object body,
+                                   Type bodyType,
+                                   Annotation[] customAnns,
+                                   OutputStream os) throws Fault {
             
             
             OperationResourceInfo ori = outMessage.getContent(OperationResourceInfo.class);
@@ -623,17 +634,26 @@ public class ClientProxyImpl extends Abs
             
             Method method = ori.getMethodToInvoke();
             int bodyIndex = (Integer)outMessage.get("BODY_INDEX");
-            Method aMethod = ori.getAnnotatedMethod();
-            Annotation[] anns = aMethod == null || bodyIndex == -1 ? new Annotation[0] 
-                                                  : aMethod.getParameterAnnotations()[bodyIndex];
+            
+            Annotation[] anns = customAnns != null ? customAnns
+                : getMethodAnnotations(ori.getAnnotatedMethod(), bodyIndex);
             try {
                 if (bodyIndex != -1) {
+                    Class<?> paramClass = method.getParameterTypes()[bodyIndex];
+                    Class<?> bodyClass = 
+                        paramClass.isAssignableFrom(body.getClass()) ? paramClass : body.getClass();
                     Type paramType = method.getGenericParameterTypes()[bodyIndex];
-                    
-                    writeBody(body, outMessage, body.getClass(), paramType,
+                    if (bodyType != null) {
+                        paramType = bodyType;
+                    }
+                    writeBody(body, outMessage, bodyClass, paramType,
                               anns, os);
                 } else {
-                    writeBody(body, outMessage, body.getClass(), body.getClass(), 
+                    Type paramType = body.getClass();
+                    if (bodyType != null) {
+                        paramType = bodyType;
+                    }
+                    writeBody(body, outMessage, body.getClass(), paramType, 
                               anns, os);
                 }
             } catch (Exception ex) {

Modified: cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java?rev=1462164&r1=1462163&r2=1462164&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
(original)
+++ cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
Thu Mar 28 15:43:54 2013
@@ -1093,7 +1093,10 @@ public class WebClient extends AbstractC
     
     private class BodyWriter extends AbstractBodyWriter {
 
-        protected void doWriteBody(Message outMessage, Object body, 
+        protected void doWriteBody(Message outMessage, 
+                                   Object body,
+                                   Type bodyType,
+                                   Annotation[] customAnns,
                                    OutputStream os) throws Fault {    
             
             Map<String, Object> requestContext = WebClient.this.getRequestContext(outMessage);
@@ -1103,11 +1106,11 @@ public class WebClient extends AbstractC
                 requestClass = (Class<?>)requestContext.get(REQUEST_CLASS);
                 requestType = (Type)requestContext.get(REQUEST_TYPE);
             }
-            Annotation[] anns = new Annotation[]{};
-            Annotation[] customAnns = (Annotation[])outMessage.get(Annotation.class.getName());
-            if (customAnns != null) {
-                anns = customAnns;
+            if (bodyType != null) {
+                requestType = bodyType;
             }
+            
+            Annotation[] anns = customAnns != null ? customAnns : new Annotation[]{};
             boolean isAssignable = requestClass != null && requestClass.isAssignableFrom(body.getClass());
             try {
                 writeBody(body, outMessage, 

Modified: cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientRequestContextImpl.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientRequestContextImpl.java?rev=1462164&r1=1462163&r2=1462164&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientRequestContextImpl.java
(original)
+++ cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientRequestContextImpl.java
Thu Mar 28 15:43:54 2013
@@ -28,12 +28,14 @@ import java.util.Map;
 import javax.ws.rs.client.Client;
 import javax.ws.rs.client.ClientRequestContext;
 import javax.ws.rs.client.Configuration;
+import javax.ws.rs.core.GenericEntity;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 
 import org.apache.cxf.jaxrs.impl.AbstractRequestContextImpl;
 import org.apache.cxf.jaxrs.impl.MetadataMap;
+import org.apache.cxf.jaxrs.utils.InjectionUtils;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.message.MessageContentsList;
 
@@ -58,13 +60,17 @@ public class ClientRequestContextImpl ex
         return null;
     }
     
-    @Override
-    public Object getEntity() {
+    private Object getMessageContent() {
         MessageContentsList objs = MessageContentsList.getContentsList(m);
         if (objs == null || objs.size() == 0) {
             return null;
         }
         return objs.get(0);
+    } 
+    
+    @Override
+    public Object getEntity() {
+        return getMessageContent();
     }
 
     @Override
@@ -76,14 +82,13 @@ public class ClientRequestContextImpl ex
     @Override
     public Class<?> getEntityClass() {
         Object entity = getEntity();
-        return entity == null ? null : entity.getClass();
+        return entity == null ? null : entity.getClass(); 
     }
 
     @Override
     public Type getEntityType() {
-        Object entity = getEntity();
-        //TODO: deal with generic entities
-        return entity == null ? null : entity.getClass();
+        Type t = m.get(Type.class);
+        return t != null ? t : getEntityClass();
     }
     
     @Override
@@ -106,9 +111,26 @@ public class ClientRequestContextImpl ex
         if (anns != null) {
             m.put(Annotation.class.getName(), anns);
         }
-        m.put(List.class, entity == null ? new MessageContentsList() : new MessageContentsList(entity));
+        doSetEntity(entity);
     }
 
+
+    private void doSetEntity(Object entity) {
+        Object actualEntity = InjectionUtils.getEntity(entity);
+        m.setContent(List.class, actualEntity == null ? new MessageContentsList() 
+            : new MessageContentsList(actualEntity));
+        Type type = null;
+        if (entity != null) {
+            if (GenericEntity.class.isAssignableFrom(entity.getClass())) {
+                type = ((GenericEntity<?>)entity).getType();
+            } else {
+                type = entity.getClass();
+            }
+        }
+        if (type != null) {
+            m.put(Type.class, type);
+        }
+    }
     
     @Override
     public URI getUri() {

Modified: cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer20.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer20.java?rev=1462164&r1=1462163&r2=1462164&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer20.java
(original)
+++ cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer20.java
Thu Mar 28 15:43:54 2013
@@ -244,6 +244,8 @@ public class BookServer20 extends Abstra
                     if (anns.length == 4 && anns[3].annotationType() == Context.class)
{
                         responseContext.getHeaders().addFirst("Annotations", "OK");
                     }
+                } else {
+                    responseContext.setEntity(new Book("book", 124L), null, null);
                 }
             } else {
                 ct += ";charset=";
@@ -256,10 +258,15 @@ public class BookServer20 extends Abstra
     
     @BindingPriority(1)
     public static class PostMatchContainerResponseFilter2 implements ContainerResponseFilter
{
-
+        @Context
+        private ResourceInfo ri;
         @Override
         public void filter(ContainerRequestContext requestContext,
                            ContainerResponseContext responseContext) throws IOException {
+            if (ri.getResourceMethod() != null
+                && "addBook2".equals(ri.getResourceMethod().getName())) {
+                return;
+            }
             if (!responseContext.getHeaders().containsKey("Response")) {
                 throw new RuntimeException();
             }

Modified: cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=1462164&r1=1462163&r2=1462164&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
(original)
+++ cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
Thu Mar 28 15:43:54 2013
@@ -995,6 +995,14 @@ public class BookStore {
     }
     
     @POST
+    @Path("/books2")
+    @Produces("text/xml")
+    @Consumes("application/xml")
+    public Book addBook2(Book book) {
+        return new Book("Book echo", book.getId() + 1);
+    }
+    
+    @POST
     @Path("/oneway")
     @Oneway
     public void onewayRequest() {

Modified: cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java?rev=1462164&r1=1462163&r2=1462164&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
(original)
+++ cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
Thu Mar 28 15:43:54 2013
@@ -21,6 +21,7 @@ package org.apache.cxf.systest.jaxrs;
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
@@ -62,21 +63,22 @@ public class JAXRS20ClientServerBookTest
     @Test
     public void testGetGenericBook() throws Exception {
         String address = "http://localhost:" + PORT + "/bookstore/genericbooks/123";
-        doTestGetGenericBook(address, false);
+        doTestGetGenericBook(address, 124L, false);
     }
     
     @Test
     public void testGetGenericBook2() throws Exception {
         String address = "http://localhost:" + PORT + "/bookstore/genericbooks2/123";
-        doTestGetGenericBook(address, true);
+        doTestGetGenericBook(address, 123L, true);
     }
     
-    private void doTestGetGenericBook(String address, boolean checkAnnotations) throws Exception
{
+    private void doTestGetGenericBook(String address, long bookId, boolean checkAnnotations)

+        throws Exception {
         WebClient wc = WebClient.create(address);
         WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000L);
         wc.accept("application/xml");
         Book book = wc.get(Book.class);
-        assertEquals(123L, book.getId());
+        assertEquals(bookId, book.getId());
         MediaType mt = wc.getResponse().getMediaType();
         assertEquals("application/xml;charset=ISO-8859-1", mt.toString());
         if (checkAnnotations) {
@@ -207,6 +209,17 @@ public class JAXRS20ClientServerBookTest
     }
     
     @Test
+    public void testPostReplaceBook() throws Exception {
+        
+        String endpointAddress = "http://localhost:" + PORT + "/bookstore/books2"; 
+        WebClient wc = WebClient.create(endpointAddress,
+                                        Collections.singletonList(new ReplaceBodyFilter()));
+        wc.accept("text/xml").type("application/xml");
+        Book book = wc.post(new Book("book", 555L), Book.class);
+        assertEquals(561L, book.getId());
+    }
+    
+    @Test
     public void testPostGetCollectionGenericEntityAndType() throws Exception {
         
         String endpointAddress =
@@ -464,6 +477,16 @@ public class JAXRS20ClientServerBookTest
         doTestPostBookAsyncHandler(address);
     }
     
+    private static class ReplaceBodyFilter implements ClientRequestFilter {
+
+        @Override
+        public void filter(ClientRequestContext rc) throws IOException {
+            rc.setEntity(new Book("book", ((Book)rc.getEntity()).getId() + 5), null, null);
+        }
+
+                
+    }
+    
     private static class ClientCacheRequestFilter implements ClientRequestFilter {
 
         @Override



Mime
View raw message