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:com.nhn.android.archetype.base.AABaseApplication.java

private void closeHttpClient(HttpClient client) {
    ClientConnectionManager mgr = client.getConnectionManager();
    mgr.closeExpiredConnections();//from   www .j  a  va 2s .  co  m
    mgr.closeIdleConnections(0, TimeUnit.MILLISECONDS);
}

From source file:org.keycloak.quickstart.appjee.ServiceClient.java

public static String callService(HttpServletRequest req, KeycloakSecurityContext session, String action)
        throws Failure {
    HttpClient client = null;
    try {//from w  w w  . j  av a  2s.c o m
        client = HttpClients.createDefault();
        HttpGet get = new HttpGet(getServiceUrl(req, session) + "/" + action);
        if (session != null) {
            get.addHeader("Authorization", "Bearer " + session.getTokenString());
        }

        HttpResponse response = client.execute(get);

        StatusLine status = response.getStatusLine();
        if (status.getStatusCode() != 200) {
            throw new Failure(status.getStatusCode(), status.getReasonPhrase());
        }

        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();
        try {
            MessageBean message = (MessageBean) JsonSerialization.readValue(is, MessageBean.class);
            return message.getMessage();
        } finally {
            is.close();
        }
    } catch (Failure f) {
        throw f;
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (client != null)
            client.getConnectionManager().shutdown();
    }
}

From source file:nl.esciencecenter.octopus.webservice.JobLauncherServiceTest.java

@Test
public void useInsecureSSL_NoHostnameVerifier()
        throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException {
    HttpClient httpClient = new DefaultHttpClient();

    service.useInsecureSSL(httpClient);/*www  .ja v  a2s.  com*/

    Scheme scheme = httpClient.getConnectionManager().getSchemeRegistry().get("https");
    SSLSocketFactory factory = (SSLSocketFactory) scheme.getSchemeSocketFactory();
    assertEquals(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER,
            factory.getHostnameVerifier());
}

From source file:org.openrepose.core.services.httpclient.impl.ClientDecommissioner.java

public void addClientToBeDecommissioned(HttpClient client) {

    synchronized (listLock) {
        PoolingClientConnectionManager connMan = (PoolingClientConnectionManager) client.getConnectionManager();
        connMan.closeExpiredConnections();
        connMan.setMaxTotal(1);//from w w  w  . ja v  a 2 s. c  o m
        connMan.setDefaultMaxPerRoute(1);
        clientList.add(client);
    }
}

From source file:cn.tc.ulife.platform.msg.http.util.HttpUtil.java

private static void sslClient(HttpClient httpClient) {
    try {/*from w  w  w .j a  va2 s .c om*/
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(X509Certificate[] xcs, String str) {

            }

            public void checkServerTrusted(X509Certificate[] xcs, String str) {

            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = httpClient.getConnectionManager();
        SchemeRegistry registry = ccm.getSchemeRegistry();
        registry.register(new Scheme("https", ssf, 443));
    } catch (KeyManagementException ex) {
        throw new RuntimeException(ex);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.aliyun.api.gateway.demo.util.HttpUtil.java

private static void sslClient(HttpClient httpClient) {
    try {/*w ww. j  a va 2  s .  c om*/
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(X509Certificate[] xcs, String str) {

            }

            public void checkServerTrusted(X509Certificate[] xcs, String str) {

            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = httpClient.getConnectionManager();
        SchemeRegistry registry = ccm.getSchemeRegistry();
        registry.register(new Scheme("https", 443, ssf));
    } catch (KeyManagementException ex) {
        throw new RuntimeException(ex);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:ca.sqlpower.wabit.enterprise.client.WabitClientSession.java

/**
 * Sends an HTTP request to a Wabit Enterprise Server to create a new remote
 * Wabit Workspace on that server.//from   w w  w .j  av a2 s.c  o  m
 * 
 * @param serviceInfo
 *            A {@link SPServerInfo} containing the connection
 *            information for that server
 * @return The {@link WorkspaceLocation} of the newly created remote
 *         WabitWorkspace
 * @throws URISyntaxException
 * @throws ClientProtocolException
 * @throws IOException
 * @throws JSONException
 */
public static WorkspaceLocation createNewServerSession(SPServerInfo serviceInfo)
        throws URISyntaxException, ClientProtocolException, IOException, JSONException {
    HttpClient httpClient = createHttpClient(serviceInfo);
    try {
        HttpUriRequest request = new HttpPost(getServerURI(serviceInfo, "workspaces"));
        String responseBody = httpClient.execute(request, new BasicResponseHandler());
        JSONObject response = new JSONObject(responseBody);
        logger.debug("New Workspace:" + responseBody);
        return new WorkspaceLocation(response.getString("name"), response.getString("UUID"), serviceInfo);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:com.viettel.dms.download.DownloadFile.java

/**
 * Download file su dung httpclient request
 * @param url/*from  ww w. jav  a  2s  .  c  o m*/
 * @param output
 * @param tmpDir
 */
public static void downloadWithHTTPClient(String url, File output, File tmpDir) {
    File tmp = null;
    HttpClient httpclient = new DefaultHttpClient();
    BufferedOutputStream os = null;
    BufferedInputStream is = null;
    try {
        VTLog.i("DownloadDB", "Downloading url :" + url);

        tmp = File.createTempFile("download", ".tmp", tmpDir);
        HttpGet httpget = new HttpGet(url);
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            is = new BufferedInputStream(entity.getContent());
            os = new BufferedOutputStream(new FileOutputStream(tmp));

            copyStream(is, os);
            tmp.renameTo(output);
            tmp = null;
            httpclient.getConnectionManager().shutdown();
        }
    } catch (IOException e) {
        VTLog.e("DownloadDB", "Loi download file db " + e.toString());
        VTLog.e("UnexceptionLog", VNMTraceUnexceptionLog.getReportFromThrowable(e));
        throw new RuntimeException(e);
    } catch (Exception e) {
        VTLog.e("DownloadDB", "Loi download file db " + e.toString());
        VTLog.e("UnexceptionLog", VNMTraceUnexceptionLog.getReportFromThrowable(e));
        throw new RuntimeException(e);
    } finally {
        if (tmp != null) {
            try {
                tmp.delete();
                tmp = null;
            } catch (Exception ignore) {
                ;
            }
        }
        if (is != null) {
            try {
                is.close();
                is = null;
            } catch (Exception ignore) {
                ;
            }
        }
        if (os != null) {
            try {
                os.close();
                os = null;
            } catch (Exception ignore) {
                ;
            }
        }
    }
}

From source file:de.escidoc.core.om.business.fedora.item.ItemHandlerBase.java

/**
 * Handle a Fedora Exception thrown while uploading content.
 *
 * @param url The URL.//from ww w  .j a  v a 2 s.  co m
 * @param e   The Fedora Exception.
 * @throws FileNotFoundException Thrown if access to remote resource failed.
 * @throws FedoraSystemException Thrown if Fedora reports an error.
 */
static void handleFedoraUploadError(final String url, final Exception e)
        throws FileNotFoundException, FedoraSystemException {

    final Matcher matcherErrorGetting = PATTERN_ERROR_GETTING.matcher(e.getMessage());
    final Matcher matcherMalformedUrl = PATTERN_MALFORMED_URL.matcher(e.getMessage());

    if (matcherErrorGetting.find() || matcherMalformedUrl.find()) {
        throw new FileNotFoundException("Error getting content from " + url, e);
    }
    if (!(url.startsWith("http://") || url.startsWith("https://"))) {
        throw new FileNotFoundException(ERROR_MSG_NO_HTTP_PROTOCOL);
    }
    // TODO: Reuse HttpClient
    final HttpClient client = new DefaultHttpClient();
    try {
        final HttpUriRequest method = new HttpGet(url);
        final HttpResponse response = client.execute(method);
        final int resultCode = response.getStatusLine().getStatusCode();
        if (resultCode != HttpServletResponse.SC_OK) {
            throw new FileNotFoundException(
                    StringUtility.format("Bad request. ", response.getStatusLine(), url));
        }

    } catch (final Exception e1) {
        throw new FileNotFoundException("Error getting content from " + url, e1);
    } finally {
        client.getConnectionManager().shutdown();
    }
    throw new FedoraSystemException(e);
}