/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package websocket.chat;

import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import util.HTMLFilter;

@ServerEndpoint(value = "/websocket/chat")
public class ChatAnnotation {

    private static final String GUEST_PREFIX = "Guest";
    private static final AtomicInteger connectionIds = new AtomicInteger(0);
    private static final Set<ChatAnnotation> connections =
            new CopyOnWriteArraySet<>();

    private final String nickname;
    private Session session;

    public ChatAnnotation() {
        nickname = GUEST_PREFIX + connectionIds.getAndIncrement();
    }


    @OnOpen
    public void start(Session session) {
        this.session = session;
        connections.add(this);
        String message = String.format("* %s %s", nickname, "has joined.");
        broadcast(message);
    }


    @OnClose
    public void end() {
        connections.remove(this);
        String message = String.format("* %s %s",
                nickname, "has disconnected.");
        broadcast(message);
    }


    @OnMessage
    public void incoming(String message) {
        // Never trust the client
        String filteredMessage = String.format("%s: %s",
                nickname, HTMLFilter.filter(message.toString()));
        broadcast(filteredMessage);
    }


    private static void broadcast(String msg) {
        for (ChatAnnotation client : connections) {
            try {
                client.session.getBasicRemote().sendText(msg);
            } catch (IOException e) {
                connections.remove(client);
                try {
                    client.session.close();
                } catch (IOException e1) {
                    // Ignore
                }
                String message = String.format("* %s %s",
                        client.nickname, "has been disconnected.");
                broadcast(message);
            }
        }
    }
}

@@ -1014,16 +1014,48 @@
 nClose;%0A
+import javax.websocket.OnError;%0A
 import j
@@ -1186,24 +1186,104 @@
 rEndpoint;%0A%0A
+import org.apache.juli.logging.Log;%0Aimport org.apache.juli.logging.LogFactory;%0A%0A
 import util.
@@ -1369,16 +1369,93 @@
 tion %7B%0A%0A
+    private static final Log log = LogFactory.getLog(ChatAnnotation.class);%0A%0A
     priv
@@ -2538,24 +2538,156 @@
 e);%0A    %7D%0A%0A%0A
+%0A%0A    @OnError%0A    public void onError(Throwable t) throws Throwable %7B%0A        log.error(%22Chat Error: %22 + t.toString(), t);%0A    %7D%0A%0A%0A
     private 
@@ -2784,32 +2784,76 @@
           try %7B%0A
+                synchronized (client) %7B%0A    
                 
@@ -2899,16 +2899,34 @@
 t(msg);%0A
+                %7D%0A
         
@@ -2951,24 +2951,102 @@
 eption e) %7B%0A
+                log.debug(%22Chat Error: Failed to send message to client%22, e);%0A
             

Implement the restriction required by the JSR356 specification that only one message can be written to a remote endpoint at a time.