2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalapp.controller;
40 import java.io.IOException;
41 import java.util.Hashtable;
44 import javax.websocket.OnClose;
45 import javax.websocket.OnMessage;
46 import javax.websocket.OnOpen;
47 import javax.websocket.Session;
48 import javax.websocket.server.ServerEndpoint;
50 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
51 import com.fasterxml.jackson.databind.ObjectMapper;
53 @ServerEndpoint("/opencontact")
54 public class PeerBroadcastSocket {
56 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PeerBroadcastSocket.class);
58 public static Map<String, Object> channelMap = new Hashtable<String, Object>();
59 public Map<String, String> sessionMap = new Hashtable<String, String>();
60 ObjectMapper mapper = new ObjectMapper();
63 public void message(String message, Session session) {
65 // JSONObject jsonObject = new JSONObject(message);
66 @SuppressWarnings("unchecked")
67 Map<String, Object> jsonObject = mapper.readValue(message, Map.class);
69 Object from = jsonObject.get("from");
71 if(channelMap.get(from.toString()) == null) {
72 channelMap.put(from.toString(), session);
73 sessionMap.put(session.getId(), from.toString());
76 } catch (Exception je) {
77 logger.error(EELFLoggerDelegate.errorLogger, "Failed to read value" + je.getMessage());
81 Object to = jsonObject.get("to");
84 Object toSessionObj = channelMap.get(to);
85 if (toSessionObj != null) {
86 Session toSession = null;
87 toSession = (Session) toSessionObj;
88 toSession.getBasicRemote().sendText(message);
91 } catch (Exception ex) {
92 logger.error(EELFLoggerDelegate.errorLogger, "Failed to send text" + ex.getMessage());
95 } catch (Exception ex) {
96 logger.error(EELFLoggerDelegate.errorLogger, "Failed" + ex.getMessage());
102 public void open(Session session) {
103 logger.info(EELFLoggerDelegate.debugLogger, "Channel opened");
107 public void close(Session session) {
108 String channel = sessionMap.get(session.getId());
109 if (channel != null) {
110 Object sessObj = channelMap.get(channel);
111 if (sessObj != null) {
113 ((Session) sessObj).close();
114 } catch (IOException e) {
115 logger.error(EELFLoggerDelegate.errorLogger, "Failed to close" + e.getMessage());
118 channelMap.remove(channel);
120 logger.info(EELFLoggerDelegate.debugLogger, "Channel closed");