org.carewebframework.fhir.client.HttpClientProxy.java Source code

Java tutorial

Introduction

Here is the source code for org.carewebframework.fhir.client.HttpClientProxy.java

Source

/**
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
 * If a copy of the MPL was not distributed with this file, You can obtain one at
 * http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is also subject to the terms of the Health-Related Additional
 * Disclaimer of Warranty and Limitation of Liability available at
 * http://www.carewebframework.org/licensing/disclaimer.
 */
package org.carewebframework.fhir.client;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.UriPatternMatcher;

/**
 * Http client proxy to allow registration of custom clients based on url patterns.
 */
@SuppressWarnings("deprecation")
public class HttpClientProxy extends CloseableHttpClient {

    /**
     * Specialized client http request factories for servicing atypical requests.
     */
    private final UriPatternMatcher<CloseableHttpClient> patterns = new UriPatternMatcher<CloseableHttpClient>();

    private final List<CloseableHttpClient> clients = new ArrayList<CloseableHttpClient>();

    public HttpClientProxy(CloseableHttpClient defaultClient) {
        super();
        clients.add(defaultClient);
    }

    public void registerHttpClient(String pattern, CloseableHttpClient client) {
        patterns.register(pattern, client);
        clients.add(client);
    }

    @Override
    public ClientConnectionManager getConnectionManager() {
        return getDefaultClient().getConnectionManager();
    }

    @Override
    public HttpParams getParams() {
        return getDefaultClient().getParams();
    }

    @Override
    public void close() throws IOException {
        for (CloseableHttpClient client : clients) {
            close(client);
        }

        clients.clear();
    }

    /**
     * Returns the default client.
     * 
     * @return The default client.
     */
    private CloseableHttpClient getDefaultClient() {
        return clients.get(0);
    }

    /**
     * Silently close a client.
     * 
     * @param client Client to close.
     */
    private void close(CloseableHttpClient client) {
        try {
            client.close();
        } catch (IOException e) {
        }
    }

    @Override
    protected CloseableHttpResponse doExecute(HttpHost host, HttpRequest request, HttpContext context)
            throws ClientProtocolException, IOException {
        String uri = request.getRequestLine().getUri();
        CloseableHttpClient client = patterns.lookup(uri);
        return (client != null ? client : getDefaultClient()).execute(host, request, context);
    }

}