Search in sources :

Example 96 with BasicHttpResponse

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;
}
Also used : Call(com.squareup.okhttp.Call) OkHttpClient(com.squareup.okhttp.OkHttpClient) Headers(com.squareup.okhttp.Headers) Request(com.android.volley.Request) BasicStatusLine(org.apache.http.message.BasicStatusLine) Response(com.squareup.okhttp.Response) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) BasicHeader(org.apache.http.message.BasicHeader)

Example 97 with BasicHttpResponse

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);
}
Also used : BasicHttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse) BasicHttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse) HttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.HttpResponse) ProtocolVersion(org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion) BasicStatusLine(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 98 with BasicHttpResponse

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);
}
Also used : BasicHttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse) BasicHttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse) HttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.HttpResponse) ProtocolVersion(org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion) BasicStatusLine(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 99 with BasicHttpResponse

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;
}
Also used : BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine)

Example 100 with BasicHttpResponse

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);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

BasicHttpResponse (org.apache.http.message.BasicHttpResponse)107 BasicStatusLine (org.apache.http.message.BasicStatusLine)64 ProtocolVersion (org.apache.http.ProtocolVersion)58 HttpResponse (org.apache.http.HttpResponse)51 StatusLine (org.apache.http.StatusLine)39 Test (org.junit.Test)39 StringEntity (org.apache.http.entity.StringEntity)34 IOException (java.io.IOException)33 List (java.util.List)24 Header (org.apache.http.Header)22 HttpHost (org.apache.http.HttpHost)19 HashMap (java.util.HashMap)18 URL (java.net.URL)17 BasicHeader (org.apache.http.message.BasicHeader)17 HttpURLConnection (java.net.HttpURLConnection)16 HttpEntity (org.apache.http.HttpEntity)14 MainResponse (org.elasticsearch.action.main.MainResponse)14 Before (org.junit.Before)13 BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)12 ElasticsearchException (org.elasticsearch.ElasticsearchException)12