Home | History | Annotate | Download | only in toolbox
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.volley.toolbox;
     17 
     18 import com.android.volley.AuthFailureError;
     19 import com.android.volley.Header;
     20 import com.android.volley.Request;
     21 
     22 import org.apache.http.ProtocolVersion;
     23 import org.apache.http.StatusLine;
     24 import org.apache.http.entity.BasicHttpEntity;
     25 import org.apache.http.message.BasicHeader;
     26 import org.apache.http.message.BasicHttpResponse;
     27 import org.apache.http.message.BasicStatusLine;
     28 
     29 import java.io.IOException;
     30 import java.io.InputStream;
     31 import java.net.SocketTimeoutException;
     32 import java.util.ArrayList;
     33 import java.util.List;
     34 import java.util.Map;
     35 
     36 /** An HTTP stack abstraction. */
     37 @SuppressWarnings("deprecation") // for HttpStack
     38 public abstract class BaseHttpStack implements HttpStack {
     39 
     40     /**
     41      * Performs an HTTP request with the given parameters.
     42      *
     43      * <p>A GET request is sent if request.getPostBody() == null. A POST request is sent otherwise,
     44      * and the Content-Type header is set to request.getPostBodyContentType().
     45      *
     46      * @param request the request to perform
     47      * @param additionalHeaders additional headers to be sent together with
     48      *         {@link Request#getHeaders()}
     49      * @return the {@link HttpResponse}
     50      * @throws SocketTimeoutException if the request times out
     51      * @throws IOException if another I/O error occurs during the request
     52      * @throws AuthFailureError if an authentication failure occurs during the request
     53      */
     54     public abstract HttpResponse executeRequest(
     55             Request<?> request, Map<String, String> additionalHeaders)
     56             throws IOException, AuthFailureError;
     57 
     58     /**
     59      * @deprecated use {@link #executeRequest} instead to avoid a dependency on the deprecated
     60      * Apache HTTP library. Nothing in Volley's own source calls this method. However, since
     61      * {@link BasicNetwork#mHttpStack} is exposed to subclasses, we provide this implementation in
     62      * case legacy client apps are dependent on that field. This method may be removed in a future
     63      * release of Volley.
     64      */
     65     @Deprecated
     66     @Override
     67     public final org.apache.http.HttpResponse performRequest(
     68             Request<?> request, Map<String, String> additionalHeaders)
     69             throws IOException, AuthFailureError {
     70         HttpResponse response = executeRequest(request, additionalHeaders);
     71 
     72         ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
     73         StatusLine statusLine = new BasicStatusLine(
     74                 protocolVersion, response.getStatusCode(), "" /* reasonPhrase */);
     75         BasicHttpResponse apacheResponse = new BasicHttpResponse(statusLine);
     76 
     77         List<org.apache.http.Header> headers = new ArrayList<>();
     78         for (Header header : response.getHeaders()) {
     79             headers.add(new BasicHeader(header.getName(), header.getValue()));
     80         }
     81         apacheResponse.setHeaders(headers.toArray(new org.apache.http.Header[headers.size()]));
     82 
     83         InputStream responseStream = response.getContent();
     84         if (responseStream != null) {
     85             BasicHttpEntity entity = new BasicHttpEntity();
     86             entity.setContent(responseStream);
     87             entity.setContentLength(response.getContentLength());
     88             apacheResponse.setEntity(entity);
     89         }
     90 
     91         return apacheResponse;
     92     }
     93 }
     94