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);
}
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);
}
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();
}
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"));
}
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;
}
Aggregations