77
88package oracle .nosql .driver .httpclient ;
99
10- import static io .netty .handler .logging .LogLevel .DEBUG ;
1110import static oracle .nosql .driver .util .LogUtil .logFine ;
1211
1312import java .net .SocketAddress ;
1615
1716import io .netty .channel .ChannelHandlerContext ;
1817import io .netty .channel .ChannelOutboundHandler ;
19- import io .netty .channel .ChannelPipeline ;
2018import io .netty .channel .ChannelPromise ;
21- import io .netty .handler .codec .http .HttpClientCodec ;
2219import io .netty .handler .codec .http .HttpMessage ;
23- import io .netty .handler .codec .http .HttpObjectAggregator ;
24- import io .netty .handler .codec .http2 .DefaultHttp2Connection ;
25- import io .netty .handler .codec .http2 .DelegatingDecompressorFrameListener ;
26- import io .netty .handler .codec .http2 .Http2Connection ;
27- import io .netty .handler .codec .http2 .Http2FrameLogger ;
28- import io .netty .handler .codec .http2 .HttpToHttp2ConnectionHandler ;
29- import io .netty .handler .codec .http2 .HttpToHttp2ConnectionHandlerBuilder ;
30- import io .netty .handler .codec .http2 .InboundHttp2ToHttpAdapterBuilder ;
3120import io .netty .handler .ssl .ApplicationProtocolNames ;
3221import io .netty .handler .ssl .ApplicationProtocolNegotiationHandler ;
3322import io .netty .util .internal .RecyclableArrayList ;
4332 * 5. {@link HttpProtocolNegotiationHandler} removes itself from the pipeline. Writes any buffered {@link HttpMessage} to the channel.
4433 */
4534public class HttpProtocolNegotiationHandler extends ApplicationProtocolNegotiationHandler implements ChannelOutboundHandler {
46- private static final Http2FrameLogger frameLogger = new Http2FrameLogger (DEBUG , HttpProtocolNegotiationHandler .class );
47-
48- private static final String CODEC_HANDLER_NAME = "http-codec" ;
49- private static final String AGG_HANDLER_NAME = "http-aggregator" ;
5035 private static final String HTTP_HANDLER_NAME = "http-client-handler" ;
5136
5237 private final Logger logger ;
@@ -64,53 +49,12 @@ public HttpProtocolNegotiationHandler(String fallbackProtocol, HttpClientHandler
6449 this .maxContentLength = maxContentLength ;
6550 }
6651
67- private void writeBufferedMessages (ChannelHandlerContext ctx ) {
68- if (!this .bufferedMessages .isEmpty ()) {
69- for (int i = 0 ; i < this .bufferedMessages .size (); ++i ) {
70- Pair <Object , ChannelPromise > p = (Pair <Object , ChannelPromise >)this .bufferedMessages .get (i );
71- ctx .channel ().write (p .first , p .second );
72- }
73-
74- this .bufferedMessages .clear ();
75- }
76- this .bufferedMessages .recycle ();
77- }
78-
79- private void configureHttp1 (ChannelHandlerContext ctx ) {
80- ChannelPipeline p = ctx .pipeline ();
81-
82- p .addLast (CODEC_HANDLER_NAME ,
83- new HttpClientCodec (4096 , // initial line
84- 8192 , // header size
85- maxChunkSize )); // chunksize
86- p .addLast (AGG_HANDLER_NAME ,
87- new HttpObjectAggregator (maxContentLength ));
88- }
89-
90- private void configureHttp2 (ChannelHandlerContext ctx ) {
91- ChannelPipeline p = ctx .pipeline ();
92-
93- Http2Connection connection = new DefaultHttp2Connection (false );
94- HttpToHttp2ConnectionHandler connectionHandler = new HttpToHttp2ConnectionHandlerBuilder ()
95- .frameListener (new DelegatingDecompressorFrameListener (
96- connection ,
97- new InboundHttp2ToHttpAdapterBuilder (connection )
98- .maxContentLength (this .maxContentLength )
99- .propagateSettings (false )
100- .build ()))
101- .frameLogger (frameLogger )
102- .connection (connection )
103- .build ();
104-
105- p .addLast (connectionHandler );
106- }
107-
10852 @ Override
10953 protected void configurePipeline (ChannelHandlerContext ctx , String protocol ) {
11054 if (ApplicationProtocolNames .HTTP_2 .equals (protocol )) {
111- configureHttp2 (ctx );
55+ HttpUtil . configureHttp2 (ctx . pipeline (), this . maxContentLength );
11256 } else if (ApplicationProtocolNames .HTTP_1_1 .equals (protocol )) {
113- configureHttp1 (ctx );
57+ HttpUtil . configureHttp1 (ctx . pipeline (), this . maxChunkSize , this . maxContentLength );
11458 } else {
11559 throw new IllegalStateException ("unknown http protocol: " + protocol );
11660 }
@@ -126,7 +70,7 @@ protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
12670 @ Override
12771 public void write (ChannelHandlerContext ctx , Object o , ChannelPromise channelPromise ) throws Exception {
12872 if (o instanceof HttpMessage ) {
129- Pair <Object , ChannelPromise > p = Pair .of (o , channelPromise );
73+ HttpUtil . Pair <Object , ChannelPromise > p = HttpUtil . Pair .of (o , channelPromise );
13074 this .bufferedMessages .add (p );
13175 return ;
13276 }
@@ -142,7 +86,7 @@ public void write(ChannelHandlerContext ctx, Object o, ChannelPromise channelPro
14286 @ Override
14387 public void handlerRemoved (ChannelHandlerContext ctx ) throws Exception {
14488 super .handlerRemoved (ctx );
145- this .writeBufferedMessages (ctx );
89+ HttpUtil .writeBufferedMessages (ctx , this . bufferedMessages );
14690 }
14791
14892 @ Override
@@ -180,41 +124,5 @@ public void flush(ChannelHandlerContext ctx) {
180124 ctx .flush ();
181125 }
182126
183- private static class Pair <A , B > {
184-
185- public final A first ;
186- public final B second ;
187-
188- public Pair (A fst , B snd ) {
189- this .first = fst ;
190- this .second = snd ;
191- }
192-
193- public String toString () {
194- return "Pair[" + first + "," + second + "]" ;
195- }
196-
197- public boolean equals (Object other ) {
198- if (other instanceof Pair <?, ?>) {
199- Pair <?,?> pair = (Pair <?,?>) other ;
200- return Objects .equals (first , pair .first ) &&
201- Objects .equals (second , pair .second );
202- }
203- return false ;
204- }
205-
206- public int hashCode () {
207- if (first == null )
208- return (second == null ) ? 0 : second .hashCode () + 1 ;
209- else if (second == null )
210- return first .hashCode () + 2 ;
211- else
212- return first .hashCode () * 17 + second .hashCode ();
213- }
214-
215- public static <A , B > Pair <A , B > of (A a , B b ) {
216- return new Pair <>(a , b );
217- }
218- }
219127}
220128
0 commit comments