Security/ Package Name changes
[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  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.controller;
39
40 import java.io.IOException;
41 import java.util.Hashtable;
42 import java.util.Map;
43
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;
49
50 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
51 import com.fasterxml.jackson.databind.ObjectMapper;
52
53 @ServerEndpoint("/opencontact")
54 public class PeerBroadcastSocket {
55
56         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PeerBroadcastSocket.class);
57
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();
61
62         @OnMessage
63         public void message(String message, Session session) {
64                 try {
65                         // JSONObject jsonObject = new JSONObject(message);
66                         @SuppressWarnings("unchecked")
67                         Map<String, Object> jsonObject = mapper.readValue(message, Map.class);
68                         try {
69                                 Object from = jsonObject.get("from");
70                                 if (from != null) {
71                                         if(channelMap.get(from.toString()) == null) {
72                                                 channelMap.put(from.toString(), session);
73                                                 sessionMap.put(session.getId(), from.toString());
74                                         }
75                                 }
76                         } catch (Exception je) {
77                                 logger.error(EELFLoggerDelegate.errorLogger, "Failed to read value" + je.getMessage());
78                         }
79
80                         try {
81                                 Object to = jsonObject.get("to");
82                                 if (to == null)
83                                         return;
84                                 Object toSessionObj = channelMap.get(to);
85                                 if (toSessionObj != null) {
86                                         Session toSession = null;
87                                         toSession = (Session) toSessionObj;
88                                         toSession.getBasicRemote().sendText(message);
89                                 }
90
91                         } catch (Exception ex) {
92                                 logger.error(EELFLoggerDelegate.errorLogger, "Failed to send text" + ex.getMessage());
93                         }
94
95                 } catch (Exception ex) {
96                         logger.error(EELFLoggerDelegate.errorLogger, "Failed" + ex.getMessage());
97                 }
98
99         }
100
101         @OnOpen
102         public void open(Session session) {
103                 logger.info(EELFLoggerDelegate.debugLogger, "Channel opened");
104         }
105
106         @OnClose
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) {
112                                 try {
113                                         ((Session) sessObj).close();
114                                 } catch (IOException e) {
115                                         logger.error(EELFLoggerDelegate.errorLogger, "Failed to close" + e.getMessage());
116                                 }
117                         }
118                         channelMap.remove(channel);
119                 }
120                 logger.info(EELFLoggerDelegate.debugLogger, "Channel closed");
121         }
122
123 }
124