Example usage for org.apache.http.impl.client SystemDefaultHttpClient SystemDefaultHttpClient

List of usage examples for org.apache.http.impl.client SystemDefaultHttpClient SystemDefaultHttpClient

Introduction

In this page you can find the example usage for org.apache.http.impl.client SystemDefaultHttpClient SystemDefaultHttpClient.

Prototype

public SystemDefaultHttpClient() 

Source Link

Usage

From source file:org.wso2.carbon.event.output.adaptor.http.HTTPEventAdaptor.java

private void checkHTTPClientInit(OutputEventAdaptorMessageConfiguration outputEventMessageConfiguration) {
    if (this.httpClient != null) {
        return;/*from w w  w .j  a  va2  s. c  o  m*/
    }
    synchronized (HTTPEventAdaptor.class) {
        if (this.httpClient != null) {
            return;
        }
        /* this needs to be created as late as possible, for the SSL truststore properties 
         * to be set by Carbon in Java system properties */
        this.httpClient = new SystemDefaultHttpClient();
        Map<String, String> props = outputEventMessageConfiguration.getOutputMessageProperties();
        String proxyHost = props.get(HTTPEventAdaptorConstants.ADAPTER_PROXY_HOST);
        String proxyPort = props.get(HTTPEventAdaptorConstants.ADAPTER_PROXY_PORT);
        if (proxyHost != null && proxyHost.trim().length() > 0) {
            try {
                HttpHost host = new HttpHost(proxyHost, Integer.parseInt(proxyPort));
                this.httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, host);
            } catch (NumberFormatException e) {
                log.error("Invalid proxy port: " + proxyPort + ", "
                        + "ignoring proxy settings for HTTP output event adaptor...");
            }
        }
    }
}

From source file:org.graphity.core.util.jena.HttpOp.java

/** Create an HttpClient that performs connection pooling.  This can be used
 * with {@link #setDefaultHttpClient} or provided in the HttpOp calls.
 *//*from  w  ww . j  a  v a2 s  .  c  o  m*/
public static HttpClient createCachingHttpClient() {
    return new SystemDefaultHttpClient() {
        /** See SystemDefaultHttpClient (4.2).  This version always sets the connection cache */
        @Override
        protected ClientConnectionManager createClientConnectionManager() {
            PoolingClientConnectionManager connmgr = new PoolingClientConnectionManager(
                    SchemeRegistryFactory.createSystemDefault());
            String s = System.getProperty("http.maxConnections", "5");
            int max = Integer.parseInt(s);
            connmgr.setDefaultMaxPerRoute(max);
            connmgr.setMaxTotal(2 * max);
            return connmgr;
        }
    };
}

From source file:com.hortonworks.registries.auth.client.AuthenticatorTestCase.java

private SystemDefaultHttpClient getHttpClient() {
    final SystemDefaultHttpClient httpClient = new SystemDefaultHttpClient();
    httpClient.getAuthSchemes().register(AuthPolicy.SPNEGO, new SPNegoSchemeFactory(true));
    Credentials use_jaas_creds = new Credentials() {
        public String getPassword() {
            return null;
        }/*from   ww  w.  j a  v a2  s .  c om*/

        public Principal getUserPrincipal() {
            return null;
        }
    };

    httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, use_jaas_creds);
    return httpClient;
}

From source file:org.graphity.core.util.jena.HttpQuery.java

private InputStream execGet() throws QueryExceptionHTTP {
    URL target = null;//from w  w w.ja  v  a 2  s .  c  o  m
    String qs = getQueryString();

    ARQ.getHttpRequestLogger().trace(qs);

    try {
        if (count() == 0)
            target = new URL(serviceURL);
        else
            target = new URL(serviceURL + (serviceParams ? "&" : "?") + qs);
    } catch (MalformedURLException malEx) {
        throw new QueryExceptionHTTP(0, "Malformed URL: " + malEx);
    }
    log.trace("GET " + target.toExternalForm());

    try {
        try {
            this.client = new SystemDefaultHttpClient();

            // Always apply a 10 second timeout to obtaining a connection lease from HTTP Client
            // This prevents a potential lock up
            this.client.getParams().setLongParameter(ConnManagerPNames.TIMEOUT, TimeUnit.SECONDS.toMillis(10));

            // If user has specified time outs apply them now
            if (this.connectTimeout > 0)
                this.client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
                        this.connectTimeout);
            if (this.readTimeout > 0)
                this.client.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, this.readTimeout);

            // Enable compression support appropriately
            HttpContext context = new BasicHttpContext();
            if (allowGZip || allowDeflate) {
                // Apply auth early as the decompressing client we're about
                // to add will block this being applied later
                HttpOp.applyAuthentication((AbstractHttpClient) client, serviceURL, context, authenticator);
                client = new DecompressingHttpClient(client);
            }

            // Get the actual response stream
            TypedInputStream stream = HttpOp.execHttpGet(target.toString(), contentTypeResult, client, context,
                    this.authenticator);
            if (stream == null)
                throw new QueryExceptionHTTP(404);
            return execCommon(stream);
        } catch (HttpException httpEx) {
            // Back-off and try POST if something complain about long URIs
            if (httpEx.getResponseCode() == 414)
                return execPost();
            throw httpEx;
        }
    } catch (HttpException httpEx) {
        // Unwrap and re-wrap the HTTP exception
        responseCode = httpEx.getResponseCode();
        throw new QueryExceptionHTTP(responseCode, "Error making the query, see cause for details",
                httpEx.getCause());
    }
}

From source file:org.graphity.core.util.jena.HttpQuery.java

private InputStream execPost() throws QueryExceptionHTTP {
    URL target = null;//  w w  w  .  j  a va  2 s.  co  m
    try {
        target = new URL(serviceURL);
    } catch (MalformedURLException malEx) {
        throw new QueryExceptionHTTP(0, "Malformed URL: " + malEx);
    }
    log.trace("POST " + target.toExternalForm());

    ARQ.getHttpRequestLogger().trace(target.toExternalForm());

    try {
        this.client = new SystemDefaultHttpClient();

        // Always apply a 10 second timeout to obtaining a connection lease from HTTP Client
        // This prevents a potential lock up
        this.client.getParams().setLongParameter(ConnManagerPNames.TIMEOUT, TimeUnit.SECONDS.toMillis(10));

        // If user has specified time outs apply them now
        if (this.connectTimeout > 0)
            this.client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
                    this.connectTimeout);
        if (this.readTimeout > 0)
            this.client.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, this.readTimeout);

        // Enable compression support appropriately
        HttpContext context = new BasicHttpContext();
        if (allowGZip || allowDeflate) {
            // Apply auth early as the decompressing client we're about
            // to add will block this being applied later
            HttpOp.applyAuthentication((AbstractHttpClient) client, serviceURL, context, authenticator);
            this.client = new DecompressingHttpClient(client);
        }

        // Get the actual response stream
        TypedInputStream stream = HttpOp.execHttpPostFormStream(serviceURL, this, contentTypeResult, client,
                context, authenticator);
        if (stream == null)
            throw new QueryExceptionHTTP(404);
        return execCommon(stream);
    } catch (HttpException httpEx) {
        // Unwrap and re-wrap the HTTP Exception
        responseCode = httpEx.getResponseCode();
        throw new QueryExceptionHTTP(responseCode, "Error making the query, see cause for details",
                httpEx.getCause());
    }
}

From source file:org.apache.jena.sparql.engine.http.HttpQuery.java

private void selectClient() {
    // May use configured default client where appropriate
    this.client = HttpOp.getDefaultHttpClient();
    if (this.client == null
            || (this.authenticator != null && !HttpOp.getUseDefaultClientWithAuthentication())) {
        // If no configured default or authentication is in-use and the user has not configured
        // to use authentication with the default client use a fresh SystemDefaultHttpClient instance
        this.client = new SystemDefaultHttpClient();
    } else {/*from w  ww  .j  a v a  2s .c om*/
        // When using the configured default client we don't want to shut it down at the end of a request
        this.requireClientShutdown = false;
    }
}

From source file:org.cloudifysource.quality.iTests.test.cli.cloudify.util.NewRestTestUtils.java

public static HttpResponse sendGetRequest(final String uri) throws IOException {
    final SystemDefaultHttpClient httpClient = new SystemDefaultHttpClient();
    final HttpGet request = new HttpGet(uri);
    return httpClient.execute(request);
}

From source file:org.graphity.core.util.jena.HttpOp.java

/**
 * Ensures that a HTTP Client is non-null, uses a Jena-wide
 * {@link SystemDefaultHttpClient} if available when no
 * authentication is required, else create a new instance.
 * // ww  w  .  j  a  v  a  2s.  com
 * @param client
 *            HTTP Client
 * @return HTTP Client
 */
private static HttpClient ensureClient(HttpClient client, HttpAuthenticator auth) {
    if (client != null)
        return client;
    if (defaultHttpClient != null && auth == null)
        return defaultHttpClient;
    return new SystemDefaultHttpClient();
}