Example usage for org.apache.commons.httpclient HostConfiguration HostConfiguration

List of usage examples for org.apache.commons.httpclient HostConfiguration HostConfiguration

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HostConfiguration HostConfiguration.

Prototype

public HostConfiguration() 

Source Link

Usage

From source file:org.eclipse.virgo.ide.runtime.internal.core.utils.WebDownloadUtils.java

public static File downloadFile(String url, File outputPath, IProgressMonitor monitor) {
    GetMethod method = null;/*from w  w  w .java2 s. c om*/
    BufferedInputStream bis = null;
    FileOutputStream fos = null;
    File outputFile = null;
    try {
        if (monitor.isCanceled()) {
            return null;
        }
        HttpClient client = new HttpClient();
        method = new GetMethod(url);
        method.setFollowRedirects(true);

        HostConfiguration hostConfiguration = new HostConfiguration();
        hostConfiguration.setHost(getHost(url), HTTP_PORT, "http");

        configureHttpClientProxy(url, client, hostConfiguration);

        client.getHttpConnectionManager().getParams().setSoTimeout(SOCKET_TIMEOUT);
        client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNNECT_TIMEOUT);

        // Execute the GET method
        int statusCode = client.executeMethod(hostConfiguration, method);
        if (statusCode == 200) {

            int ix = method.getPath().lastIndexOf('/');
            String fileName = method.getPath().substring(ix + 1);
            monitor.subTask("Downloading file '" + fileName + "'");

            if (monitor.isCanceled()) {
                return null;
            }

            Header[] header = method.getResponseHeaders("content-length");
            long totalBytes = Long.valueOf(header[0].getValue());

            InputStream is = method.getResponseBodyAsStream();
            bis = new BufferedInputStream(is);
            outputFile = new File(outputPath, fileName);
            fos = new FileOutputStream(outputFile);
            long bytesRead = 0;
            byte[] bytes = new byte[8192];
            int count = bis.read(bytes);
            while (count != -1 && count <= 8192) {
                if (monitor.isCanceled()) {
                    return null;
                }
                bytesRead = bytesRead + count;
                float percent = Float.valueOf(bytesRead) / Float.valueOf(totalBytes) * 100;
                monitor.subTask("Downloading file '" + fileName + "': " + bytesRead + "bytes/" + totalBytes
                        + "bytes (" + Math.round(percent) + "%)");
                fos.write(bytes, 0, count);
                count = bis.read(bytes);
            }
            if (count != -1) {
                fos.write(bytes, 0, count);
            }
        }

        return outputFile;
    } catch (Exception e) {
        ServerCorePlugin.getDefault().getLog().log(new Status(IStatus.ERROR, ServerCorePlugin.PLUGIN_ID, 1,
                "Error downloading bundle/library", e));
    } finally {
        try {
            if (method != null) {
                method.releaseConnection();
            }
        } catch (Exception e2) {
        }
        try {
            if (bis != null) {
                bis.close();
            }
        } catch (Exception e2) {
        }
        try {
            if (fos != null) {
                fos.close();
            }
        } catch (Exception e2) {
        }
    }

    return null;
}

From source file:org.elasticsearch.hadoop.rest.commonshttp.CommonsHttpTransport.java

public CommonsHttpTransport(Settings settings, String host) {
    this.settings = settings;
    httpInfo = host;// ww w .j  a  v a 2 s .  c o m

    HttpClientParams params = new HttpClientParams();
    params.setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(settings.getHttpRetries(), false) {

                @Override
                public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
                    if (super.retryMethod(method, exception, executionCount)) {
                        stats.netRetries++;
                        return true;
                    }
                    return false;
                }
            });

    params.setConnectionManagerTimeout(settings.getHttpTimeout());
    params.setSoTimeout((int) settings.getHttpTimeout());
    HostConfiguration hostConfig = new HostConfiguration();

    hostConfig = setupSSLIfNeeded(settings, hostConfig);
    hostConfig = setupSocksProxy(settings, hostConfig);
    Object[] authSettings = setupHttpProxy(settings, hostConfig);
    hostConfig = (HostConfiguration) authSettings[0];

    try {
        hostConfig.setHost(new URI(escapeUri(host, settings.getNetworkSSLEnabled()), false));
    } catch (IOException ex) {
        throw new EsHadoopTransportException("Invalid target URI " + host, ex);
    }
    client = new HttpClient(params, new SocketTrackingConnectionManager());
    client.setHostConfiguration(hostConfig);

    addHttpAuth(settings, authSettings);
    completeAuth(authSettings);

    HttpConnectionManagerParams connectionParams = client.getHttpConnectionManager().getParams();
    // make sure to disable Nagle's protocol
    connectionParams.setTcpNoDelay(true);

    if (log.isTraceEnabled()) {
        log.trace("Opening HTTP transport to " + httpInfo);
    }
}

From source file:org.elasticsearch.hadoop.rest.RestClient.java

public RestClient(Settings settings) {
    HttpClientParams params = new HttpClientParams();
    params.setConnectionManagerTimeout(settings.getHttpTimeout());

    client = new HttpClient(params);
    HostConfiguration hostConfig = new HostConfiguration();
    String targetUri = settings.getTargetUri();
    try {/*w  ww .jav a2  s.  c om*/
        hostConfig.setHost(new URI(targetUri, false));
    } catch (IOException ex) {
        throw new IllegalArgumentException("Invalid target URI " + targetUri, ex);
    }
    client.setHostConfiguration(hostConfig);

    scrollKeepAlive = TimeValue.timeValueMillis(settings.getScrollKeepAlive());
    indexReadMissingAsEmpty = settings.getIndexReadMissingAsEmpty();
}

From source file:org.exoplatform.bonitaextension.connectors.webdav.common.WebDAVClient.java

/**
 * Create a new webdav Client with a user name and a password to connect to eXo server
 *
 * @param host//w  w w  .j  a v  a 2 s  .  c  om
 * @param port
 * @param username
 * @param password
 */
public WebDAVClient(String host, Long port, String username, String password) {

    if (LOGGER.isLoggable(Level.INFO)) {
        LOGGER.info("WebDAVConnector {host=" + host + ", port=" + port + ", username=" + username
                + ", password=******}");
    }

    HostConfiguration hostConfig = new HostConfiguration();
    hostConfig.setHost(host, port.intValue());

    HttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    int maxHostConnections = 20;
    params.setMaxConnectionsPerHost(hostConfig, maxHostConnections);
    connectionManager.setParams(params);

    client = new HttpClient(connectionManager);
    Credentials creds = new UsernamePasswordCredentials(username, password);
    client.getState().setCredentials(AuthScope.ANY, creds);
    client.setHostConfiguration(hostConfig);

}

From source file:org.exoplatform.calendar.service.impl.RemoteCalendarServiceImpl.java

@Override
public boolean isValidRemoteUrl(String url, String type, String remoteUser, String remotePassword)
        throws IOException, UnsupportedOperationException {
    try {/*from www  .  ja  v a2  s.  co  m*/
        HttpClient client = new HttpClient();
        HostConfiguration hostConfig = new HostConfiguration();
        String host = new URL(url).getHost();
        if (StringUtils.isEmpty(host))
            host = url;
        hostConfig.setHost(host);
        client.setHostConfiguration(hostConfig);
        Credentials credentials = null;
        client.setHostConfiguration(hostConfig);
        if (!StringUtils.isEmpty(remoteUser)) {
            credentials = new UsernamePasswordCredentials(remoteUser, remotePassword);
            client.getState().setCredentials(new AuthScope(host, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
                    credentials);
        }

        if (CalendarService.ICALENDAR.equals(type)) {
            GetMethod get = new GetMethod(url);
            client.executeMethod(get);
            int statusCode = get.getStatusCode();
            get.releaseConnection();
            return (statusCode == HttpURLConnection.HTTP_OK);
        } else {
            if (CalendarService.CALDAV.equals(type)) {
                OptionsMethod options = new OptionsMethod(url);
                client.executeMethod(options);
                Header header = options.getResponseHeader("DAV");
                options.releaseConnection();
                if (header == null) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Cannot connect to remoter server or not support WebDav access");
                    }
                    return false;
                }
                Boolean support = header.toString().contains("calendar-access");
                options.releaseConnection();
                if (!support) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Remote server does not support CalDav access");
                    }
                    throw new UnsupportedOperationException("Remote server does not support CalDav access");
                }
                return support;
            }
            return false;
        }
    } catch (MalformedURLException e) {
        if (logger.isDebugEnabled())
            logger.debug(e.getMessage(), e);
        throw new IOException("URL is invalid. Maybe no legal protocol or URl could not be parsed");
    } catch (IOException e) {
        if (logger.isDebugEnabled())
            logger.debug(e.getMessage(), e);
        throw new IOException("Error occurs when connecting to remote server");
    }
}

From source file:org.exoplatform.calendar.service.impl.RemoteCalendarServiceImpl.java

/**
 * Get the HttpClient object to prepare for the connection with remote server
 * @param remoteCalendar holds information about remote server
 * @return HttpClient object//from  ww  w  . j  av  a2  s .  c o  m
 * @throws Exception
 */
public HttpClient getRemoteClient(RemoteCalendar remoteCalendar) throws Exception {
    HostConfiguration hostConfig = new HostConfiguration();
    String host = new URL(remoteCalendar.getRemoteUrl()).getHost();
    if (Utils.isEmpty(host))
        host = remoteCalendar.getRemoteUrl();
    hostConfig.setHost(host);
    HttpClient client = new HttpClient();
    client.setHostConfiguration(hostConfig);
    client.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
    client.getHttpConnectionManager().getParams().setSoTimeout(10000);
    // basic authentication
    if (!Utils.isEmpty(remoteCalendar.getRemoteUser())) {
        Credentials credentials = new UsernamePasswordCredentials(remoteCalendar.getRemoteUser(),
                remoteCalendar.getRemotePassword());
        client.getState().setCredentials(new AuthScope(host, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
                credentials);
    }
    return client;
}

From source file:org.fao.geonet.lib.NetLib.java

/** Setup proxy for http client
  *///from  w  w  w .jav  a2s.  c  o  m
public void setupProxy(SettingManager sm, HttpClient client) {
    boolean enabled = sm.getValueAsBool(ENABLED, false);
    String host = sm.getValue(HOST);
    String port = sm.getValue(PORT);
    String username = sm.getValue(USERNAME);
    String password = sm.getValue(PASSWORD);

    if (enabled) {
        if (!Lib.type.isInteger(port)) {
            Log.error(Geonet.GEONETWORK, "Proxy port is not an integer : " + port);
        } else {
            HostConfiguration config = client.getHostConfiguration();
            if (config == null)
                config = new HostConfiguration();
            config.setProxy(host, Integer.parseInt(port));
            client.setHostConfiguration(config);

            if (username.trim().length() != 0) {
                Credentials cred = new UsernamePasswordCredentials(username, password);
                AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);

                client.getState().setProxyCredentials(scope, cred);
            }
            List authPrefs = new ArrayList(2);
            authPrefs.add(AuthPolicy.DIGEST);
            authPrefs.add(AuthPolicy.BASIC);
            // This will exclude the NTLM authentication scheme
            client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
        }
    }
}

From source file:org.globus.axis.transport.commons.tests.CommonsHttpConnectionManagerTest.java

public void testConnectionReuseWithoutParams() throws Exception {
    CommonsHttpConnectionManager manager = new CommonsHttpConnectionManager(null);

    HostConfiguration h1 = new HostConfiguration();
    h1.setHost(address, server1.getLocalPort());

    HttpConnection c1 = manager.getConnection(h1);

    // new connection
    assertTrue(!c1.isOpen());/*from w  w  w.j  av  a 2s .  co  m*/

    c1.open();
    c1.releaseConnection();

    HostConfiguration h2 = new HostConfiguration();
    h2.setHost(address, server1.getLocalPort());

    HttpConnection c2 = manager.getConnection(h2);

    // connection should have been released
    // so c2 is c1
    assertTrue(h2.equals(h1));
    assertTrue(c2 == c1);
    assertTrue(c2.isOpen());

    HttpConnection c3 = manager.getConnection(h2);

    // connection c2 was not released so new connection
    // c2 != c3
    assertTrue(!c3.isOpen());
    assertTrue(c3 != c2);
    assertTrue(c3 != c1);

    c2.releaseConnection();
    c3.releaseConnection();

    Server server2 = new Server();

    // it's a new port
    HostConfiguration h4 = new HostConfiguration();
    h4.setHost(address, server2.getLocalPort());

    HttpConnection c4 = manager.getConnection(h4);

    assertTrue(!c4.isOpen());
    assertTrue(c4 != c1);
    assertTrue(c4 != c2);
    assertTrue(c4 != c3);

    server2.close();
}

From source file:org.globus.axis.transport.commons.tests.CommonsHttpConnectionManagerTest.java

public void testConnectionReuseWithParams() throws Exception {
    CommonsHttpConnectionManager manager = new CommonsHttpConnectionManager(PARAMS);

    HostConfiguration h1 = new HostConfiguration();
    h1.setHost(address, server1.getLocalPort());
    h1.getParams().setParameter("A", "foo");
    h1.getParams().setParameter("B", "bar");
    h1.getParams().setParameter("C", "fff");

    HttpConnection c1 = manager.getConnection(h1);

    assertTrue(!c1.isOpen());/*w  ww  .  j a v a 2  s  .co m*/
    c1.open();
    c1.releaseConnection();

    HostConfiguration h2 = new HostConfiguration();
    h2.setHost(address, server1.getLocalPort());
    h2.getParams().setParameter("A", "foo");
    h2.getParams().setParameter("B", "bar");
    // still should be reused since C is not checked param
    h2.getParams().setParameter("C", "ggg");

    HttpConnection c2 = manager.getConnection(h2);

    // connection should have been released
    // so c2 is c1
    assertTrue(h2.equals(h1));
    assertTrue(c2.isOpen());
    assertTrue(c2 == c1);

    HttpConnection c3 = manager.getConnection(h2);

    // new connection becuase it wasn't released
    assertTrue(c3 != c1);
    assertTrue(c3 != c2);
    assertTrue(!c3.isOpen());

    c2.releaseConnection();
    c3.releaseConnection();

    // this one does not have params
    HostConfiguration h4 = new HostConfiguration();
    h4.setHost(address, server1.getLocalPort());

    HttpConnection c4 = manager.getConnection(h4);

    // new connection
    assertTrue(c4 != c1);
    assertTrue(c4 != c2);
    assertTrue(c4 != c3);
    assertTrue(!c4.isOpen());

    c4.open();
    c4.releaseConnection();

    // this one only has B parameter
    HostConfiguration h5 = new HostConfiguration();
    h5.setHost(address, server1.getLocalPort());
    h5.getParams().setParameter("B", "bar");

    HttpConnection c5 = manager.getConnection(h5);

    // also a new connection
    assertTrue(c5 != c1);
    assertTrue(c5 != c2);
    assertTrue(c5 != c3);
    assertTrue(c5 != c4);
    assertTrue(!c5.isOpen());

    c5.open();
    c5.releaseConnection();

    // this one only has different B parameter
    HostConfiguration h6 = new HostConfiguration();
    h6.setHost(address, server1.getLocalPort());
    h6.getParams().setParameter("A", "fooo");
    h6.getParams().setParameter("B", "bar");

    HttpConnection c6 = manager.getConnection(h6);

    assertTrue(c6 != c1);
    assertTrue(c6 != c2);
    assertTrue(c6 != c3);
    assertTrue(c6 != c4);
    assertTrue(c6 != c5);
    assertTrue(!c6.isOpen());

    c6.open();
    c6.releaseConnection();
}

From source file:org.globus.axis.transport.commons.tests.CommonsHttpConnectionManagerTest.java

public void testIdleConnectionSweeper() throws Exception {
    CommonsHttpConnectionManager manager = new CommonsHttpConnectionManager(null);
    manager.setConnectionIdleTime(1000 * 2);

    HostConfiguration h1 = new HostConfiguration();
    h1.setHost(address, server1.getLocalPort());

    HttpConnection c1 = manager.getConnection(h1);

    // new connection
    assertTrue(!c1.isOpen());/*from   w w  w.j a  v a  2s. c om*/
    c1.open();

    Thread.sleep(1000);

    c1.releaseConnection();

    assertTrue(c1 == manager.getConnection(h1));
    assertTrue(c1.isOpen());
    c1.releaseConnection();

    Thread.sleep(1000 * 4);

    HttpConnection c2 = manager.getConnection(h1);

    assertTrue(c1 != c2);
}