Migrate use of deprecated netty API elements 22/77522/3
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 6 Nov 2018 08:11:42 +0000 (09:11 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 6 Nov 2018 09:14:28 +0000 (10:14 +0100)
Straightforward change to use recommended equivalents.

Change-Id: I092eb6f0a2c745b5b39cde3f35ab21f612a62215
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/websockets/WebSocketServerHandler.java
restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/websockets/client/WebSocketClientHandler.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/websockets/WebSocketServerHandler.java

index aa56910523bcf44e74e531838e3df95ae3c3e6f9..5dd596c3e79a8f736c918e1b7d73d1c812177fcb 100755 (executable)
@@ -5,16 +5,15 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.netconf.sal.streams.websockets;
 
-import static io.netty.handler.codec.http.HttpHeaders.Names.HOST;
-import static io.netty.handler.codec.http.HttpHeaders.isKeepAlive;
-import static io.netty.handler.codec.http.HttpHeaders.setContentLength;
+import static io.netty.handler.codec.http.HttpHeaderNames.HOST;
 import static io.netty.handler.codec.http.HttpMethod.GET;
 import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
 import static io.netty.handler.codec.http.HttpResponseStatus.FORBIDDEN;
 import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR;
+import static io.netty.handler.codec.http.HttpUtil.isKeepAlive;
+import static io.netty.handler.codec.http.HttpUtil.setContentLength;
 import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
 
 import io.netty.buffer.ByteBuf;
@@ -71,18 +70,18 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object>
      */
     private void handleHttpRequest(final ChannelHandlerContext ctx, final FullHttpRequest req) {
         // Handle a bad request.
-        if (!req.getDecoderResult().isSuccess()) {
+        if (!req.decoderResult().isSuccess()) {
             sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
             return;
         }
 
         // Allow only GET methods.
-        if (req.getMethod() != GET) {
+        if (req.method() != GET) {
             sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
             return;
         }
 
-        final String streamName = Notificator.createStreamNameFromUri(req.getUri());
+        final String streamName = Notificator.createStreamNameFromUri(req.uri());
         if (streamName.contains(RestconfImpl.DATA_SUBSCR)) {
             final ListenerAdapter listener = Notificator.getListenerFor(streamName);
             if (listener != null) {
@@ -131,8 +130,8 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object>
     private static void sendHttpResponse(final ChannelHandlerContext ctx, final HttpRequest req,
             final FullHttpResponse res) {
         // Generate an error page if response getStatus code is not OK (200).
-        if (res.getStatus().code() != 200) {
-            final ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
+        if (res.status().code() != 200) {
+            final ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);
             res.content().writeBytes(buf);
             buf.release();
             setContentLength(res, res.content().readableBytes());
@@ -140,7 +139,7 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object>
 
         // Send the response and close the connection if necessary.
         final ChannelFuture f = ctx.channel().writeAndFlush(res);
-        if (!isKeepAlive(req) || res.getStatus().code() != 200) {
+        if (!isKeepAlive(req) || res.status().code() != 200) {
             f.addListener(ChannelFutureListener.CLOSE);
         }
     }
@@ -193,7 +192,6 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object>
      * @return String representation of web socket location.
      */
     private static String getWebSocketLocation(final HttpRequest req) {
-        return "ws://" + req.headers().get(HOST) + req.getUri();
+        return "ws://" + req.headers().get(HOST) + req.uri();
     }
-
 }
index 03c132b899469747430c7fd8ceabdf8075226dc4..19dbbc37851f1103c3a1e4d07a31118929a52777 100644 (file)
@@ -28,7 +28,7 @@ public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object>
     private ChannelPromise handshakeFuture;
     private final IClientMessageCallback messageListener;
 
-    public WebSocketClientHandler(WebSocketClientHandshaker handshaker, IClientMessageCallback listener) {
+    public WebSocketClientHandler(final WebSocketClientHandshaker handshaker, final IClientMessageCallback listener) {
         this.handshaker = handshaker;
         this.messageListener = listener;
     }
@@ -38,22 +38,22 @@ public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object>
     }
 
     @Override
-    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
+    public void handlerAdded(final ChannelHandlerContext ctx) throws Exception {
         handshakeFuture = ctx.newPromise();
     }
 
     @Override
-    public void channelActive(ChannelHandlerContext ctx) throws Exception {
+    public void channelActive(final ChannelHandlerContext ctx) throws Exception {
         handshaker.handshake(ctx.channel());
     }
 
     @Override
-    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
+    public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
         LOG.info("WebSocket Client disconnected!");
     }
 
     @Override
-    public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
+    public void channelRead0(final ChannelHandlerContext ctx, final Object msg) throws Exception {
         Channel ch = ctx.channel();
         if (!handshaker.isHandshakeComplete()) {
             handshaker.finishHandshake(ch, (FullHttpResponse) msg);
@@ -64,7 +64,7 @@ public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object>
 
         if (msg instanceof FullHttpResponse) {
             FullHttpResponse response = (FullHttpResponse) msg;
-            throw new Exception("Unexpected FullHttpResponse (getStatus=" + response.getStatus() + ", content="
+            throw new Exception("Unexpected FullHttpResponse (getStatus=" + response.status() + ", content="
                     + response.content().toString(CharsetUtil.UTF_8) + ')');
         }
 
@@ -80,7 +80,7 @@ public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object>
     }
 
     @Override
-    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
+    public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) throws Exception {
         LOG.info("Cause: {} .", cause.toString());
 
         if (!handshakeFuture.isDone()) {
index 55aacdaee809052c9a91119dac63fe233509f817..697b220b8e6a25632d633036fa6cb1d38afef53e 100755 (executable)
@@ -8,12 +8,13 @@
 
 package org.opendaylight.restconf.nb.rfc8040.streams.websockets;
 
-import static io.netty.handler.codec.http.HttpHeaders.isKeepAlive;
-import static io.netty.handler.codec.http.HttpHeaders.setContentLength;
+import static io.netty.handler.codec.http.HttpHeaderNames.HOST;
 import static io.netty.handler.codec.http.HttpMethod.GET;
 import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
 import static io.netty.handler.codec.http.HttpResponseStatus.FORBIDDEN;
 import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR;
+import static io.netty.handler.codec.http.HttpUtil.isKeepAlive;
+import static io.netty.handler.codec.http.HttpUtil.setContentLength;
 import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
 
 import io.netty.buffer.ByteBuf;
@@ -25,7 +26,6 @@ import io.netty.channel.SimpleChannelInboundHandler;
 import io.netty.handler.codec.http.DefaultFullHttpResponse;
 import io.netty.handler.codec.http.FullHttpRequest;
 import io.netty.handler.codec.http.FullHttpResponse;
-import io.netty.handler.codec.http.HttpHeaders.Names;
 import io.netty.handler.codec.http.HttpRequest;
 import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
 import io.netty.handler.codec.http.websocketx.PingWebSocketFrame;
@@ -64,25 +64,23 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object>
     /**
      * Checks if HTTP request method is GET and if is possible to decode HTTP result of request.
      *
-     * @param ctx
-     *            ChannelHandlerContext
-     * @param req
-     *            FullHttpRequest
+     * @param ctx ChannelHandlerContext
+     * @param req FullHttpRequest
      */
     private void handleHttpRequest(final ChannelHandlerContext ctx, final FullHttpRequest req) {
         // Handle a bad request.
-        if (!req.getDecoderResult().isSuccess()) {
+        if (!req.decoderResult().isSuccess()) {
             sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
             return;
         }
 
         // Allow only GET methods.
-        if (req.getMethod() != GET) {
+        if (req.method() != GET) {
             sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
             return;
         }
 
-        final String streamName = Notificator.createStreamNameFromUri(req.getUri());
+        final String streamName = Notificator.createStreamNameFromUri(req.uri());
         if (streamName.contains(RestconfConstants.DATA_SUBSCR)) {
             final ListenerAdapter listener = Notificator.getListenerFor(streamName);
             if (listener != null) {
@@ -121,18 +119,15 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object>
     /**
      * Checks response status, send response and close connection if necessary.
      *
-     * @param ctx
-     *            ChannelHandlerContext
-     * @param req
-     *            HttpRequest
-     * @param res
-     *            FullHttpResponse
+     * @param ctx ChannelHandlerContext
+     * @param req HttpRequest
+     * @param res FullHttpResponse
      */
     private static void sendHttpResponse(final ChannelHandlerContext ctx, final HttpRequest req,
             final FullHttpResponse res) {
         // Generate an error page if response getStatus code is not OK (200).
-        if (res.getStatus().code() != 200) {
-            final ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
+        if (res.status().code() != 200) {
+            final ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);
             res.content().writeBytes(buf);
             buf.release();
             setContentLength(res, res.content().readableBytes());
@@ -140,7 +135,7 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object>
 
         // Send the response and close the connection if necessary.
         final ChannelFuture f = ctx.channel().writeAndFlush(res);
-        if (!isKeepAlive(req) || res.getStatus().code() != 200) {
+        if (!isKeepAlive(req) || res.status().code() != 200) {
             f.addListener(ChannelFutureListener.CLOSE);
         }
     }
@@ -187,12 +182,10 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object>
     /**
      * Get web socket location from HTTP request.
      *
-     * @param req
-     *            HTTP request from which the location will be returned
+     * @param req HTTP request from which the location will be returned
      * @return String representation of web socket location.
      */
     private static String getWebSocketLocation(final HttpRequest req) {
-        return "ws://" + req.headers().get(Names.HOST) + req.getUri();
+        return "ws://" + req.headers().get(HOST) + req.uri();
     }
-
 }