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 import java.io.IOException;
     22 import java.net.SocketTimeoutException;
     23 import java.util.ArrayList;
     24 import java.util.List;
     25 import java.util.Map;
     26 import org.apache.http.conn.ConnectTimeoutException;
     27 
     28 /**
     29  * {@link BaseHttpStack} implementation wrapping a {@link HttpStack}.
     30  *
     31  * <p>{@link BasicNetwork} uses this if it is provided a {@link HttpStack} at construction time,
     32  * allowing it to have one implementation based atop {@link BaseHttpStack}.
     33  */
     34 @SuppressWarnings("deprecation")
     35 class AdaptedHttpStack extends BaseHttpStack {
     36 
     37     private final HttpStack mHttpStack;
     38 
     39     AdaptedHttpStack(HttpStack httpStack) {
     40         mHttpStack = httpStack;
     41     }
     42 
     43     @Override
     44     public HttpResponse executeRequest(Request<?> request, Map<String, String> additionalHeaders)
     45             throws IOException, AuthFailureError {
     46         org.apache.http.HttpResponse apacheResp;
     47         try {
     48             apacheResp = mHttpStack.performRequest(request, additionalHeaders);
     49         } catch (ConnectTimeoutException e) {
     50             // BasicNetwork won't know that this exception should be retried like a timeout, since
     51             // it's an Apache-specific error, so wrap it in a standard timeout exception.
     52             throw new SocketTimeoutException(e.getMessage());
     53         }
     54 
     55         int statusCode = apacheResp.getStatusLine().getStatusCode();
     56 
     57         org.apache.http.Header[] headers = apacheResp.getAllHeaders();
     58         List<Header> headerList = new ArrayList<>(headers.length);
     59         for (org.apache.http.Header header : headers) {
     60             headerList.add(new Header(header.getName(), header.getValue()));
     61         }
     62 
     63         if (apacheResp.getEntity() == null) {
     64             return new HttpResponse(statusCode, headerList);
     65         }
     66 
     67         long contentLength = apacheResp.getEntity().getContentLength();
     68         if ((int) contentLength != contentLength) {
     69             throw new IOException("Response too large: " + contentLength);
     70         }
     71 
     72         return new HttpResponse(
     73                 statusCode,
     74                 headerList,
     75                 (int) apacheResp.getEntity().getContentLength(),
     76                 apacheResp.getEntity().getContent());
     77     }
     78 }
     79