Set secure flag & log exception
[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 com.fasterxml.jackson.databind.ObjectMapper;
43 import java.io.IOException;
44 import java.util.HashMap;
45 import java.util.Map;
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;
53
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();
58
59     private static final Map<String, Object> channelMap = new HashMap<>();
60     private Map<String, String> sessionMap = new HashMap<>();
61
62     @OnMessage
63     public void message(String message, Session session) {
64         try {
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);
69         }
70     }
71
72     @OnOpen
73     public void open(Session session) {
74         logger.info(EELFLoggerDelegate.debugLogger, "Channel opened");
75     }
76
77     @OnClose
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) {
83                 try {
84                     ((Session) sessObj).close();
85                 } catch (IOException e) {
86                     logger.error(EELFLoggerDelegate.errorLogger, "Failed to close", e);
87                 }
88             }
89             channelMap.remove(channel);
90         }
91         logger.info(EELFLoggerDelegate.debugLogger, "Channel closed");
92     }
93
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());
99         }
100     }
101
102 }