Fixed some major sonar issues
[yangtools.git] / websocket / websocket-client / src / main / java / org / opendaylight / yangtools / websocket / client / WebSocketClientHandler.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.websocket.client;
9
10 import io.netty.channel.Channel;
11 import io.netty.channel.ChannelFuture;
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.channel.ChannelPromise;
14 import io.netty.channel.SimpleChannelInboundHandler;
15 import io.netty.handler.codec.http.FullHttpResponse;
16 import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
17 import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
18 import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
19 import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
20 import io.netty.handler.codec.http.websocketx.WebSocketFrame;
21 import io.netty.util.CharsetUtil;
22 import org.opendaylight.yangtools.websocket.client.callback.ClientMessageCallback;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * {@link WebSocketClientHandler} is implementation of
28  * {@link SimpleChannelInboundHandler} which handle {@link TextWebSocketFrame},
29  * {@link PongWebSocketFrame} and {@link CloseWebSocketFrame} messages.
30  */
31 public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object> {
32
33     private static final Logger LOGGER = LoggerFactory
34             .getLogger(WebSocketClientHandler.class.toString());
35     private final WebSocketClientHandshaker handshaker;
36     private ChannelPromise handshakeFuture;
37     private ClientMessageCallback messageListener;
38
39     /**
40      * Create new Web Socket Client Handler.
41      * 
42      * @param handshaker
43      *            manages handshake process
44      * @param listener
45      * 
46      * 
47      */
48     public WebSocketClientHandler(WebSocketClientHandshaker handshaker,
49             ClientMessageCallback listener) {
50         this.handshaker = handshaker;
51         this.messageListener = listener;
52     }
53
54     /**
55      * Notifies by Future when handshake process succeeds or fails.
56      * 
57      * @return information about the completation of the handshake
58      */
59     public ChannelFuture handshakeFuture() {
60         return handshakeFuture;
61     }
62
63     @Override
64     public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
65         handshakeFuture = ctx.newPromise();
66     }
67
68     @Override
69     public void channelActive(ChannelHandlerContext ctx) throws Exception {
70         handshaker.handshake(ctx.channel());
71     }
72
73     @Override
74     public void channelInactive(ChannelHandlerContext ctx) throws Exception {
75         LOGGER.info("WebSocket Client disconnected!");
76     }
77
78     @Override
79     public void channelRead0(ChannelHandlerContext ctx, Object msg)
80             throws Exception {
81         Channel ch = ctx.channel();
82         if (!handshaker.isHandshakeComplete()) {
83             handshaker.finishHandshake(ch, (FullHttpResponse) msg);
84             LOGGER.info("WebSocket Client connected!");
85             handshakeFuture.setSuccess();
86             return;
87         }
88
89         if (msg instanceof FullHttpResponse) {
90             FullHttpResponse response = (FullHttpResponse) msg;
91             throw new RuntimeException(
92                     "Unexpected FullHttpResponse (getStatus="
93                             + response.getStatus() + ", content="
94                             + response.content().toString(CharsetUtil.UTF_8)
95                             + ')');
96         }
97
98         messageListener.onMessageReceived(msg);
99         WebSocketFrame frame = (WebSocketFrame) msg;
100
101         if (frame instanceof TextWebSocketFrame) {
102             TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
103             LOGGER.info("WebSocket Client received message: "
104                     + textFrame.text());
105         } else if (frame instanceof PongWebSocketFrame) {
106             LOGGER.info("WebSocket Client received pong");
107         } else if (frame instanceof CloseWebSocketFrame) {
108             LOGGER.info("WebSocket Client received closing");
109             ch.close();
110         }
111     }
112
113     @Override
114     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
115             throws Exception {
116         if (!handshakeFuture.isDone()) {
117             handshakeFuture.setFailure(cause);
118         }
119         ctx.close();
120     }
121 }