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

org.jboss.resteasy.client.jaxrs.internal.PublisherRxInvokerImpl Maven / Gradle / Ivy

There is a newer version: 7.0.0.Alpha4
Show newest version
package org.jboss.resteasy.client.jaxrs.internal;

import javax.ws.rs.HttpMethod;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;

import org.jboss.resteasy.client.jaxrs.PublisherRxInvoker;
import org.reactivestreams.Publisher;

import java.util.concurrent.CompletionStage;
import java.util.function.Function;

@SuppressWarnings("unchecked")
public abstract class PublisherRxInvokerImpl implements PublisherRxInvoker {

    private final ClientInvocationBuilder builder;

    public PublisherRxInvokerImpl(final ClientInvocationBuilder builder) {
        this.builder = builder;
    }

    protected abstract  Publisher toPublisher(CompletionStage completable);

    private  Publisher mkPublisher(
        final String method,
        final Entity entity,
        final Function> mkPublisher
    ) {
        ClientInvocation invocation = builder.createClientInvocation(builder.invocation);
        invocation.setMethod(method);
        invocation.setEntity(entity);
        return mkPublisher.apply(invocation);
    }

    private Publisher mkPublisher(final String method, final Entity entity) {
        return mkPublisher(method, entity, invocation ->
            invocation.reactive()
                .map(ClientInvocation.ReactiveInvocation::submit)
                .orElseGet(() -> toPublisher(invocation.submitCF()))
        );
    }

    private  Publisher mkPublisher(final String method, final Entity entity, final Class responseType) {
        return mkPublisher(method, entity, invocation ->
            invocation.reactive()
                .map(r -> r.submit(responseType))
                .orElseGet(() -> toPublisher(invocation.submitCF(responseType)))
        );
    }

    private  Publisher mkPublisher(final String method, final Entity entity, final GenericType responseType) {
        return mkPublisher(method, entity, invocation ->
            invocation.reactive()
                .map(r -> r.submit(responseType))
                .orElseGet(() -> toPublisher(invocation.submitCF(responseType)))
        );
    }

    @Override
    public Publisher get() {
        return mkPublisher(HttpMethod.GET, null);
    }

    @Override
    public  Publisher get(final Class responseType) {
        return mkPublisher(HttpMethod.GET, null, responseType);
    }

    @Override
    public  Publisher get(final GenericType responseType) {
        return mkPublisher(HttpMethod.GET, null, responseType);
    }

    @Override
    public Publisher put(final Entity entity) {
        return mkPublisher(HttpMethod.PUT, entity);
    }

    @Override
    public  Publisher put(final Entity entity, final Class clazz) {
        return mkPublisher(HttpMethod.PUT, entity, clazz);
    }

    @Override
    public  Publisher put(final Entity entity, final GenericType type) {
        return mkPublisher(HttpMethod.PUT, entity, type);
    }

    @Override
    public Publisher post(final Entity entity) {
        return mkPublisher(HttpMethod.POST, entity);
    }

    @Override
    public  Publisher post(final Entity entity, final Class clazz) {
        return mkPublisher(HttpMethod.POST, entity, clazz);
    }

    @Override
    public  Publisher post(final Entity entity, final GenericType type) {
        return mkPublisher(HttpMethod.POST, entity, type);
    }

    @Override
    public Publisher delete() {
        return mkPublisher(HttpMethod.DELETE, null);
    }

    @Override
    public  Publisher delete(final Class responseType) {
        return mkPublisher(HttpMethod.DELETE, null, responseType);
    }

    @Override
    public  Publisher delete(final GenericType responseType) {
        return mkPublisher(HttpMethod.DELETE, null, responseType);
    }

    @Override
    public Publisher head() {
        return mkPublisher(HttpMethod.HEAD, null);
    }

    @Override
    public Publisher options() {
        return mkPublisher(HttpMethod.OPTIONS, null);
    }

    @Override
    public  Publisher options(final Class responseType) {
        return mkPublisher(HttpMethod.OPTIONS, null, responseType);
    }

    @Override
    public  Publisher options(final GenericType responseType) {
        return mkPublisher(HttpMethod.OPTIONS, null, responseType);
    }

    @Override
    public Publisher trace() {
        return method("TRACE");
    }

    @Override
    public  Publisher trace(final Class responseType) {
        return method("TRACE", responseType);
    }

    @Override
    public  Publisher trace(final GenericType responseType) {
        return method("TRACE", responseType);
    }

    @Override
    public Publisher method(final String name) {
        return mkPublisher(name, null);
    }

    @Override
    public  Publisher method(final String name, final Class responseType) {
        return mkPublisher(name, null, responseType);
    }

    @Override
    public  Publisher method(final String name, final GenericType responseType) {
        return mkPublisher(name, null, responseType);
    }

    @Override
    public Publisher method(final String name, final Entity entity) {
        return mkPublisher(name, entity);
    }

    @Override
    public  Publisher method(final String name, final Entity entity, final Class responseType) {
        return mkPublisher(name, entity, responseType);
    }

    @Override
    public  Publisher method(final String name, final Entity entity, final GenericType responseType) {
        return mkPublisher(name, entity, responseType);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy