All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.winterly.rxjersey.server.SingleInvocationHandlerProvider Maven / Gradle / Ivy

There is a newer version: 0.11.0
Show newest version
package net.winterly.rxjersey.server;

import org.glassfish.jersey.server.model.Invocable;
import org.glassfish.jersey.server.spi.internal.ResourceMethodInvocationHandlerProvider;
import rx.Completable;
import rx.Observable;
import rx.Single;

import java.lang.reflect.InvocationHandler;
import java.util.HashMap;

/**
 * Provides {@link InvocationHandler} for resources returning {@link Observable} or {@link Single}
 * and converts both to single Observable
 */
public class SingleInvocationHandlerProvider implements ResourceMethodInvocationHandlerProvider {

    private final HashMap> handlers = new HashMap<>();

    public SingleInvocationHandlerProvider() {
        handlers.put(Observable.class, new ObservableHandler());
        handlers.put(Single.class, new SingleHandler());
        handlers.put(Completable.class, new CompletableHandler());
    }

    @Override
    public InvocationHandler create(Invocable invocable) {
        return handlers.get(invocable.getRawResponseType());
    }

    private static class ObservableHandler implements RxInvocationHandler> {
        @Override
        public Single convert(Observable result) throws Throwable {
            return result.singleOrDefault(null).toSingle();
        }
    }

    private static class SingleHandler implements RxInvocationHandler> {
        @Override
        public Single convert(Single result) throws Throwable {
            return result;
        }
    }

    private static class CompletableHandler implements RxInvocationHandler {
        @Override
        public Single convert(Completable result) throws Throwable {
            return result.andThen(Single.just(null));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy