/*
 * Copyright (c) 2010-2012 Sonatype, Inc. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */
package com.ning.http.client.providers.netty;

import com.ning.http.client.websocket.WebSocket;
import com.ning.http.client.websocket.WebSocketByteListener;
import com.ning.http.client.websocket.WebSocketCloseCodeReasonListener;
import com.ning.http.client.websocket.WebSocketListener;
import com.ning.http.client.websocket.WebSocketTextListener;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.PingWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentLinkedQueue;

import static org.jboss.netty.buffer.ChannelBuffers.wrappedBuffer;

public class NettyWebSocket implements WebSocket {
    private final static Logger logger = LoggerFactory.getLogger(NettyWebSocket.class);

    private final Channel channel;
    private final ConcurrentLinkedQueue<WebSocketListener> listeners = new ConcurrentLinkedQueue<WebSocketListener>();

    public NettyWebSocket(Channel channel) {
        this.channel = channel;
    }

    @Override
    public WebSocket sendMessage(byte[] message) {
        channel.write(new BinaryWebSocketFrame(wrappedBuffer(message)));
        return this;
    }

    @Override
    public WebSocket stream(byte[] fragment, boolean last) {
        throw new UnsupportedOperationException("Streaming currently only supported by the Grizzly provider.");
    }

    @Override
    public WebSocket stream(byte[] fragment, int offset, int len, boolean last) {
        throw new UnsupportedOperationException("Streaming currently only supported by the Grizzly provider.");
    }

    @Override
    public WebSocket sendTextMessage(String message) {
        channel.write(new TextWebSocketFrame(message));
        return this;
    }

    @Override
    public WebSocket streamText(String fragment, boolean last) {
        throw new UnsupportedOperationException("Streaming currently only supported by the Grizzly provider.");
    }

    @Override
    public WebSocket sendPing(byte[] payload) {
        channel.write(new PingWebSocketFrame(wrappedBuffer(payload)));
        return this;
    }

    @Override
    public WebSocket sendPong(byte[] payload) {
        channel.write(new PongWebSocketFrame(wrappedBuffer(payload)));
        return this;
    }

    @Override
    public WebSocket addWebSocketListener(WebSocketListener l) {
        listeners.add(l);
        return this;
    }

    @Override
    public WebSocket removeWebSocketListener(WebSocketListener l) {
        listeners.remove(l);
        return this;
    }

    @Override
    public boolean isOpen() {
        return channel.isOpen();
    }

    @Override
    public void close() {
        onClose();
        listeners.clear();
        channel.close();
    }

    protected void onMessage(byte[] message) {
        for (WebSocketListener l : listeners) {
            if (WebSocketByteListener.class.isAssignableFrom(l.getClass())) {
                try {
                    WebSocketByteListener.class.cast(l).onMessage(message);
                } catch (Exception ex) {
                    l.onError(ex);
                }
            }
        }
    }

    protected void onTextMessage(String message) {
        for (WebSocketListener l : listeners) {
            if (WebSocketTextListener.class.isAssignableFrom(l.getClass())) {
                try {
                    WebSocketTextListener.class.cast(l).onMessage(message);
                } catch (Exception ex) {
                    l.onError(ex);
                }
            }
        }
    }

    protected void onError(Throwable t) {
        for (WebSocketListener l : listeners) {
            try {
                l.onError(t);
            } catch (Throwable t2) {
                logger.error("", t2);
            }

        }
    }

    protected void onClose() {
        onClose(1000, "Normal closure; the connection successfully completed whatever purpose for which it was created.");
    }

    protected void onClose(int code, String reason) {
        for (WebSocketListener l : listeners) {
            try {
                if (WebSocketCloseCodeReasonListener.class.isAssignableFrom(l.getClass())) {
                    WebSocketCloseCodeReasonListener.class.cast(l).onClose(this, code, reason);
                }
                l.onClose(this);
            } catch (Throwable t) {
                l.onError(t);
            }
        }
    }

    @Override
    public String toString() {
        return "NettyWebSocket{" +
                "channel=" + channel +
                '}';
    }
}

@@ -1922,32 +1922,35 @@
 nnel;%0A    %7D%0A%0A   
+ //
  @Override%0A    p
@@ -1936,32 +1936,32 @@
    // @Override%0A
-
     public WebSo
@@ -2091,32 +2091,35 @@
 this;%0A    %7D%0A%0A   
+ //
  @Override%0A    p
@@ -2288,32 +2288,35 @@
 r.%22);%0A    %7D%0A%0A   
+ //
  @Override%0A    p
@@ -2506,32 +2506,35 @@
 r.%22);%0A    %7D%0A%0A   
+ //
  @Override%0A    p
@@ -2662,32 +2662,35 @@
 this;%0A    %7D%0A%0A   
+ //
  @Override%0A    p
@@ -2863,32 +2863,35 @@
 r.%22);%0A    %7D%0A%0A   
+ //
  @Override%0A    p
@@ -3027,32 +3027,35 @@
 this;%0A    %7D%0A%0A   
+ //
  @Override%0A    p
@@ -3191,32 +3191,35 @@
 this;%0A    %7D%0A%0A   
+ //
  @Override%0A    p
@@ -3327,32 +3327,35 @@
 this;%0A    %7D%0A%0A   
+ //
  @Override%0A    p
@@ -3469,32 +3469,35 @@
 this;%0A    %7D%0A%0A   
+ //
  @Override%0A    p
@@ -3553,35 +3553,38 @@
 sOpen();%0A    %7D%0A%0A
-
    
+ //
  @Override%0A    p

Make JDK 5 happy