Repository: cxf Updated Branches: refs/heads/master 0d4cd0bbc -> bb4ddb5be
[CXF-6670] Fixing a class cast issue Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/bb4ddb5b Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/bb4ddb5b Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/bb4ddb5b Branch: refs/heads/master Commit: bb4ddb5be95d3ce415bab2aec290ff09a3a572f6 Parents: 0d4cd0b Author: Sergey Beryozkin <sberyoz...@gmail.com> Authored: Fri Nov 6 16:54:49 2015 +0000 Committer: Sergey Beryozkin <sberyoz...@gmail.com> Committed: Fri Nov 6 16:54:49 2015 +0000 ---------------------------------------------------------------------- .../apache/cxf/jaxrs/utils/InjectionUtils.java | 10 ++++- .../apache/cxf/systest/jaxrs/BookServer.java | 39 ++++++++++++++------ .../org/apache/cxf/systest/jaxrs/BookStore.java | 5 ++- .../jaxrs/JAXRSClientServerBookTest.java | 5 ++- 4 files changed, 42 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/bb4ddb5b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java index 3850393..fa42c6f 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java @@ -357,6 +357,7 @@ public final class InjectionUtils { return null; } + @SuppressWarnings("unchecked") public static <T> T handleParameter(String value, boolean decoded, Class<T> pClass, @@ -385,11 +386,16 @@ public final class InjectionUtils { throw createParamConversionException(pType, nfe); } if (result != null) { - return pClass.cast(result); + T theResult = null; + if (pClass.isPrimitive()) { + theResult = (T)result; + } else { + theResult = pClass.cast(result); + } + return theResult; } if (pClass.isPrimitive()) { try { - @SuppressWarnings("unchecked") T ret = (T)PrimitiveUtils.read(value, pClass); // cannot us pClass.cast as the pClass is something like // Boolean.TYPE (representing the boolean primitive) and http://git-wip-us.apache.org/repos/asf/cxf/blob/bb4ddb5b/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 8c7f992..9c1764d 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 @@ -124,7 +124,7 @@ public class BookServer extends AbstractBusTestServerBase { providers.add(new BlockingRequestFilter()); providers.add(new FaultyResponseFilter()); providers.add(new BlockedExceptionMapper()); - providers.add(new XmlBookParamConverter()); + providers.add(new ParamConverterImpl()); sf.setProviders(providers); List<Interceptor<? extends Message>> inInts = new ArrayList<Interceptor<? extends Message>>(); inInts.add(new CustomInFaultyInterceptor()); @@ -270,7 +270,7 @@ public class BookServer extends AbstractBusTestServerBase { } } - public static class XmlBookParamConverter implements ParamConverterProvider { + public static class ParamConverterImpl implements ParamConverterProvider { @Context private Providers providers; @@ -278,20 +278,35 @@ public class BookServer extends AbstractBusTestServerBase { @Override public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { - if (rawType != Book.class) { + if (rawType == Book.class) { + + MessageBodyReader<Book> mbr = providers.getMessageBodyReader(Book.class, + Book.class, + annotations, + MediaType.APPLICATION_XML_TYPE); + MessageBodyWriter<Book> mbw = providers.getMessageBodyWriter(Book.class, + Book.class, + annotations, + MediaType.APPLICATION_XML_TYPE); + return (ParamConverter<T>)new XmlParamConverter(mbr, mbw); + } else if (rawType == byte.class) { + return (ParamConverter<T>)new ByteConverter(); + } else { return null; } - MessageBodyReader<Book> mbr = providers.getMessageBodyReader(Book.class, - Book.class, - annotations, - MediaType.APPLICATION_XML_TYPE); - MessageBodyWriter<Book> mbw = providers.getMessageBodyWriter(Book.class, - Book.class, - annotations, - MediaType.APPLICATION_XML_TYPE); - return (ParamConverter<T>)new XmlParamConverter(mbr, mbw); } + private static class ByteConverter implements ParamConverter<Byte> { + @Override + public Byte fromString(String t) { + return new Byte(t); + } + + @Override + public String toString(Byte b) { + return b.toString(); + } + } private static class XmlParamConverter implements ParamConverter<Book> { private MessageBodyReader<Book> mbr; private MessageBodyWriter<Book> mbw; http://git-wip-us.apache.org/repos/asf/cxf/blob/bb4ddb5b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java ---------------------------------------------------------------------- diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java index 4837784..cde5c7f 100644 --- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java +++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java @@ -306,7 +306,10 @@ public class BookStore { @POST @Path("/echoxmlbookquery") @Produces("application/xml") - public Book echoXmlBookQuery(@QueryParam("book") Book book) { + public Book echoXmlBookQuery(@QueryParam("book") Book book, @QueryParam("id") byte id) { + if (book.getId() != (long)id) { + throw new RuntimeException(); + } return book; } http://git-wip-us.apache.org/repos/asf/cxf/blob/bb4ddb5b/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 2bb461b..33ea415 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 @@ -135,11 +135,12 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase { public void testEchoXmlBookQuery() throws Exception { String address = "http://localhost:" + PORT; BookStore store = JAXRSClientFactory.create(address, BookStore.class, - Collections.singletonList(new BookServer.XmlBookParamConverter())); - Book b = store.echoXmlBookQuery(new Book("query", 125L)); + Collections.singletonList(new BookServer.ParamConverterImpl())); + Book b = store.echoXmlBookQuery(new Book("query", 125L), (byte)125); assertEquals(125L, b.getId()); assertEquals("query", b.getName()); } + @Test public void testGetBookRoot() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/;JSESSIONID=xxx";