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.InetAddress;
32  import java.net.Socket;
33  import java.net.SocketException;
34  import java.util.concurrent.TimeUnit;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import org.apache.http.ConnectionClosedException;
39  import org.apache.http.ExceptionLogger;
40  import org.apache.http.HttpConnectionFactory;
41  import org.apache.http.config.SocketConfig;
42  import org.apache.http.impl.bootstrap.ServerBootstrap;
43  import org.apache.http.protocol.HttpExpectationVerifier;
44  import org.apache.http.protocol.HttpRequestHandler;
45  import org.apache.http.protocol.UriHttpRequestHandlerMapper;
46  import org.apache.http.util.Asserts;
47  
48  public class HttpServer {
49  
50      private final UriHttpRequestHandlerMapper reqistry;
51      private volatile HttpExpectationVerifier expectationVerifier;
52      private volatile int timeout;
53  
54      private volatile org.apache.http.impl.bootstrap.HttpServer server;
55  
56      public HttpServer() {
57          super();
58          this.reqistry = new UriHttpRequestHandlerMapper();
59      }
60  
61      public int getTimeout() {
62          return this.timeout;
63      }
64  
65      public void setTimeout(final int timeout) {
66          this.timeout = timeout;
67      }
68  
69      public void registerHandler(
70              final String pattern,
71              final HttpRequestHandler handler) {
72          this.reqistry.register(pattern, handler);
73      }
74  
75      public void setExpectationVerifier(final HttpExpectationVerifier expectationVerifier) {
76          this.expectationVerifier = expectationVerifier;
77      }
78  
79      public int getPort() {
80          final org.apache.http.impl.bootstrap.HttpServer local = this.server;
81          if (local != null) {
82              return this.server.getLocalPort();
83          }
84          throw new IllegalStateException("Server not running");
85      }
86  
87      public InetAddress getInetAddress() {
88          final org.apache.http.impl.bootstrap.HttpServer local = this.server;
89          if (local != null) {
90              return local.getInetAddress();
91          }
92          throw new IllegalStateException("Server not running");
93      }
94  
95      public void start() throws IOException {
96          Asserts.check(this.server == null, "Server already running");
97          this.server = ServerBootstrap.bootstrap()
98                  .setSocketConfig(SocketConfig.custom()
99                          .setSoTimeout(this.timeout)
100                         .build())
101                 .setServerInfo("TEST-SERVER/1.1")
102                 .setConnectionFactory(new LoggingConnFactory())
103                 .setExceptionLogger(new SimpleExceptionLogger())
104                 .setExpectationVerifier(this.expectationVerifier)
105                 .setHandlerMapper(this.reqistry)
106                 .create();
107         this.server.start();
108     }
109 
110     public void shutdown() {
111         final org.apache.http.impl.bootstrap.HttpServer local = this.server;
112         this.server = null;
113         if (local != null) {
114             local.shutdown(5, TimeUnit.SECONDS);
115         }
116     }
117 
118     static class LoggingConnFactory implements HttpConnectionFactory<LoggingBHttpServerConnection> {
119 
120         @Override
121         public LoggingBHttpServerConnection createConnection(final Socket socket) throws IOException {
122             final LoggingBHttpServerConnectionnection.html#LoggingBHttpServerConnection">LoggingBHttpServerConnection conn = new LoggingBHttpServerConnection(8 * 1024);
123             conn.bind(socket);
124             return conn;
125         }
126     }
127 
128     static class SimpleExceptionLogger implements ExceptionLogger {
129 
130         private final Log log = LogFactory.getLog(HttpServer.class);
131 
132         @Override
133         public void log(final Exception ex) {
134             if (ex instanceof ConnectionClosedException) {
135                 this.log.debug(ex.getMessage());
136             } else if (ex instanceof SocketException) {
137                 this.log.debug(ex.getMessage());
138             } else {
139                 this.log.error(ex.getMessage(), ex);
140             }
141         }
142     }
143 
144 }