Search in sources :

Example 41 with BasicHttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project FastDev4Android by jiangqqlmj.

the class HurlStack method performRequest.

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
    String url = request.getUrl();
    HashMap<String, String> map = new HashMap<String, String>();
    map.putAll(request.getHeaders());
    map.putAll(additionalHeaders);
    if (mUrlRewriter != null) {
        String rewritten = mUrlRewriter.rewriteUrl(url);
        if (rewritten == null) {
            throw new IOException("URL blocked by rewriter: " + url);
        }
        url = rewritten;
    }
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl, request);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    setConnectionParametersForRequest(connection, request);
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // Signal to the caller that something was wrong with the connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
        response.setEntity(entityFromConnection(connection));
    }
    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }
    return response;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) ProtocolVersion(org.apache.http.ProtocolVersion) URL(java.net.URL) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpURLConnection(java.net.HttpURLConnection) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) List(java.util.List) BasicHeader(org.apache.http.message.BasicHeader)

Example 42 with BasicHttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project FastDev4Android by jiangqqlmj.

the class BasicNetworkTest method headersAndPostParams.

@Test
public void headersAndPostParams() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 200, "OK");
    fakeResponse.setEntity(new StringEntity("foobar"));
    mockHttpStack.setResponseToReturn(fakeResponse);
    BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
    Request<String> request = new Request<String>(Request.Method.GET, "http://foo", null) {

        @Override
        protected Response<String> parseNetworkResponse(NetworkResponse response) {
            return null;
        }

        @Override
        protected void deliverResponse(String response) {
        }

        @Override
        public Map<String, String> getHeaders() {
            Map<String, String> result = new HashMap<String, String>();
            result.put("requestheader", "foo");
            return result;
        }

        @Override
        public Map<String, String> getParams() {
            Map<String, String> result = new HashMap<String, String>();
            result.put("requestpost", "foo");
            return result;
        }
    };
    httpNetwork.performRequest(request);
    assertEquals("foo", mockHttpStack.getLastHeaders().get("requestheader"));
    assertEquals("requestpost=foo&", new String(mockHttpStack.getLastPostBody()));
}
Also used : StringEntity(org.apache.http.entity.StringEntity) MockHttpStack(com.android.volley.mock.MockHttpStack) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HashMap(java.util.HashMap) Request(com.android.volley.Request) NetworkResponse(com.android.volley.NetworkResponse) ProtocolVersion(org.apache.http.ProtocolVersion) Test(org.junit.Test)

Example 43 with BasicHttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project android-uploader by nightscout.

the class RestLegacyUploaderTest method setUpExecuteCaptor.

public void setUpExecuteCaptor(int status) throws IOException {
    captor = ArgumentCaptor.forClass(HttpUriRequest.class);
    HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("mock", 1, 2), status, ""));
    response.setEntity(new StringEntity(""));
    when(mockHttpClient.execute(captor.capture())).thenReturn(response);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) StringEntity(org.apache.http.entity.StringEntity) 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 44 with BasicHttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project android-uploader by nightscout.

the class AbstractRestUploaderTest method setUpExecuteCaptor.

public void setUpExecuteCaptor(int status) throws IOException {
    HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("mock", 1, 2), status, ""));
    response.setEntity(new StringEntity(""));
    when(mockHttpClient.execute(captor.capture())).thenReturn(response);
}
Also used : StringEntity(org.apache.http.entity.StringEntity) 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 45 with BasicHttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project jo-client-platform by jo-source.

the class HttpClientStub method get.

private HttpResponse get(final HttpGet request) throws IOException, ClientProtocolException {
    getInvocations.incrementAndGet();
    final boolean firstGet = firstGetWithSuccess.get();
    if (firstGet) {
        final StatusLine status = firstGetStatus.get();
        final BasicHttpResponse result = new BasicHttpResponse(status);
        if (status.getStatusCode() == 200) {
            return firstGetWithSuccess(request, result);
        } else {
            return firstGetWithError(request, result);
        }
    } else {
        final StatusLine status = getStatus.get();
        final BasicHttpResponse result = new BasicHttpResponse(status);
        if (status.getStatusCode() == 200) {
            return getWithSuccess(request, result);
        } else {
            return getWithError(request, result);
        }
    }
}
Also used : BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse)

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