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

io.vrap.rmf.base.client.ContextApiHttpClientImpl Maven / Gradle / Ivy

There is a newer version: 17.17.0
Show newest version

package io.vrap.rmf.base.client;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

public class ContextApiHttpClientImpl extends AutoCloseableService implements ContextApiHttpClient {
    private final ApiHttpClient client;

    private final boolean closeHttpClient;

    private Map contextMap;

    private final Consumer> consumer;

    public ContextApiHttpClientImpl(final ApiHttpClient client) {
        this(client, new HashMap<>(), null);
    }

    public ContextApiHttpClientImpl(final ApiHttpClient client, final Map contextMap) {
        this(client, contextMap, null);
    }

    public ContextApiHttpClientImpl(final ApiHttpClient client, final Map contextMap,
            final Consumer> consumer) {
        this(client, contextMap, consumer, true);
    }

    public ContextApiHttpClientImpl(final ApiHttpClient client, final boolean closeHttpClient) {
        this(client, new HashMap<>(), null, closeHttpClient);
    }

    public ContextApiHttpClientImpl(final ApiHttpClient client, final Map contextMap,
            final boolean closeHttpClient) {
        this(client, contextMap, null, closeHttpClient);
    }

    public ContextApiHttpClientImpl(final ApiHttpClient client, final Map contextMap,
            final Consumer> consumer, final boolean closeHttpClient) {
        this.client = client;
        this.contextMap = contextMap;
        this.consumer = consumer;
        this.closeHttpClient = closeHttpClient;
    }

    public ContextApiHttpClientImpl(final ContextApiHttpClientImpl c) {
        this.client = c.client;
        this.contextMap = c.contextMap;
        this.consumer = c.consumer;
        this.closeHttpClient = c.closeHttpClient;
    }

    public Map getContextMap() {
        return contextMap;
    }

    public ContextApiHttpClient withContextMap(final Map contextMap) {
        final ContextApiHttpClientImpl client = copy();
        client.contextMap = new HashMap<>(contextMap);

        return client;
    }

    @SuppressWarnings("unchecked")
    public  T getContext(Class key) {
        return (T) contextMap.get(key);
    }

    public  ContextApiHttpClient addContext(final T value) {
        final ContextApiHttpClientImpl client = copy();
        final Map contextMap = new HashMap<>(client.contextMap);
        contextMap.put(value.getClass(), value);
        if (value instanceof ClassReferenceContext) {
            contextMap.put(((ClassReferenceContext) value).classReference(), value);
        }
        client.contextMap = contextMap;

        return client;
    }

    public Object getContext(final Object key) {
        return contextMap.get(key);
    }

    public ContextApiHttpClient addContext(final Object key, final Object value) {
        final ContextApiHttpClientImpl client = copy();
        final Map contextMap = new HashMap<>(client.contextMap);
        contextMap.put(key, value);
        client.contextMap = contextMap;

        return client;
    }

    private ContextApiHttpClientImpl copy() {
        return new ContextApiHttpClientImpl(this);
    }

    private void restoreContext() {
        Optional.ofNullable(consumer).ifPresent(c -> c.accept(this));
    }

    @Override
    public ResponseSerializer getSerializerService() {
        return client.getSerializerService();
    }

    @Override
    public URI getBaseUri() {
        return client.getBaseUri();
    }

    @Override
    public CompletableFuture> execute(final ApiHttpRequest request) {
        restoreContext();
        return client.execute(request.withContextMap(contextMap));
    }

    @Override
    protected void internalClose() {
        if (closeHttpClient) {
            closeQuietly(client);
        }
        contextMap.values().forEach(o -> {
            if (o instanceof AutoCloseable) {
                closeQuietly((AutoCloseable) o);
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy