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

org.wisdom.test.http.Options Maven / Gradle / Ivy

There is a newer version: 0.10.0
Show newest version
/*
 * #%L
 * Wisdom-Framework
 * %%
 * Copyright (C) 2013 - 2014 Wisdom Framework
 * %%
 * 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.
 * #L%
 */
package org.wisdom.test.http;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;

import java.util.HashMap;
import java.util.Map;

/**
 * Stores HTTP Client options.
 */
public class Options {

    /**
     * The set of options.
     */
    public enum Option {
        HTTPCLIENT, ASYNCHTTPCLIENT, CONNECTION_TIMEOUT, SOCKET_TIMEOUT, DEFAULT_HEADERS
    }

    /**
     * Default connection timeout.
     */
    public static final long CONNECTION_TIMEOUT = 10000;

    /**
     * Default socket timeout.
     */
    private static final long SOCKET_TIMEOUT = 60000;

    /**
     * The stored options.
     */
    private static final Map options = new HashMap<>();

    /**
     * Sets an option.
     *
     * @param option the option, must not be {@literal null}
     * @param value  the value
     */
    public static void setOption(Option option, Object value) {
        options.put(option, value);
    }

    /**
     * Gets the value of an option.
     *
     * @param option the option, must not be {@literal null}
     * @return the value, {@literal null} if not set
     */
    public static Object getOption(Option option) {
        return options.get(option);
    }

    static {
        // Initialize the options.
        refresh();
    }

    /**
     * Refreshes the options, and restores defaults.
     */
    public static void refresh() {
        // Load timeouts
        Object connectionTimeout = Options.getOption(Option.CONNECTION_TIMEOUT);
        if (connectionTimeout == null) {
            connectionTimeout = CONNECTION_TIMEOUT;
        }

        Object socketTimeout = Options.getOption(Option.SOCKET_TIMEOUT);
        if (socketTimeout == null) {
            socketTimeout = SOCKET_TIMEOUT;
        }

        // Create common default configuration
        RequestConfig clientConfig = RequestConfig.custom().setConnectTimeout(((Long) connectionTimeout).intValue()).setSocketTimeout(((Long) socketTimeout).intValue()).setConnectionRequestTimeout(((Long) socketTimeout).intValue()).build();

        // Create clients
        setOption(Option.HTTPCLIENT, HttpClientBuilder.create().setDefaultRequestConfig(clientConfig).build());

        CloseableHttpAsyncClient asyncClient = HttpAsyncClientBuilder.create().setDefaultRequestConfig(clientConfig).build();
        setOption(Option.ASYNCHTTPCLIENT, asyncClient);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy