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.net.URL;
31  
32  import javax.net.ssl.SSLContext;
33  
34  import org.apache.http.impl.nio.pool.BasicNIOConnFactory;
35  import org.apache.http.nio.util.TestingSupport;
36  import org.apache.http.ssl.SSLContextBuilder;
37  import org.junit.After;
38  
39  /**
40   * Base class for all HttpCore NIO integration tests
41   *
42   */
43  public abstract class HttpCoreNIOTestBase {
44  
45      private static int JRE_LEVEL = TestingSupport.determineJRELevel();
46  
47      public enum ProtocolScheme { http, https }
48  
49      private final ProtocolScheme scheme;
50  
51      protected HttpServerNio server;
52      protected HttpClientNio client;
53  
54      public HttpCoreNIOTestBase(final ProtocolScheme scheme) {
55          this.scheme = scheme;
56      }
57  
58      public HttpCoreNIOTestBase() {
59          this(ProtocolScheme.http);
60      }
61  
62      public ProtocolScheme getScheme() {
63          return this.scheme;
64      }
65  
66      protected SSLContext createServerSSLContext() throws Exception {
67          if (JRE_LEVEL >= 8) {
68              final URL keyStoreURL = getClass().getResource("/test-server.p12");
69              final String storePassword = "nopassword";
70              return SSLContextBuilder.create()
71                      .setKeyStoreType("pkcs12")
72                      .loadTrustMaterial(keyStoreURL, storePassword.toCharArray())
73                      .loadKeyMaterial(keyStoreURL, storePassword.toCharArray(), storePassword.toCharArray())
74                      .build();
75          } else {
76              final URL keyStoreURL = getClass().getResource("/test.keystore");
77              final String storePassword = "nopassword";
78              return SSLContextBuilder.create()
79                      .loadTrustMaterial(keyStoreURL, storePassword.toCharArray())
80                      .loadKeyMaterial(keyStoreURL, storePassword.toCharArray(), storePassword.toCharArray())
81                      .build();
82          }
83      }
84  
85      protected SSLContext createClientSSLContext() throws Exception {
86          if (JRE_LEVEL >= 8) {
87              final URL keyStoreURL = getClass().getResource("/test-client.p12");
88              final String storePassword = "nopassword";
89              return SSLContextBuilder.create()
90                      .setKeyStoreType("pkcs12")
91                      .loadTrustMaterial(keyStoreURL, storePassword.toCharArray())
92                      .build();
93          } else {
94              final URL keyStoreURL = getClass().getResource("/test.keystore");
95              final String storePassword = "nopassword";
96              return SSLContextBuilder.create()
97                      .loadTrustMaterial(keyStoreURL, storePassword.toCharArray())
98                      .build();
99          }
100     }
101 
102     protected ServerConnectionFactory createServerConnectionFactory() throws Exception {
103         return new ServerConnectionFactory(
104                 this.scheme.equals(ProtocolScheme.https) ? createServerSSLContext() : null);
105     }
106 
107     protected BasicNIOConnFactory createClientConnectionFactory() throws Exception {
108         return new BasicNIOConnFactory(
109                 new ClientConnectionFactory(),
110                 this.scheme.equals(ProtocolScheme.https) ? new ClientConnectionFactory(createClientSSLContext()) : null);
111 
112     }
113 
114     public void initServer() throws Exception {
115         this.server = new HttpServerNio();
116         this.server.setConnectionFactory(createServerConnectionFactory());
117         this.server.setTimeout(5000);
118     }
119 
120     public void initClient() throws Exception {
121         this.client = new HttpClientNio(createClientConnectionFactory());
122         this.client.setTimeout(5000);
123     }
124 
125     @After
126     public void shutDownClient() throws Exception {
127         if (this.client != null) {
128             this.client.shutdown();
129             this.client = null;
130         }
131     }
132 
133     @After
134     public void shutDownServer() throws Exception {
135         if (this.server != null) {
136             this.server.shutdown();
137             this.server = null;
138         }
139     }
140 
141 }