1/*
2 * Copyright (C) 2011 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
17package com.android.volley.toolbox;
18
19import com.android.volley.AuthFailureError;
20import com.android.volley.Request;
21
22import org.apache.http.HttpEntity;
23import org.apache.http.HttpResponse;
24import org.apache.http.NameValuePair;
25import org.apache.http.client.HttpClient;
26import org.apache.http.client.methods.HttpGet;
27import org.apache.http.client.methods.HttpPost;
28import org.apache.http.client.methods.HttpUriRequest;
29import org.apache.http.entity.ByteArrayEntity;
30import org.apache.http.message.BasicNameValuePair;
31import org.apache.http.params.HttpConnectionParams;
32import org.apache.http.params.HttpParams;
33
34import java.io.IOException;
35import java.util.ArrayList;
36import java.util.List;
37import java.util.Map;
38
39/**
40 * An HttpStack that performs request over an {@link HttpClient}.
41 */
42public class HttpClientStack implements HttpStack {
43    protected final HttpClient mClient;
44
45    public HttpClientStack(HttpClient client) {
46        mClient = client;
47    }
48
49    private static void addHeaders(HttpUriRequest httpRequest, Map<String, String> headers) {
50        for (String key : headers.keySet()) {
51            httpRequest.setHeader(key, headers.get(key));
52        }
53    }
54
55    @SuppressWarnings("unused")
56    private static List<NameValuePair> getPostParameterPairs(Map<String, String> postParams) {
57        List<NameValuePair> result = new ArrayList<NameValuePair>(postParams.size());
58        for (String key : postParams.keySet()) {
59            result.add(new BasicNameValuePair(key, postParams.get(key)));
60        }
61        return result;
62    }
63
64    @Override
65    public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
66            throws IOException, AuthFailureError {
67        HttpUriRequest httpRequest;
68        byte[] postBody = request.getPostBody();
69        if (postBody != null) {
70            HttpPost postRequest = new HttpPost(request.getUrl());
71            postRequest.addHeader("Content-Type", request.getPostBodyContentType());
72            HttpEntity entity;
73            entity = new ByteArrayEntity(postBody);
74            postRequest.setEntity(entity);
75            httpRequest = postRequest;
76        } else {
77            httpRequest = new HttpGet(request.getUrl());
78        }
79        addHeaders(httpRequest, additionalHeaders);
80        addHeaders(httpRequest, request.getHeaders());
81        onPrepareRequest(httpRequest);
82        HttpParams httpParams = httpRequest.getParams();
83        int timeoutMs = request.getTimeoutMs();
84        // TODO: Reevaluate this connection timeout based on more wide-scale
85        // data collection and possibly different for wifi vs. 3G.
86        HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
87        HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
88        return mClient.execute(httpRequest);
89    }
90
91    /**
92     * Called before the request is executed using the underlying HttpClient.
93     *
94     * <p>Overwrite in subclasses to augment the request.</p>
95     */
96    protected void onPrepareRequest(HttpUriRequest request) throws IOException {
97        // Nothing.
98    }
99}
100