Do not hard-code OK status code 23/77523/3
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 6 Nov 2018 08:18:19 +0000 (09:18 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 6 Nov 2018 09:14:28 +0000 (10:14 +0100)
HttpStatusResponse.OK gives us everything we need, simplify
response handling using its equals() method.

Change-Id: I16cb7bc0a6e6e8a8ac10a046538566e32019a59c
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-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/websockets/WebSocketServerHandler.java

index 5dd596c3e79a8f736c918e1b7d73d1c812177fcb..666ad758a5118d4b7afb2cc3758142ba2974dfa4 100755 (executable)
@@ -12,6 +12,7 @@ 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.HttpResponseStatus.OK;
 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;
@@ -46,7 +47,6 @@ import org.slf4j.LoggerFactory;
  * {@link FullHttpRequest} and {@link WebSocketFrame} messages.
  */
 public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object> {
-
     private static final Logger LOG = LoggerFactory.getLogger(WebSocketServerHandler.class);
 
     private WebSocketServerHandshaker handshaker;
@@ -63,10 +63,8 @@ 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.
@@ -114,23 +112,20 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object>
         } else {
             this.handshaker.handshake(ctx.channel(), req);
         }
-
     }
 
     /**
      * 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.status().code() != 200) {
+        final boolean notOkay = !OK.equals(res.status());
+        if (notOkay) {
             final ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);
             res.content().writeBytes(buf);
             buf.release();
@@ -139,7 +134,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.status().code() != 200) {
+        if (notOkay || !isKeepAlive(req)) {
             f.addListener(ChannelFutureListener.CLOSE);
         }
     }
@@ -147,10 +142,8 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object>
     /**
      * Handles web socket frame.
      *
-     * @param ctx
-     *            {@link ChannelHandlerContext}
-     * @param frame
-     *            {@link WebSocketFrame}
+     * @param ctx {@link ChannelHandlerContext}
+     * @param frame {@link WebSocketFrame}
      */
     private void handleWebSocketFrame(final ChannelHandlerContext ctx, final WebSocketFrame frame) {
         if (frame instanceof CloseWebSocketFrame) {
@@ -187,8 +180,7 @@ 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) {
index 697b220b8e6a25632d633036fa6cb1d38afef53e..7438ced14b297b8eb027241172a6a76637cd9de8 100755 (executable)
@@ -13,6 +13,7 @@ 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.HttpResponseStatus.OK;
 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;
@@ -126,7 +127,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.status().code() != 200) {
+        final boolean notOkay = !OK.equals(res.status());
+        if (notOkay) {
             final ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);
             res.content().writeBytes(buf);
             buf.release();
@@ -135,7 +137,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.status().code() != 200) {
+        if (notOkay || !isKeepAlive(req)) {
             f.addListener(ChannelFutureListener.CLOSE);
         }
     }