View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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 }