1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.testserver;
29
30 import java.io.IOException;
31 import java.net.InetSocketAddress;
32 import java.net.Socket;
33
34 import org.apache.http.ConnectionReuseStrategy;
35 import org.apache.http.HttpClientConnection;
36 import org.apache.http.HttpException;
37 import org.apache.http.HttpHost;
38 import org.apache.http.HttpRequest;
39 import org.apache.http.HttpResponse;
40 import org.apache.http.impl.DefaultBHttpClientConnection;
41 import org.apache.http.impl.DefaultConnectionReuseStrategy;
42 import org.apache.http.protocol.HttpContext;
43 import org.apache.http.protocol.HttpCoreContext;
44 import org.apache.http.protocol.HttpProcessor;
45 import org.apache.http.protocol.HttpRequestExecutor;
46 import org.apache.http.protocol.ImmutableHttpProcessor;
47 import org.apache.http.protocol.RequestConnControl;
48 import org.apache.http.protocol.RequestContent;
49 import org.apache.http.protocol.RequestExpectContinue;
50 import org.apache.http.protocol.RequestTargetHost;
51 import org.apache.http.protocol.RequestUserAgent;
52
53 public class HttpClient {
54
55 private final HttpProcessor httpproc;
56 private final HttpRequestExecutor httpexecutor;
57 private final ConnectionReuseStrategy connStrategy;
58 private final HttpCoreContext context;
59
60 private volatile int timeout;
61
62 public HttpClient(final HttpProcessor httpproc) {
63 super();
64 this.httpproc = httpproc;
65 this.connStrategy = DefaultConnectionReuseStrategy.INSTANCE;
66 this.httpexecutor = new HttpRequestExecutor();
67 this.context = new HttpCoreContext();
68 }
69
70 public HttpClient() {
71 this(new ImmutableHttpProcessor(
72 new RequestContent(),
73 new RequestTargetHost(),
74 new RequestConnControl(),
75 new RequestUserAgent("TEST-CLIENT/1.1"),
76 new RequestExpectContinue(true)));
77 }
78
79 public HttpContext getContext() {
80 return this.context;
81 }
82
83 public int getTimeout() {
84 return this.timeout;
85 }
86
87 public void setTimeout(final int timeout) {
88 this.timeout = timeout;
89 }
90
91 public DefaultBHttpClientConnection createConnection() {
92 return new LoggingBHttpClientConnection(8 * 1024);
93 }
94
95 public void connect(final HttpHost host, final DefaultBHttpClientConnection conn) throws IOException {
96 final Socket socket = new Socket();
97 socket.connect(new InetSocketAddress(host.getHostName(), host.getPort()), this.timeout);
98 conn.bind(socket);
99 conn.setSocketTimeout(this.timeout);
100 }
101
102 public HttpResponse execute(
103 final HttpRequest request,
104 final HttpHost targetHost,
105 final HttpClientConnection conn) throws HttpException, IOException {
106 this.context.setTargetHost(targetHost);
107 this.httpexecutor.preProcess(request, this.httpproc, this.context);
108 final HttpResponse response = this.httpexecutor.execute(request, conn, this.context);
109 this.httpexecutor.postProcess(response, this.httpproc, this.context);
110 return response;
111 }
112
113 public boolean keepAlive(final HttpResponse response) {
114 return this.connStrategy.keepAlive(response, this.context);
115 }
116
117 }