com.seajas.search.contender.http.ParameterizableHttpClient.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.contender.http.ParameterizableHttpClient.java

Source

/**
 * Copyright (C) 2013 Seajas, the Netherlands.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.seajas.search.contender.http;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnRouteParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;

/**
 * Parameterizable HTTP client.
 * 
 * @author Jasper van Veghel <jasper@seajas.com>
 */
public class ParameterizableHttpClient extends DefaultHttpClient {
    /**
     * Default constructor.
     * 
     * @param connectionManager
     * @param parameters
     * @param httpHost
     * @param httpPort
     * @param userAgent
     * @param connectionTimeout
     */
    public ParameterizableHttpClient(final ClientConnectionManager connectionManager, final HttpParams parameters,
            final String httpHost, final Integer httpPort, final String userAgent,
            final Integer connectionTimeout) {
        super(connectionManager, parameters);

        if (!StringUtils.isEmpty(httpHost))
            ConnRouteParams.setDefaultProxy(getParams(), new HttpHost(httpHost, httpPort));
        HttpProtocolParams.setUserAgent(getParams(), userAgent);

        if (connectionTimeout > 0) {
            HttpConnectionParams.setSoTimeout(getParams(), connectionTimeout);
            HttpConnectionParams.setConnectionTimeout(getParams(), connectionTimeout);
            HttpClientParams.setConnectionManagerTimeout(getParams(), connectionTimeout);
        }

        setRedirectStrategy(new DefaultRedirectStrategy());
    }
}