Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 6E002200B9B for ; Wed, 28 Sep 2016 00:33:31 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 6C89C160AE5; Tue, 27 Sep 2016 22:33:31 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 3CC2D160AD2 for ; Wed, 28 Sep 2016 00:33:30 +0200 (CEST) Received: (qmail 12692 invoked by uid 500); 27 Sep 2016 22:33:29 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 12682 invoked by uid 99); 27 Sep 2016 22:33:29 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 27 Sep 2016 22:33:29 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 3D6A5DFD9F; Tue, 27 Sep 2016 22:33:29 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sergeyb@apache.org To: commits@cxf.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: cxf git commit: [CXF-6881] Prototyping the boilerplate CompletionStage invoker code Date: Tue, 27 Sep 2016 22:33:29 +0000 (UTC) archived-at: Tue, 27 Sep 2016 22:33:31 -0000 Repository: cxf Updated Branches: refs/heads/master 8595d5ce7 -> 26f430253 [CXF-6881] Prototyping the boilerplate CompletionStage invoker code Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/26f43025 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/26f43025 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/26f43025 Branch: refs/heads/master Commit: 26f43025348e101e981e3f64ecc3ddf483ad7fb3 Parents: 8595d5c Author: Sergey Beryozkin Authored: Tue Sep 27 23:33:07 2016 +0100 Committer: Sergey Beryozkin Committed: Tue Sep 27 23:33:07 2016 +0100 ---------------------------------------------------------------------- .../cxf/jaxrs/client/JaxrsClientCallback.java | 233 ++++++++++++++++++- .../org/apache/cxf/jaxrs/client/WebClient.java | 174 ++++++++++++++ .../client/spec/InvocationBuilderImpl.java | 3 +- .../cxf/systest/jaxrs/JAXRSAsyncClientTest.java | 24 ++ 4 files changed, 429 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/26f43025/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JaxrsClientCallback.java ---------------------------------------------------------------------- diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JaxrsClientCallback.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JaxrsClientCallback.java index 390ab23..a1e7049 100644 --- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JaxrsClientCallback.java +++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/JaxrsClientCallback.java @@ -22,10 +22,17 @@ package org.apache.cxf.jaxrs.client; import java.lang.reflect.Type; import java.util.Map; import java.util.concurrent.CancellationException; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import java.util.function.BiConsumer; +import java.util.function.BiFunction; +import java.util.function.Consumer; +import java.util.function.Function; import javax.ws.rs.client.InvocationCallback; @@ -65,7 +72,11 @@ class JaxrsClientCallback extends ClientCallback { } public Future createFuture() { - return new JaxrsResponseCallback(this); + return new JaxrsResponseFuture(this); + } + + public CompletionStage createCompletionStage() { + return null; } @@ -97,9 +108,9 @@ class JaxrsClientCallback extends ClientCallback { - static class JaxrsResponseCallback implements Future { + static class JaxrsResponseFuture implements Future { JaxrsClientCallback callback; - JaxrsResponseCallback(JaxrsClientCallback cb) { + JaxrsResponseFuture(JaxrsClientCallback cb) { callback = cb; } @@ -148,4 +159,220 @@ class JaxrsClientCallback extends ClientCallback { return callback.isDone(); } } + static class JaxrsResponseStage implements CompletionStage { + JaxrsClientCallback callback; + JaxrsResponseStage(JaxrsClientCallback cb) { + callback = cb; + } + @Override + public CompletionStage thenApply(Function fn) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage thenApplyAsync(Function fn) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage thenApplyAsync(Function fn, Executor executor) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage thenAccept(Consumer action) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage thenAcceptAsync(Consumer action) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage thenAcceptAsync(Consumer action, Executor executor) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage thenRun(Runnable action) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage thenRunAsync(Runnable action) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage thenRunAsync(Runnable action, Executor executor) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage thenCombine(CompletionStage other, + BiFunction fn) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage thenCombineAsync(CompletionStage other, + BiFunction fn) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage thenCombineAsync(CompletionStage other, + BiFunction fn, + Executor executor) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage thenAcceptBoth(CompletionStage other, + BiConsumer action) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage thenAcceptBothAsync(CompletionStage other, + BiConsumer action) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage thenAcceptBothAsync(CompletionStage other, + BiConsumer action, + Executor executor) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage runAfterBoth(CompletionStage other, Runnable action) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage runAfterBothAsync(CompletionStage other, Runnable action) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage runAfterBothAsync(CompletionStage other, Runnable action, + Executor executor) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage applyToEither(CompletionStage other, + Function fn) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage applyToEitherAsync(CompletionStage other, + Function fn) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage applyToEitherAsync(CompletionStage other, + Function fn, Executor executor) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage acceptEither(CompletionStage other, + Consumer action) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage acceptEitherAsync(CompletionStage other, + Consumer action) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage acceptEitherAsync(CompletionStage other, + Consumer action, Executor executor) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage runAfterEither(CompletionStage other, Runnable action) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage runAfterEitherAsync(CompletionStage other, Runnable action) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage runAfterEitherAsync(CompletionStage other, Runnable action, + Executor executor) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage thenCompose(Function> fn) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage thenComposeAsync(Function> fn) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage thenComposeAsync(Function> fn, + Executor executor) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage exceptionally(Function fn) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage whenComplete(BiConsumer action) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage whenCompleteAsync(BiConsumer action) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage whenCompleteAsync(BiConsumer action, + Executor executor) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage handle(BiFunction fn) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage handleAsync(BiFunction fn) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletionStage handleAsync(BiFunction fn, + Executor executor) { + // TODO Auto-generated method stub + return null; + } + @Override + public CompletableFuture toCompletableFuture() { + // TODO Auto-generated method stub + return null; + } + + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cxf/blob/26f43025/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java ---------------------------------------------------------------------- diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java index 892ce20..356edfe 100644 --- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java +++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java @@ -28,12 +28,14 @@ import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Map; +import java.util.concurrent.CompletionStage; import java.util.concurrent.Future; import javax.ws.rs.HttpMethod; import javax.ws.rs.ProcessingException; import javax.ws.rs.WebApplicationException; import javax.ws.rs.client.AsyncInvoker; +import javax.ws.rs.client.CompletionStageRxInvoker; import javax.ws.rs.client.Entity; import javax.ws.rs.client.InvocationCallback; import javax.ws.rs.client.SyncInvoker; @@ -946,6 +948,44 @@ public class WebClient extends AbstractClient { return cb.createFuture(); } + + protected CompletionStage doInvokeAsyncStage(String httpMethod, + Object body, + Class requestClass, + Type inType, + Class respClass, + Type outType) { + Annotation[] inAnns = null; + if (body instanceof Entity) { + Entity entity = (Entity)body; + setEntityHeaders(entity); + body = entity.getEntity(); + requestClass = body.getClass(); + inType = body.getClass(); + inAnns = entity.getAnnotations(); + } + + if (body instanceof GenericEntity) { + GenericEntity genericEntity = (GenericEntity)body; + body = genericEntity.getEntity(); + requestClass = genericEntity.getRawType(); + inType = genericEntity.getType(); + } + + MultivaluedMap headers = prepareHeaders(respClass, body); + resetResponse(); + + Message m = finalizeMessage(httpMethod, headers, body, requestClass, inType, + inAnns, respClass, outType, null, null); + + m.getExchange().setSynchronous(false); + JaxrsClientCallback cb = new JaxrsClientCallback(null, respClass, outType); + m.getExchange().put(JaxrsClientCallback.class, cb); + + doRunInterceptorChain(m); + + return cb.createCompletionStage(); + } private MultivaluedMap prepareHeaders(Class responseClass, Object body) { @@ -1244,6 +1284,11 @@ public class WebClient extends AbstractClient { return new SyncInvokerImpl(); } + // Link to JAX-RS 2.1 CompletionStageRxInvoker + public CompletionStageRxInvoker rx() { + return new CompletionStageRxInvokerImpl(); + } + private void setEntityHeaders(Entity entity) { type(entity.getMediaType()); if (entity.getLanguage() != null) { @@ -1567,4 +1612,133 @@ public class WebClient extends AbstractClient { return invoke(method, entity, genericType); } } + + class CompletionStageRxInvokerImpl implements CompletionStageRxInvoker { + + @Override + public CompletionStage get() { + return get(Response.class); + } + + @Override + public CompletionStage get(Class responseType) { + return method(HttpMethod.GET, responseType); + } + + @Override + public CompletionStage get(GenericType responseType) { + return method(HttpMethod.GET, responseType); + } + + @Override + public CompletionStage put(Entity entity) { + return put(entity, Response.class); + } + + @Override + public CompletionStage put(Entity entity, Class responseType) { + return method(HttpMethod.PUT, entity, responseType); + } + + @Override + public CompletionStage put(Entity entity, GenericType responseType) { + return method(HttpMethod.PUT, entity, responseType); + } + + @Override + public CompletionStage post(Entity entity) { + return post(entity, Response.class); + } + + @Override + public CompletionStage post(Entity entity, Class responseType) { + return method(HttpMethod.POST, entity, responseType); + } + + @Override + public CompletionStage post(Entity entity, GenericType responseType) { + return method(HttpMethod.POST, entity, responseType); + } + + @Override + public CompletionStage delete() { + return delete(Response.class); + } + + @Override + public CompletionStage delete(Class responseType) { + return method(HttpMethod.DELETE, responseType); + } + + @Override + public CompletionStage delete(GenericType responseType) { + return method(HttpMethod.DELETE, responseType); + } + + @Override + public CompletionStage head() { + return method(HttpMethod.HEAD); + } + + @Override + public CompletionStage options() { + return options(Response.class); + } + + @Override + public CompletionStage options(Class responseType) { + return method(HttpMethod.OPTIONS, responseType); + } + + @Override + public CompletionStage options(GenericType responseType) { + return method(HttpMethod.OPTIONS, responseType); + } + + @Override + public CompletionStage trace() { + return trace(Response.class); + } + + @Override + public CompletionStage trace(Class responseType) { + return method("TRACE", responseType); + } + + @Override + public CompletionStage trace(GenericType responseType) { + return method("TRACE", responseType); + } + + @Override + public CompletionStage method(String name) { + return method(name, Response.class); + } + + @Override + public CompletionStage method(String name, Entity entity) { + return method(name, entity, Response.class); + } + + @Override + public CompletionStage method(String name, Entity entity, Class responseType) { + return doInvokeAsyncStage(name, entity, null, null, responseType, responseType); + } + + @Override + public CompletionStage method(String name, Entity entity, GenericType responseType) { + return doInvokeAsyncStage(name, entity, null, null, responseType.getRawType(), responseType.getType()); + } + + @Override + public CompletionStage method(String name, Class responseType) { + return doInvokeAsyncStage(name, null, null, null, responseType, responseType); + } + + @Override + public CompletionStage method(String name, GenericType responseType) { + return doInvokeAsyncStage(name, null, null, null, responseType.getRawType(), responseType.getType()); + } + + } } http://git-wip-us.apache.org/repos/asf/cxf/blob/26f43025/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/InvocationBuilderImpl.java ---------------------------------------------------------------------- diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/InvocationBuilderImpl.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/InvocationBuilderImpl.java index 10f5d9b..899f1a8 100644 --- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/InvocationBuilderImpl.java +++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/InvocationBuilderImpl.java @@ -377,8 +377,7 @@ public class InvocationBuilderImpl implements Invocation.Builder { @Override public CompletionStageRxInvoker rx() { - // TODO: Implementation required (JAX-RS 2.1) - return null; + return webClient.rx(); } @Override http://git-wip-us.apache.org/repos/asf/cxf/blob/26f43025/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSAsyncClientTest.java ---------------------------------------------------------------------- diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSAsyncClientTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSAsyncClientTest.java index 8713229..8f14c8c 100644 --- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSAsyncClientTest.java +++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSAsyncClientTest.java @@ -27,8 +27,10 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.CompletionStage; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import java.util.function.Consumer; import javax.ws.rs.Consumes; import javax.ws.rs.NotFoundException; @@ -59,6 +61,7 @@ import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase; import org.junit.Before; import org.junit.BeforeClass; +import org.junit.Ignore; import org.junit.Test; public class JAXRSAsyncClientTest extends AbstractBusClientServerTestBase { @@ -264,6 +267,7 @@ public class JAXRSAsyncClientTest extends AbstractBusClientServerTestBase { assertTrue(((GenericInvocationCallback)callback).getResult().readEntity(Boolean.class)); } + @Test public void testAsyncProxyPrimitiveResponse() throws Exception { String address = "http://localhost:" + PORT; @@ -365,6 +369,26 @@ public class JAXRSAsyncClientTest extends AbstractBusClientServerTestBase { } + @Test + @Ignore + public void testGetBookAsyncStage() throws Exception { + String address = "http://localhost:" + PORT + "/bookstore/books"; + WebClient wc = createWebClient(address); + CompletionStage stage = wc.path("123").rx().get(Book.class); + Holder h = new Holder(); + Consumer action = new Consumer() { + + @Override + public void accept(Book t) { + h.value = t; + + } + + }; + stage.thenAccept(action); + assertEquals(123L, h.value.getId()); + } + private WebClient createWebClient(String address) { List providers = new ArrayList(); return WebClient.create(address, providers);