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.hc.core5.http2.impl.nio;
29  
30  import org.apache.hc.core5.annotation.Contract;
31  import org.apache.hc.core5.annotation.Internal;
32  import org.apache.hc.core5.annotation.ThreadingBehavior;
33  import org.apache.hc.core5.http.URIScheme;
34  import org.apache.hc.core5.http.impl.nio.ServerHttp1StreamDuplexerFactory;
35  import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
36  import org.apache.hc.core5.http2.HttpVersionPolicy;
37  import org.apache.hc.core5.reactor.EndpointParameters;
38  import org.apache.hc.core5.reactor.IOEventHandlerFactory;
39  import org.apache.hc.core5.reactor.ProtocolIOSession;
40  import org.apache.hc.core5.util.Args;
41  import org.apache.hc.core5.util.Asserts;
42  import org.apache.hc.core5.util.Timeout;
43  
44  /**
45   * {@link ServerHttpProtocolNegotiator} factory.
46   *
47   * @since 5.0
48   */
49  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
50  @Internal
51  public class ServerHttpProtocolNegotiatorFactory implements IOEventHandlerFactory {
52  
53      private final ServerHttp1StreamDuplexerFactory http1StreamDuplexerFactory;
54      private final ServerH2StreamMultiplexerFactory http2StreamMultiplexerFactory;
55      private final HttpVersionPolicy versionPolicy;
56      private final TlsStrategy tlsStrategy;
57      private final Timeout handshakeTimeout;
58  
59      public ServerHttpProtocolNegotiatorFactory(
60              final ServerHttp1StreamDuplexerFactory http1StreamDuplexerFactory,
61              final ServerH2StreamMultiplexerFactory http2StreamMultiplexerFactory,
62              final HttpVersionPolicy versionPolicy,
63              final TlsStrategy tlsStrategy,
64              final Timeout handshakeTimeout) {
65          this.http1StreamDuplexerFactory = Args.notNull(http1StreamDuplexerFactory, "HTTP/1.1 stream handler factory");
66          this.http2StreamMultiplexerFactory = Args.notNull(http2StreamMultiplexerFactory, "HTTP/2 stream handler factory");
67          this.versionPolicy = versionPolicy != null ? versionPolicy : HttpVersionPolicy.NEGOTIATE;
68          this.tlsStrategy = tlsStrategy;
69          this.handshakeTimeout = handshakeTimeout;
70      }
71  
72      @Override
73      public ServerHttpProtocolNegotiator createHandler(final ProtocolIOSession ioSession, final Object attachment) {
74          HttpVersionPolicy endpointPolicy = versionPolicy;
75          if (attachment instanceof EndpointParameters) {
76              final EndpointParameters../../../org/apache/hc/core5/reactor/EndpointParameters.html#EndpointParameters">EndpointParameters params = (EndpointParameters) attachment;
77              if (URIScheme.HTTPS.same(params.getScheme())) {
78                  Asserts.notNull(tlsStrategy, "TLS strategy");
79                  tlsStrategy.upgrade(
80                          ioSession,
81                          null,
82                          ioSession.getLocalAddress(),
83                          ioSession.getRemoteAddress(),
84                          params.getAttachment(),
85                          handshakeTimeout);
86              }
87              if (params.getAttachment() instanceof HttpVersionPolicy) {
88                  endpointPolicy = (HttpVersionPolicy) params.getAttachment();
89              }
90          }
91          return new ServerHttpProtocolNegotiator(
92                  ioSession,
93                  http1StreamDuplexerFactory,
94                  http2StreamMultiplexerFactory,
95                  endpointPolicy);
96      }
97  
98  }