Example usage for org.apache.http.impl.client DefaultHttpClient addResponseInterceptor

List of usage examples for org.apache.http.impl.client DefaultHttpClient addResponseInterceptor

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient addResponseInterceptor.

Prototype

public synchronized void addResponseInterceptor(final HttpResponseInterceptor itcp) 

Source Link

Usage

From source file:com.googlecode.sardine.FunctionalSardineTest.java

@Test
public void testGetRange() throws Exception {
    final DefaultHttpClient client = new DefaultHttpClient();
    client.addResponseInterceptor(new HttpResponseInterceptor() {
        public void process(final HttpResponse r, final HttpContext context) throws HttpException, IOException {
            // Verify partial content response
            assertEquals(206, r.getStatusLine().getStatusCode());
            assertNotNull(r.getHeaders(HttpHeaders.CONTENT_RANGE));
            assertEquals(1, r.getHeaders(HttpHeaders.CONTENT_RANGE).length);
            client.removeResponseInterceptorByClass(this.getClass());
        }/*from   w w w .  j  a  v  a 2s .co  m*/
    });
    Sardine sardine = new SardineImpl(client);
    // mod_dav supports Range headers for GET
    final String url = "http://sudo.ch/dav/anon/sardine/single/file";
    // Resume
    final Map<String, String> header = Collections.singletonMap(HttpHeaders.RANGE, "bytes=" + 1 + "-");
    final InputStream in = sardine.get(url, header);
    assertNotNull(in);
}

From source file:com.googlecode.sardine.FunctionalSardineTest.java

@Test
public void testGetSingleFileGzip() throws Exception {
    final DefaultHttpClient client = new DefaultHttpClient();
    client.addResponseInterceptor(new HttpResponseInterceptor() {
        public void process(final HttpResponse r, final HttpContext context) throws HttpException, IOException {
            assertEquals(200, r.getStatusLine().getStatusCode());
            assertNotNull(r.getHeaders(HttpHeaders.CONTENT_ENCODING));
            assertEquals(1, r.getHeaders(HttpHeaders.CONTENT_ENCODING).length);
            assertEquals("gzip", r.getHeaders(HttpHeaders.CONTENT_ENCODING)[0].getValue());
            client.removeResponseInterceptorByClass(this.getClass());
        }/* w w  w . ja  v a 2 s  .  c  om*/
    });
    Sardine sardine = new SardineImpl(client);
    sardine.enableCompression();
    //      final String url = "http://sardine.googlecode.com/svn/trunk/README.html";
    final String url = "http://sudo.ch/dav/anon/sardine/single/file";
    final InputStream in = sardine.get(url);
    assertNotNull(in);
    assertNotNull(in.read());
    try {
        in.close();
    } catch (EOFException e) {
        fail("Issue https://issues.apache.org/jira/browse/HTTPCLIENT-1075 pending");
    }
}

From source file:jetbrains.teamcilty.github.api.impl.HttpClientWrapperImpl.java

public HttpClientWrapperImpl()
        throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
    final String serverVersion = ServerVersionHolder.getVersion().getDisplayVersion();

    final HttpParams ps = new BasicHttpParams();

    DefaultHttpClient.setDefaultHttpParams(ps);
    final int timeout = TeamCityProperties.getInteger("teamcity.github.http.timeout", 300 * 1000);
    HttpConnectionParams.setConnectionTimeout(ps, timeout);
    HttpConnectionParams.setSoTimeout(ps, timeout);
    HttpProtocolParams.setUserAgent(ps, "JetBrains TeamCity " + serverVersion);

    final SchemeRegistry schemaRegistry = SchemeRegistryFactory.createDefault();
    final SSLSocketFactory sslSocketFactory = new SSLSocketFactory(new TrustStrategy() {
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return !TeamCityProperties.getBoolean("teamcity.github.verify.ssl.certificate");
        }//from   w  w  w.  j a  va 2s.  c om
    });
    schemaRegistry.register(new Scheme("https", 443, sslSocketFactory));

    final DefaultHttpClient httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager(schemaRegistry),
            ps);

    setupProxy(httpclient);

    httpclient.setRoutePlanner(new ProxySelectorRoutePlanner(
            httpclient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault()));
    httpclient.addRequestInterceptor(new RequestAcceptEncoding());
    httpclient.addResponseInterceptor(new ResponseContentEncoding());
    httpclient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, true));

    myClient = httpclient;
}

From source file:jetbrains.buildServer.commitPublisher.github.api.impl.HttpClientWrapperImpl.java

public HttpClientWrapperImpl()
        throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
    final String serverVersion = ServerVersionHolder.getVersion().getDisplayVersion();

    final HttpParams ps = new BasicHttpParams();

    DefaultHttpClient.setDefaultHttpParams(ps);
    final int timeout = TeamCityProperties.getInteger("teamcity.github.http.timeout", 300 * 1000);
    HttpConnectionParams.setConnectionTimeout(ps, timeout);
    HttpConnectionParams.setSoTimeout(ps, timeout);
    HttpProtocolParams.setUserAgent(ps, "JetBrains TeamCity " + serverVersion);

    final SchemeRegistry schemaRegistry = SchemeRegistryFactory.createDefault();
    final SSLSocketFactory sslSocketFactory = new SSLSocketFactory(new TrustStrategy() {
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return !TeamCityProperties.getBoolean("teamcity.github.verify.ssl.certificate");
        }/*from w  w w .  ja  va  2 s .c o  m*/
    }) {
        @Override
        public Socket connectSocket(int connectTimeout, Socket socket, HttpHost host,
                InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpContext context)
                throws IOException {
            if (socket instanceof SSLSocket) {
                try {
                    PropertyUtils.setProperty(socket, "host", host.getHostName());
                } catch (Exception ex) {
                    LOG.warn(String.format(
                            "A host name is not passed to SSL connection for the purpose of supporting SNI due to the following exception: %s",
                            ex.toString()));
                }
            }
            return super.connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context);
        }
    };
    schemaRegistry.register(new Scheme("https", 443, sslSocketFactory));

    final DefaultHttpClient httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager(schemaRegistry),
            ps);

    setupProxy(httpclient);

    httpclient.setRoutePlanner(new ProxySelectorRoutePlanner(
            httpclient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault()));
    httpclient.addRequestInterceptor(new RequestAcceptEncoding());
    httpclient.addResponseInterceptor(new ResponseContentEncoding());
    httpclient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, true));

    myClient = httpclient;
}

From source file:com.nlworks.wowapi.util.ConnectionManager.java

public ConnectionManager() {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.addRequestInterceptor(new HttpRequestInterceptor() {

        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding")) {
                request.addHeader("Accept-Encoding", "gzip");
            }/*from  w w w . ja  v a2  s. c o  m*/
        }

    });

    httpclient.addResponseInterceptor(new HttpResponseInterceptor() {

        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header ceheader = entity.getContentEncoding();
            if (ceheader != null) {
                HeaderElement[] codecs = ceheader.getElements();
                for (int i = 0; i < codecs.length; i++) {
                    if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }

    });
    httpClient = httpclient;
}

From source file:org.ocpsoft.rewrite.servlet.wrapper.WrappedResponseStreamTest.java

/**
 * Ignored on Wildlfy because there seems to be some issue with the content length.
 * // ww w.  j av a 2  s. c om
 * @see https://github.com/ocpsoft/rewrite/issues/145
 */
@Test
@Category(IgnoreForWildfly.class)
public void testWrappedResponseStreamToGZip() throws Exception {

    DefaultHttpClient client = new DefaultHttpClient();
    client.addRequestInterceptor(new HttpRequestInterceptor() {

        @Override
        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding")) {
                request.addHeader("Accept-Encoding", "gzip");
            }
        }

    });

    client.addResponseInterceptor(new HttpResponseInterceptor() {

        @Override
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                Header header = entity.getContentEncoding();
                if (header != null) {
                    HeaderElement[] codecs = header.getElements();
                    for (int i = 0; i < codecs.length; i++) {
                        if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                            response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                            return;
                        }
                    }
                }
            }
        }

    });

    HttpAction<HttpGet> action = get(client, "/gzip.html");
    Assert.assertEquals(200, action.getStatusCode());
    Assert.assertEquals("gzip", action.getResponseHeaderValues("Content-Encoding").get(0));
    Assert.assertEquals("zip me to gzip please and make it zippy", action.getResponseContent());
}

From source file:com.juick.android.Utils.java

private static void initCompressionSupport(DefaultHttpClient httpclient) {

    httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding")) {
                request.addHeader("Accept-Encoding", "gzip");
            }//from  w w  w  . jav  a2s .  co m
        }

    });

    httpclient.addResponseInterceptor(new HttpResponseInterceptor() {
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                Header ceheader = entity.getContentEncoding();
                if (ceheader != null) {
                    HeaderElement[] codecs = ceheader.getElements();
                    for (int i = 0; i < codecs.length; i++) {
                        if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                            response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                            return;
                        }
                    }
                }
            }
        }

    });
}

From source file:com.todoroo.andlib.service.HttpRestClient.java

@SuppressWarnings("nls")
protected void actsAsGzippable(DefaultHttpClient client) {
    client.addRequestInterceptor(new HttpRequestInterceptor() {
        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding"))
                request.addHeader("Accept-Encoding", "gzip");
        }/*www  .  ja v a2s . c  om*/

    });

    client.addResponseInterceptor(new HttpResponseInterceptor() {
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header ceheader = entity.getContentEncoding();
            if (ceheader != null) {
                HeaderElement[] codecs = ceheader.getElements();
                for (int i = 0; i < codecs.length; i++) {
                    if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }
    });
}

From source file:org.apache.olingo.client.core.http.AbstractOAuth2HttpClientFactory.java

@Override
public HttpClient create(final HttpMethod method, final URI uri) {
    if (!isInited()) {
        init();/*from w  w  w. ja v a 2s. com*/
    }

    final DefaultHttpClient httpClient = wrapped.create(method, uri);
    accessToken(httpClient);

    httpClient.addRequestInterceptor(new HttpRequestInterceptor() {

        @Override
        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            if (request instanceof HttpUriRequest) {
                currentRequest = (HttpUriRequest) request;
            } else {
                currentRequest = null;
            }
        }
    });
    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {

        @Override
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
                refreshToken(httpClient);

                if (currentRequest != null) {
                    httpClient.execute(currentRequest);
                }
            }
        }
    });

    return httpClient;
}