001/**
002 * Copyright (C) 2006-2020 Talend Inc. - www.talend.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.api.service.http;
017
018import java.util.List;
019import java.util.Map;
020
021/**
022 * Callback to configure the connection more deeply. Typically used to configure timeouts.
023 */
024public interface Configurer {
025
026    /**
027     * @param connection the current connection to customize.
028     * @param configuration the configuration of the invocation if any.
029     */
030    void configure(Connection connection, ConfigurerConfiguration configuration);
031
032    /**
033     * Represents actions doable on a connection.
034     */
035    interface Connection {
036
037        /**
038         * @return the method of current connection.
039         */
040        String getMethod();
041
042        /**
043         * @return the url of current connection.
044         */
045        String getUrl();
046
047        /**
048         * @return headers already set.
049         */
050        Map<String, List<String>> getHeaders();
051
052        /**
053         * @return payload of the request or null.
054         */
055        byte[] getPayload();
056
057        /**
058         * Adds a header to the request.
059         *
060         * @param name header name.
061         * @param value header value.
062         * @return current connection.
063         */
064        Connection withHeader(String name, String value);
065
066        /**
067         * Sets the read timeout of the connection.
068         *
069         * @param timeout timeout value in milliseconds.
070         * @return current connection.
071         */
072        Connection withReadTimeout(int timeout);
073
074        /**
075         * Sets the connection timeout of the connection.
076         *
077         * @param timeout timeout value in milliseconds.
078         * @return current connection.
079         */
080        Connection withConnectionTimeout(int timeout);
081
082        /**
083         * Prevents the client to follow redirections.
084         */
085        Connection withoutFollowRedirects();
086    }
087
088    /**
089     * Represents the potential {@link ConfigurerOption} parameters of the invocation.
090     */
091    interface ConfigurerConfiguration {
092
093        /**
094         * @return all options at once.
095         */
096        Object[] configuration();
097
098        /**
099         * @param name the option name.
100         * @param type the expected type of the option.
101         * @param <T> the type of the option.
102         * @return the option value or null if missing.
103         */
104        <T> T get(String name, Class<T> type);
105    }
106}