Search in sources :

Example 91 with Header

use of org.apache.commons.httpclient.Header in project sling by apache.

the class HeadServletTest method assertResponseHeader.

private void assertResponseHeader(HttpMethod m, String name, String expectedRegex) {
    final Header h = m.getResponseHeader(name);
    assertNotNull("Expecting header " + name, h);
    final String value = h.getValue();
    assertTrue("Expected regexp " + expectedRegex + " for header " + name + ", header value is " + value, Pattern.matches(expectedRegex, value));
}
Also used : Header(org.apache.commons.httpclient.Header)

Example 92 with Header

use of org.apache.commons.httpclient.Header in project nutch by apache.

the class Http method configureClient.

/**
 * Configures the HTTP client
 */
private void configureClient() {
    // Set up an HTTPS socket factory that accepts self-signed certs.
    // ProtocolSocketFactory factory = new SSLProtocolSocketFactory();
    ProtocolSocketFactory factory = new DummySSLProtocolSocketFactory();
    Protocol https = new Protocol("https", factory, 443);
    Protocol.registerProtocol("https", https);
    HttpConnectionManagerParams params = connectionManager.getParams();
    params.setConnectionTimeout(timeout);
    params.setSoTimeout(timeout);
    params.setSendBufferSize(BUFFER_SIZE);
    params.setReceiveBufferSize(BUFFER_SIZE);
    // --------------------------------------------------------------------------------
    // NUTCH-1836: Modification to increase the number of available connections
    // for multi-threaded crawls.
    // --------------------------------------------------------------------------------
    params.setMaxTotalConnections(conf.getInt("mapred.tasktracker.map.tasks.maximum", 5) * conf.getInt("fetcher.threads.fetch", maxThreadsTotal));
    // Also set max connections per host to maxThreadsTotal since all threads
    // might be used to fetch from the same host - otherwise timeout errors can
    // occur
    params.setDefaultMaxConnectionsPerHost(conf.getInt("fetcher.threads.fetch", maxThreadsTotal));
    // executeMethod(HttpMethod) seems to ignore the connection timeout on the
    // connection manager.
    // set it explicitly on the HttpClient.
    client.getParams().setConnectionManagerTimeout(timeout);
    HostConfiguration hostConf = client.getHostConfiguration();
    ArrayList<Header> headers = new ArrayList<Header>();
    // Note: some header fields (e.g., "User-Agent") are set per GET request
    if (!acceptLanguage.isEmpty()) {
        headers.add(new Header("Accept-Language", acceptLanguage));
    }
    if (!acceptCharset.isEmpty()) {
        headers.add(new Header("Accept-Charset", acceptCharset));
    }
    if (!accept.isEmpty()) {
        headers.add(new Header("Accept", accept));
    }
    // accept gzipped content
    headers.add(new Header("Accept-Encoding", "x-gzip, gzip, deflate"));
    hostConf.getParams().setParameter("http.default-headers", headers);
    // HTTP proxy server details
    if (useProxy) {
        hostConf.setProxy(proxyHost, proxyPort);
        if (proxyUsername.length() > 0) {
            AuthScope proxyAuthScope = getAuthScope(this.proxyHost, this.proxyPort, this.proxyRealm);
            NTCredentials proxyCredentials = new NTCredentials(this.proxyUsername, this.proxyPassword, Http.agentHost, this.proxyRealm);
            client.getState().setProxyCredentials(proxyAuthScope, proxyCredentials);
        }
    }
}
Also used : ProtocolSocketFactory(org.apache.commons.httpclient.protocol.ProtocolSocketFactory) Header(org.apache.commons.httpclient.Header) HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) HostConfiguration(org.apache.commons.httpclient.HostConfiguration) ArrayList(java.util.ArrayList) AuthScope(org.apache.commons.httpclient.auth.AuthScope) Protocol(org.apache.commons.httpclient.protocol.Protocol) NTCredentials(org.apache.commons.httpclient.NTCredentials)

Example 93 with Header

use of org.apache.commons.httpclient.Header in project knime-core by knime.

the class AbstractFileDownloadTripleProvider method getInputStream.

private static InputStream getInputStream(final GetMethod method) throws IOException {
    InputStream in = method.getResponseBodyAsStream();
    Header encoding = method.getResponseHeader("Content-Encoding");
    if (encoding != null && encoding.getValue().equals("gzip")) {
        in = new GZIPInputStream(in);
    }
    return in;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) Header(org.apache.commons.httpclient.Header) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream)

Aggregations

Header (org.apache.commons.httpclient.Header)93 GetMethod (org.apache.commons.httpclient.methods.GetMethod)23 Test (org.junit.Test)22 PostMethod (org.apache.commons.httpclient.methods.PostMethod)21 IOException (java.io.IOException)20 HttpClient (org.apache.commons.httpclient.HttpClient)20 NameValuePair (org.apache.commons.httpclient.NameValuePair)18 HttpMethod (org.apache.commons.httpclient.HttpMethod)17 ArrayList (java.util.ArrayList)16 InputStream (java.io.InputStream)14 HttpException (org.apache.commons.httpclient.HttpException)13 PutMethod (org.apache.commons.httpclient.methods.PutMethod)10 Account (com.zimbra.cs.account.Account)9 ServiceException (com.zimbra.common.service.ServiceException)6 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)6 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)6 Pair (com.zimbra.common.util.Pair)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 HashMap (java.util.HashMap)5 DefaultHttpMethodRetryHandler (org.apache.commons.httpclient.DefaultHttpMethodRetryHandler)5