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