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 com.fasterxml.jackson.databind.ObjectMapper;
43 import java.io.IOException;
44 import java.util.HashMap;
46 import java.util.Optional;
47 import javax.websocket.OnClose;
48 import javax.websocket.OnMessage;
49 import javax.websocket.OnOpen;
50 import javax.websocket.Session;
51 import javax.websocket.server.ServerEndpoint;
52 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
54 @ServerEndpoint("/opencontact")
55 public class PeerBroadcastSocket {
56 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PeerBroadcastSocket.class);
57 private static final ObjectMapper mapper = new ObjectMapper();
59 private static final Map<String, Object> channelMap = new HashMap<>();
60 private Map<String, String> sessionMap = new HashMap<>();
63 public void message(String message, Session session) {
65 Map<String, Object> jsonObject = mapper.readValue(message, Map.class);
66 save(jsonObject, session);
67 } catch (Exception ex) {
68 logger.error(EELFLoggerDelegate.errorLogger, "Failed", ex);
73 public void open(Session session) {
74 logger.info(EELFLoggerDelegate.debugLogger, "Channel opened");
78 public void close(Session session) {
79 String channel = sessionMap.get(session.getId());
80 if (channel != null) {
81 Object sessObj = channelMap.get(channel);
82 if (sessObj != null) {
84 ((Session) sessObj).close();
85 } catch (IOException e) {
86 logger.error(EELFLoggerDelegate.errorLogger, "Failed to close", e);
89 channelMap.remove(channel);
91 logger.info(EELFLoggerDelegate.debugLogger, "Channel closed");
94 private void save(Map<String, Object> jsonObject, Session session) {
95 final Optional<String> from = Optional.of(jsonObject.get("from").toString());
96 if (from.isPresent() && channelMap.get(from.get()) == null) {
97 channelMap.put(from.toString(), session);
98 this.sessionMap.put(session.getId(), from.toString());