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

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

Introduction

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

Prototype

public HttpClientParams getParams() 

Source Link

Usage

From source file:org.apache.solr.client.solrj.SolrExceptionTest.java

public void testSolrException() throws Throwable {
    // test a connection to a solr server that probably doesn't exist
    // this is a very simple test and most of the test should be considered verified 
    // if the compiler won't let you by without the try/catch
    boolean gotExpectedError = false;
    try {/*  ww w  . ja v  a 2 s. c o  m*/
        // switched to a local address to avoid going out on the net, ns lookup issues, etc.
        // set a 1ms timeout to let the connection fail faster.
        HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
        httpClient.getParams().setParameter("http.connection.timeout", new Integer(1));
        SolrServer client = new CommonsHttpSolrServer("http://localhost:11235/solr/", httpClient);
        SolrQuery query = new SolrQuery("test123");
        client.query(query);
    } catch (SolrServerException sse) {
        gotExpectedError = true;
        /***
        assertTrue(UnknownHostException.class == sse.getRootCause().getClass()
                //If one is using OpenDNS, then you don't get UnknownHostException, instead you get back that the query couldn't execute
                || (sse.getRootCause().getClass() == SolrException.class && ((SolrException) sse.getRootCause()).code() == 302 && sse.getMessage().equals("Error executing query")));
        ***/
    }
    assertTrue(gotExpectedError);
}

From source file:org.apache.solr.client.solrj.TestLBHttpSolrServer.java

public void testReliability() throws Exception {
    String[] s = new String[solr.length];
    for (int i = 0; i < solr.length; i++) {
        s[i] = solr[i].getUrl();//from w w w .  ja  v a2 s. c o  m
    }
    HttpClient myHttpClient = new HttpClient(new MultiThreadedHttpConnectionManager());

    myHttpClient.getParams().setParameter("http.connection.timeout", new Integer(100));
    myHttpClient.getParams().setParameter("http.socket.timeout", new Integer(100));
    LBHttpSolrServer lbHttpSolrServer = new LBHttpSolrServer(myHttpClient, s);
    lbHttpSolrServer.setAliveCheckInterval(500);

    // Kill a server and test again
    solr[1].jetty.stop();
    solr[1].jetty = null;

    // query the servers
    for (String value : s)
        lbHttpSolrServer.query(new SolrQuery("*:*"));

    // Start the killed server once again
    solr[1].startJetty();
    // Wait for the alive check to complete
    waitForServer(30000, lbHttpSolrServer, 3, "solr1");
}

From source file:org.apache.solr.handler.component.SearchHandler.java

private static HttpClient makeHttpClient() {
    HttpClient cli = new HttpClient();
    cli.getHttpConnectionManager().getParams().setConnectionTimeout(1000 * 60 * 100);
    cli.getHttpConnectionManager().getParams().setSoTimeout(1000 * 60 * 100);
    cli.getParams().setConnectionManagerTimeout(1000 * 60 * 100);

    return cli;//from  www  .  jav  a  2s.c o  m
}

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

private String executeMethod(HttpMethod method, Configuration properties)
        throws Exception, AuthenticationException {
    // Execute the method.
    try {//  w w w.  j ava2  s . co  m
        HttpClient client = new HttpClient();

        // set the clients proxy values if needed
        ConnectionsPrefsManager.setProxySettings(client, properties);

        if (fUseProxyAuthentication) {
            if (fBase64Auth != null) {
                method.setRequestHeader("Authorization", fBase64Auth);
            } else {
                List<String> authPrefs = new ArrayList<String>(2);
                authPrefs.add(AuthPolicy.DIGEST);
                authPrefs.add(AuthPolicy.BASIC);
                client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
                // send the basic authentication response even before the server gives an unauthorized response
                client.getParams().setAuthenticationPreemptive(true);
                // Pass our credentials to HttpClient
                client.getState().setCredentials(
                        new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
                        new UsernamePasswordCredentials(fProxyUsername, fProxyPassword));
            }
        }

        // Add user language to http request in order to notify server of user's language
        Locale locale = Locale.getDefault();

        method.setRequestHeader("Accept-Language", locale.getLanguage()); //$NON-NLS-1$
        method.removeRequestHeader("Content-Type");
        //method.setRequestHeader("Content-Type","application/json");
        //method.setRequestHeader("Referer", "");
        //method.removeRequestHeader("Referer");
        method.setRequestHeader("Accept", "*/*");

        int statusCode = client.executeMethod(method);

        //System.out.println("response="+method.getResponseBodyAsString());
        //System.out.println("method="+method.toString());
        //System.out.println("response="+method.getResponseBodyAsStream());

        if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) {
            Header hType = method.getResponseHeader("Content-Type");
            if (hType != null) {
                fContentType = hType.getValue();
            }
            // for now we are only expecting Strings
            //return method.getResponseBodyAsString();
            return readFully(new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8"));
        } else if (statusCode == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED
                || statusCode == HttpStatus.SC_UNAUTHORIZED) {
            throw new AuthenticationException("Authentication failed:" + method.getStatusLine() + ' '
                    + method.getURI() + ' ' + method.getStatusText());
        } else {
            throw new Exception("Method failed: " + method.getStatusLine() + ' ' + method.getURI() + ' ' //$NON-NLS-1$
                    + method.getStatusText());
        }
    } catch (IOException e) {
        throw e;
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
}

From source file:org.apache.wookie.tests.functional.ProxyTest.java

@Test
public void postWithOnlyQueryStringParameters() throws Exception {
    HttpClient client = new HttpClient();
    List<String> authPrefs = new ArrayList<String>(2);
    authPrefs.add(AuthPolicy.DIGEST);//from   w ww.  j a va2s .  co  m
    authPrefs.add(AuthPolicy.BASIC);
    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
    // send the basic authentication response even before the server gives an unauthorized response
    client.getParams().setAuthenticationPreemptive(true);
    client.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
            new UsernamePasswordCredentials("java", "java"));
    PostMethod req;
    req = new PostMethod(PROXY_URL + "?instanceid_key=" + instance_id_key + "&url=" + PROTECTED_SITE_URL);
    client.executeMethod(req);
    int code = req.getStatusCode();
    req.releaseConnection();
    assertEquals(200, code);
}

From source file:org.apache.wookie.tests.functional.ProxyTest.java

@Test
public void postWithMixedQueryAndParameters() throws Exception {
    HttpClient client = new HttpClient();
    List<String> authPrefs = new ArrayList<String>(2);
    authPrefs.add(AuthPolicy.DIGEST);/*  w  ww  . j av  a 2  s  .c om*/
    authPrefs.add(AuthPolicy.BASIC);
    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
    // send the basic authentication response even before the server gives an unauthorized response
    client.getParams().setAuthenticationPreemptive(true);
    client.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
            new UsernamePasswordCredentials("java", "java"));
    PostMethod req;
    req = new PostMethod(PROXY_URL + "?instanceid_key=" + instance_id_key);
    req.addParameter("url", PROTECTED_SITE_URL);
    client.executeMethod(req);
    int code = req.getStatusCode();
    req.releaseConnection();
    assertEquals(200, code);
}

From source file:org.apache.wookie.tests.functional.ProxyTest.java

@Test
public void postWithOnlyParameters() throws Exception {
    HttpClient client = new HttpClient();
    List<String> authPrefs = new ArrayList<String>(2);
    authPrefs.add(AuthPolicy.DIGEST);/*from w  ww  .  jav  a  2s .  co  m*/
    authPrefs.add(AuthPolicy.BASIC);
    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
    // send the basic authentication response even before the server gives an unauthorized response
    client.getParams().setAuthenticationPreemptive(true);
    client.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
            new UsernamePasswordCredentials("java", "java"));
    PostMethod req;
    req = new PostMethod(PROXY_URL);
    req.addParameter("url", PROTECTED_SITE_URL);
    req.addParameter("instanceid_key", instance_id_key);
    client.executeMethod(req);
    int code = req.getStatusCode();
    req.releaseConnection();
    assertEquals(200, code);
}

From source file:org.apache.wookie.tests.functional.ProxyTest.java

@Test
public void getProtectedSiteWithBasicAuth() throws Exception {
    HttpClient client = new HttpClient();
    List<String> authPrefs = new ArrayList<String>(2);
    authPrefs.add(AuthPolicy.DIGEST);//  w ww  .  j a v  a  2  s . c o m
    authPrefs.add(AuthPolicy.BASIC);
    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
    // send the basic authentication response even before the server gives an unauthorized response
    client.getParams().setAuthenticationPreemptive(true);
    client.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
            new UsernamePasswordCredentials("java", "java"));
    HttpMethod req;
    req = new GetMethod(PROXY_URL + "?instanceid_key=" + instance_id_key + "&url=" + PROTECTED_SITE_URL);
    client.executeMethod(req);
    int code = req.getStatusCode();
    req.releaseConnection();
    assertEquals(200, code);
}

From source file:org.araqne.pkg.HttpWagon.java

public static InputStream openDownloadStream(URL url, boolean useAuth, String username, String password)
        throws IOException {
    Logger logger = LoggerFactory.getLogger(HttpWagon.class.getName());
    logger.trace("http wagon: downloading {}", url);

    int socketTimeout = getSocketTimeout();
    int connectionTimeout = getConnectTimeout();

    HttpClient client = new HttpClient();

    client.getParams().setParameter("http.socket.timeout", socketTimeout);
    client.getParams().setParameter("http.connection.timeout", connectionTimeout);
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, null);

    String realm = "";
    if (useAuth) {
        client.getParams().setAuthenticationPreemptive(true);
        if (realmCache.containsKey(getRealmCacheKey(url)))
            realm = realmCache.get(getRealmCacheKey(url));
        setClientAuth(url, username, password, realm, client);
    }//from www.  j  av a  2 s  .  co m

    HttpMethod method = new GetMethod(url.toString());

    int statusCode = client.executeMethod(method);

    if (useAuth && statusCode == HttpStatus.SC_UNAUTHORIZED) {
        realm = getRealm(method);
        setClientAuth(url, username, password, realm, client);
        method = new GetMethod(url.toString());
        statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            throw new IOException("digest auth failed: " + method.getStatusLine());
        } else {
            realmCache.put(getRealmCacheKey(url), realm);
        }
    }

    if (statusCode != HttpStatus.SC_OK) {
        throw new IOException("method failed: " + method.getStatusLine());
    }

    return method.getResponseBodyAsStream();
}

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 w  ww . j a v  a 2s .c o  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;
}