/*
 * Copyright 2014-2015 Red Hat, Inc, and individual contributors.
 *
 * Licensed 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 org.projectodd.wunderboss.web.async.websocket;

import javax.websocket.CloseReason;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.Session;
import java.io.IOException;

public class DelegatingJavaxEndpoint extends Endpoint {

    public static String endpointKey() {
        return "session-endpoint-" + Thread.currentThread().getId();
    }

    @Override
    public void onOpen(Session session, EndpointConfig endpointConfig) {
        Endpoint endpoint = delegate(session);
        if (endpoint != null) {
            endpoint.onOpen(session, endpointConfig);
        } else {
            close(session);
        }
    }

    @Override
    public void onClose(Session session, CloseReason closeReason) {
        Endpoint endpoint = delegate(session);
        if (endpoint != null) {
            endpoint.onClose(session, closeReason);
        }
    }

    @Override
    public void onError(Session session, Throwable err) {
        Endpoint endpoint = delegate(session);
        if (endpoint != null) {
            endpoint.onError(session, err);
        } else {
            close(session);
        }
    }

    private void close(Session session) {
        try {
            session.close(POLICY_CLOSE);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private synchronized Endpoint delegate(final Session session) {
        if (this.delegate == null) {
            this.delegate = (Endpoint)session.getUserProperties().remove(endpointKey());
        }
        return this.delegate;
    }

    private Endpoint delegate;

    private final static CloseReason POLICY_CLOSE = new CloseReason(new CloseReason.CloseCode() {
        @Override
        public int getCode() {
            return 1003;
        }
    }, null);
}

@@ -907,125 +907,8 @@
  %7B%0A%0A
-    public static String endpointKey() %7B%0A        return %22session-endpoint-%22 + Thread.currentThread().getId();%0A    %7D%0A%0A
     
@@ -1019,39 +1019,32 @@
 oint = delegate(
-session
 );%0A        if (e
@@ -1289,39 +1289,32 @@
 oint = delegate(
-session
 );%0A        if (e
@@ -1510,23 +1510,16 @@
 elegate(
-session
 );%0A     
@@ -1643,32 +1643,136 @@
        %7D%0A    %7D%0A%0A
+    public static void setCurrentDelegate(Endpoint endpoint) %7B%0A        delegateTL.set(endpoint);%0A    %7D%0A%0A
     private void
@@ -1993,29 +1993,8 @@
 ate(
-final Session session
 ) %7B%0A
@@ -2062,66 +2062,56 @@
 e = 
-(Endpoint)session.getUserProperties().remove(endpointKey()
+delegateTL.get();%0A            delegateTL.remove(
 );%0A 
@@ -2115,24 +2115,33 @@
 ;%0A        %7D%0A
+        %0A
         retu
@@ -2393,10 +2393,91 @@
  null);%0A
+    private final static ThreadLocal%3CEndpoint%3E delegateTL = new ThreadLocal%3C%3E();%0A
 %7D%0A

Use a TL instead of touching the userProperties map [IMMUTANT-561]