[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-os / src / main / java / org / openecomp / portalapp / controller / PeerBroadcastSocket.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalapp.controller;
21
22 import java.io.IOException;
23 import java.util.Hashtable;
24 import java.util.Map;
25
26 import javax.websocket.OnClose;
27 import javax.websocket.OnMessage;
28 import javax.websocket.OnOpen;
29 import javax.websocket.Session;
30 import javax.websocket.server.ServerEndpoint;
31
32 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34
35 @ServerEndpoint("/opencontact")
36 public class PeerBroadcastSocket {
37
38         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PeerBroadcastSocket.class);
39
40         public static Map<String, Object> channelMap = new Hashtable<String, Object>();
41         public Map<String, String> sessionMap = new Hashtable<String, String>();
42         ObjectMapper mapper = new ObjectMapper();
43
44         @OnMessage
45         public void message(String message, Session session) {
46                 try {
47                         // JSONObject jsonObject = new JSONObject(message);
48                         @SuppressWarnings("unchecked")
49                         Map<String, Object> jsonObject = mapper.readValue(message, Map.class);
50                         try {
51                                 Object from = jsonObject.get("from");
52                                 if (from != null) {
53                                         if(channelMap.get(from.toString()) == null) {
54                                                 channelMap.put(from.toString(), session);
55                                                 sessionMap.put(session.getId(), from.toString());
56                                         }
57                                 }
58                         } catch (Exception je) {
59                                 logger.error(EELFLoggerDelegate.errorLogger, "Failed to read value" + je.getMessage());
60                         }
61
62                         try {
63                                 Object to = jsonObject.get("to");
64                                 if (to == null)
65                                         return;
66                                 Object toSessionObj = channelMap.get(to);
67                                 if (toSessionObj != null) {
68                                         Session toSession = null;
69                                         toSession = (Session) toSessionObj;
70                                         toSession.getBasicRemote().sendText(message);
71                                 }
72
73                         } catch (Exception ex) {
74                                 logger.error(EELFLoggerDelegate.errorLogger, "Failed to send text" + ex.getMessage());
75                         }
76
77                 } catch (Exception ex) {
78                         logger.error(EELFLoggerDelegate.errorLogger, "Failed" + ex.getMessage());
79                 }
80
81         }
82
83         @OnOpen
84         public void open(Session session) {
85                 logger.info(EELFLoggerDelegate.debugLogger, "Channel opened");
86         }
87
88         @OnClose
89         public void close(Session session) {
90                 String channel = sessionMap.get(session.getId());
91                 if (channel != null) {
92                         Object sessObj = channelMap.get(channel);
93                         if (sessObj != null) {
94                                 try {
95                                         ((Session) sessObj).close();
96                                 } catch (IOException e) {
97                                         logger.error(EELFLoggerDelegate.errorLogger, "Failed to close" + e.getMessage());
98                                 }
99                         }
100                         channelMap.remove(channel);
101                 }
102                 logger.info(EELFLoggerDelegate.debugLogger, "Channel closed");
103         }
104
105 }
106