Skip to content

Commit 983d4a8

Browse files
committed
Enable to use AsyncInvoker in Rx client
Signed-off-by: Jan Supol <jan.supol@oracle.com>
1 parent 584664c commit 983d4a8

File tree

8 files changed

+407
-352
lines changed

8 files changed

+407
-352
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.client;
18+
19+
import javax.ws.rs.client.Entity;
20+
import javax.ws.rs.core.GenericType;
21+
22+
/* package */ abstract class AbstractNonSyncInvoker<T> {
23+
24+
public T get() {
25+
return method("GET");
26+
}
27+
28+
public <R> T get(final Class<R> responseType) {
29+
return method("GET", responseType);
30+
}
31+
32+
public <R> T get(final GenericType<R> responseType) {
33+
return method("GET", responseType);
34+
}
35+
36+
public T put(final Entity<?> entity) {
37+
return method("PUT", entity);
38+
}
39+
40+
public <R> T put(final Entity<?> entity, final Class<R> clazz) {
41+
return method("PUT", entity, clazz);
42+
}
43+
44+
public <R> T put(final Entity<?> entity, final GenericType<R> type) {
45+
return method("PUT", entity, type);
46+
}
47+
48+
public T post(final Entity<?> entity) {
49+
return method("POST", entity);
50+
}
51+
52+
public <R> T post(final Entity<?> entity, final Class<R> clazz) {
53+
return method("POST", entity, clazz);
54+
}
55+
56+
public <R> T post(final Entity<?> entity, final GenericType<R> type) {
57+
return method("POST", entity, type);
58+
}
59+
60+
public T delete() {
61+
return method("DELETE");
62+
}
63+
64+
public <R> T delete(final Class<R> responseType) {
65+
return method("DELETE", responseType);
66+
}
67+
68+
public <R> T delete(final GenericType<R> responseType) {
69+
return method("DELETE", responseType);
70+
}
71+
72+
public T head() {
73+
return method("HEAD");
74+
}
75+
76+
public T options() {
77+
return method("OPTIONS");
78+
}
79+
80+
public <R> T options(final Class<R> responseType) {
81+
return method("OPTIONS", responseType);
82+
}
83+
84+
public <R> T options(final GenericType<R> responseType) {
85+
return method("OPTIONS", responseType);
86+
}
87+
88+
public T trace() {
89+
return method("TRACE");
90+
}
91+
92+
public <R> T trace(final Class<R> responseType) {
93+
return method("TRACE", responseType);
94+
}
95+
96+
public <R> T trace(final GenericType<R> responseType) {
97+
return method("TRACE", responseType);
98+
}
99+
100+
public abstract T method(final String name);
101+
102+
public abstract <R> T method(final String name, final Class<R> responseType);
103+
104+
public abstract <R> T method(final String name, final GenericType<R> responseType);
105+
106+
public abstract T method(final String name, final Entity<?> entity);
107+
108+
public abstract <R> T method(final String name, final Entity<?> entity, final Class<R> responseType);
109+
110+
public abstract <R> T method(final String name, final Entity<?> entity, final GenericType<R> responseType);
111+
}
Lines changed: 8 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -16,6 +16,8 @@
1616

1717
package org.glassfish.jersey.client;
1818

19+
import org.glassfish.jersey.client.internal.LocalizationMessages;
20+
1921
import java.util.concurrent.ExecutorService;
2022

2123
import javax.ws.rs.client.Entity;
@@ -25,7 +27,7 @@
2527
import javax.ws.rs.core.Response;
2628

2729
/**
28-
* Default implementation of {@link javax.ws.rs.client.rx.RxInvoker reactive invoker}. Extensions of this class are
30+
* Default implementation of {@link javax.ws.rs.client.RxInvoker reactive invoker}. Extensions of this class are
2931
* supposed to implement {@link #method(String, Entity, Class)} and
3032
* {@link #method(String, Entity, GenericType)} methods to which implementations of the rest
3133
* of the methods from the contract delegate to.
@@ -35,16 +37,15 @@
3537
* @author Michal Gajdos
3638
* @since 2.26
3739
*/
38-
public abstract class AbstractRxInvoker<T> implements RxInvoker<T> {
40+
public abstract class AbstractRxInvoker<T> extends AbstractNonSyncInvoker<T> implements RxInvoker<T> {
3941

40-
private final SyncInvoker syncInvoker;
4142
private final ExecutorService executorService;
43+
private final SyncInvoker syncInvoker;
4244

4345
public AbstractRxInvoker(final SyncInvoker syncInvoker, final ExecutorService executor) {
4446
if (syncInvoker == null) {
45-
throw new IllegalArgumentException("Invocation builder cannot be null.");
47+
throw new IllegalArgumentException(LocalizationMessages.NULL_INVOCATION_BUILDER());
4648
}
47-
4849
this.syncInvoker = syncInvoker;
4950
this.executorService = executor;
5051
}
@@ -67,101 +68,6 @@ protected ExecutorService getExecutorService() {
6768
return executorService;
6869
}
6970

70-
@Override
71-
public T get() {
72-
return method("GET");
73-
}
74-
75-
@Override
76-
public <R> T get(final Class<R> responseType) {
77-
return method("GET", responseType);
78-
}
79-
80-
@Override
81-
public <R> T get(final GenericType<R> responseType) {
82-
return method("GET", responseType);
83-
}
84-
85-
@Override
86-
public T put(final Entity<?> entity) {
87-
return method("PUT", entity);
88-
}
89-
90-
@Override
91-
public <R> T put(final Entity<?> entity, final Class<R> clazz) {
92-
return method("PUT", entity, clazz);
93-
}
94-
95-
@Override
96-
public <R> T put(final Entity<?> entity, final GenericType<R> type) {
97-
return method("PUT", entity, type);
98-
}
99-
100-
@Override
101-
public T post(final Entity<?> entity) {
102-
return method("POST", entity);
103-
}
104-
105-
@Override
106-
public <R> T post(final Entity<?> entity, final Class<R> clazz) {
107-
return method("POST", entity, clazz);
108-
}
109-
110-
@Override
111-
public <R> T post(final Entity<?> entity, final GenericType<R> type) {
112-
return method("POST", entity, type);
113-
}
114-
115-
@Override
116-
public T delete() {
117-
return method("DELETE");
118-
}
119-
120-
@Override
121-
public <R> T delete(final Class<R> responseType) {
122-
return method("DELETE", responseType);
123-
}
124-
125-
@Override
126-
public <R> T delete(final GenericType<R> responseType) {
127-
return method("DELETE", responseType);
128-
}
129-
130-
@Override
131-
public T head() {
132-
return method("HEAD");
133-
}
134-
135-
@Override
136-
public T options() {
137-
return method("OPTIONS");
138-
}
139-
140-
@Override
141-
public <R> T options(final Class<R> responseType) {
142-
return method("OPTIONS", responseType);
143-
}
144-
145-
@Override
146-
public <R> T options(final GenericType<R> responseType) {
147-
return method("OPTIONS", responseType);
148-
}
149-
150-
@Override
151-
public T trace() {
152-
return method("TRACE");
153-
}
154-
155-
@Override
156-
public <R> T trace(final Class<R> responseType) {
157-
return method("TRACE", responseType);
158-
}
159-
160-
@Override
161-
public <R> T trace(final GenericType<R> responseType) {
162-
return method("TRACE", responseType);
163-
}
164-
16571
@Override
16672
public T method(final String name) {
16773
return method(name, Response.class);
@@ -181,4 +87,5 @@ public <R> T method(final String name, final GenericType<R> responseType) {
18187
public T method(final String name, final Entity<?> entity) {
18288
return method(name, entity, Response.class);
18389
}
90+
18491
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.client;
18+
19+
import javax.ws.rs.client.AsyncInvoker;
20+
import javax.ws.rs.client.Entity;
21+
import javax.ws.rs.client.InvocationCallback;
22+
import javax.ws.rs.core.GenericType;
23+
import javax.ws.rs.core.Response;
24+
import java.util.concurrent.CompletableFuture;
25+
26+
/*package*/ abstract class CompletableFutureAsyncInvoker
27+
extends AbstractNonSyncInvoker<CompletableFuture> implements AsyncInvoker {
28+
@Override
29+
public <R> CompletableFuture<R> get(InvocationCallback<R> callback) {
30+
return method("GET", callback);
31+
}
32+
33+
@Override
34+
public <R> CompletableFuture<R> put(Entity<?> entity, InvocationCallback<R> callback) {
35+
return method("PUT", entity, callback);
36+
}
37+
38+
@Override
39+
public <R> CompletableFuture<R> post(Entity<?> entity, InvocationCallback<R> callback) {
40+
return method("POST", entity, callback);
41+
}
42+
43+
@Override
44+
public <R> CompletableFuture<R> delete(InvocationCallback<R> callback) {
45+
return method("DELETE", callback);
46+
}
47+
48+
@Override
49+
public CompletableFuture<Response> head(InvocationCallback<Response> callback) {
50+
return method("HEAD", callback);
51+
}
52+
53+
@Override
54+
public <R> CompletableFuture<R> options(InvocationCallback<R> callback) {
55+
return method("OPTIONS", callback);
56+
}
57+
58+
@Override
59+
public <R> CompletableFuture<R> trace(InvocationCallback<R> callback) {
60+
return method("TRACE", callback);
61+
}
62+
63+
@Override
64+
public abstract <R> CompletableFuture<R> method(String name, InvocationCallback<R> callback);
65+
66+
@Override
67+
public abstract <R> CompletableFuture<R> method(String name, Entity<?> entity, InvocationCallback<R> callback);
68+
69+
@Override
70+
public abstract <R> CompletableFuture method(String name, Entity<?> entity, Class<R> responseType);
71+
72+
@Override
73+
public abstract <R> CompletableFuture method(String name, Entity<?> entity, GenericType<R> responseType);
74+
}
Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -16,42 +16,20 @@
1616

1717
package org.glassfish.jersey.client;
1818

19-
import java.util.concurrent.CompletableFuture;
20-
import java.util.concurrent.CompletionStage;
21-
import java.util.concurrent.ExecutorService;
22-
2319
import javax.ws.rs.client.CompletionStageRxInvoker;
24-
import javax.ws.rs.client.Entity;
25-
import javax.ws.rs.client.Invocation;
26-
import javax.ws.rs.core.GenericType;
2720

2821
/**
2922
* Implementation of Reactive Invoker for {@code CompletionStage}.
3023
*
24+
* This class allows for using {@link javax.ws.rs.client.InvocationCallback} in
25+
* {@link javax.ws.rs.client.Invocation.Builder#rx(Class) Invocation.Builder.rx(JerseyCompletionStageRxInvoker.class)}
26+
* requests.
27+
*
3128
* @author Michal Gajdos
3229
* @since 2.26
3330
*/
34-
public class JerseyCompletionStageRxInvoker extends AbstractRxInvoker<CompletionStage> implements CompletionStageRxInvoker {
35-
36-
JerseyCompletionStageRxInvoker(Invocation.Builder builder, ExecutorService executor) {
37-
super(builder, executor);
38-
}
39-
40-
@Override
41-
public <T> CompletionStage<T> method(final String name, final Entity<?> entity, final Class<T> responseType) {
42-
final ExecutorService executorService = getExecutorService();
43-
44-
return executorService == null
45-
? CompletableFuture.supplyAsync(() -> getSyncInvoker().method(name, entity, responseType))
46-
: CompletableFuture.supplyAsync(() -> getSyncInvoker().method(name, entity, responseType), executorService);
47-
}
48-
49-
@Override
50-
public <T> CompletionStage<T> method(final String name, final Entity<?> entity, final GenericType<T> responseType) {
51-
final ExecutorService executorService = getExecutorService();
52-
53-
return executorService == null
54-
? CompletableFuture.supplyAsync(() -> getSyncInvoker().method(name, entity, responseType))
55-
: CompletableFuture.supplyAsync(() -> getSyncInvoker().method(name, entity, responseType), executorService);
31+
public class JerseyCompletionStageRxInvoker extends JerseyInvocation.AsyncInvoker implements CompletionStageRxInvoker {
32+
JerseyCompletionStageRxInvoker(JerseyInvocation.Builder builder) {
33+
super(builder);
5634
}
57-
}
35+
}

0 commit comments

Comments
 (0)