Example usage for org.apache.commons.httpclient HttpClient getHostConfiguration

List of usage examples for org.apache.commons.httpclient HttpClient getHostConfiguration

Introduction

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

Prototype

public HostConfiguration getHostConfiguration()

Source Link

Usage

From source file:org.apache.wookie.proxy.ConnectionsPrefsManager.java

public static void setProxySettings(HttpClient client, Configuration properties) {
    if (!fParsedFile)
        init(properties); // just do this once - will have to reboot for changes to take effect

    HostConfiguration hConf = client.getHostConfiguration();

    hConf.setProxy("wwwcache.open.ac.uk", 80);
    //client.s//  w  w w.j a v a  2s  .  c om

    if (isProxyServerSet()) {
        int port;
        try {
            port = properties.getInt("widget.proxy.port");
        } catch (Exception ex) {
            port = 8080; // default for now if not specified
        }
        String username = properties.getString("widget.proxy.username");
        String password = properties.getString("widget.proxy.password");

        hConf = client.getHostConfiguration();
        hConf.setProxy(fHostname, port);

        hConf.setProxy("wwwcache.open.ac.uk", 80);
        //AuthScope scopeProxy = new AuthScope(fHostname,port , AuthScope.ANY_REALM);
        //             if(fUseNTLMAuthentication){
        //             String domain = System.getenv("USERDOMAIN");    //$NON-NLS-1$
        //             String computerName = System.getenv("COMPUTERNAME");//$NON-NLS-1$
        //             if (domain!=null && computerName!=null){
        //                NTCredentials userNTLMCredentials = new NTCredentials(username, password, computerName, domain);
        //                client.getState().setProxyCredentials(scopeProxy, userNTLMCredentials);
        //             }
        //             else {
        //                fLogger.error("Cannot find domain or computername for NTLM proxy authentication");
        //             }
        //          }
        //          else{
        //             client.getState().setProxyCredentials(scopeProxy, new UsernamePasswordCredentials(username, password));
        //          }
    }

}

From source file:org.archive.crawler.fetcher.OptimizeFetchHTTP.java

/**
 * Configure the HttpMethod setting options and headers.
 *
 * @param curi CrawlURI from which we pull configuration.
 * @param method The Method to configure.
 * @return HostConfiguration copy customized for this CrawlURI
 *///ww  w  . j a v  a 2 s  . c o  m
protected HostConfiguration configureMethod(CrawlURI curi, HttpMethod method) {
    // Don't auto-follow redirects
    method.setFollowRedirects(false);

    //        // set soTimeout
    //        method.getParams().setSoTimeout(
    //                ((Integer) getUncheckedAttribute(curi, ATTR_SOTIMEOUT_MS))
    //                        .intValue());

    // Set cookie policy.
    method.getParams()
            .setCookiePolicy((((Boolean) getUncheckedAttribute(curi, ATTR_IGNORE_COOKIES)).booleanValue())
                    ? CookiePolicy.IGNORE_COOKIES
                    : CookiePolicy.BROWSER_COMPATIBILITY);

    // Use only HTTP/1.0 (to avoid receiving chunked responses)
    method.getParams().setVersion(HttpVersion.HTTP_1_0);

    CrawlOrder order = getSettingsHandler().getOrder();
    String userAgent = curi.getUserAgent();
    if (userAgent == null) {
        userAgent = order.getUserAgent(curi);
    }
    method.setRequestHeader("User-Agent", userAgent);
    method.setRequestHeader("From", order.getFrom(curi));

    // Set retry handler.
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HeritrixHttpMethodRetryHandler());

    final long maxLength = getMaxLength(curi);
    if (maxLength > 0 && ((Boolean) getUncheckedAttribute(curi, ATTR_SEND_RANGE)).booleanValue()) {
        method.addRequestHeader(RANGE, RANGE_PREFIX.concat(Long.toString(maxLength - 1)));
    }

    if (((Boolean) getUncheckedAttribute(curi, ATTR_SEND_CONNECTION_CLOSE)).booleanValue()) {
        method.addRequestHeader(HEADER_SEND_CONNECTION_CLOSE);
    }

    if (((Boolean) getUncheckedAttribute(curi, ATTR_SEND_REFERER)).booleanValue()
            && (curi.getViaContext() == null || !Link.PREREQ_MISC.equals(curi.getViaContext().toString()))) {
        // RFC2616 says no referer header if referer is https and the url
        // is not
        String via = curi.flattenVia();
        if (via != null && via.length() > 0
                && !(via.startsWith(HTTPS_SCHEME) && curi.getUURI().getScheme().equals(HTTP_SCHEME))) {
            method.setRequestHeader(REFERER, via);
        }
    }

    /*if(!curi.isPrerequisite() && curi.containsKey(URLInfo.MODIFY_TIME) &&
       (Boolean)getUncheckedAttribute(curi, ATTR_SEND_IF_MODIFIED_SINCE)) {
      long modifyTime = curi.getLong(URLInfo.MODIFY_TIME);
      if (modifyTime != 0) {
      Date date = new Date(modifyTime);
      method.setRequestHeader("If-Modified-Since", date.toString());
      logger.debug(curi.getUURI().toString() + " send header modifyTime:" + date.toGMTString());
      }
              
      setConditionalGetHeader(curi, method, ATTR_SEND_IF_MODIFIED_SINCE, 
           CoreAttributeConstants.A_LAST_MODIFIED_HEADER, "If-Modified-Since");
      setConditionalGetHeader(curi, method, ATTR_SEND_IF_NONE_MATCH, 
        CoreAttributeConstants.A_ETAG_HEADER, "If-None-Match");
    }*/

    // TODO: What happens if below method adds a header already
    // added above: e.g. Connection, Range, or Referer?
    setAcceptHeaders(curi, method);

    HttpClient http = getClient();
    HostConfiguration config = new HostConfiguration(http.getHostConfiguration());
    configureProxy(curi, config);
    configureBindAddress(curi, config);
    return config;
}

From source file:org.archive.crawler.fetcher.OptimizeFetchHTTP.java

private void setLocalIP(HttpClient http) {
    String localIP = IPSwitcher.getLocalIP();
    if (localIP != null) {
        try {/*  www  .  java  2 s.  co  m*/
            http.getHostConfiguration().setLocalAddress(InetAddress.getByName(localIP));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.archive.crawler.fetcher.OptimizeFetchHTTP.java

protected HttpClient configureHttp() throws RuntimeException {
    // Get timeout.  Use it for socket and for connection timeout.
    int timeout = (getSoTimeout(null) > 0) ? getSoTimeout(null) : 0;

    // HttpConnectionManager cm = new ThreadLocalHttpConnectionManager();
    HttpConnectionManager cm = new SingleHttpConnectionManager();

    // TODO: The following settings should be made in the corresponding
    // HttpConnectionManager, not here.
    HttpConnectionManagerParams hcmp = cm.getParams();
    hcmp.setConnectionTimeout(timeout);/*from  ww w.j ava  2  s .co  m*/
    hcmp.setStaleCheckingEnabled(true);
    // Minimizes bandwidth usage.  Setting to true disables Nagle's
    // algorithm.  IBM JVMs < 142 give an NPE setting this boolean
    // on ssl sockets.
    hcmp.setTcpNoDelay(false);

    HttpClient http = new HttpClient(cm);
    HttpClientParams hcp = http.getParams();
    // Set default socket timeout.
    hcp.setSoTimeout(timeout);
    // Set client to be version 1.0.
    hcp.setVersion(HttpVersion.HTTP_1_0);

    configureHttpCookies(http);

    // Configure how we want the method to act.
    http.getParams().setParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, new Boolean(true));
    http.getParams().setParameter(HttpMethodParams.UNAMBIGUOUS_STATUS_LINE, new Boolean(false));
    http.getParams().setParameter(HttpMethodParams.STRICT_TRANSFER_ENCODING, new Boolean(false));
    http.getParams().setIntParameter(HttpMethodParams.STATUS_LINE_GARBAGE_LIMIT, 10);

    // modify the default config with any global settings
    HostConfiguration config = http.getHostConfiguration();
    configureProxy(null, config);
    configureBindAddress(null, config);

    // Use our own protocol factory, one that gets IP to use from
    // heritrix cache (They're cached in CrawlHost instances).
    final ServerCache cache = getController().getServerCache();
    hcmp.setParameter(SERVER_CACHE_KEY, cache);
    hcmp.setParameter(SSL_FACTORY_KEY, this.sslfactory);

    return http;
}

From source file:org.artifactory.repo.HttpRepoTest.java

@Test
public void testProxyRemoteAuthAndMultihome() {
    ProxyDescriptor proxyDescriptor = new ProxyDescriptor();
    proxyDescriptor.setHost("proxyHost");
    proxyDescriptor.setUsername("proxy-username");
    proxyDescriptor.setPassword("proxy-password");

    HttpRepoDescriptor httpRepoDescriptor = new HttpRepoDescriptor();
    httpRepoDescriptor.setUrl("http://test");

    httpRepoDescriptor.setProxy(proxyDescriptor);

    httpRepoDescriptor.setUsername("repo-username");
    httpRepoDescriptor.setPassword("repo-password");

    httpRepoDescriptor.setLocalAddress("0.0.0.0");

    HttpRepo httpRepo = new HttpRepo(httpRepoDescriptor, internalRepoService, false, null);
    HttpClient client = httpRepo.createHttpClient();

    Credentials proxyCredentials = client.getState().getProxyCredentials(AuthScope.ANY);
    Assert.assertNotNull(proxyCredentials);
    Assert.assertTrue(proxyCredentials instanceof UsernamePasswordCredentials,
            "proxyCredentials are of the wrong class");
    Assert.assertEquals(((UsernamePasswordCredentials) proxyCredentials).getUserName(), "proxy-username");
    Assert.assertEquals(((UsernamePasswordCredentials) proxyCredentials).getPassword(), "proxy-password");

    Credentials repoCredentials = client.getState()
            .getCredentials(new AuthScope("test", AuthScope.ANY_PORT, AuthScope.ANY_REALM));
    Assert.assertNotNull(repoCredentials);
    Assert.assertTrue(repoCredentials instanceof UsernamePasswordCredentials,
            "repoCredentials are of the wrong class");
    Assert.assertEquals(((UsernamePasswordCredentials) repoCredentials).getUserName(), "repo-username");
    Assert.assertEquals(((UsernamePasswordCredentials) repoCredentials).getPassword(), "repo-password");

    Assert.assertEquals(client.getHostConfiguration().getLocalAddress().getHostAddress(), "0.0.0.0");
}

From source file:org.asynchttpclient.providers.apache.ApacheAsyncHttpProvider.java

private HttpMethodBase createMethod(HttpClient client, Request request)
        throws IOException, FileNotFoundException {
    String methodName = request.getMethod();
    HttpMethodBase method = null;//from ww w  .ja v a 2  s  . c o m
    if (methodName.equalsIgnoreCase("POST") || methodName.equalsIgnoreCase("PUT")) {
        EntityEnclosingMethod post = methodName.equalsIgnoreCase("POST") ? new PostMethod(request.getUrl())
                : new PutMethod(request.getUrl());

        String bodyCharset = request.getBodyEncoding() == null ? DEFAULT_CHARSET : request.getBodyEncoding();

        post.getParams().setContentCharset("ISO-8859-1");
        if (request.getByteData() != null) {
            post.setRequestEntity(new ByteArrayRequestEntity(request.getByteData()));
            post.setRequestHeader("Content-Length", String.valueOf(request.getByteData().length));
        } else if (request.getStringData() != null) {
            post.setRequestEntity(new StringRequestEntity(request.getStringData(), "text/xml", bodyCharset));
            post.setRequestHeader("Content-Length",
                    String.valueOf(request.getStringData().getBytes(bodyCharset).length));
        } else if (request.getStreamData() != null) {
            InputStreamRequestEntity r = new InputStreamRequestEntity(request.getStreamData());
            post.setRequestEntity(r);
            post.setRequestHeader("Content-Length", String.valueOf(r.getContentLength()));

        } else if (request.getParams() != null) {
            StringBuilder sb = new StringBuilder();
            for (final Map.Entry<String, List<String>> paramEntry : request.getParams()) {
                final String key = paramEntry.getKey();
                for (final String value : paramEntry.getValue()) {
                    if (sb.length() > 0) {
                        sb.append("&");
                    }
                    UTF8UrlEncoder.appendEncoded(sb, key);
                    sb.append("=");
                    UTF8UrlEncoder.appendEncoded(sb, value);
                }
            }

            post.setRequestHeader("Content-Length", String.valueOf(sb.length()));
            post.setRequestEntity(new StringRequestEntity(sb.toString(), "text/xml", "ISO-8859-1"));

            if (!request.getHeaders().containsKey("Content-Type")) {
                post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            }
        } else if (request.getParts() != null) {
            MultipartRequestEntity mre = createMultipartRequestEntity(bodyCharset, request.getParts(),
                    post.getParams());
            post.setRequestEntity(mre);
            post.setRequestHeader("Content-Type", mre.getContentType());
            post.setRequestHeader("Content-Length", String.valueOf(mre.getContentLength()));
        } else if (request.getEntityWriter() != null) {
            post.setRequestEntity(new EntityWriterRequestEntity(request.getEntityWriter(),
                    computeAndSetContentLength(request, post)));
        } else if (request.getFile() != null) {
            File file = request.getFile();
            if (!file.isFile()) {
                throw new IOException(
                        String.format(Thread.currentThread() + "File %s is not a file or doesn't exist",
                                file.getAbsolutePath()));
            }
            post.setRequestHeader("Content-Length", String.valueOf(file.length()));

            FileInputStream fis = new FileInputStream(file);
            try {
                InputStreamRequestEntity r = new InputStreamRequestEntity(fis);
                post.setRequestEntity(r);
                post.setRequestHeader("Content-Length", String.valueOf(r.getContentLength()));
            } finally {
                fis.close();
            }
        } else if (request.getBodyGenerator() != null) {
            Body body = request.getBodyGenerator().createBody();
            try {
                int length = (int) body.getContentLength();
                if (length < 0) {
                    length = (int) request.getContentLength();
                }

                // TODO: This is suboptimal
                if (length >= 0) {
                    post.setRequestHeader("Content-Length", String.valueOf(length));

                    // This is totally sub optimal
                    byte[] bytes = new byte[length];
                    ByteBuffer buffer = ByteBuffer.wrap(bytes);
                    for (;;) {
                        buffer.clear();
                        if (body.read(buffer) < 0) {
                            break;
                        }
                    }
                    post.setRequestEntity(new ByteArrayRequestEntity(bytes));
                }
            } finally {
                try {
                    body.close();
                } catch (IOException e) {
                    logger.warn("Failed to close request body: {}", e.getMessage(), e);
                }
            }
        }

        String expect = request.getHeaders().getFirstValue("Expect");
        if (expect != null && expect.equalsIgnoreCase("100-Continue")) {
            post.setUseExpectHeader(true);
        }
        method = post;
    } else if (methodName.equalsIgnoreCase("DELETE")) {
        method = new DeleteMethod(request.getUrl());
    } else if (methodName.equalsIgnoreCase("HEAD")) {
        method = new HeadMethod(request.getUrl());
    } else if (methodName.equalsIgnoreCase("GET")) {
        method = new GetMethod(request.getUrl());
    } else if (methodName.equalsIgnoreCase("OPTIONS")) {
        method = new OptionsMethod(request.getUrl());
    } else {
        throw new IllegalStateException(String.format("Invalid Method", methodName));
    }

    ProxyServer proxyServer = ProxyUtils.getProxyServer(config, request);
    if (proxyServer != null) {

        if (proxyServer.getPrincipal() != null) {
            Credentials defaultcreds = new UsernamePasswordCredentials(proxyServer.getPrincipal(),
                    proxyServer.getPassword());
            client.getState().setProxyCredentials(new AuthScope(null, -1, AuthScope.ANY_REALM), defaultcreds);
        }

        ProxyHost proxyHost = proxyServer == null ? null
                : new ProxyHost(proxyServer.getHost(), proxyServer.getPort());
        client.getHostConfiguration().setProxyHost(proxyHost);
    }
    if (request.getLocalAddress() != null) {
        client.getHostConfiguration().setLocalAddress(request.getLocalAddress());
    }

    method.setFollowRedirects(false);
    Collection<Cookie> cookies = request.getCookies();
    if (isNonEmpty(cookies)) {
        method.setRequestHeader("Cookie", AsyncHttpProviderUtils.encodeCookies(request.getCookies()));
    }

    if (request.getHeaders() != null) {
        for (String name : request.getHeaders().keySet()) {
            if (!"host".equalsIgnoreCase(name)) {
                for (String value : request.getHeaders().get(name)) {
                    method.setRequestHeader(name, value);
                }
            }
        }
    }

    String ua = request.getHeaders().getFirstValue("User-Agent");
    if (ua != null) {
        method.setRequestHeader("User-Agent", ua);
    } else if (config.getUserAgent() != null) {
        method.setRequestHeader("User-Agent", config.getUserAgent());
    } else {
        method.setRequestHeader("User-Agent",
                AsyncHttpProviderUtils.constructUserAgent(ApacheAsyncHttpProvider.class, config));
    }

    if (config.isCompressionEnabled()) {
        Header acceptableEncodingHeader = method.getRequestHeader("Accept-Encoding");
        if (acceptableEncodingHeader != null) {
            String acceptableEncodings = acceptableEncodingHeader.getValue();
            if (acceptableEncodings.indexOf("gzip") == -1) {
                StringBuilder buf = new StringBuilder(acceptableEncodings);
                if (buf.length() > 1) {
                    buf.append(",");
                }
                buf.append("gzip");
                method.setRequestHeader("Accept-Encoding", buf.toString());
            }
        } else {
            method.setRequestHeader("Accept-Encoding", "gzip");
        }
    }

    if (request.getVirtualHost() != null) {

        String vs = request.getVirtualHost();
        int index = vs.indexOf(":");
        if (index > 0) {
            vs = vs.substring(0, index);
        }
        method.getParams().setVirtualHost(vs);
    }

    return method;
}

From source file:org.biomart.martservice.MartServiceUtils.java

/**
 * //w  w  w.  j  a  v a2 s .  c o  m
 * @param client
 */
public static void setProxy(HttpClient client) {
    String host = System.getProperty("http.proxyHost");
    String port = System.getProperty("http.proxyPort");
    String user = System.getProperty("http.proxyUser");
    String password = System.getProperty("http.proxyPassword");

    if (host != null && port != null) {
        try {
            int portInteger = Integer.parseInt(port);
            client.getHostConfiguration().setProxy(host, portInteger);
            if (user != null && password != null) {
                client.getState().setProxyCredentials(new AuthScope(host, portInteger),
                        new UsernamePasswordCredentials(user, password));
            }
        } catch (NumberFormatException e) {
            logger.error("Proxy port not an integer", e);
        }
    }
}

From source file:org.bonitasoft.connectors.drools.common.DroolsConnector.java

private HttpClient getHttpClient() {
    final HttpClient httpClient = new HttpClient();
    // set proxy/* w  w w  .  j a  v a2  s .  c  o m*/
    if (proxyHost != null && proxyPort != 0) {
        httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
    }
    // set proxy username & password
    if (proxyPassword != null && proxyUsername != null) {
        final Credentials creds = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
        httpClient.getState().setProxyCredentials(AuthScope.ANY, creds);
    }
    // set username & password
    if (username != null && password != null) {
        final Credentials creds = new UsernamePasswordCredentials(username, password);
        httpClient.getState().setCredentials(AuthScope.ANY, creds);
    }
    // set connection/read timeout
    final HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager().getParams();
    if (connectionTimeout != 0) {
        managerParams.setConnectionTimeout(connectionTimeout);
    }
    if (readTimeout != 0) {
        managerParams.setSoTimeout(readTimeout);
    }
    return httpClient;
}

From source file:org.candlepin.client.DefaultCandlepinClientFacade.java

/**
 * @param httpclient//from w  ww.j  av  a 2  s .c om
 * @param factory
 * @throws MalformedURLException
 */
private void setHttpsProtocol(HttpClient httpclient, ProtocolSocketFactory factory)
        throws MalformedURLException {
    URL hostUrl = new URL(config.getServerURL());
    Protocol customHttps = new Protocol("https", factory, 8443);
    Protocol.registerProtocol("https", customHttps);
    httpclient.getHostConfiguration().setHost(hostUrl.getHost(), hostUrl.getPort(), customHttps);
}

From source file:org.cytoscape.cpath2.internal.CPathProtocol.java

/**
 * Sets Proxy Information (if set).//from   w ww.  ja  v  a2 s.c om
 */
private void setProxyInfo(HttpClient client) {
    // TODO: Port this.
    //        Proxy proxyServer = ProxyHandler.getProxyServer();
    Proxy proxyServer = null;

    //  The java.net.Proxy object does not provide getters for host and port.
    //  So, we have to hack it by using the toString() method.

    //  Note to self for future reference: I was able to test all this code
    //  by downloading and installing Privoxy, a local HTTP proxy,
    //  available at:  http://www.privoxy.org/.  Once it was running, I used the
    //  following props in ~/.cytoscape/cytoscape3.props:
    //  proxy.server=127.0.0.1
    //  proxy.server.port=8118
    //  proxy.server.type=HTTP
    if (proxyServer != null) {
        String proxyAddress = proxyServer.toString();
        if (debug)
            logger.debug("full proxy string:  " + proxyAddress);
        String[] addressComponents = proxyAddress.split("@");
        if (addressComponents.length == 2) {
            String parts[] = addressComponents[1].split(":");
            if (parts.length == 2) {
                String hostString = parts[0].trim();
                String hostParts[] = hostString.split("/");
                if (hostParts.length > 0) {
                    String host = hostParts[0].trim();
                    String port = parts[1].trim();
                    if (debug)
                        logger.debug("proxy host: " + host);
                    if (debug)
                        logger.debug("proxy port:  " + port);
                    client.getHostConfiguration().setProxy(host, Integer.parseInt(port));
                }
            }
        }
    }
}