@@ -44,9 +44,15 @@ public class NettyHttpClient implements HttpClient, WsClient, WsClientAutoReconn
4444 public static final int MAX_HTTP_REQUEST = 16 * 1024 * 1024 ; // 16MB
4545 public static final int MAX_HTTP_BIN_REQUEST = 150 * 1024 * 1024 ; // 150MB
4646
47+ private static final String HTTP = "http" ;
48+ private static final String HTTPS = "https" ;
49+ private static final String HTTP_CODEC = "http-codec" ;
50+ private static final String HTTP_AGGREGATOR = "http-aggregator" ;
51+ private static final String HTTP_HANDLER = "http-handler" ;
52+
4753 private Logger logger = LoggerFactory .getLogger (NettyHttpClient .class );
4854
49- protected Bootstrap bootStrap ;
55+ protected Bootstrap httpBootstrap ;
5056 protected URI baseUri ;
5157 private EventLoopGroup group ;
5258 private EventLoopGroup shutDownGroup ;
@@ -82,16 +88,16 @@ public void initialize(String baseUrl, String username, String password) throws
8288 logger .debug ("initialize url: {}, user: {}" , baseUrl , username );
8389 baseUri = new URI (baseUrl );
8490 String protocol = baseUri .getScheme ();
85- if (!"http" .equalsIgnoreCase (protocol ) && !"https" .equalsIgnoreCase (protocol )) {
91+ if (!HTTP .equalsIgnoreCase (protocol ) && !HTTPS .equalsIgnoreCase (protocol )) {
8692 logger .warn ("Not http(s), protocol: {}" , protocol );
8793 throw new IllegalArgumentException ("Unsupported protocol: " + protocol );
8894 }
8995 this .auth = "Basic " + Base64 .encode (Unpooled .copiedBuffer ((username + ":" + password ), ARIEncoder .ENCODING )).toString (ARIEncoder .ENCODING );
90- bootstrap ();
96+ initHttpBootstrap ();
9197 }
9298
93- protected void bootstrap () {
94- if (bootStrap == null ) {
99+ protected void initHttpBootstrap () {
100+ if (httpBootstrap == null ) {
95101 // Bootstrap is the factory for HTTP connections
96102 logger .debug ("Bootstrap with\n " +
97103 " connection timeout: {},\n " +
@@ -100,17 +106,17 @@ protected void bootstrap() {
100106 CONNECTION_TIMEOUT_SEC ,
101107 READ_TIMEOUT_SEC ,
102108 MAX_HTTP_REQUEST );
103- bootStrap = new Bootstrap ();
104- bootstrapOptions (bootStrap );
105- bootStrap .handler (new ChannelInitializer <SocketChannel >() {
109+ httpBootstrap = new Bootstrap ();
110+ bootstrapOptions (httpBootstrap );
111+ httpBootstrap .handler (new ChannelInitializer <SocketChannel >() {
106112 @ Override
107113 public void initChannel (SocketChannel ch ) throws Exception {
108114 ChannelPipeline pipeline = ch .pipeline ();
109- addSSLIfRequired (pipeline );
115+ addSSLIfRequired (pipeline , baseUri );
110116 pipeline .addLast ("read-timeout" , new ReadTimeoutHandler (READ_TIMEOUT_SEC ));
111- pipeline .addLast ("http-codec" , new HttpClientCodec ());
112- pipeline .addLast ("http-aggregator" , new HttpObjectAggregator (MAX_HTTP_REQUEST ));
113- pipeline .addLast ("http-handler" , new NettyHttpClientHandler ());
117+ pipeline .addLast (HTTP_CODEC , new HttpClientCodec ());
118+ pipeline .addLast (HTTP_AGGREGATOR , new HttpObjectAggregator (MAX_HTTP_REQUEST ));
119+ pipeline .addLast (HTTP_HANDLER , new NettyHttpClientHandler ());
114120 }
115121 });
116122 }
@@ -125,8 +131,8 @@ private void bootstrapOptions(Bootstrap bootStrap) {
125131 bootStrap .option (ChannelOption .CONNECT_TIMEOUT_MILLIS , CONNECTION_TIMEOUT_SEC * 1000 );
126132 }
127133
128- private void addSSLIfRequired (ChannelPipeline pipeline ) throws SSLException {
129- if ("https" .equalsIgnoreCase (baseUri .getScheme ())) {
134+ private synchronized static void addSSLIfRequired (ChannelPipeline pipeline , URI baseUri ) throws SSLException {
135+ if (HTTPS .equalsIgnoreCase (baseUri .getScheme ())) {
130136 if (sslContext == null ) {
131137 sslContext = SslContextBuilder .forClient ().trustManager (InsecureTrustManagerFactory .INSTANCE ).build ();
132138 }
@@ -137,9 +143,9 @@ private void addSSLIfRequired(ChannelPipeline pipeline) throws SSLException {
137143 private int getPort () {
138144 int port = baseUri .getPort ();
139145 if (port == -1 ) {
140- if ("http" .equalsIgnoreCase (baseUri .getScheme ())) {
146+ if (HTTP .equalsIgnoreCase (baseUri .getScheme ())) {
141147 port = 80 ;
142- } else if ("https" .equalsIgnoreCase (baseUri .getScheme ())) {
148+ } else if (HTTPS .equalsIgnoreCase (baseUri .getScheme ())) {
143149 port = 443 ;
144150 }
145151 }
@@ -148,7 +154,7 @@ private int getPort() {
148154
149155 protected ChannelFuture httpConnect () {
150156 logger .debug ("HTTP Connect uri: {}" , baseUri .toString ());
151- return bootStrap .connect (baseUri .getHost (), getPort ());
157+ return httpBootstrap .connect (baseUri .getHost (), getPort ());
152158 }
153159
154160 public void destroy () {
@@ -221,7 +227,7 @@ protected String buildURL(String path, List<HttpParam> parametersQuery, boolean
221227 // Factory for WS handshakes
222228 protected WebSocketClientHandshaker getWsHandshake (String path , List <HttpParam > parametersQuery ) throws URISyntaxException {
223229 String url = buildURL (path , parametersQuery , true );
224- if (url .regionMatches (true , 0 , "http" , 0 , 4 )) {
230+ if (url .regionMatches (true , 0 , HTTP , 0 , 4 )) {
225231 // http(s):// -> ws(s)://
226232 url = "ws" + url .substring (4 );
227233 }
@@ -306,7 +312,7 @@ public void operationComplete(ChannelFuture future) throws Exception {
306312 }
307313 }
308314 }).syncUninterruptibly ().channel ();
309- NettyHttpClientHandler handler = (NettyHttpClientHandler ) ch .pipeline ().get ("http-handler" );
315+ NettyHttpClientHandler handler = (NettyHttpClientHandler ) ch .pipeline ().get (HTTP_HANDLER );
310316 ch .writeAndFlush (request );
311317 ch .closeFuture ().syncUninterruptibly ();
312318 if (handler .getException () != null ) {
@@ -322,7 +328,7 @@ private void replaceAggregator(boolean binary, Channel ch) {
322328 if (binary ) {
323329 logger .debug ("Is Binary, replace http-aggregator ..." );
324330 ch .pipeline ().replace (
325- "http-aggregator" , "http-aggregator" , new HttpObjectAggregator (MAX_HTTP_BIN_REQUEST ));
331+ HTTP_AGGREGATOR , HTTP_AGGREGATOR , new HttpObjectAggregator (MAX_HTTP_BIN_REQUEST ));
326332 }
327333 }
328334
@@ -351,7 +357,7 @@ public void operationComplete(ChannelFuture future) throws Exception {
351357 public void operationComplete (ChannelFuture future ) throws Exception {
352358 responseHandler .onResponseReceived ();
353359 if (future .isSuccess ()) {
354- NettyHttpClientHandler handler = (NettyHttpClientHandler ) future .channel ().pipeline ().get ("http-handler" );
360+ NettyHttpClientHandler handler = (NettyHttpClientHandler ) future .channel ().pipeline ().get (HTTP_HANDLER );
355361 if (handler .getException () != null ) {
356362 responseHandler .onFailure (new RestException (handler .getException ()));
357363 } else if (httpResponseOkay (handler .getResponseStatus ())) {
@@ -397,9 +403,9 @@ protected WsClientConnection connect(Bootstrap wsBootStrap, final HttpResponseHa
397403 @ Override
398404 public void initChannel (SocketChannel ch ) throws Exception {
399405 ChannelPipeline pipeline = ch .pipeline ();
400- addSSLIfRequired (pipeline );
401- pipeline .addLast ("http-codec" , new HttpClientCodec ());
402- pipeline .addLast ("http-aggregator" , new HttpObjectAggregator (MAX_HTTP_REQUEST ));
406+ addSSLIfRequired (pipeline , baseUri );
407+ pipeline .addLast (HTTP_CODEC , new HttpClientCodec ());
408+ pipeline .addLast (HTTP_AGGREGATOR , new HttpObjectAggregator (MAX_HTTP_REQUEST ));
403409 pipeline .addLast ("ws-handler" , wsHandler );
404410 }
405411 });
@@ -471,9 +477,9 @@ public void run() {
471477 for (int i = 0 ; i < 10 ; i ++) {
472478 try {
473479 Thread .sleep (1000 );
474- } catch (InterruptedException e ) {
480+ } catch (InterruptedException e ) {//NOSONAR
475481 // probably from the reconnect, so stop running...
476- return ; //NOSONAR
482+ return ;
477483 }
478484 if ((System .currentTimeMillis () - lastPong ) < 10000 ) {
479485 logger .debug ("Pong at {}" , lastPong );
0 commit comments