SDMClient.java [src/org/csu/csip/sdm] Revision: aec0ee9401db8dd200a03a888ea4b9dcb1f6fd61  Date: Thu Jul 20 10:44:56 MDT 2017
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API and application suite.
 *
 * 2012-2017, Olaf David and others, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package org.csu.csip.sdm;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.HttpsURLConnection;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.auth.AuthScope;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.pool.PoolStats;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.ssl.SSLContexts;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
public class SDMClient {
    private String url;

    private static final PoolingHttpClientConnectionManager CM;

    static final int DEF_TIMEOUT = 30; // 30 seconds
    static {

        CM = new PoolingHttpClientConnectionManager(
                RegistryBuilder.<ConnectionSocketFactory>create()
                        .register("http", PlainConnectionSocketFactory.getSocketFactory())
                        .register("https", new SSLConnectionSocketFactory(SSLContexts.createSystemDefault()))
                        .build()
        );

        CM.setMaxTotal(250);
        // Increase default max connection per route to 75
        CM.setDefaultMaxPerRoute(100);
        // Increase max connections for localhost:80 to 125
        HttpHost localhost = new HttpHost("locahost", 80);
        CM.setMaxPerRoute(new HttpRoute(localhost), 150);
    }
    /**
     * Pings a HTTP URL. This effectively sends a HEAD request and returns
     * <code>true</code> if the response code is in the 200-399 range.
     *
     * @param url The HTTP URL to be pinged.
     * @param timeout The timeout in millis for both the connection timeout and
     * the response read timeout. Note that the total timeout is effectively two
     * times the given timeout.
     * @return the time to respond if the given HTTP URL has returned response
     * code 200-399 on a HEAD request within the given timeout, otherwise
     * <code>-1</code>.
     */
    public static long ping(String url, int timeout) {
        try {
            HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
            connection.setConnectTimeout(timeout);
            connection.setReadTimeout(timeout);
            connection.setRequestMethod("HEAD");
            long start = System.currentTimeMillis();
            int responseCode = connection.getResponseCode();
            long end = System.currentTimeMillis();
            return (200 <= responseCode && responseCode <= 399) ? (end - start) : -1;
        } catch (IOException E) {
            return -1;
        }
    }

    private CredentialsProvider creds;
    private CloseableHttpClient httpClient;

    private Logger LOG;

    /**
     * Create a Client
     */
    public SDMClient() {
        this(DEF_TIMEOUT, Logger.getLogger(SDMClient.class.getName()));
    }

    /**
     * Create a client with a custom timeout.
     *
     * @param timeout the timeout in
     */
    public SDMClient(int timeout) {
        this(timeout, Logger.getLogger(SDMClient.class.getName()));
    }

    public SDMClient(int timeout, Logger log) {
        LOG = log;
        RequestConfig reqConfig = init(timeout, log);
        httpClient = HttpClientBuilder.create()
                .setDefaultRequestConfig(reqConfig)
                .setConnectionManager(CM)
                .build();
    }

    public void setURL(String url){
        this.url = url;
    }
    
    private RequestConfig init(int timeout, Logger log) {
        RequestConfig reqConfig = RequestConfig.custom()
                .setConnectTimeout(timeout * 1000)
                .setConnectionRequestTimeout(timeout * 1000)
                .setSocketTimeout(timeout * 1000) // 10 minute max per run
                .setCookieSpec(CookieSpecs.BEST_MATCH)
                .setExpectContinueEnabled(true)
                .setStaleConnectionCheckEnabled(true)
                .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
                .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))
                .build();
        return reqConfig;
    }


    /**
     * HTTP Post with the request JSON.
     *
     * @param uri the endpoint
     * @param req the request JSON
     * @return the response JSON
     * @throws java.sql.SQLException
     */
    public JSONObject doPOST(JSONObject req) throws SQLException {
        String uri = this.url;
        JSONObject ret_val = null;

        EntityBuilder builder = EntityBuilder.create();
        builder.setText(req.toString());
        builder.setContentType(ContentType.APPLICATION_JSON);

        HttpEntity reqEntity = builder.build();
        HttpPost post = new HttpPost(uri);
        post.setEntity(reqEntity);

        if (LOG != null && LOG.isLoggable(Level.INFO)) {
            LOG.info("POST: " + uri + " ...");
        }
        String response = req.toString();

        try (CloseableHttpResponse httpResponse = httpClient.execute(post)) {
            InputStream responseStream = httpResponse.getEntity().getContent();
            response = IOUtils.toString(responseStream);
            if (LOG != null && LOG.isLoggable(Level.INFO)) {
                LOG.info("response: " + response);
                PoolStats ps = CM.getTotalStats();
                LOG.info("http-client-connections pool [avail=" + ps.getAvailable() + " leased=" + ps.getLeased() + " pending=" + ps.getPending() + "] ");
            }
        } catch (IOException ex) {
            Logger.getLogger(SDMClient.class.getName()).log(Level.SEVERE, null, ex);
            throw new SQLException("Communication error with SDM host: " + ex.getMessage(), ex);
        } finally {
            post.releaseConnection();
        }

        try {
            ret_val = new JSONObject(response);
        } catch (JSONException ex) {
            Logger.getLogger(SDMClient.class.getName()).log(Level.SEVERE, null, ex);
            throw new SQLException("Communication error with SDM host.  Invalid return data: " + ex.getMessage(), ex);
        }
        return ret_val;
    }
    private class PreemptiveAuthInterceptor implements HttpRequestInterceptor {
        
        @Override
        @SuppressWarnings("deprecation")
        public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
            request.addHeader(BasicScheme.authenticate(creds.getCredentials(AuthScope.ANY), "US-ASCII", false));
        }
    }
}