Repository: cxf Updated Branches: refs/heads/master 5e5a4ed20 -> 955d69b34
[CXF-6688] Updating ClientProxyImpl to do the best effort at mapping BeanParam bean's PathParam values to class-level template vars Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/8eea5c1a Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/8eea5c1a Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/8eea5c1a Branch: refs/heads/master Commit: 8eea5c1abb25ec98a6c1f4c643d0e5318aee7bb4 Parents: bfe6427 Author: Sergey Beryozkin <[email protected]> Authored: Fri Nov 20 14:40:21 2015 +0000 Committer: Sergey Beryozkin <[email protected]> Committed: Fri Nov 20 14:43:47 2015 +0000 ---------------------------------------------------------------------- .../cxf/jaxrs/client/ClientProxyImpl.java | 28 +++++++++++++++----- .../cxf/systest/jaxrs/BookStoreSimple.java | 22 +++++++++++++++ ...ientServerResourceCreatedSpringBookTest.java | 19 +++++++++++++ 3 files changed, 63 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/8eea5c1a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java ---------------------------------------------------------------------- diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java index d41721f..37b798f 100644 --- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java +++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java @@ -398,12 +398,32 @@ public class ClientProxyImpl extends AbstractClient implements OperationResourceInfo ori, int bodyIndex) { List<Object> list = new LinkedList<Object>(); + + List<String> methodVars = ori.getURITemplate().getVariables(); + List<Parameter> paramsList = getParameters(map, ParameterType.PATH); + Map<String, BeanPair> beanParamValues = new HashMap<String, BeanPair>(beanParams.size()); + for (Parameter p : beanParams) { + beanParamValues.putAll(getValuesFromBeanParam(params[p.getIndex()], PathParam.class)); + } + if (!beanParamValues.isEmpty() && !methodVars.containsAll(beanParamValues.keySet())) { + List<String> classVars = ori.getClassResourceInfo().getURITemplate().getVariables(); + for (String classVar : classVars) { + BeanPair pair = beanParamValues.get(classVar); + if (pair != null) { + Object paramValue = convertParamValue(pair.getValue(), pair.getAnns()); + if (isRoot) { + valuesMap.put(classVar, paramValue); + } else { + list.add(paramValue); + } + } + } + } if (isRoot) { list.addAll(valuesMap.values()); } - List<String> methodVars = ori.getURITemplate().getVariables(); - List<Parameter> paramsList = getParameters(map, ParameterType.PATH); + Map<String, Parameter> paramsMap = new LinkedHashMap<String, Parameter>(); for (Parameter p : paramsList) { if (p.getName().length() == 0) { @@ -417,10 +437,6 @@ public class ClientProxyImpl extends AbstractClient implements } } - Map<String, BeanPair> beanParamValues = new HashMap<String, BeanPair>(beanParams.size()); - for (Parameter p : beanParams) { - beanParamValues.putAll(getValuesFromBeanParam(params[p.getIndex()], PathParam.class)); - } Object requestBody = bodyIndex == -1 ? null : params[bodyIndex]; for (String varName : methodVars) { Parameter p = paramsMap.remove(varName); http://git-wip-us.apache.org/repos/asf/cxf/blob/8eea5c1a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSimple.java ---------------------------------------------------------------------- diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSimple.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSimple.java index a046a42..46ee30c 100644 --- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSimple.java +++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreSimple.java @@ -20,12 +20,29 @@ package org.apache.cxf.systest.jaxrs; import javax.annotation.PostConstruct; import javax.annotation.Resource; +import javax.ws.rs.BeanParam; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; @Path("/simplebooks/{id}") public class BookStoreSimple { + public static class BookBean { + private long id; + public BookBean() { + + } + public BookBean(long id) { + this.id = id; + } + public long getId() { + return id; + } + @PathParam("id") + public void setId(long id) { + this.id = id; + } + } @Resource private Book injectedBook; @@ -46,4 +63,9 @@ public class BookStoreSimple { throw new IllegalStateException("Book resource has not been injected"); } } + @GET + @Path("/beanparam") + public Book getBookBeanParam(@BeanParam BookBean bookBean) { + return getBook(bookBean.getId()); + } } http://git-wip-us.apache.org/repos/asf/cxf/blob/8eea5c1a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringBookTest.java ---------------------------------------------------------------------- diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringBookTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringBookTest.java index 58736ef..efa661f 100644 --- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringBookTest.java +++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerResourceCreatedSpringBookTest.java @@ -25,6 +25,7 @@ import java.net.URLConnection; 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.jaxrs.model.AbstractResourceInfo; import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase; @@ -67,6 +68,24 @@ public class JAXRSClientServerResourceCreatedSpringBookTest extends AbstractBusC } @Test + public void testGetBookSimpleProxy() throws Exception { + + String address = "http://localhost:" + PORT + "/webapp/rest"; + BookStoreSimple bookStore = JAXRSClientFactory.create(address, BookStoreSimple.class); + Book book = bookStore.getBook(444L); + assertEquals(444L, book.getId()); + } + + @Test + public void testGetBookSimpleBeanParamProxy() throws Exception { + + String address = "http://localhost:" + PORT + "/webapp/rest"; + BookStoreSimple bookStore = JAXRSClientFactory.create(address, BookStoreSimple.class); + Book book = bookStore.getBookBeanParam(new BookStoreSimple.BookBean(444)); + assertEquals(444L, book.getId()); + } + + @Test public void testGetBook123() throws Exception { String endpointAddress =
