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.apache.maven.wagon.shared.http.AbstractHttpClientWagon.java

public void openConnectionInternal() {
    repository.setUrl(getURL(repository));
    client = new HttpClient(connectionManager);
    String username = null;/*from   w  w w .  j a  va2 s  .  c o  m*/
    String password = null;

    if (authenticationInfo != null) {
        username = authenticationInfo.getUserName();

        password = authenticationInfo.getPassword();
    }

    String host = getRepository().getHost();

    if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) {
        Credentials creds = new UsernamePasswordCredentials(username, password);

        int port = getRepository().getPort() > -1 ? getRepository().getPort() : AuthScope.ANY_PORT;

        AuthScope scope = new AuthScope(host, port);
        client.getState().setCredentials(scope, creds);
    }

    HostConfiguration hc = new HostConfiguration();

    ProxyInfo proxyInfo = getProxyInfo(getRepository().getProtocol(), getRepository().getHost());
    if (proxyInfo != null) {
        String proxyUsername = proxyInfo.getUserName();
        String proxyPassword = proxyInfo.getPassword();
        String proxyHost = proxyInfo.getHost();
        int proxyPort = proxyInfo.getPort();
        String proxyNtlmHost = proxyInfo.getNtlmHost();
        String proxyNtlmDomain = proxyInfo.getNtlmDomain();
        if (proxyHost != null) {
            hc.setProxy(proxyHost, proxyPort);

            if (proxyUsername != null && proxyPassword != null) {
                Credentials creds;
                if (proxyNtlmHost != null || proxyNtlmDomain != null) {
                    creds = new NTCredentials(proxyUsername, proxyPassword, proxyNtlmHost, proxyNtlmDomain);
                } else {
                    creds = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
                }

                int port = proxyInfo.getPort() > -1 ? proxyInfo.getPort() : AuthScope.ANY_PORT;

                AuthScope scope = new AuthScope(proxyHost, port);
                client.getState().setProxyCredentials(scope, creds);
            }
        }
    }

    hc.setHost(host);

    //start a session with the webserver
    client.setHostConfiguration(hc);
}

From source file:org.apache.servicemix.http.processors.ProviderProcessor.java

private HostConfiguration getHostConfiguration(String locationURI, MessageExchange exchange,
        NormalizedMessage message) throws Exception {
    HostConfiguration host;/* w w  w  . j  a  v  a2  s. c  o m*/
    URI uri = new URI(locationURI, false);
    if (uri.getScheme().equals("https")) {
        synchronized (this) {
            if (protocol == null) {
                ProtocolSocketFactory sf = new CommonsHttpSSLSocketFactory(endpoint.getSsl(),
                        KeystoreManager.Proxy.create(endpoint.getKeystoreManager()));
                protocol = new Protocol("https", sf, 443);
            }
        }
        HttpHost httphost = new HttpHost(uri.getHost(), uri.getPort(), protocol);
        host = new HostConfiguration();
        host.setHost(httphost);
    } else {
        host = new HostConfiguration();
        host.setHost(uri.getHost(), uri.getPort());
    }
    if (endpoint.getProxy() != null) {
        if ((endpoint.getProxy().getProxyHost() != null) && (endpoint.getProxy().getProxyPort() != 0)) {
            host.setProxy(endpoint.getProxy().getProxyHost(), endpoint.getProxy().getProxyPort());
        }
        if (endpoint.getProxy().getProxyCredentials() != null) {
            endpoint.getProxy().getProxyCredentials().applyProxyCredentials(getClient(), exchange, message);
        }
    } else if ((getConfiguration().getProxyHost() != null) && (getConfiguration().getProxyPort() != 0)) {
        host.setProxy(getConfiguration().getProxyHost(), getConfiguration().getProxyPort());
    }
    //host.getParams().setLongParameter(HttpConnectionParams.CONNECTION_TIMEOUT, getClient().getParams().getSoTimeout());
    return host;
}

From source file:org.apache.sshd.common.forward.PortForwardingLoadTest.java

protected void checkHtmlPage(HttpClient client, URL url) throws IOException {
    client.setHostConfiguration(new HostConfiguration());
    client.getHostConfiguration().setHost(url.getHost(), url.getPort());
    GetMethod get = new GetMethod("");
    get.getParams().setVersion(HttpVersion.HTTP_1_1);
    client.executeMethod(get);/*from   w w  w. j  av a  2  s  .c  om*/
    String str = get.getResponseBodyAsString();
    if (str.indexOf("</html>") <= 0) {
        System.err.println(str);
    }
    assertTrue("Missing HTML close tag", str.indexOf("</html>") > 0);
    get.releaseConnection();
    //        url.openConnection().setDefaultUseCaches(false);
    //        Reader reader = new BufferedReader(new InputStreamReader(url.openStream()));
    //        try {
    //            StringWriter sw = new StringWriter();
    //            char[] buf = new char[8192];
    //            while (true) {
    //                int len = reader.read(buf);
    //                if (len < 0) {
    //                    break;
    //                }
    //                sw.write(buf, 0, len);
    //            }
    //            assertTrue(sw.toString().indexOf("</html>") > 0);
    //        } finally {
    //            reader.close();
    //        }
}

From source file:org.apache.sshd.PortForwardingLoadTest.java

protected void checkHtmlPage(HttpClient client, URL url) throws IOException {
    client.setHostConfiguration(new HostConfiguration());
    client.getHostConfiguration().setHost(url.getHost(), url.getPort());
    GetMethod get = new GetMethod("");
    get.getParams().setVersion(HttpVersion.HTTP_1_1);
    client.executeMethod(get);//w  ww  .j  a  va  2s  . c  om
    String str = get.getResponseBodyAsString();
    if (str.indexOf("</html>") <= 0) {
        System.err.println(str);
    }
    assertTrue((str.indexOf("</html>") > 0));
    get.releaseConnection();
    //        url.openConnection().setDefaultUseCaches(false);
    //        Reader reader = new BufferedReader(new InputStreamReader(url.openStream()));
    //        try {
    //            StringWriter sw = new StringWriter();
    //            char[] buf = new char[8192];
    //            while (true) {
    //                int len = reader.read(buf);
    //                if (len < 0) {
    //                    break;
    //                }
    //                sw.write(buf, 0, len);
    //            }
    //            assertTrue(sw.toString().indexOf("</html>") > 0);
    //        } finally {
    //            reader.close();
    //        }
}

From source file:org.apache.xmlrpc.CommonsXmlRpcTransport.java

public InputStream sendXmlRpc(byte[] request) throws IOException, XmlRpcClientException {
    method = new PostMethod(url.toString());
    method.setHttp11(http11);/*from   w ww.  j a  v  a  2 s  .  c o m*/
    method.setRequestHeader(new Header("Content-Type", "text/xml"));

    if (rgzip)
        method.setRequestHeader(new Header("Content-Encoding", "gzip"));

    if (gzip)
        method.setRequestHeader(new Header("Accept-Encoding", "gzip"));

    method.setRequestHeader(userAgentHeader);

    if (rgzip) {
        ByteArrayOutputStream lBo = new ByteArrayOutputStream();
        GZIPOutputStream lGzo = new GZIPOutputStream(lBo);
        lGzo.write(request);
        lGzo.finish();
        lGzo.close();
        byte[] lArray = lBo.toByteArray();
        method.setRequestBody(new ByteArrayInputStream(lArray));
        method.setRequestContentLength(-1);
    } else
        method.setRequestBody(new ByteArrayInputStream(request));

    URI hostURI = new URI(url.toString());
    HostConfiguration hostConfig = new HostConfiguration();
    hostConfig.setHost(hostURI);
    client.executeMethod(hostConfig, method);

    boolean lgzipo = false;

    Header lHeader = method.getResponseHeader("Content-Encoding");
    if (lHeader != null) {
        String lValue = lHeader.getValue();
        if (lValue != null)
            lgzipo = (lValue.indexOf("gzip") >= 0);
    }

    if (lgzipo)
        return (new GZIPInputStream(method.getResponseBodyAsStream()));
    else
        return method.getResponseBodyAsStream();
}

From source file:org.apdplat.qa.util.Tools.java

public static String getRewindEvidenceText(String question, String answer) {
    //1??//from  w w w.  j a va  2s.  c  om
    String rewindEvidenceText = MySQLUtils.getRewindEvidenceText(question, answer);
    if (rewindEvidenceText != null) {
        //?
        LOG.info("?" + question + " " + answer);
        return rewindEvidenceText;
    }
    //2???google
    StringBuilder text = new StringBuilder();
    String query = question + answer;
    try {
        query = URLEncoder.encode(query, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        LOG.error("url", e);
        return null;
    }
    query = "http://ajax.googleapis.com/ajax/services/search/web?start=0&rsz=large&v=1.0&q=" + query;
    try {
        HostConfiguration hcf = new HostConfiguration();
        hcf.setProxy("127.0.0.1", 8087);

        HttpClient httpClient = new HttpClient();
        GetMethod getMethod = new GetMethod(query);

        //httpClient.executeMethod(hcf, getMethod);
        httpClient.executeMethod(getMethod);
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());

        int statusCode = httpClient.executeMethod(getMethod);
        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + getMethod.getStatusLine());
        }
        byte[] responseBody = getMethod.getResponseBody();
        String response = new String(responseBody, "UTF-8");
        LOG.debug("??" + response);
        JSONObject json = new JSONObject(response);
        String totalResult = json.getJSONObject("responseData").getJSONObject("cursor")
                .getString("estimatedResultCount");
        int totalResultCount = Integer.parseInt(totalResult);
        LOG.info("? " + totalResultCount);

        JSONArray results = json.getJSONObject("responseData").getJSONArray("results");

        LOG.debug(" Results:");
        for (int i = 0; i < results.length(); i++) {
            JSONObject result = results.getJSONObject(i);
            String title = result.getString("titleNoFormatting");
            LOG.debug(title);
            //URL???
            String url = result.get("url").toString();
            String content = null;//Tools.getHTMLContent(url);
            if (content == null) {
                //????
                content = result.get("content").toString();
                content = content.replaceAll("<b>", "");
                content = content.replaceAll("</b>", "");
                content = content.replaceAll("\\.\\.\\.", "");
            }
            LOG.debug(content);
            text.append(title).append(content);
        }
        LOG.info("" + question + " " + answer + " MySQL?");
        MySQLUtils.saveRewindEvidenceText(question, answer, text.toString());
        return text.toString();
    } catch (Exception e) {
        LOG.debug("?", e);
    }
    return null;
}

From source file:org.archive.wayback.liveweb.ArcRemoteLiveWebCache.java

/**
 * //from w ww  .  ja  va2 s.c  o  m
 */
public ArcRemoteLiveWebCache() {
    connectionManager = new MultiThreadedHttpConnectionManager();
    hostConfiguration = new HostConfiguration();
    HttpClientParams params = new HttpClientParams();
    params.setParameter(HttpClientParams.RETRY_HANDLER, new NoRetryHandler());
    http = new HttpClient(params, connectionManager);
    http.setHostConfiguration(hostConfiguration);
}

From source file:org.archive.wayback.liveweb.ARCUnwrappingProxy.java

/**
 * 
 */
public ARCUnwrappingProxy() {
    connectionManager = new MultiThreadedHttpConnectionManager();
    hostConfiguration = new HostConfiguration();
}

From source file:org.archive.wayback.liveweb.RemoteLiveWebCache.java

/**
 * /* w  ww.j  av a 2 s.  co  m*/
 */
public RemoteLiveWebCache() {
    connectionManager = new MultiThreadedHttpConnectionManager();
    hostConfiguration = new HostConfiguration();
    HttpClientParams params = new HttpClientParams();
    params.setParameter(HttpClientParams.RETRY_HANDLER, new NoRetryHandler());
    http = new HttpClient(params, connectionManager);
    http.setHostConfiguration(hostConfiguration);
}

From source file:org.archive.wayback.liveweb.StdRemoteLiveWebCache.java

/**
 * StdRemoteLiveWebCache constructor initializes and configures connection objects.
 *///from  ww  w.  j a  va  2s  .  co  m
public StdRemoteLiveWebCache() {
    connectionManager = new MultiThreadedHttpConnectionManager();
    hostConfiguration = new HostConfiguration();
    HttpClientParams params = new HttpClientParams();
    params.setParameter(HttpClientParams.RETRY_HANDLER, new NoRetryHandler());
    httpClient = new HttpClient(params, connectionManager);
    httpClient.setHostConfiguration(hostConfiguration);
}