Search in sources :

Example 26 with BasicHttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project AndroidLife by CaMnter.

the class HurlStack method performRequest.

/*
     * 执行处理 Volley内的 抽象请求 Request<?>
     * 这里会调用 HttpURLConnection 去处理网络请求
     * 但是 HttpURLConnection 处理后,都返回 Apache 的请求结果( HttpResponse )
     * performRequest(...) 接下来会将:Apache HttpResponse -> Volley NetworkResponse 进行转化
     */
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
    // 获取 Volley 抽象请求 Request 中的 String 类型的 Url
    String url = request.getUrl();
    // 实例化一个 HashMap 来存放 Header 信息
    HashMap<String, String> map = new HashMap<String, String>();
    map.putAll(request.getHeaders());
    map.putAll(additionalHeaders);
    // 判断是否有 Url 重写的逻辑
    if (mUrlRewriter != null) {
        String rewritten = mUrlRewriter.rewriteUrl(url);
        if (rewritten == null) {
            throw new IOException("URL blocked by rewriter: " + url);
        }
        // 重新赋值上 UrlRewriter 接口 重写的 Url
        url = rewritten;
    }
    // 实例化一个 java.net.Url 对象
    URL parsedUrl = new URL(url);
    /*
         * 将 URL 对象 和 Volley 的抽象请求 Request 传入到 openConnection(...) 方法内
         * 完成 Volley 抽象请求 Request -> HttpURLConnection 的转换过渡
         * 此时拿到一个 HttpURLConnection 对象
         */
    HttpURLConnection connection = openConnection(parsedUrl, request);
    // 根据刚才存放 Header 信息的 Map,给 HttpURLConnection 添加头信息
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    // 设置 HttpURLConnection 请求方法类型
    setConnectionParametersForRequest(connection, request);
    // Initialize HttpResponse with data from the HttpURLConnection.
    /*
         * 初始化 一个 Apache 的 HTTP 协议 ( ProtocolVersion )
         */
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    /*
         * 正常情况下 HttpURLConnection 是依靠 connect() 发请求的
         *
         * 但是 HttpURLConnection 的 getInputStream() 和 getOutputStream() 也会自动调用 connect()
         *
         * HttpURLConnection 的 getResponseCode() 会调用 getInputStream()
         * 然后 getInputStream() 又会自动调用 connect(),于是
         *
         * 这里就是 发请求了
         */
    int responseCode = connection.getResponseCode();
    // responseCode == -1 表示 没有返回内容
    if (responseCode == -1) {
        // Signal to the caller that something was wrong with the connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    // 实例化  org.apache.http.StatusLine 对象
    StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
    // 用 org.apache.http.StatusLine 去实例化一个 Apache 的 Response
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    /*
         * 判断请求结果 Response 是否存在 body
         *
         * 有的话,给刚才实例话的 Apache Response 设置 HttpEntity( 调用 entityFromConnection(...)
         * 通过一个 HttpURLConnection 获取其对应的 HttpEntity )
         */
    if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
        response.setEntity(entityFromConnection(connection));
    }
    // 设置 请求结果 Response 的头信息
    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);
        }
    }
    // 返回设置好的 Apache Response
    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 27 with BasicHttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project k-9 by k9mail.

the class WebDavStoreTest method createOkResponseWithForm.

private BasicHttpResponse createOkResponseWithForm() {
    BasicHttpResponse okayResponseWithForm = createOkResponse();
    BasicHttpEntity okayResponseWithFormEntity = new BasicHttpEntity();
    String form = "<form action=\"owaauth.dll\"></form>";
    okayResponseWithFormEntity.setContent(new ByteArrayInputStream(form.getBytes()));
    okayResponseWithForm.setEntity(okayResponseWithFormEntity);
    return okayResponseWithForm;
}
Also used : BasicHttpResponse(org.apache.http.message.BasicHttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity)

Example 28 with BasicHttpResponse

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

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);
    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 29 with BasicHttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project TaEmCasa by Dionen.

the class BasicNetworkTest method redirect.

@Test
public void redirect() throws Exception {
    for (int i = 300; i <= 399; i++) {
        MockHttpStack mockHttpStack = new MockHttpStack();
        BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), i, "");
        mockHttpStack.setResponseToReturn(fakeResponse);
        BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
        Request<String> request = buildRequest();
        request.setRetryPolicy(mMockRetryPolicy);
        doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
        try {
            httpNetwork.performRequest(request);
        } catch (VolleyError e) {
        // expected
        }
        // should not retry 300 responses.
        verify(mMockRetryPolicy, never()).retry(any(VolleyError.class));
        reset(mMockRetryPolicy);
    }
}
Also used : VolleyError(com.android.volley.VolleyError) MockHttpStack(com.android.volley.mock.MockHttpStack) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion) Test(org.junit.Test)

Example 30 with BasicHttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project TaEmCasa by Dionen.

the class BasicNetworkTest method unauthorized.

@Test
public void unauthorized() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 401, "Unauthorized");
    mockHttpStack.setResponseToReturn(fakeResponse);
    BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
    Request<String> request = buildRequest();
    request.setRetryPolicy(mMockRetryPolicy);
    doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
    try {
        httpNetwork.performRequest(request);
    } catch (VolleyError e) {
    // expected
    }
    // should retry in case it's an auth failure.
    verify(mMockRetryPolicy).retry(any(AuthFailureError.class));
}
Also used : VolleyError(com.android.volley.VolleyError) MockHttpStack(com.android.volley.mock.MockHttpStack) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) AuthFailureError(com.android.volley.AuthFailureError) ProtocolVersion(org.apache.http.ProtocolVersion) 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