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;
29  
30  import java.io.IOException;
31  
32  import org.apache.http.config.ConnectionConfig;
33  import org.apache.http.impl.nio.DefaultNHttpServerConnection;
34  import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
35  import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
36  import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
37  import org.apache.http.impl.nio.reactor.IOReactorConfig;
38  import org.apache.http.localserver.HttpServerNio;
39  import org.apache.http.nio.NHttpConnectionFactory;
40  import org.apache.http.nio.reactor.IOReactorExceptionHandler;
41  import org.apache.http.protocol.HttpProcessor;
42  import org.apache.http.protocol.ImmutableHttpProcessor;
43  import org.apache.http.protocol.ResponseConnControl;
44  import org.apache.http.protocol.ResponseContent;
45  import org.apache.http.protocol.ResponseDate;
46  import org.apache.http.protocol.ResponseServer;
47  import org.junit.After;
48  
49  @SuppressWarnings("RedundantArrayCreation")
50  public abstract class HttpAsyncTestBase {
51  
52      protected HttpServerNio server;
53      protected IOReactorConfig serverReactorConfig;
54      protected ConnectionConfig serverConnectionConfig;
55      protected HttpProcessor serverHttpProc;
56      protected DefaultConnectingIOReactor clientIOReactor;
57      protected IOReactorConfig clientReactorConfig;
58      protected ConnectionConfig clientrConnectionConfig;
59      protected PoolingNHttpClientConnectionManager connMgr;
60      protected CloseableHttpAsyncClient httpclient;
61  
62      protected abstract NHttpConnectionFactory<DefaultNHttpServerConnection> createServerConnectionFactory(
63              ConnectionConfig config) throws Exception;
64  
65      protected abstract String getSchemeName();
66  
67      public static class SimpleIOReactorExceptionHandler implements IOReactorExceptionHandler {
68  
69          public boolean handle(final RuntimeException ex) {
70              ex.printStackTrace(System.out);
71              return false;
72          }
73  
74          public boolean handle(final IOException ex) {
75              ex.printStackTrace(System.out);
76              return false;
77          }
78  
79      }
80  
81      public void initServer() throws Exception {
82          this.server = new HttpServerNio(
83                  this.serverReactorConfig, createServerConnectionFactory(this.serverConnectionConfig));
84          this.server.setExceptionHandler(new SimpleIOReactorExceptionHandler());
85          this.serverHttpProc = new ImmutableHttpProcessor(new ResponseDate(),
86                  new ResponseServer("TEST-SERVER/1.1"),
87                  new ResponseContent(),
88                  new ResponseConnControl());
89      }
90  
91      public void initConnectionManager() throws Exception {
92          this.clientIOReactor = new DefaultConnectingIOReactor(this.clientReactorConfig);
93          this.connMgr = new PoolingNHttpClientConnectionManager(this.clientIOReactor);
94       }
95  
96      @After
97      public void shutDownClient() throws Exception {
98          if (this.httpclient != null) {
99              this.httpclient.close();
100         }
101     }
102 
103     @After
104     public void shutDownServer() throws Exception {
105         if (this.server != null) {
106             this.server.shutdown();
107         }
108     }
109 
110 }