Do not allocate a short-lived ByteBuf
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / sal / streams / websockets / WebSocketServerHandler.java
index aa56910523bcf44e74e531838e3df95ae3c3e6f9..ed90e3f2369ed3818691802cad1daf44e4ed4610 100755 (executable)
@@ -5,20 +5,18 @@
  * 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.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;
 
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
 import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelFutureListener;
 import io.netty.channel.ChannelHandlerContext;
@@ -47,7 +45,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;
@@ -64,25 +61,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(RestconfImpl.DATA_SUBSCR)) {
             final ListenerAdapter listener = Notificator.getListenerFor(streamName);
             if (listener != null) {
@@ -115,32 +110,27 @@ 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.getStatus().code() != 200) {
-            final ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
-            res.content().writeBytes(buf);
-            buf.release();
+        final boolean notOkay = !OK.equals(res.status());
+        if (notOkay) {
+            res.content().writeCharSequence(res.status().toString(), CharsetUtil.UTF_8);
             setContentLength(res, res.content().readableBytes());
         }
 
         // Send the response and close the connection if necessary.
         final ChannelFuture f = ctx.channel().writeAndFlush(res);
-        if (!isKeepAlive(req) || res.getStatus().code() != 200) {
+        if (notOkay || !isKeepAlive(req)) {
             f.addListener(ChannelFutureListener.CLOSE);
         }
     }
@@ -148,10 +138,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) {
@@ -188,12 +176,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(HOST) + req.getUri();
+        return "ws://" + req.headers().get(HOST) + req.uri();
     }
-
 }