Example usage for org.apache.http.client HttpClient getConnectionManager

List of usage examples for org.apache.http.client HttpClient getConnectionManager

Introduction

In this page you can find the example usage for org.apache.http.client HttpClient getConnectionManager.

Prototype

@Deprecated
ClientConnectionManager getConnectionManager();

Source Link

Document

Obtains the connection manager used by this client.

Usage

From source file:ca.sqlpower.enterprise.ClientSideSessionUtils.java

/**
 * Requests the server for persist calls from version 0 to the given revision
 * of the given project, and persists them to the given decoder.
 * /*  w w w.  ja  v a  2 s.c  o  m*/
 * @param projectLocation
 * @param revisionNo Must be greater than zero, and no greater than the current revision number
 * @param decoder
 * @throws IOException
 * @throws URISyntaxException
 * @throws SPPersistenceException
 * @throws IllegalArgumentException Thrown if the server rejects the given revisionNo
 */
public static void persistRevisionFromServer(ProjectLocation projectLocation, int revisionNo,
        SPJSONMessageDecoder decoder, CookieStore cookieStore)
        throws IOException, URISyntaxException, SPPersistenceException, IllegalArgumentException {

    SPServerInfo serviceInfo = projectLocation.getServiceInfo();
    HttpClient httpClient = ClientSideSessionUtils.createHttpClient(serviceInfo, cookieStore);

    try {
        JSONMessage response = ClientSideSessionUtils.executeServerRequest(httpClient, serviceInfo, "/"
                + ClientSideSessionUtils.REST_TAG + "/project/" + projectLocation.getUUID() + "/" + revisionNo,
                new JSONResponseHandler());

        if (response.isSuccessful()) {
            decoder.decode(response.getBody());
        } else {
            throw new IllegalArgumentException("The server rejected the revision number "
                    + "(it must be greater than 0, and no greater than the current revision number)");
        }

    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:org.openremote.android.console.net.ORConnection.java

/**
 * TODO/*  w  ww . j a  v  a  2 s .c  o  m*/
 *
 * Establish the httpconnection with url for caller and then the caller can deal with the
 * httprequest result within ORConnectionDelegate instance. if check failed, return null.
 *
 * @param context         global Android application context
 * @param httpMethod      enum POST or GET
 * @param url             the URL to connect to
 * @param useHTTPAuth     indicates whether the HTTP 'Authentication' header should be added
 *                        to the HTTP request
 *
 * @return TODO
 *
 * @throws IOException  if the URL cannot be resolved by DNS (UnknownHostException),
 *                      if the URL was not correctly formed (MalformedURLException),
 *                      if the connection timed out (SocketTimeoutException),
 *                      there was an error in the HTTP protocol (ClientProtocolException),
 *                      or any other IO error occured (generic IOException)
 */
public static HttpResponse checkURLWithHTTPProtocol(Context context, ORHttpMethod httpMethod, String urlString,
        boolean useHTTPAuth) throws IOException {
    // TODO : could move this method to ORNetworkCheck class, no one else is using it.
    //
    // TODO : use URL in the API instead of string
    //
    // Validate the URL by creating a proper URL instance from the string... it will throw
    // an MalformedURLException (IOException) in case the URL was invalid.
    //
    // This can go away when the API is fixed...

    URL targetURL = new URL(urlString);
    URI targetURI;

    try {
        targetURI = targetURL.toURI();
    } catch (URISyntaxException e) {
        // Not sure if we're ever going to hit this, but in case we do, just convert to
        // MalformedURLException...

        throw new MalformedURLException(
                "Could not convert " + urlString + " to a compliant URI: " + e.getMessage());
    }

    HttpRequestBase request = null;
    HttpResponse response = null;

    HttpParams params = new BasicHttpParams();

    // TODO : seems like timeouts ought to be externalized...

    HttpConnectionParams.setConnectionTimeout(params, 5 * 1000);
    HttpConnectionParams.setSoTimeout(params, 5 * 1000);

    HttpClient client = new DefaultHttpClient(params);

    switch (httpMethod) {
    case POST:
        request = new HttpPost(targetURI);
        break;

    case GET:
        request = new HttpGet(targetURI);
        break;

    default:
        throw new IOException("Unsupported HTTP Method: " + httpMethod);
    }

    if (useHTTPAuth) {
        SecurityUtil.addCredentialToHttpRequest(context, request);
    }

    if ("https".equals(targetURL.getProtocol())) {
        Scheme sch = new Scheme(targetURL.getProtocol(), new SelfCertificateSSLSocketFactory(context),
                targetURL.getPort());

        client.getConnectionManager().getSchemeRegistry().register(sch);
    }

    try {
        response = client.execute(request);
    } catch (IllegalArgumentException e) {
        throw new MalformedURLException("Illegal argument: " + e.getMessage());
    }

    return response;
}

From source file:org.keycloak.example.AdminClient.java

public static List<RoleRepresentation> getRealmRoles(HttpServletRequest req) throws Failure {
    KeycloakSecurityContext session = (KeycloakSecurityContext) req
            .getAttribute(KeycloakSecurityContext.class.getName());

    HttpClient client = new DefaultHttpClient();
    try {/*  w  w w.  j a  va  2s .  c  o m*/
        HttpGet get = new HttpGet(
                UriUtils.getOrigin(req.getRequestURL().toString()) + "/auth/admin/realms/demo/roles");
        get.addHeader("Authorization", "Bearer " + session.getTokenString());
        try {
            HttpResponse response = client.execute(get);
            if (response.getStatusLine().getStatusCode() != 200) {
                throw new Failure(response.getStatusLine().getStatusCode());
            }
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            try {
                return JsonSerialization.readValue(is, TypedList.class);
            } finally {
                is.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}

From source file:sh.calaba.driver.server.CalabashNodeConfiguration.java

protected static HttpClient getDefaultHttpClient() throws KeyManagementException, NoSuchAlgorithmException {
    HttpClient base = new DefaultHttpClient();

    SSLContext ctx = SSLContext.getInstance("TLS");
    X509TrustManager tm = new X509TrustManager() {

        public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
        }/*w w w .j av a2 s. c  om*/

        public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };
    ctx.init(null, new TrustManager[] { tm }, null);
    SSLSocketFactory ssf = new SSLSocketFactory(ctx);
    ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    ClientConnectionManager ccm = base.getConnectionManager();
    SchemeRegistry sr = ccm.getSchemeRegistry();
    sr.register(new Scheme("https", ssf, 443));
    return new DefaultHttpClient(ccm, base.getParams());
}

From source file:eu.lod2.ExportSelector3.java

public static List<String> request_graphs() throws Exception {

    List<String> result = null;

    HttpClient httpclient = new DefaultHttpClient();
    try {/*from   w  w  w .j  a va  2 s .c  o m*/

        String prefixurl = "http://localhost:8080/lod2webapi/graphs";

        HttpGet httpget = new HttpGet(prefixurl);
        httpget.addHeader("accept", "application/json");

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpget, responseHandler);

        result = parse_graph_api_result(responseBody);

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }

    return result;
}

From source file:org.apache.olingo.client.core.http.DefaultHttpClientFactory.java

@Override
public void close(final HttpClient httpClient) {
    httpClient.getConnectionManager().shutdown();
}

From source file:com.payu.sdk.helper.WebClientDevWrapper.java

/**
 * Wraps a default and secure httpClient
 *
 * @param base the original httpClient//from   www  . jav  a 2s  .c o m
 * @return the hhtpClient wrapped
 * @throws ConnectionException
 */
public static HttpClient wrapClient(HttpClient base) throws ConnectionException {
    try {
        SSLContext ctx = SSLContext.getInstance(Constants.SSL_PROVIDER);

        X509TrustManager tm = new X509TrustManager() {

            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
                    throws java.security.cert.CertificateException {
            }

            public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)
                    throws java.security.cert.CertificateException {
            }

            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

        };

        ctx.init(null, new TrustManager[] { tm }, null);

        SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();

        sr.register(new Scheme("https", Constants.HTTPS_PORT, ssf));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        throw new ConnectionException("Invalid SSL connection", ex);
    }
}

From source file:gov.nist.appvet.tool.synchtest.util.SSLWrapper.java

@SuppressWarnings("deprecation")
public static HttpClient wrapClient(HttpClient base) {
    SSLContext ctx = null;/*from w  ww.j av a  2s.co  m*/
    X509TrustManager tm = null;
    SSLSocketFactory ssf = null;
    SchemeRegistry sr = null;
    try {
        ctx = SSLContext.getInstance("TLSv1.2");
        tm = new X509TrustManager() {

            @Override
            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

        };
        ctx.init(null, new TrustManager[] { tm }, null);
        ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        final ClientConnectionManager ccm = base.getConnectionManager();
        sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (final Exception e) {
        return null;
    } finally {
        sr = null;
        ssf = null;
        tm = null;
        ctx = null;
    }
}

From source file:gov.nist.appvet.servlet.shared.SSLWrapper.java

@SuppressWarnings("deprecation")
public synchronized static HttpClient wrapClient(HttpClient base) {
    SSLContext ctx = null;//from   w  w w  .  j  ava2s .  co m
    X509TrustManager tm = null;
    SSLSocketFactory ssf = null;
    SchemeRegistry sr = null;
    try {
        ctx = SSLContext.getInstance("TLSv1.2");
        tm = new X509TrustManager() {

            @Override
            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

        };
        ctx.init(null, new TrustManager[] { tm }, null);
        ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        final ClientConnectionManager ccm = base.getConnectionManager();
        sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (final Exception e) {
        log.error(e.getMessage().toString());
        return null;
    } finally {
        sr = null;
        ssf = null;
        tm = null;
        ctx = null;
    }
}

From source file:org.keycloak.example.CxfRsClient.java

public static List<String> getCustomers(HttpServletRequest req) throws Failure {
    KeycloakSecurityContext session = (KeycloakSecurityContext) req
            .getAttribute(KeycloakSecurityContext.class.getName());

    HttpClient client = new HttpClientBuilder().disableTrustManager().build();
    try {/*from   w w  w .j  a  va  2  s .c o  m*/
        HttpGet get = new HttpGet(
                UriUtils.getOrigin(req.getRequestURL().toString()) + "/cxf/customerservice/customers");
        get.addHeader("Authorization", "Bearer " + session.getTokenString());
        try {
            HttpResponse response = client.execute(get);
            if (response.getStatusLine().getStatusCode() != 200) {
                throw new Failure(response.getStatusLine().getStatusCode());
            }
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            try {
                return JsonSerialization.readValue(is, TypedList.class);
            } finally {
                is.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}