cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1519399 - in /cxf/branches/2.7.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/
Date Mon, 02 Sep 2013 11:52:50 GMT
Author: sergeyb
Date: Mon Sep  2 11:52:50 2013
New Revision: 1519399

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

........
  r1519395 | sergeyb | 2013-09-02 12:31:04 +0100 (Mon, 02 Sep 2013) | 1 line
  
  [CXF-5247] Support for BeanParam in rs client proxies
........

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/ClientProxyImpl.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/JAXRSClientServerBookTest.java

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

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/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=1519399&r1=1519398&r2=1519399&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
Mon Sep  2 11:52:50 2013
@@ -29,6 +29,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
@@ -37,6 +38,12 @@ import java.util.Map;
 import java.util.ResourceBundle;
 import java.util.logging.Logger;
 
+import javax.ws.rs.CookieParam;
+import javax.ws.rs.FormParam;
+import javax.ws.rs.HeaderParam;
+import javax.ws.rs.MatrixParam;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.QueryParam;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.client.ClientException;
 import javax.ws.rs.container.AsyncResponse;
@@ -149,7 +156,9 @@ public class ClientProxyImpl extends Abs
         }
         
         MultivaluedMap<ParameterType, Parameter> types = getParametersInfo(params,
ori);
-        List<Object> pathParams = getPathParamValues(types, params, ori);
+        List<Parameter> beanParamsList =  getParameters(types, ParameterType.BEAN);
+        
+        List<Object> pathParams = getPathParamValues(types, beanParamsList, params,
ori);
         
         int bodyIndex = getBodyIndex(types, ori);
         
@@ -159,15 +168,15 @@ public class ClientProxyImpl extends Abs
         }
         addNonEmptyPath(builder, ori.getURITemplate().getValue());
         
-        handleMatrixes(types, params, builder);
-        handleQueries(types, params, builder);
+        handleMatrixes(types, beanParamsList, params, builder);
+        handleQueries(types, beanParamsList, params, builder);
         
         URI uri = builder.buildFromEncoded(pathParams.toArray()).normalize();
         
         MultivaluedMap<String, String> headers = getHeaders();
         MultivaluedMap<String, String> paramHeaders = new MetadataMap<String, String>();
-        handleHeaders(paramHeaders, types, params);
-        handleCookies(paramHeaders, types, params);
+        handleHeaders(paramHeaders, beanParamsList, types, params);
+        handleCookies(paramHeaders, beanParamsList, types, params);
                 
         if (ori.isSubResourceLocator()) {
             ClassResourceInfo subCri = cri.getSubResource(m.getReturnType(), m.getReturnType());
@@ -198,7 +207,7 @@ public class ClientProxyImpl extends Abs
         if (bodyIndex != -1) {
             body = params[bodyIndex];
         } else if (types.containsKey(ParameterType.FORM))  {
-            body = handleForm(types, params);
+            body = handleForm(types, beanParamsList, params);
         } else if (types.containsKey(ParameterType.REQUEST_BODY))  {
             body = handleMultipart(types, ori, params);
         }
@@ -370,6 +379,7 @@ public class ClientProxyImpl extends Abs
     }
     
     private List<Object> getPathParamValues(MultivaluedMap<ParameterType, Parameter>
map,
+                                            List<Parameter> beanParams,   
                                             Object[] params,
                                             OperationResourceInfo ori) {
         List<Object> list = new LinkedList<Object>();
@@ -392,10 +402,17 @@ public class ClientProxyImpl extends Abs
             }
         }
         
+        Map<String, Object> beanParamValues = new HashMap<String, Object>(beanParams.size());
+        for (Parameter p : beanParams) {
+            beanParamValues.putAll(getValuesFromBeanParam(params[p.getIndex()], PathParam.class));
+        }
+        
         for (String varName : methodVars) {
             Parameter p = paramsMap.remove(varName);
             if (p != null) {
                 list.add(convertParamValue(params[p.getIndex()]));
+            } else if (beanParamValues.containsKey(varName)) {
+                list.add(convertParamValue(beanParamValues.get(varName)));
             }
         }
         
@@ -421,18 +438,47 @@ public class ClientProxyImpl extends Abs
         return  map.get(key) == null ? Collections.EMPTY_LIST : map.get(key);
     }
     
-    private void handleQueries(MultivaluedMap<ParameterType, Parameter> map, 
-                                      Object[] params,
-                                      UriBuilder ub) {
+    private void handleQueries(MultivaluedMap<ParameterType, Parameter> map,
+                               List<Parameter> beanParams,
+                               Object[] params,
+                               UriBuilder ub) {
         List<Parameter> qs = getParameters(map, ParameterType.QUERY);
         for (Parameter p : qs) {
             if (params[p.getIndex()] != null) {
                 addMatrixQueryParamsToBuilder(ub, p.getName(), ParameterType.QUERY, params[p.getIndex()]);
             }
         }
+        for (Parameter p : beanParams) {
+            Map<String, Object> values = getValuesFromBeanParam(params[p.getIndex()],
QueryParam.class);
+            for (Map.Entry<String, Object> entry : values.entrySet()) {
+                if (entry.getValue() != null) {
+                    addMatrixQueryParamsToBuilder(ub, entry.getKey(), ParameterType.QUERY,
entry.getValue());
+                }
+            }
+        }
     }
     
-    private void handleMatrixes(MultivaluedMap<ParameterType, Parameter> map, Object[]
params,
+    private Map<String, Object> getValuesFromBeanParam(Object bean, Class<? extends
Annotation> annClass) {
+        Map<String, Object> values = new HashMap<String, Object>();
+        
+        for (Method m : bean.getClass().getMethods()) {
+            if (m.getAnnotation(annClass) != null) {
+                try {
+                    String propertyName = m.getName().substring(3);
+                    Method getter = bean.getClass().getMethod("get" + propertyName, new Class[]{});
+                    Object value = getter.invoke(bean, new Object[]{});
+                    values.put(propertyName.toLowerCase(), value);
+                } catch (Throwable t) {
+                    // ignore
+                }
+            }
+        }
+        return values;
+    }
+    
+    private void handleMatrixes(MultivaluedMap<ParameterType, Parameter> map,
+                                List<Parameter> beanParams,
+                                Object[] params,
                                 UriBuilder ub) {
         List<Parameter> mx = getParameters(map, ParameterType.MATRIX);
         for (Parameter p : mx) {
@@ -440,34 +486,54 @@ public class ClientProxyImpl extends Abs
                 addMatrixQueryParamsToBuilder(ub, p.getName(), ParameterType.MATRIX, params[p.getIndex()]);
             }
         }
+        for (Parameter p : beanParams) {
+            Map<String, Object> values = getValuesFromBeanParam(params[p.getIndex()],
MatrixParam.class);
+            for (Map.Entry<String, Object> entry : values.entrySet()) {
+                if (entry.getValue() != null) {
+                    addMatrixQueryParamsToBuilder(ub, entry.getKey(), ParameterType.MATRIX,
entry.getValue());
+                }
+            }
+        }
     }
 
     private MultivaluedMap<String, String> handleForm(MultivaluedMap<ParameterType,
Parameter> map, 
+                                                      List<Parameter> beanParams,
                                                       Object[] params) {
         
         MultivaluedMap<String, String> form = new MetadataMap<String, String>();
         
         List<Parameter> fm = getParameters(map, ParameterType.FORM);
         for (Parameter p : fm) {
-            Object pValue = params[p.getIndex()];
-            if (pValue != null) {
-                if (InjectionUtils.isSupportedCollectionOrArray(pValue.getClass())) {
-                    Collection<?> c = pValue.getClass().isArray() 
-                        ? Arrays.asList((Object[]) pValue) : (Collection<?>) pValue;
-                    for (Iterator<?> it = c.iterator(); it.hasNext();) {
-                        FormUtils.addPropertyToForm(form, p.getName(), convertParamValue(it.next()));
-                    }
-                } else { 
-                    String name = p.getName();
-                    FormUtils.addPropertyToForm(form, name, name.isEmpty() ? pValue : convertParamValue(pValue));

-                }
-                
+            addFormValue(form, p.getName(), params[p.getIndex()]);
+        }
+        for (Parameter p : beanParams) {
+            Map<String, Object> values = getValuesFromBeanParam(params[p.getIndex()],
FormParam.class);
+            for (Map.Entry<String, Object> entry : values.entrySet()) {
+                addFormValue(form, entry.getKey(), entry.getValue());
             }
         }
         
         return form;
     }
     
+    private void addFormValue(MultivaluedMap<String, String> form, String name, Object
pValue) {
+        if (pValue != null) {
+            if (InjectionUtils.isSupportedCollectionOrArray(pValue.getClass())) {
+                Collection<?> c = pValue.getClass().isArray() 
+                    ? Arrays.asList((Object[]) pValue) : (Collection<?>) pValue;
+                for (Iterator<?> it = c.iterator(); it.hasNext();) {
+                    FormUtils.addPropertyToForm(form, name, convertParamValue(it.next()));
+                }
+            } else { 
+                FormUtils.addPropertyToForm(form, name, name.isEmpty() ? pValue : convertParamValue(pValue));

+            }
+            
+        }
+        
+    }
+    
+    
+    
     private List<Attachment> handleMultipart(MultivaluedMap<ParameterType, Parameter>
map,
                                              OperationResourceInfo ori,
                                              Object[] params) {
@@ -484,13 +550,23 @@ public class ClientProxyImpl extends Abs
     }
     
     private void handleHeaders(MultivaluedMap<String, String> headers,
-                               MultivaluedMap<ParameterType, Parameter> map, Object[]
params) {
+                               List<Parameter> beanParams,
+                               MultivaluedMap<ParameterType, Parameter> map, 
+                               Object[] params) {
         List<Parameter> hs = getParameters(map, ParameterType.HEADER);
         for (Parameter p : hs) {
             if (params[p.getIndex()] != null) {
                 headers.add(p.getName(), convertParamValue(params[p.getIndex()]));
             }
         }
+        for (Parameter p : beanParams) {
+            Map<String, Object> values = getValuesFromBeanParam(params[p.getIndex()],
HeaderParam.class);
+            for (Map.Entry<String, Object> entry : values.entrySet()) {
+                if (entry.getValue() != null) {
+                    headers.add(entry.getKey(), convertParamValue(entry.getValue()));
+                }
+            }
+        }
     }
     
     private static Multipart getMultipart(OperationResourceInfo ori, int index) {
@@ -500,7 +576,9 @@ public class ClientProxyImpl extends Abs
     }
     
     private void handleCookies(MultivaluedMap<String, String> headers,
-                               MultivaluedMap<ParameterType, Parameter> map, Object[]
params) {
+                               List<Parameter> beanParams,
+                               MultivaluedMap<ParameterType, Parameter> map, 
+                               Object[] params) {
         List<Parameter> cs = getParameters(map, ParameterType.COOKIE);
         for (Parameter p : cs) {
             if (params[p.getIndex()] != null) {
@@ -509,6 +587,15 @@ public class ClientProxyImpl extends Abs
                             + convertParamValue(params[p.getIndex()].toString()));
             }
         }
+        for (Parameter p : beanParams) {
+            Map<String, Object> values = getValuesFromBeanParam(params[p.getIndex()],
CookieParam.class);
+            for (Map.Entry<String, Object> entry : values.entrySet()) {
+                if (entry.getValue() != null) {
+                    headers.add(HttpHeaders.COOKIE, 
+                                entry.getKey() + "=" +  convertParamValue(entry.getValue()));
+                }
+            }
+        }
     }
     
     private Object doChainedInvocation(URI uri, 

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=1519399&r1=1519398&r2=1519399&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
Mon Sep  2 11:52:50 2013
@@ -235,11 +235,11 @@ public class BookStore {
     }
     
     @GET
-    @Path("/beanparam")
+    @Path("/beanparam/{id}")
     @Produces("application/xml")
     public Book getBeanParamBook(@BeanParam BookBean bean) {
         
-        long id = bean.getId() + bean.getId1(); 
+        long id = bean.getId() + bean.getId2() + bean.getId3(); 
         
         return books.get(id);
     }
@@ -251,7 +251,7 @@ public class BookStore {
     }
     
     @GET
-    @Path("/beanparam2")
+    @Path("/beanparam2/{id}")
     @Produces("application/xml")
     public Book getBeanParamBook2() {
         return getBeanParamBook(theBookBean);
@@ -1470,28 +1470,40 @@ public class BookStore {
     
     public static class BookBean {
         private long id;
-        private long id1;
+        private long id2;
+        private long id3;
 
         public long getId() {
             return id;
         }
 
-        @QueryParam("id")
+        @PathParam("id")
         public void setId(long id) {
             this.id = id;
         }
         
+        public long getId2() {
+            return id2;
+        }
+
+        @QueryParam("id2")
+        public void setId2(long id2) {
+            this.id2 = id2;
+        }
+        
         @Context
         public void setUriInfo(UriInfo ui) {
-            String id1Value = ui.getQueryParameters().getFirst("id1");
-            if (id1Value != null) {
-                this.id1 = Long.valueOf(id1Value);
+            String id3Value = ui.getQueryParameters().getFirst("id3");
+            if (id3Value != null) {
+                this.id3 = Long.valueOf(id3Value);
             }
         }
 
-        public long getId1() {
-            return id1;
+        public long getId3() {
+            return id3;
         }
+
+        
     }
     
     public static class BookNotReturnedException extends RuntimeException {

Modified: cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=1519399&r1=1519398&r2=1519399&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++ cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Mon Sep  2 11:52:50 2013
@@ -239,8 +239,9 @@ public class JAXRSClientServerBookTest e
     
     private void doTestUseParamBeanWebClient(String address) {
         WebClient wc = WebClient.create(address);
-        wc.query("id", "120");
-        wc.query("id1", "3");
+        wc.path("100");
+        wc.query("id2", "20");
+        wc.query("id3", "3");
         Book book = wc.get(Book.class);
         assertEquals(123L, book.getId());
     }
@@ -304,6 +305,18 @@ public class JAXRSClientServerBookTest e
     }
     
     @Test
+    public void testProxyBeanParam() throws Exception {
+        BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class);
+        BookStore.BookBean bean = new BookStore.BookBean();
+        bean.setId(100L);
+        bean.setId2(23L);
+                
+        Book book = store.getBeanParamBook(bean);
+        assertEquals(123L, book.getId());
+        
+    }
+    
+    @Test
     public void testGetBookWithCustomHeader() throws Exception {
         
         String endpointAddress =



Mime
View raw message