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

org.ehoffman.test.aspects.HttpClientLookup Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package org.ehoffman.test.aspects;

import java.io.Closeable;
import java.io.IOException;
import java.net.URI;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.protocol.BasicHttpContext;

/**
 * Expecting all requests to be to the same host...
 * 
 * @author rex
 */
class HttpClientLookup implements Closeable {

    private static final int HTTP_SUCCESS = 200;
    private static final int HTTP_REDIRECTION = 300;

    private static final int MAX_CONNECTIONS = 200;
    private static final int MAX_CONNECTIONS_PER_ROUTE = 20;

    private final DefaultHttpClient httpclient;
    private final BasicHttpContext localContext;
    private final String host;

    // URI uri = new URI(urlBase + input + usernameSuffix);

    /**
     * Return true of the http resource exists (200), false if not (404), throws an {@link IOException} otherwise.
     * 
     * @param uri
     *            to attempt to access (this will server as the source of the host, port and scheme for httphost access)
     *            No attempt to connect to this uri will be made.
     * @param username
     *            user name to attempt to login it with via basic auth
     * @param password
     *            ditto but password.
     */
    public HttpClientLookup(final URI uri, final String username, final String password) {
        final PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
        cm.setMaxTotal(MAX_CONNECTIONS);
        cm.setDefaultMaxPerRoute(MAX_CONNECTIONS_PER_ROUTE);
        httpclient = new DefaultHttpClient(cm);
        host = uri.getHost();
        final HttpHost targetHost = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
        final CredentialsProvider credsProvider = new BasicCredentialsProvider();
        if (username != null && !"".equals(username)) {
            credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()),
                    new UsernamePasswordCredentials(username, password));
        }
        httpclient.setCredentialsProvider(credsProvider);
        final AuthCache authCache = new BasicAuthCache();
        final BasicScheme basicAuth = new BasicScheme();
        authCache.put(targetHost, basicAuth);
        localContext = new BasicHttpContext();
        localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
        localContext.setAttribute("preemptive-auth", basicAuth);
    }

    /**
     * Return true of the http resource exists (200), false if not (404), throws an {@link IOException} otherwise.
     * 
     * @param uri
     *            to attempt to access
     * @return true or false based on whether or not the resource exists.
     * 
     */
    public int statusCode(final URI uri) throws IOException {
        if (!host.equalsIgnoreCase(uri.getHost())) {
            throw new IOException("Can only connect to " + host + " attempted to connect to url " + uri.getHost());
        }
        final HttpGet get = new HttpGet(uri);
        final HttpResponse response = httpclient.execute(get, localContext);
        return response.getStatusLine().getStatusCode();
    }


    /**
     * Return true of the http resource exists (status code 200 <= x < 300), false otherwise.
     * 
     * @param uri
     *            to attempt to access
     * @return true or false based on whether or not the resource exists.
     */
   public boolean verifyExists(final URI uri) throws IOException {
        final int statusCode = statusCode(uri);
        return statusCode >= HTTP_SUCCESS && statusCode < HTTP_REDIRECTION;
    }

    public void close() {
        httpclient.getConnectionManager().shutdown();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy