Search in sources :

Example 6 with HttpRequestInterceptor

use of org.apache.http.HttpRequestInterceptor in project jaeger-client-java by jaegertracing.

the class TracingRequestInterceptorTest method testProcessNullScope.

@Test
public void testProcessNullScope() throws Exception {
    ScopeManager mockScopeManager = Mockito.mock(ScopeManager.class);
    when(mockScopeManager.active()).thenReturn(null);
    Tracer mockTracer = Mockito.mock(Tracer.class);
    when(mockTracer.scopeManager()).thenReturn(mockScopeManager);
    HttpRequestInterceptor interceptor = new TracingRequestInterceptor(mockTracer);
    PowerMockito.spy(interceptor);
    HttpRequest mockRequest = Mockito.mock(HttpRequest.class);
    HttpContext mockContext = Mockito.mock(HttpContext.class);
    interceptor.process(mockRequest, mockContext);
    PowerMockito.verifyPrivate(interceptor, times(0)).invoke("onSpanStarted", any(Span.class), mockRequest, mockContext);
}
Also used : HttpRequest(org.apache.http.HttpRequest) ScopeManager(io.opentracing.ScopeManager) Tracer(io.opentracing.Tracer) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) HttpContext(org.apache.http.protocol.HttpContext) Span(io.opentracing.Span) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 7 with HttpRequestInterceptor

use of org.apache.http.HttpRequestInterceptor in project crnk-framework by crnk-project.

the class CharsetTest method testUTF8isDefault.

public void testUTF8isDefault(boolean okHttp) throws InstantiationException, IllegalAccessException {
    requestContentType = null;
    responseContentType = null;
    if (okHttp) {
        OkHttpAdapter adapter = OkHttpAdapter.newInstance();
        adapter.addListener(new OkHttpAdapterListener() {

            @Override
            public void onBuild(OkHttpClient.Builder builder) {
                builder.addInterceptor(new Interceptor() {

                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        requestContentType = chain.request().header(HttpHeaders.HTTP_CONTENT_TYPE);
                        Response response = chain.proceed(chain.request());
                        responseContentType = response.header(HttpHeaders.HTTP_CONTENT_TYPE);
                        return response;
                    }
                });
            }
        });
        client.setHttpAdapter(adapter);
    } else {
        HttpClientAdapter adapter = HttpClientAdapter.newInstance();
        adapter.addListener(new HttpClientAdapterListener() {

            @Override
            public void onBuild(HttpClientBuilder builder) {
                builder.addInterceptorFirst(new HttpRequestInterceptor() {

                    @Override
                    public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {
                        Header header = httpRequest.getFirstHeader(HttpHeaders.HTTP_CONTENT_TYPE);
                        requestContentType = header != null ? header.getValue() : null;
                    }
                });
                builder.addInterceptorFirst(new HttpResponseInterceptor() {

                    @Override
                    public void process(HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
                        Header header = httpResponse.getFirstHeader(HttpHeaders.HTTP_CONTENT_TYPE);
                        responseContentType = header != null ? header.getValue() : null;
                    }
                });
            }
        });
        client.setHttpAdapter(adapter);
    }
    ResourceRepositoryV2<Task, Long> testRepo = client.getRepositoryForType(Task.class);
    Task entity = new Task();
    entity.setId(1L);
    entity.setName("äöüé@¢€");
    testRepo.create(entity);
    Assert.assertEquals(HttpHeaders.JSONAPI_CONTENT_TYPE_AND_CHARSET, requestContentType);
    Assert.assertEquals(HttpHeaders.JSONAPI_CONTENT_TYPE_AND_CHARSET, responseContentType);
    Task savedEntity = testRepo.findOne(1L, new QuerySpec(Task.class));
    Assert.assertEquals(entity.getName(), savedEntity.getName());
    Assert.assertNull(requestContentType);
    Assert.assertEquals(HttpHeaders.JSONAPI_CONTENT_TYPE_AND_CHARSET, responseContentType);
}
Also used : HttpRequest(org.apache.http.HttpRequest) Task(io.crnk.test.mock.models.Task) OkHttpClient(okhttp3.OkHttpClient) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) HttpClientAdapterListener(io.crnk.client.http.apache.HttpClientAdapterListener) Response(okhttp3.Response) HttpResponse(org.apache.http.HttpResponse) HttpClientAdapter(io.crnk.client.http.apache.HttpClientAdapter) Header(org.apache.http.Header) OkHttpAdapterListener(io.crnk.client.http.okhttp.OkHttpAdapterListener) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) OkHttpAdapter(io.crnk.client.http.okhttp.OkHttpAdapter) HttpResponseInterceptor(org.apache.http.HttpResponseInterceptor) QuerySpec(io.crnk.core.queryspec.QuerySpec) Interceptor(okhttp3.Interceptor) HttpResponseInterceptor(org.apache.http.HttpResponseInterceptor) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor)

Example 8 with HttpRequestInterceptor

use of org.apache.http.HttpRequestInterceptor in project connect-sdk-java by Ingenico-ePayments.

the class DefaultConnection method createHttpClient.

private CloseableHttpClient createHttpClient(ProxyConfiguration proxyConfiguration) {
    HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connectionManager);
    HttpRoutePlanner routePlanner;
    CredentialsProvider credentialsProvider;
    if (proxyConfiguration != null) {
        HttpHost proxy = new HttpHost(proxyConfiguration.getHost(), proxyConfiguration.getPort(), proxyConfiguration.getScheme());
        routePlanner = new DefaultProxyRoutePlanner(proxy, DefaultSchemePortResolver.INSTANCE);
        credentialsProvider = new BasicCredentialsProvider();
        if (proxyConfiguration.getUsername() != null) {
            AuthScope authscope = new AuthScope(proxyConfiguration.getHost(), proxyConfiguration.getPort());
            final Credentials credentials = new UsernamePasswordCredentials(proxyConfiguration.getUsername(), proxyConfiguration.getPassword());
            credentialsProvider.setCredentials(authscope, credentials);
            // enable preemptive authentication
            HttpRequestInterceptor proxyAuthenticationInterceptor = new HttpRequestInterceptor() {

                @Override
                public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
                    Header header = request.getFirstHeader(AUTH.PROXY_AUTH_RESP);
                    if (header == null) {
                        header = new BasicScheme((Charset) null).authenticate(credentials, request, context);
                        if (!AUTH.PROXY_AUTH_RESP.equals(header.getName())) {
                            header = new BasicHeader(AUTH.PROXY_AUTH_RESP, header.getValue());
                        }
                        request.setHeader(header);
                    }
                }
            };
            builder = builder.addInterceptorLast(proxyAuthenticationInterceptor);
        }
    } else {
        // add support for system properties
        routePlanner = new SystemDefaultRoutePlanner(DefaultSchemePortResolver.INSTANCE, ProxySelector.getDefault());
        credentialsProvider = new SystemDefaultCredentialsProvider();
    }
    // add logging - last for requests, first for responses
    LoggingInterceptor loggingInterceptor = new LoggingInterceptor();
    builder = builder.addInterceptorLast((HttpRequestInterceptor) loggingInterceptor);
    builder = builder.addInterceptorFirst((HttpResponseInterceptor) loggingInterceptor);
    return builder.setRoutePlanner(routePlanner).setDefaultCredentialsProvider(credentialsProvider).build();
}
Also used : HttpRequest(org.apache.http.HttpRequest) BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) DefaultProxyRoutePlanner(org.apache.http.impl.conn.DefaultProxyRoutePlanner) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) SystemDefaultCredentialsProvider(org.apache.http.impl.client.SystemDefaultCredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Header(org.apache.http.Header) ResponseHeader(com.ingenico.connect.gateway.sdk.java.ResponseHeader) RequestHeader(com.ingenico.connect.gateway.sdk.java.RequestHeader) BasicHeader(org.apache.http.message.BasicHeader) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) HttpHost(org.apache.http.HttpHost) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) AuthScope(org.apache.http.auth.AuthScope) HttpResponseInterceptor(org.apache.http.HttpResponseInterceptor) SystemDefaultCredentialsProvider(org.apache.http.impl.client.SystemDefaultCredentialsProvider) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) BasicHeader(org.apache.http.message.BasicHeader) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner)

Example 9 with HttpRequestInterceptor

use of org.apache.http.HttpRequestInterceptor in project epp.mpc by eclipse.

the class TransportFactoryTest method testHttpClientCustomizer.

@Test
public void testHttpClientCustomizer() throws Exception {
    final HttpClientCustomizer customizer = Mockito.mock(HttpClientCustomizer.class);
    Mockito.when(customizer.customizeBuilder(Matchers.any())).thenAnswer(new Answer<HttpClientBuilder>() {

        public HttpClientBuilder answer(InvocationOnMock invocation) {
            HttpClientBuilder builder = (HttpClientBuilder) invocation.getArguments()[0];
            return builder == null ? null : builder.addInterceptorFirst(new HttpRequestInterceptor() {

                @Override
                public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
                    request.addHeader("X-Customizer-Test", "true");
                }
            });
        }
    });
    Mockito.when(customizer.customizeCredentialsProvider(Matchers.any())).thenReturn(null);
    HttpRequest request = interceptRequest(customizer).getInterceptedRequest();
    Mockito.verify(customizer).customizeBuilder(Matchers.any());
    Mockito.verify(customizer).customizeCredentialsProvider(Matchers.any());
    assertThat(request.getFirstHeader("X-Customizer-Test"), LambdaMatchers.<Header, String>map(x -> x == null ? null : x.getValue()).matches("true"));
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpClientCustomizer(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientCustomizer) Header(org.apache.http.Header) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) HttpException(org.apache.http.HttpException) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) IOException(java.io.IOException) Test(org.junit.Test)

Example 10 with HttpRequestInterceptor

use of org.apache.http.HttpRequestInterceptor in project epp.mpc by eclipse.

the class HttpClientFactory method createCredentialsProvider.

private static CredentialsProvider createCredentialsProvider(HttpClientBuilder clientBuilder) {
    // TODO we should handle configured proxy passwords and dialogs to prompt for unknown credentials on our own...
    CredentialsProvider credentialsProvider = new SystemCredentialsProvider();
    credentialsProvider = customizeCredentialsProvider(credentialsProvider);
    final CacheCredentialsProvider cacheProvider = new CacheCredentialsProvider();
    credentialsProvider = new ChainedCredentialsProvider(cacheProvider, credentialsProvider);
    credentialsProvider = new SynchronizedCredentialsProvider(credentialsProvider);
    clientBuilder.addInterceptorFirst(new HttpRequestInterceptor() {

        public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
            context.setAttribute(CacheCredentialsAuthenticationStrategy.CREDENTIALS_CACHE_ATTRIBUTE, cacheProvider);
        }
    });
    return credentialsProvider;
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) HttpContext(org.apache.http.protocol.HttpContext) HttpException(org.apache.http.HttpException) CredentialsProvider(org.apache.http.client.CredentialsProvider) IOException(java.io.IOException)

Aggregations

HttpRequestInterceptor (org.apache.http.HttpRequestInterceptor)24 HttpRequest (org.apache.http.HttpRequest)11 HttpContext (org.apache.http.protocol.HttpContext)11 IOException (java.io.IOException)9 HttpResponseInterceptor (org.apache.http.HttpResponseInterceptor)8 ArrayList (java.util.ArrayList)6 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)6 Header (org.apache.http.Header)5 CredentialsProvider (org.apache.http.client.CredentialsProvider)5 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)5 Test (org.junit.Test)5 HttpResponse (org.apache.http.HttpResponse)4 AuthScope (org.apache.http.auth.AuthScope)4 HttpGet (org.apache.http.client.methods.HttpGet)4 ImmutableHttpProcessor (org.apache.http.protocol.ImmutableHttpProcessor)4 URI (java.net.URI)3 Map (java.util.Map)3 HttpException (org.apache.http.HttpException)3 HttpHost (org.apache.http.HttpHost)3 Credentials (org.apache.http.auth.Credentials)3