1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.core5.testing.nio;
29
30 import javax.net.ssl.SSLContext;
31
32 import org.apache.hc.core5.http.config.CharCodingConfig;
33 import org.apache.hc.core5.http.config.Http1Config;
34 import org.apache.hc.core5.http.impl.HttpProcessors;
35 import org.apache.hc.core5.http.impl.nio.ServerHttp1StreamDuplexerFactory;
36 import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
37 import org.apache.hc.core5.http.nio.HandlerFactory;
38 import org.apache.hc.core5.http.protocol.HttpProcessor;
39 import org.apache.hc.core5.http2.HttpVersionPolicy;
40 import org.apache.hc.core5.http2.config.H2Config;
41 import org.apache.hc.core5.http2.impl.H2Processors;
42 import org.apache.hc.core5.http2.impl.nio.ServerH2StreamMultiplexerFactory;
43 import org.apache.hc.core5.http2.impl.nio.ServerHttpProtocolNegotiator;
44 import org.apache.hc.core5.reactor.IOEventHandler;
45 import org.apache.hc.core5.reactor.IOEventHandlerFactory;
46 import org.apache.hc.core5.reactor.ProtocolIOSession;
47 import org.apache.hc.core5.reactor.ssl.SSLSessionInitializer;
48 import org.apache.hc.core5.reactor.ssl.SSLSessionVerifier;
49 import org.apache.hc.core5.util.Args;
50
51 class InternalServerH2EventHandlerFactory implements IOEventHandlerFactory {
52
53 private final HttpProcessor httpProcessor;
54 private final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory;
55 private final HttpVersionPolicy versionPolicy;
56 private final H2Config h2Config;
57 private final Http1Config http1Config;
58 private final CharCodingConfig charCodingConfig;
59 private final SSLContext sslContext;
60 private final SSLSessionInitializer sslSessionInitializer;
61 private final SSLSessionVerifier sslSessionVerifier;
62
63 public InternalServerH2EventHandlerFactory(
64 final HttpProcessor httpProcessor,
65 final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory,
66 final HttpVersionPolicy versionPolicy,
67 final H2Config h2Config,
68 final Http1Config http1Config,
69 final CharCodingConfig charCodingConfig,
70 final SSLContext sslContext,
71 final SSLSessionInitializer sslSessionInitializer,
72 final SSLSessionVerifier sslSessionVerifier) {
73 this.httpProcessor = Args.notNull(httpProcessor, "HTTP processor");
74 this.exchangeHandlerFactory = Args.notNull(exchangeHandlerFactory, "Exchange handler factory");
75 this.versionPolicy = versionPolicy != null ? versionPolicy : HttpVersionPolicy.NEGOTIATE;
76 this.h2Config = h2Config != null ? h2Config : H2Config.DEFAULT;
77 this.http1Config = http1Config != null ? http1Config : Http1Config.DEFAULT;
78 this.charCodingConfig = charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT;
79 this.sslContext = sslContext;
80 this.sslSessionInitializer = sslSessionInitializer;
81 this.sslSessionVerifier = sslSessionVerifier;
82 }
83
84 @Override
85 public IOEventHandler createHandler(final ProtocolIOSession ioSession, final Object attachment) {
86 if (sslContext != null) {
87 ioSession.startTls(sslContext, null, null, sslSessionInitializer, sslSessionVerifier, null);
88 }
89 final ServerHttp1StreamDuplexerFactory http1StreamHandlerFactory = new ServerHttp1StreamDuplexerFactory(
90 httpProcessor != null ? httpProcessor : HttpProcessors.server(),
91 exchangeHandlerFactory,
92 http1Config,
93 charCodingConfig,
94 LoggingHttp1StreamListener.INSTANCE_SERVER);
95 final ServerH2StreamMultiplexerFactory http2StreamHandlerFactory = new ServerH2StreamMultiplexerFactory(
96 httpProcessor != null ? httpProcessor : H2Processors.server(),
97 exchangeHandlerFactory,
98 h2Config,
99 charCodingConfig,
100 LoggingH2StreamListener.INSTANCE);
101 return new ServerHttpProtocolNegotiator(
102 ioSession,
103 http1StreamHandlerFactory,
104 http2StreamHandlerFactory,
105 versionPolicy);
106 }
107
108 }