/* * 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 org.ofbiz.service.asterisk.websocket; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.atomic.AtomicInteger; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import org.apache.catalina.websocket.MessageInbound; import org.apache.catalina.websocket.StreamInbound; import org.apache.catalina.websocket.WebSocketServlet; import org.apache.catalina.websocket.WsOutbound; import org.apache.commons.lang3.StringEscapeUtils; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.service.asterisk.AsteriskServiceProvider; @SuppressWarnings("deprecation") public class AsteriskSocket extends WebSocketServlet { public static String module = AsteriskSocket.class.getName(); private static final long serialVersionUID = 1L; private final AtomicInteger sessionId = new AtomicInteger(0); @Override public void init() throws ServletException { super.init(); asp.setSockets(sessions); } @Override protected StreamInbound createWebSocketInbound(String subProtocol, HttpServletRequest request) { return new AsteriskSocketSession(sessionId.incrementAndGet()); } public final class AsteriskSocketSession extends MessageInbound { public final int id; public AsteriskSocketSession(int id) { super(); this.id = id; } public int getId() { return id; } @Override protected void onBinaryMessage(ByteBuffer message) throws IOException { } @Override protected void onTextMessage(CharBuffer msg) throws IOException { Debug.logInfo("^^^^^^^^^^^^^^^^^^^^^^^^^message received " + msg.toString(), module); } @Override protected void onOpen(WsOutbound outbound) { if(Debug.verboseOn()) { Debug.logInfo("^^^^^^^^^^^^^^^^^^^^^^^^^create session " + this.getId(), module); } sessions.add(this); } @Override protected void onClose(int status) { if(Debug.verboseOn()) { Debug.logInfo("^^^^^^^^^^^^^^^^^^^^^^^^^destroy sessions " + this.getId(), module); } sessions.remove(this); } } }