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.io.InputStream; 23 import java.net.SocketTimeoutException; 24 import java.util.ArrayList; 25 import java.util.List; 26 import java.util.Map; 27 import org.apache.http.ProtocolVersion; 28 import org.apache.http.StatusLine; 29 import org.apache.http.entity.BasicHttpEntity; 30 import org.apache.http.message.BasicHeader; 31 import org.apache.http.message.BasicHttpResponse; 32 import org.apache.http.message.BasicStatusLine; 33 34 /** An HTTP stack abstraction. */ 35 @SuppressWarnings("deprecation") // for HttpStack 36 public abstract class BaseHttpStack implements HttpStack { 37 38 /** 39 * Performs an HTTP request with the given parameters. 40 * 41 * <p>A GET request is sent if request.getPostBody() == null. A POST request is sent otherwise, 42 * and the Content-Type header is set to request.getPostBodyContentType(). 43 * 44 * @param request the request to perform 45 * @param additionalHeaders additional headers to be sent together with {@link 46 * Request#getHeaders()} 47 * @return the {@link HttpResponse} 48 * @throws SocketTimeoutException if the request times out 49 * @throws IOException if another I/O error occurs during the request 50 * @throws AuthFailureError if an authentication failure occurs during the request 51 */ executeRequest( Request<?> request, Map<String, String> additionalHeaders)52 public abstract HttpResponse executeRequest( 53 Request<?> request, Map<String, String> additionalHeaders) 54 throws IOException, AuthFailureError; 55 56 /** 57 * @deprecated use {@link #executeRequest} instead to avoid a dependency on the deprecated 58 * Apache HTTP library. Nothing in Volley's own source calls this method. However, since 59 * {@link BasicNetwork#mHttpStack} is exposed to subclasses, we provide this implementation 60 * in case legacy client apps are dependent on that field. This method may be removed in a 61 * future release of Volley. 62 */ 63 @Deprecated 64 @Override performRequest( Request<?> request, Map<String, String> additionalHeaders)65 public final org.apache.http.HttpResponse performRequest( 66 Request<?> request, Map<String, String> additionalHeaders) 67 throws IOException, AuthFailureError { 68 HttpResponse response = executeRequest(request, additionalHeaders); 69 70 ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); 71 StatusLine statusLine = 72 new BasicStatusLine( 73 protocolVersion, response.getStatusCode(), /* reasonPhrase= */ ""); 74 BasicHttpResponse apacheResponse = new BasicHttpResponse(statusLine); 75 76 List<org.apache.http.Header> headers = new ArrayList<>(); 77 for (Header header : response.getHeaders()) { 78 headers.add(new BasicHeader(header.getName(), header.getValue())); 79 } 80 apacheResponse.setHeaders(headers.toArray(new org.apache.http.Header[0])); 81 82 InputStream responseStream = response.getContent(); 83 if (responseStream != null) { 84 BasicHttpEntity entity = new BasicHttpEntity(); 85 entity.setContent(responseStream); 86 entity.setContentLength(response.getContentLength()); 87 apacheResponse.setEntity(entity); 88 } 89 90 return apacheResponse; 91 } 92 } 93