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

org.jsoftware.restclient.impl.InvocationChain Maven / Gradle / Ivy

There is a newer version: 1.5
Show newest version
package org.jsoftware.restclient.impl;

import org.apache.http.client.methods.HttpRequestBase;
import org.jsoftware.restclient.RestClientPlugin;
import org.jsoftware.restclient.RestClientResponse;

import java.util.concurrent.Callable;

/**
 * Chain of responsibility
 * @author szalik
 */
class InvocationChain implements RestClientPlugin.PluginChain {
    final Callable operation;

    InvocationChain(Callable operation) {
        this.operation = operation;
    }

    @Override
    public void continueChain() throws Exception {
        operation.call();
    }

    public static InvocationChain create(RestClientPlugin[] plugins, RestClientPlugin.PluginContext ctx, Callable dispatcher) {
        InvocationChain last = new InvocationChain(dispatcher);
        for(int i=plugins.length -1; i>=0; i--) {
            final int j = i;
            final InvocationChain next = last;
            last = new InvocationChain(()->{
                plugins[j].plugin(ctx, next);
                return ctx;
            });
        }
        return last;
    }
}

class PluginContextImpl implements RestClientPlugin.PluginContext {
    private HttpRequestBase request;
    private RestClientResponse response;

    @Override
    public HttpRequestBase getRequest() {
        return request;
    }

    @Override
    public void setRequest(HttpRequestBase request) {
        this.request = request;
    }

    @Override
    public RestClientResponse getResponse() {
        if (response == null) {
            throw new IllegalStateException("Request wasn't submitted yet!");
        }
        return response;
    }

    @Override
    public void setResponse(RestClientResponse response) {
        this.response = response;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy