2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalapp.controller;
22 import java.io.IOException;
23 import java.util.Hashtable;
26 import javax.websocket.OnClose;
27 import javax.websocket.OnMessage;
28 import javax.websocket.OnOpen;
29 import javax.websocket.Session;
30 import javax.websocket.server.ServerEndpoint;
32 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
33 import com.fasterxml.jackson.databind.ObjectMapper;
35 @ServerEndpoint("/opencontact")
36 public class PeerBroadcastSocket {
38 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PeerBroadcastSocket.class);
40 public static Map<String, Object> channelMap = new Hashtable<String, Object>();
41 public Map<String, String> sessionMap = new Hashtable<String, String>();
42 ObjectMapper mapper = new ObjectMapper();
45 public void message(String message, Session session) {
47 // JSONObject jsonObject = new JSONObject(message);
48 @SuppressWarnings("unchecked")
49 Map<String, Object> jsonObject = mapper.readValue(message, Map.class);
51 Object from = jsonObject.get("from");
53 if(channelMap.get(from.toString()) == null) {
54 channelMap.put(from.toString(), session);
55 sessionMap.put(session.getId(), from.toString());
58 } catch (Exception je) {
59 logger.error(EELFLoggerDelegate.errorLogger, "Failed to read value" + je.getMessage());
63 Object to = jsonObject.get("to");
66 Object toSessionObj = channelMap.get(to);
67 if (toSessionObj != null) {
68 Session toSession = null;
69 toSession = (Session) toSessionObj;
70 toSession.getBasicRemote().sendText(message);
73 } catch (Exception ex) {
74 logger.error(EELFLoggerDelegate.errorLogger, "Failed to send text" + ex.getMessage());
77 } catch (Exception ex) {
78 logger.error(EELFLoggerDelegate.errorLogger, "Failed" + ex.getMessage());
84 public void open(Session session) {
85 logger.info(EELFLoggerDelegate.debugLogger, "Channel opened");
89 public void close(Session session) {
90 String channel = sessionMap.get(session.getId());
91 if (channel != null) {
92 Object sessObj = channelMap.get(channel);
93 if (sessObj != null) {
95 ((Session) sessObj).close();
96 } catch (IOException e) {
97 logger.error(EELFLoggerDelegate.errorLogger, "Failed to close" + e.getMessage());
100 channelMap.remove(channel);
102 logger.info(EELFLoggerDelegate.debugLogger, "Channel closed");