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

groovyx.net.http.ChainedHttpConfig Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2017 HttpBuilder-NG Project
 *
 * Licensed 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 groovyx.net.http;

import java.net.HttpCookie;
import java.nio.charset.Charset;
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;

import static groovyx.net.http.Traverser.traverse;

public interface ChainedHttpConfig extends HttpConfig {

    interface ChainedRequest extends Request {
        ChainedRequest getParent();

        List getCookies();

        Object getBody();

        String getContentType();

        Map> getEncoderMap();

        Charset getCharset();

        default Charset actualCharset() {
            return traverse(this, ChainedRequest::getParent, ChainedRequest::getCharset, Traverser::notNull);
        }

        default String actualContentType() {
            return traverse(this, ChainedRequest::getParent, ChainedRequest::getContentType, Traverser::notNull);
        }

        default Object actualBody() {
            return traverse(this, ChainedRequest::getParent, ChainedRequest::getBody, Traverser::notNull);
        }

        default Map actualHeaders(final Map map) {
            Predicate> addValues = (headers) -> {
                map.putAll(headers);
                return false;
            };
            traverse(this, ChainedRequest::getParent, Request::getHeaders, addValues);
            return map;
        }

        default BiConsumer actualEncoder(final String contentType) {
            final Function> theValue = (cr) -> {
                BiConsumer ret = cr.encoder(contentType);
                if (ret != null) {
                    return ret;
                } else {
                    return cr.encoder(ContentTypes.ANY.getAt(0));
                }
            };

            return traverse(this, ChainedRequest::getParent, theValue, Traverser::notNull);
        }

        default Auth actualAuth() {
            final Predicate choose = (a) -> a != null && a.getAuthType() != null;
            return traverse(this, ChainedRequest::getParent, Request::getAuth, choose);
        }

        default List actualCookies(final List list) {
            Predicate> addAll = (cookies) -> {
                list.addAll(cookies);
                return false;
            };
            traverse(this, ChainedRequest::getParent, ChainedRequest::getCookies, addAll);
            return list;
        }

        HttpVerb getVerb();

        void setVerb(HttpVerb verb);
    }

    interface ChainedResponse extends Response {
        ChainedResponse getParent();

        Class getType();

        Function getException();

        default BiFunction actualAction(final Integer code) {
            return traverse(this, ChainedResponse::getParent, (cr) -> cr.when(code), Traverser::notNull);
        }

        default Function actualException() {
            return traverse(this, ChainedResponse::getParent, ChainedResponse::getException, Traverser::notNull);
        }

        default BiFunction actualParser(final String contentType) {
            final Function> theValue = (cr) -> {
                BiFunction ret = cr.parser(contentType);
                if (ret != null) {
                    return ret;
                } else {
                    return cr.parser(ContentTypes.ANY.getAt(0));
                }
            };

            return traverse(this, ChainedResponse::getParent, theValue, Traverser::notNull);
        }
    }

    Map, Object> getContextMap();

    default Object actualContext(final String contentType, final Object id) {
        final Map.Entry key = new AbstractMap.SimpleImmutableEntry<>(contentType, id);
        final Map.Entry anyKey = new AbstractMap.SimpleImmutableEntry<>(ContentTypes.ANY.getAt(0), id);

        final Function theValue = (config) -> {
            Object ctx = config.getContextMap().get(key);
            if (ctx != null) {
                return ctx;
            } else {
                return config.getContextMap().get(anyKey);
            }
        };

        return traverse(this, ChainedHttpConfig::getParent, theValue, Traverser::notNull);
    }

    ChainedResponse getChainedResponse();

    ChainedRequest getChainedRequest();

    ChainedHttpConfig getParent();

    default BiFunction findParser(final String contentType) {
        final BiFunction found = getChainedResponse().actualParser(contentType);
        return found == null ? NativeHandlers.Parsers::streamToBytes : found;
    }

    /**
     * Used to find the encoder configured to encode the current resolved content-type.
     *
     * @return the configured encoder
     * @throws IllegalStateException if no coder was found
     */
    default BiConsumer findEncoder() {
        return findEncoder(findContentType());
    }

    /**
     * Used to find the encoder configured to encode the specified content-type.
     *
     * @param contentType the content-type to be encoded
     * @return the configured encoder
     * @throws IllegalStateException if no coder was found
     */
    default BiConsumer findEncoder(final String contentType) {
        final BiConsumer encoder = getChainedRequest().actualEncoder(contentType);
        if (encoder == null) {
            throw new IllegalStateException("Could not find encoder for content-type (" + contentType + ")");
        }

        return encoder;
    }

    default String findContentType() {
        final String contentType = getChainedRequest().actualContentType();
        if (contentType == null) {
            throw new IllegalStateException("Found request body, but content type is undefined");
        }

        return contentType;
    }

    default Charset findCharset(){
        return getChainedRequest().actualCharset();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy