org.apache.cxf.jaxrs.rx3.client.ObservableRxInvokerImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cxf-rt-rs-extension-rx3 Show documentation
Show all versions of cxf-rt-rs-extension-rx3 Show documentation
Apache CXF JAX-RS Extensions: RxJava3
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cxf.jaxrs.rx3.client;
import java.util.concurrent.ExecutorService;
import java.util.function.Supplier;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.SyncInvoker;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Scheduler;
import io.reactivex.rxjava3.schedulers.Schedulers;
public class ObservableRxInvokerImpl implements ObservableRxInvoker {
private final Scheduler sc;
private final SyncInvoker syncInvoker;
public ObservableRxInvokerImpl(SyncInvoker syncInvoker, ExecutorService ex) {
this.syncInvoker = syncInvoker;
this.sc = ex == null ? null : Schedulers.from(ex);
}
@Override
public Observable get() {
return get(Response.class);
}
@Override
public Observable get(Class responseType) {
return method(HttpMethod.GET, responseType);
}
@Override
public Observable get(GenericType responseType) {
return method(HttpMethod.GET, responseType);
}
@Override
public Observable put(Entity> entity) {
return put(entity, Response.class);
}
@Override
public Observable put(Entity> entity, Class responseType) {
return method(HttpMethod.PUT, entity, responseType);
}
@Override
public Observable put(Entity> entity, GenericType responseType) {
return method(HttpMethod.PUT, entity, responseType);
}
@Override
public Observable post(Entity> entity) {
return post(entity, Response.class);
}
@Override
public Observable post(Entity> entity, Class responseType) {
return method(HttpMethod.POST, entity, responseType);
}
@Override
public Observable post(Entity> entity, GenericType responseType) {
return method(HttpMethod.POST, entity, responseType);
}
@Override
public Observable delete() {
return delete(Response.class);
}
@Override
public Observable delete(Class responseType) {
return method(HttpMethod.DELETE, responseType);
}
@Override
public Observable delete(GenericType responseType) {
return method(HttpMethod.DELETE, responseType);
}
@Override
public Observable head() {
return method(HttpMethod.HEAD);
}
@Override
public Observable options() {
return options(Response.class);
}
@Override
public Observable options(Class responseType) {
return method(HttpMethod.OPTIONS, responseType);
}
@Override
public Observable options(GenericType responseType) {
return method(HttpMethod.OPTIONS, responseType);
}
@Override
public Observable trace() {
return trace(Response.class);
}
@Override
public Observable trace(Class responseType) {
return method("TRACE", responseType);
}
@Override
public Observable trace(GenericType responseType) {
return method("TRACE", responseType);
}
@Override
public Observable method(String name) {
return method(name, Response.class);
}
@Override
public Observable method(String name, Entity> entity) {
return method(name, entity, Response.class);
}
@Override
public Observable method(String name, Entity> entity, Class responseType) {
return create(() -> syncInvoker.method(name, entity, responseType));
}
@Override
public Observable method(String name, Entity> entity, GenericType responseType) {
return create(() -> syncInvoker.method(name, entity, responseType));
}
@Override
public Observable method(String name, Class responseType) {
return create(() -> syncInvoker.method(name, responseType));
}
@Override
public Observable method(String name, GenericType responseType) {
return create(() -> syncInvoker.method(name, responseType));
}
private Observable create(Supplier supplier) {
Observable observable = Observable.create(new ObservableOnSubscribe() {
@Override
public void subscribe(ObservableEmitter emitter) throws Exception {
try {
T response = supplier.get();
if (!emitter.isDisposed()) {
emitter.onNext(response);
}
if (!emitter.isDisposed()) {
emitter.onComplete();
}
} catch (Throwable e) {
if (!emitter.isDisposed()) {
emitter.onError(e);
}
}
}
});
if (sc == null) {
return observable.subscribeOn(Schedulers.io());
}
return observable.subscribeOn(sc).observeOn(sc);
}
}