/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 
 */
package org.ocelotds.web.ws;

import org.ocelotds.configuration.OcelotRequestConfigurator;
import org.ocelotds.encoders.MessageToClientEncoder;
import java.io.IOException;
import javax.enterprise.inject.spi.CDI;
import javax.inject.Inject;
import javax.websocket.CloseReason;
import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.ocelotds.core.CdiBeanResolver;

/**
 * WebSocket endpoint
 *
 * @author hhfrancois
 */
@ServerEndpoint(value = "/ocelot-endpoint", encoders = {MessageToClientEncoder.class}, configurator = OcelotRequestConfigurator.class)
public class WSEndpoint {

	@Inject
	private WSController controller;

	@OnOpen
	public void handleOpenConnexion(Session session, EndpointConfig config) throws IOException {
		getWSController().handleOpenConnexion(session, config);
	}

	@OnError
	public void onError(Session session, Throwable t) {
		getWSController().onError(session, t);
	}

	@OnClose
	public void handleClosedConnexion(Session session, CloseReason closeReason) {
		getWSController().handleClosedConnexion(session, closeReason);
	}

	@OnMessage
	public void receiveCommandMessage(Session client, String json) {
		getWSController().receiveCommandMessage(client, json);
	}
	
	
	/**
	 * Fix OpenWebBean issues
	 * @return 
	 */
	WSController getWSController() {
		if (null == controller) {
			controller = CDI.current().select(WSController.class).get();
//			controller = getCdiBeanResolver().getBean(WSController.class);
		}
		return controller;
	}

	CdiBeanResolver getCdiBeanResolver() {
		return new CdiBeanResolver();
	}

}

@@ -744,51 +744,8 @@
 nt;%0D
-%0Aimport org.ocelotds.core.CdiBeanResolver;%0D
 %0A%0D%0A/
@@ -1894,16 +1894,18 @@
 %0D%0A%09%7D%0D%0A%0D%0A
+//
 %09CdiBean
@@ -1937,16 +1937,18 @@
 er() %7B%0D%0A
+//
 %09%09return
@@ -1968,21 +1968,23 @@
 esolver();%0D%0A
+//
 %09%7D%0D%0A%0D%0A%7D%0D%0A

use CDI.current for getController in wsendpoint