2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright (C) 2019 IBM.
8 * ===================================================================
10 * Unless otherwise specified, all software contained herein is licensed
11 * under the Apache License, Version 2.0 (the "License");
12 * you may not use this software except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * Unless otherwise specified, all documentation contained herein is licensed
24 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25 * you may not use this documentation except in compliance with the License.
26 * You may obtain a copy of the License at
28 * https://creativecommons.org/licenses/by/4.0/
30 * Unless required by applicable law or agreed to in writing, documentation
31 * distributed under the License is distributed on an "AS IS" BASIS,
32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 * See the License for the specific language governing permissions and
34 * limitations under the License.
36 * ============LICENSE_END============================================
40 package org.onap.portalapp.controller;
42 import java.io.IOException;
43 import java.util.Hashtable;
46 import javax.websocket.OnClose;
47 import javax.websocket.OnMessage;
48 import javax.websocket.OnOpen;
49 import javax.websocket.Session;
50 import javax.websocket.server.ServerEndpoint;
52 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
53 import com.fasterxml.jackson.databind.ObjectMapper;
55 @ServerEndpoint("/opencontact")
56 public class PeerBroadcastSocket {
58 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PeerBroadcastSocket.class);
60 public final static Map<String, Object> channelMap = new Hashtable<String, Object>();
61 public Map<String, String> sessionMap = new Hashtable<String, String>();
62 ObjectMapper mapper = new ObjectMapper();
65 public void message(String message, Session session) {
67 // JSONObject jsonObject = new JSONObject(message);
68 @SuppressWarnings("unchecked")
69 Map<String, Object> jsonObject = mapper.readValue(message, Map.class);
71 Object from = jsonObject.get("from");
73 if(channelMap.get(from.toString()) == null) {
74 channelMap.put(from.toString(), session);
75 sessionMap.put(session.getId(), from.toString());
78 } catch (Exception je) {
79 logger.error(EELFLoggerDelegate.errorLogger, "Failed to read value" + je.getMessage());
83 Object to = jsonObject.get("to");
86 Object toSessionObj = channelMap.get(to);
87 /*if (toSessionObj != null) {
88 Session toSession = null;
89 toSession = (Session) toSessionObj;
90 toSession.getBasicRemote().sendText(message);
93 } catch (Exception ex) {
94 logger.error(EELFLoggerDelegate.errorLogger, "Failed to send text" + ex.getMessage());
97 } catch (Exception ex) {
98 logger.error(EELFLoggerDelegate.errorLogger, "Failed" + ex.getMessage());
104 public void open(Session session) {
105 logger.info(EELFLoggerDelegate.debugLogger, "Channel opened");
109 public void close(Session session) {
110 String channel = sessionMap.get(session.getId());
111 if (channel != null) {
112 Object sessObj = channelMap.get(channel);
113 if (sessObj != null) {
115 ((Session) sessObj).close();
116 } catch (IOException e) {
117 logger.error(EELFLoggerDelegate.errorLogger, "Failed to close" + e.getMessage());
120 channelMap.remove(channel);
122 logger.info(EELFLoggerDelegate.debugLogger, "Channel closed");