1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha6/module-main/src/test/java/org/apache/http/mockup/TestHttpClient.java $
3 * $Revision: 576077 $
4 * $Date: 2007-09-16 04:50:22 -0700 (Sun, 16 Sep 2007) $
5 *
6 * ====================================================================
7 * Licensed to the Apache Software Foundation (ASF) under one
8 * or more contributor license agreements.  See the NOTICE file
9 * distributed with this work for additional information
10 * regarding copyright ownership.  The ASF licenses this file
11 * to you under the Apache License, Version 2.0 (the
12 * "License"); you may not use this file except in compliance
13 * with the License.  You may obtain a copy of the License at
14 *
15 *   http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing,
18 * software distributed under the License is distributed on an
19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20 * KIND, either express or implied.  See the License for the
21 * specific language governing permissions and limitations
22 * under the License.
23 * ====================================================================
24 *
25 * This software consists of voluntary contributions made by many
26 * individuals on behalf of the Apache Software Foundation.  For more
27 * information on the Apache Software Foundation, please see
28 * <http://www.apache.org/>.
29 *
30 */
31
32package android.core;
33
34import java.io.IOException;
35
36import org.apache.http.ConnectionReuseStrategy;
37import org.apache.http.HttpClientConnection;
38import org.apache.http.HttpException;
39import org.apache.http.HttpHost;
40import org.apache.http.HttpRequest;
41import org.apache.http.HttpResponse;
42import org.apache.http.HttpVersion;
43import org.apache.http.impl.DefaultConnectionReuseStrategy;
44import org.apache.http.params.BasicHttpParams;
45import org.apache.http.params.CoreConnectionPNames;
46import org.apache.http.params.DefaultedHttpParams;
47import org.apache.http.params.HttpParams;
48import org.apache.http.params.CoreProtocolPNames;
49import org.apache.http.protocol.BasicHttpProcessor;
50import org.apache.http.protocol.HttpContext;
51import org.apache.http.protocol.BasicHttpContext;
52import org.apache.http.protocol.ExecutionContext;
53import org.apache.http.protocol.HttpRequestExecutor;
54import org.apache.http.protocol.RequestConnControl;
55import org.apache.http.protocol.RequestContent;
56import org.apache.http.protocol.RequestExpectContinue;
57import org.apache.http.protocol.RequestTargetHost;
58import org.apache.http.protocol.RequestUserAgent;
59
60public class TestHttpClient {
61
62    private final HttpParams params;
63    private final BasicHttpProcessor httpproc;
64    private final HttpRequestExecutor httpexecutor;
65    private final ConnectionReuseStrategy connStrategy;
66    private final HttpContext context;
67
68    public TestHttpClient() {
69        super();
70        this.params = new BasicHttpParams();
71        this.params
72            .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
73            .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
74            .setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1)
75            .setParameter(CoreProtocolPNames.USER_AGENT, "TEST-CLIENT/1.1");
76
77        this.httpproc = new BasicHttpProcessor();
78        // Required protocol interceptors
79        this.httpproc.addInterceptor(new RequestContent());
80        this.httpproc.addInterceptor(new RequestTargetHost());
81        // Recommended protocol interceptors
82        this.httpproc.addInterceptor(new RequestConnControl());
83        this.httpproc.addInterceptor(new RequestUserAgent());
84        this.httpproc.addInterceptor(new RequestExpectContinue());
85
86        this.httpexecutor = new HttpRequestExecutor();
87        this.connStrategy = new DefaultConnectionReuseStrategy();
88        this.context = new BasicHttpContext(null);
89    }
90
91    public HttpParams getParams() {
92        return this.params;
93    }
94
95    public HttpResponse execute(
96            final HttpRequest request,
97            final HttpHost targetHost,
98            final HttpClientConnection conn) throws HttpException, IOException {
99        this.context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
100        this.context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, targetHost);
101        this.context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
102        request.setParams(
103                    new DefaultedHttpParams(request.getParams(), this.params));
104        this.httpexecutor.preProcess(request, this.httpproc, this.context);
105        HttpResponse response = this.httpexecutor.execute(request, conn, this.context);
106        response.setParams(
107                new DefaultedHttpParams(response.getParams(), this.params));
108        this.httpexecutor.postProcess(response, this.httpproc, this.context);
109        return response;
110    }
111
112    public boolean keepAlive(final HttpResponse response) {
113        return this.connStrategy.keepAlive(response, this.context);
114    }
115
116}
117