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

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

Introduction

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

Prototype

public synchronized void addRequestInterceptor(final HttpRequestInterceptor itcp) 

Source Link

Usage

From source file:net.oneandone.shared.artifactory.ArtifactoryModule.java

@Provides
@Named(value = "httpclient")
HttpClient provideHttpClient() {// w w w . jav a  2s .co m
    final DefaultHttpClient client = new DefaultHttpClient();
    client.addRequestInterceptor(new RequestAcceptEncoding());
    client.addResponseInterceptor(new ResponseContentEncoding());
    client.addRequestInterceptor(providePreemptiveRequestInterceptor());
    return client;
}

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

@Override
public DefaultHttpClient create(final HttpMethod method, final URI uri) {

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

    httpClient.addRequestInterceptor(new HttpRequestInterceptor() {

        @Override//from w ww.  j  a v  a2  s .  com
        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            request.addHeader("CUSTOM_HEADER", "CUSTOM VALUE");
        }

    });

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {

        @Override
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            if ("ANOTHER CUSTOM VALUE".equals(response.getFirstHeader("ANOTHER_CUSTOM_HEADER"))) {
                // do something
            }
        }
    });

    return 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.
 * //from  w  w  w . j  a v  a 2 s.c  o m
 * @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.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   www . j  a  v a  2  s .co  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.overlord.dtgov.client.DtgovApiClient.java

/**
 * Creates the client executor that will be used by RESTEasy when
 * making the request.//from   w  ww  .  j a v a2 s  .  co m
 */
private ClientExecutor createClientExecutor() {
    // TODO I think the http client is thread safe - so let's try to create just one of these
    DefaultHttpClient httpClient = new DefaultHttpClient();
    httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
        @Override
        public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
            Locale l = getLocale();
            if (l == null) {
                l = Locale.getDefault();
            }
            request.addHeader("Accept-Language", l.toString()); //$NON-NLS-1$
        }
    });
    if (this.authProvider != null) {
        httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
            @Override
            public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
                authProvider.provideAuthentication(request);
            }
        });
    }
    return new ApacheHttpClient4Executor(httpClient);
}

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 www .j  ava 2s. c  o  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.googlecode.sardine.AuthenticationTest.java

@Test
public void testBasicPreemptiveAuthHeader() throws Exception {
    final DefaultHttpClient client = new DefaultHttpClient();
    client.addRequestInterceptor(new HttpRequestInterceptor() {
        public void process(final HttpRequest r, final HttpContext context) throws HttpException, IOException {
            assertNotNull(r.getHeaders(HttpHeaders.AUTHORIZATION));
            assertEquals(1, r.getHeaders(HttpHeaders.AUTHORIZATION).length);
            client.removeRequestInterceptorByClass(this.getClass());
        }//from   w  w  w.  ja  v a2s  . com
    });
    Sardine sardine = new SardineImpl(client);
    sardine.setCredentials("anonymous", null);
    // mod_dav supports Range headers for PUT
    final URI url = URI.create("http://sardine.googlecode.com/svn/trunk/README.html");
    sardine.enablePreemptiveAuthentication(url.getHost());
    assertTrue(sardine.exists(url.toString()));
}

From source file:org.apache.olingo.fit.CXFOAuth2HttpClientFactory.java

@Override
protected void accessToken(final DefaultHttpClient client) throws OAuth2Exception {
    client.addRequestInterceptor(new HttpRequestInterceptor() {

        @Override/*  w ww  .  j  a v  a  2 s . c  o  m*/
        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            request.removeHeaders(HttpHeaders.AUTHORIZATION);
            request.addHeader(HttpHeaders.AUTHORIZATION,
                    OAuthClientUtils.createAuthorizationHeader(accessToken));
        }
    });
}

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. j  a  va  2 s  .  c  om*/
    }

    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;
}

From source file:net.tirasa.olingooauth2.AzureADOAuth2HttpClientFactory.java

@Override
protected void accessToken(final DefaultHttpClient client) throws OAuth2Exception {
    client.addRequestInterceptor(new HttpRequestInterceptor() {

        @Override/*  ww w  .  j  a  v  a 2s .com*/
        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            request.removeHeaders(HttpHeaders.AUTHORIZATION);
            request.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token.get("access_token").asText());
        }
    });
}