use of org.apache.http.message.BasicHttpResponse in project SimplifyReader by chentao0707.
the class OkHttpStack method performRequest.
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
OkHttpClient client = mClient.clone();
int timeoutMs = request.getTimeoutMs();
client.setConnectTimeout(timeoutMs, TimeUnit.MILLISECONDS);
client.setReadTimeout(timeoutMs, TimeUnit.MILLISECONDS);
client.setWriteTimeout(timeoutMs, TimeUnit.MILLISECONDS);
com.squareup.okhttp.Request.Builder okHttpRequestBuilder = new com.squareup.okhttp.Request.Builder();
okHttpRequestBuilder.url(request.getUrl());
Map<String, String> headers = request.getHeaders();
for (final String name : headers.keySet()) {
okHttpRequestBuilder.addHeader(name, headers.get(name));
}
for (final String name : additionalHeaders.keySet()) {
okHttpRequestBuilder.addHeader(name, additionalHeaders.get(name));
}
setConnectionParametersForRequest(okHttpRequestBuilder, request);
com.squareup.okhttp.Request okHttpRequest = okHttpRequestBuilder.build();
Call okHttpCall = client.newCall(okHttpRequest);
Response okHttpResponse = okHttpCall.execute();
StatusLine responseStatus = new BasicStatusLine(parseProtocol(okHttpResponse.protocol()), okHttpResponse.code(), okHttpResponse.message());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromOkHttpResponse(okHttpResponse));
Headers responseHeaders = okHttpResponse.headers();
for (int i = 0, len = responseHeaders.size(); i < len; i++) {
final String name = responseHeaders.name(i), value = responseHeaders.value(i);
if (name != null) {
response.addHeader(new BasicHeader(name, value));
}
}
return response;
}
use of org.apache.http.message.BasicHttpResponse in project graylog2-server by Graylog2.
the class ElasticsearchFilterDeprecationWarningsInterceptorTest method testInterceptorNoHeader.
@Test
public void testInterceptorNoHeader() throws IOException, HttpException {
ElasticsearchFilterDeprecationWarningsInterceptor interceptor = new ElasticsearchFilterDeprecationWarningsInterceptor();
HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 0, 0), 0, null));
interceptor.process(response, null);
assertThat(response.getAllHeaders()).as("Number of Headers should be 0 and the interceptor should not fail in itself.").hasSize(0);
}
use of org.apache.http.message.BasicHttpResponse in project graylog2-server by Graylog2.
the class ElasticsearchFilterDeprecationWarningsInterceptorTest method testInterceptorMultipleHeaderFilteredWarning2.
@Test
public void testInterceptorMultipleHeaderFilteredWarning2() throws IOException, HttpException {
ElasticsearchFilterDeprecationWarningsInterceptor interceptor = new ElasticsearchFilterDeprecationWarningsInterceptor();
HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 0, 0), 0, null));
response.addHeader("Test", "This header should not trigger the interceptor.");
response.addHeader("Warning", "This warning should not trigger the interceptor.");
response.addHeader("Warning", "This text contains the trigger: but in a future major version, direct access to system indices and their aliases will not be allowed - and should be filtered out");
assertThat(response.getAllHeaders()).as("Number of Headers should be 3 before start.").hasSize(3);
interceptor.process(response, null);
assertThat(response.getAllHeaders()).as("Number of Headers should be 1 less after running the interceptor.").hasSize(2);
}
use of org.apache.http.message.BasicHttpResponse in project FastDev4Android by jiangqqlmj.
the class MockHttpClient method execute.
// This is the only one we actually use.
@Override
public HttpResponse execute(HttpUriRequest request, HttpContext context) {
requestExecuted = request;
StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), mStatusCode, "");
HttpResponse response = new BasicHttpResponse(statusLine);
response.setEntity(mResponseEntity);
return response;
}
use of org.apache.http.message.BasicHttpResponse in project java by wavefrontHQ.
the class HttpGetTokenIntrospectionAuthenticatorTest method testIntrospectionUrlCachedLastResultExpires.
@Test
public void testIntrospectionUrlCachedLastResultExpires() throws Exception {
HttpClient client = EasyMock.createMock(HttpClient.class);
AtomicLong fakeClock = new AtomicLong(1_000_000);
TokenAuthenticator authenticator = new HttpGetTokenIntrospectionAuthenticator(client, "http://acme.corp/{{token}}/something", null, 300, 600, fakeClock::get);
String uuid = UUID.randomUUID().toString();
EasyMock.expect(client.execute(httpEq(new HttpGet("http://acme.corp/" + uuid + "/something")))).andReturn(new BasicHttpResponse(HttpVersion.HTTP_1_1, 204, "")).once().andThrow(new IOException("Timeout!")).times(3);
EasyMock.replay(client);
// should call http
assertTrue(authenticator.authorize(uuid));
fakeClock.getAndAdd(630_000);
// should call http, fail, but still return last valid result
assertTrue(authenticator.authorize(uuid));
// Thread.sleep(100);
// TTL expired - should fail
assertTrueWithTimeout(100, () -> !authenticator.authorize(uuid));
// Thread.sleep(100);
// Should call http again - TTL expired
assertTrueWithTimeout(100, () -> !authenticator.authorize(uuid));
EasyMock.verify(client);
}
Aggregations