You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by co...@apache.org on 2018/09/10 15:13:31 UTC

[cxf] branch master updated: CXF-7332 - JAXRS Client API doesnt support lambda

This is an automated email from the ASF dual-hosted git repository.

coheigea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/master by this push:
     new 84a6fb5  CXF-7332 - JAXRS Client API doesnt support lambda
84a6fb5 is described below

commit 84a6fb5de9a324b5f79b2358cf6f4ed7d562333a
Author: Colm O hEigeartaigh <co...@apache.org>
AuthorDate: Mon Sep 10 16:13:08 2018 +0100

    CXF-7332 - JAXRS Client API doesnt support lambda
---
 .../cxf/jaxrs/client/ClientProviderFactory.java    |  4 ++
 .../systest/jaxrs/JAXRS20ClientServerBookTest.java | 45 ++++++++++++++++++++--
 2 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ClientProviderFactory.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ClientProviderFactory.java
index 8807f99..8100309 100644
--- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ClientProviderFactory.java
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ClientProviderFactory.java
@@ -74,6 +74,10 @@ public final class ClientProviderFactory extends ProviderFactory {
         super.setCommonProviders(theProviders);
         for (ProviderInfo<? extends Object> provider : theProviders) {
             Class<?> providerCls = ClassHelper.getRealClass(getBus(), provider.getProvider());
+            if (providerCls == Object.class) {
+                // If the provider is a lambda, ClassHelper.getRealClass returns Object.class
+                providerCls = provider.getProvider().getClass();
+            }
             if (filterContractSupported(provider, providerCls, ClientRequestFilter.class)) {
                 addProviderToList(clientRequestFilters, provider);
             }
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
index f3b37a8..d2bc099 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
@@ -119,7 +119,7 @@ public class JAXRS20ClientServerBookTest extends AbstractBusClientServerTestBase
         Book book = echoEndpointTarget.request().accept("text/xml").get(Book.class);
         assertEquals(1023L, book.getId());
     }
-    
+
     @Test
     public void testGetGenericBook() throws Exception {
         String address = "http://localhost:" + PORT + "/bookstore/genericbooks/123";
@@ -281,7 +281,7 @@ public class JAXRS20ClientServerBookTest extends AbstractBusClientServerTestBase
                      response.getHeaderString("ServerWriterInterceptorHttpResponse"));
         assertEquals("text/plain;charset=us-ascii", response.getMediaType().toString());
     }
-    
+
     @Test
     public void testPreMatchContainerFilterThrowsIOException() {
         String address = "http://localhost:" + PORT + "/throwExceptionIO";
@@ -720,6 +720,28 @@ public class JAXRS20ClientServerBookTest extends AbstractBusClientServerTestBase
     }
 
     @Test
+    public void testClientFiltersLocalResponseLambdas() {
+        String address = "http://localhost:" + PORT + "/bookstores";
+        List<Object> providers = new ArrayList<>();
+
+        providers.add((ClientRequestFilter) ctx -> {
+            ctx.abortWith(Response.status(201).entity(ctx.getEntity()).type(MediaType.TEXT_XML_TYPE).build());
+        });
+
+        providers.add((ClientResponseFilter) (reqContext, respContext) -> {
+            MultivaluedMap<String, String> headers = respContext.getHeaders();
+            headers.putSingle(HttpHeaders.LOCATION, "http://localhost/redirect");
+        });
+        WebClient wc = WebClient.create(address, providers);
+        Book theBook = new Book("Echo", 123L);
+        Response r = wc.post(theBook);
+        assertEquals(201, r.getStatus());
+        assertEquals("http://localhost/redirect", r.getHeaderString(HttpHeaders.LOCATION));
+        Book responseBook = r.readEntity(Book.class);
+        assertSame(theBook, responseBook);
+    }
+
+    @Test
     public void testPostBook() {
         String address = "http://localhost:" + PORT + "/bookstore/bookheaders/simple";
         WebClient wc = createWebClientPost(address);
@@ -834,7 +856,7 @@ public class JAXRS20ClientServerBookTest extends AbstractBusClientServerTestBase
                     ExceptionUtils.getRootCause(e) instanceof UnknownHostException);
         }
     }
-    
+
     @Test
     public void testGetSetEntityStream() {
         String address = "http://localhost:" + PORT + "/bookstore/entityecho";
@@ -856,6 +878,23 @@ public class JAXRS20ClientServerBookTest extends AbstractBusClientServerTestBase
         assertEquals(entity, response.readEntity(String.class));
     }
 
+    @Test
+    public void testGetSetEntityStreamLambda() {
+        String address = "http://localhost:" + PORT + "/bookstore/entityecho";
+        String entity = "BOOKSTORE";
+
+        Client client = ClientBuilder.newClient();
+        client.register((ClientRequestFilter) context -> {
+            context.setEntityStream(new ReplacingOutputStream(context.getEntityStream(), 'X', 'O'));
+        });
+
+        WebTarget target = client.target(address);
+
+        Response response = target.request().post(
+                Entity.entity(entity.replace('O', 'X'), "text/plain"));
+        assertEquals(entity, response.readEntity(String.class));
+    }
+
     private static class ReplaceBodyFilter implements ClientRequestFilter {
 
         @Override