PeerBroadcastSocket(Make "channelMap" field final)
[portal.git] / ecomp-portal-BE-os / src / main / java / org / onap / portalapp / controller / PeerBroadcastSocket.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  *
7  * Modifications Copyright (C) 2019 IBM.
8  * ===================================================================
9  *
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
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
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.
22  *
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
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
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.
35  *
36  * ============LICENSE_END============================================
37  *
38  * 
39  */
40 package org.onap.portalapp.controller;
41
42 import java.io.IOException;
43 import java.util.Hashtable;
44 import java.util.Map;
45
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;
51
52 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
53 import com.fasterxml.jackson.databind.ObjectMapper;
54
55 @ServerEndpoint("/opencontact")
56 public class PeerBroadcastSocket {
57
58         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PeerBroadcastSocket.class);
59
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();
63
64         @OnMessage
65         public void message(String message, Session session) {
66                 try {
67                         // JSONObject jsonObject = new JSONObject(message);
68                         @SuppressWarnings("unchecked")
69                         Map<String, Object> jsonObject = mapper.readValue(message, Map.class);
70                         try {
71                                 Object from = jsonObject.get("from");
72                                 if (from != null) {
73                                         if(channelMap.get(from.toString()) == null) {
74                                                 channelMap.put(from.toString(), session);
75                                                 sessionMap.put(session.getId(), from.toString());
76                                         }
77                                 }
78                         } catch (Exception je) {
79                                 logger.error(EELFLoggerDelegate.errorLogger, "Failed to read value" + je.getMessage());
80                         }
81
82                         try {
83                                 Object to = jsonObject.get("to");
84                                 if (to == null)
85                                         return;
86                                 Object toSessionObj = channelMap.get(to);
87                                 /*if (toSessionObj != null) {
88                                         Session toSession = null;
89                                         toSession = (Session) toSessionObj;
90                                         toSession.getBasicRemote().sendText(message);
91                                 }
92 */
93                         } catch (Exception ex) {
94                                 logger.error(EELFLoggerDelegate.errorLogger, "Failed to send text" + ex.getMessage());
95                         }
96
97                 } catch (Exception ex) {
98                         logger.error(EELFLoggerDelegate.errorLogger, "Failed" + ex.getMessage());
99                 }
100
101         }
102
103         @OnOpen
104         public void open(Session session) {
105                 logger.info(EELFLoggerDelegate.debugLogger, "Channel opened");
106         }
107
108         @OnClose
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) {
114                                 try {
115                                         ((Session) sessObj).close();
116                                 } catch (IOException e) {
117                                         logger.error(EELFLoggerDelegate.errorLogger, "Failed to close" + e.getMessage());
118                                 }
119                         }
120                         channelMap.remove(channel);
121                 }
122                 logger.info(EELFLoggerDelegate.debugLogger, "Channel closed");
123         }
124
125 }
126