Package org.apache.commons.httpclient

Examples of org.apache.commons.httpclient.HttpConnectionManager


    private void initHttpClient() throws URIException {
        httpclient = new HttpClient();
        HostConfiguration hostConfig = new HostConfiguration();
        org.apache.commons.httpclient.URI uri = new org.apache.commons.httpclient.URI(getNuxeoRestUrl(), false);
        hostConfig.setHost(uri);
        HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
        HttpConnectionManagerParams params = new HttpConnectionManagerParams();
        int maxHostConnections = 20;
        params.setMaxConnectionsPerHost(hostConfig, maxHostConnections);
        connectionManager.setParams(params);
        httpclient = new HttpClient(connectionManager);
        httpclient.setHostConfiguration(hostConfig);
        Credentials creds = new UsernamePasswordCredentials(userName, password);
        AuthScope authScope = new AuthScope(hostConfig.getHost(), hostConfig.getPort());
        httpclient.getState().setCredentials(authScope, creds);
View Full Code Here


    public synchronized void run() {
        while (!shutdown) {
            Iterator iter = connectionManagers.iterator();
           
            while (iter.hasNext()) {
                HttpConnectionManager connectionManager = (HttpConnectionManager) iter.next();
                handleCloseIdleConnections(connectionManager);
            }
           
            try {
                this.wait(timeoutInterval);
View Full Code Here

    {
        HttpConnectionManagerParams managerParams = new HttpConnectionManagerParams();
        managerParams.setSoTimeout(timeout);
        managerParams.setConnectionTimeout(timeout);

        HttpConnectionManager manager = new SimpleHttpConnectionManager();
        manager.setParams(managerParams);

        HttpClientParams params = new HttpClientParams();
        params.setConnectionManagerTimeout(timeout);
        params.setSoTimeout(timeout);
View Full Code Here

     */
    public static String get(String url, String username, String pw) {

        GetMethod httpMethod = null;
        HttpClient client = new HttpClient();
        HttpConnectionManager connectionManager = client.getHttpConnectionManager();
        try {
            setAuth(client, url, username, pw);
            httpMethod = new GetMethod(url);
            connectionManager.getParams().setConnectionTimeout(5000);
            int status = client.executeMethod(httpMethod);
            if (status == HttpStatus.SC_OK) {
                InputStream is = httpMethod.getResponseBodyAsStream();
                String response = IOUtils.toString(is);
                IOUtils.closeQuietly(is);
                if (response.trim().length() == 0) { // sometime gs rest fails
                    LOGGER.warn("ResponseBody is empty");
                    return null;
                } else {
                    return response;
                }
            } else {
                LOGGER.info("(" + status + ") " + HttpStatus.getStatusText(status) + " -- " + url);
            }
        } catch (ConnectException e) {
            LOGGER.info("Couldn't connect to [" + url + "]");
        } catch (IOException e) {
            LOGGER.info("Error talking to [" + url + "]", e);
        } finally {
            if (httpMethod != null)
                httpMethod.releaseConnection();
            connectionManager.closeIdleConnections(0);
        }

        return null;
    }
View Full Code Here

     * @return the HTTP response or <TT>null</TT> on errors.
     */
    private static String send(final EntityEnclosingMethod httpMethod, String url,
                               RequestEntity requestEntity, String username, String pw) {
        HttpClient client = new HttpClient();
        HttpConnectionManager connectionManager = client.getHttpConnectionManager();
        try {
            setAuth(client, url, username, pw);
            connectionManager.getParams().setConnectionTimeout(5000);
            if (requestEntity != null)
                httpMethod.setRequestEntity(requestEntity);
            int status = client.executeMethod(httpMethod);

            switch (status) {
            case HttpURLConnection.HTTP_OK:
            case HttpURLConnection.HTTP_CREATED:
            case HttpURLConnection.HTTP_ACCEPTED:
                String response = IOUtils.toString(httpMethod.getResponseBodyAsStream());
                // LOGGER.info("================= POST " + url);
                if (LOGGER.isInfoEnabled())
                    LOGGER.info("HTTP " + httpMethod.getStatusText() + ": " + response);
                return response;
            default:
                LOGGER.warn("Bad response: code[" + status + "]" + " msg[" + httpMethod.getStatusText() + "]"
                            + " url[" + url + "]" + " method[" + httpMethod.getClass().getSimpleName()
                            + "]: " + IOUtils.toString(httpMethod.getResponseBodyAsStream()));
                return null;
            }
        } catch (ConnectException e) {
            LOGGER.info("Couldn't connect to [" + url + "]");
            return null;
        } catch (IOException e) {
            LOGGER.error("Error talking to " + url + " : " + e.getLocalizedMessage());
            return null;
        } finally {
            if (httpMethod != null)
                httpMethod.releaseConnection();
            connectionManager.closeIdleConnections(0);
        }
    }
View Full Code Here

    public static boolean delete(String url, final String user, final String pw) {

        DeleteMethod httpMethod = null;
        HttpClient client = new HttpClient();
        HttpConnectionManager connectionManager = client.getHttpConnectionManager();
        try {
            setAuth(client, url, user, pw);
            httpMethod = new DeleteMethod(url);
            connectionManager.getParams().setConnectionTimeout(5000);
            int status = client.executeMethod(httpMethod);
            String response = "";
            if (status == HttpStatus.SC_OK) {
                InputStream is = httpMethod.getResponseBodyAsStream();
                response = IOUtils.toString(is);
                IOUtils.closeQuietly(is);
                if (response.trim().equals("")) {
                    if (LOGGER.isDebugEnabled())
                        LOGGER
                            .debug("ResponseBody is empty (this may be not an error since we just performed a DELETE call)");
                    return true;
                }
                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("(" + status + ") " + httpMethod.getStatusText() + " -- " + url);
                return true;
            } else {
                LOGGER.info("(" + status + ") " + httpMethod.getStatusText() + " -- " + url);
                LOGGER.info("Response: '" + response + "'");
            }
        } catch (ConnectException e) {
            LOGGER.info("Couldn't connect to [" + url + "]");
        } catch (IOException e) {
            LOGGER.info("Error talking to [" + url + "]", e);
        } finally {
            if (httpMethod != null)
                httpMethod.releaseConnection();
            connectionManager.closeIdleConnections(0);
        }

        return false;
    }
View Full Code Here

    public static boolean httpPing(String url, String username, String pw) {

        GetMethod httpMethod = null;
        HttpClient client = new HttpClient();
        HttpConnectionManager connectionManager = client.getHttpConnectionManager();
        try {
            setAuth(client, url, username, pw);
            httpMethod = new GetMethod(url);
            connectionManager.getParams().setConnectionTimeout(2000);
            int status = client.executeMethod(httpMethod);
            if (status != HttpStatus.SC_OK) {
                LOGGER.warn("PING failed at '" + url + "': (" + status + ") " + httpMethod.getStatusText());
                return false;
            } else {
                return true;
            }
        } catch (ConnectException e) {
            return false;
        } catch (IOException e) {
            LOGGER.error(e.getLocalizedMessage(),e);
            return false;
        } finally {
            if (httpMethod != null)
                httpMethod.releaseConnection();
            connectionManager.closeIdleConnections(0);
        }
    }
View Full Code Here

     */
    public static boolean exists(String url, String username, String pw) {

        GetMethod httpMethod = null;
        HttpClient client = new HttpClient();
        HttpConnectionManager connectionManager = client.getHttpConnectionManager();
        try {
            setAuth(client, url, username, pw);
            httpMethod = new GetMethod(url);
            connectionManager.getParams().setConnectionTimeout(2000);
            int status = client.executeMethod(httpMethod);
            switch (status) {
            case HttpStatus.SC_OK:
                return true;
            case HttpStatus.SC_NOT_FOUND:
                return false;
            default:
                throw new RuntimeException("Unhandled response status at '" + url + "': (" + status + ") "
                                           + httpMethod.getStatusText());
            }
        } catch (ConnectException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (httpMethod != null)
                httpMethod.releaseConnection();
            connectionManager.closeIdleConnections(0);
        }
    }
View Full Code Here

        HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
        // setup the httpConnectionManagerParams
        IntrospectionSupport.setProperties(connectionManagerParams, parameters, "httpConnectionManager.");
        validateParameters(uri, parameters, "httpConnectionManager.");
        // make sure the component httpConnectionManager is take effect
        HttpConnectionManager thisHttpConnectionManager = httpConnectionManager;
        if (thisHttpConnectionManager == null) {
            // only set the params on the new created http connection manager
            thisHttpConnectionManager = new MultiThreadedHttpConnectionManager();
            thisHttpConnectionManager.setParams(connectionManagerParams);
        }
        // create the configurer to use for this endpoint (authMethods contains the used methods created by the configurer)
        final Set<AuthMethod> authMethods = new LinkedHashSet<AuthMethod>();
        HttpClientConfigurer configurer = createHttpClientConfigurer(parameters, authMethods);
        URI endpointUri = URISupport.createRemainingURI(new URI(addressUri), httpClientParameters);
View Full Code Here

    @Override
    public void closeCommunicationLink()
    {
        if (getClient() != null)
        {
            final HttpConnectionManager mgr = getClient().getHttpConnectionManager();
            if (mgr instanceof MultiThreadedHttpConnectionManager)
            {
                ((MultiThreadedHttpConnectionManager) mgr).shutdown();
            }
        }
View Full Code Here

TOP

Related Classes of org.apache.commons.httpclient.HttpConnectionManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.