summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-BE-os
diff options
context:
space:
mode:
authorDominik Mizyn <d.mizyn@samsung.com>2019-06-21 13:07:43 +0200
committerDominik Mizyn <d.mizyn@samsung.com>2019-06-21 13:07:49 +0200
commitc2502f81cc07d6b6c619d334f7e81ea82e47223a (patch)
tree38cdea0c33c141b4fe852b3dbc6bf12e56beadce /ecomp-portal-BE-os
parent6a9580d025114106e82fc49da895bbc857a3e91b (diff)
PeerBroadcastSocket sonar issues fix and code refactor
Sonar issues fix and code refactor. Session data save moved to another method. Rest of code don't really do anything. Issue-ID: PORTAL-624 Change-Id: I53b36377f2d2645d8c24ad2384959f0599e07303 Signed-off-by: Dominik Mizyn <d.mizyn@samsung.com>
Diffstat (limited to 'ecomp-portal-BE-os')
-rw-r--r--ecomp-portal-BE-os/src/main/java/org/onap/portalapp/controller/PeerBroadcastSocket.java55
1 files changed, 16 insertions, 39 deletions
diff --git a/ecomp-portal-BE-os/src/main/java/org/onap/portalapp/controller/PeerBroadcastSocket.java b/ecomp-portal-BE-os/src/main/java/org/onap/portalapp/controller/PeerBroadcastSocket.java
index cbc34337..c80419f9 100644
--- a/ecomp-portal-BE-os/src/main/java/org/onap/portalapp/controller/PeerBroadcastSocket.java
+++ b/ecomp-portal-BE-os/src/main/java/org/onap/portalapp/controller/PeerBroadcastSocket.java
@@ -39,65 +39,34 @@
*/
package org.onap.portalapp.controller;
+import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
-import java.util.Hashtable;
+import java.util.HashMap;
import java.util.Map;
-
+import java.util.Optional;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
-
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
-import com.fasterxml.jackson.databind.ObjectMapper;
@ServerEndpoint("/opencontact")
public class PeerBroadcastSocket {
+ private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PeerBroadcastSocket.class);
+ private static final ObjectMapper mapper = new ObjectMapper();
- EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PeerBroadcastSocket.class);
-
- public final static Map<String, Object> channelMap = new Hashtable<String, Object>();
- public Map<String, String> sessionMap = new Hashtable<String, String>();
- ObjectMapper mapper = new ObjectMapper();
+ protected static final Map<String, Object> channelMap = new HashMap<>();
+ private Map<String, String> sessionMap = new HashMap<>();
@OnMessage
public void message(String message, Session session) {
try {
- // JSONObject jsonObject = new JSONObject(message);
- @SuppressWarnings("unchecked")
Map<String, Object> jsonObject = mapper.readValue(message, Map.class);
- try {
- Object from = jsonObject.get("from");
- if (from != null) {
- if(channelMap.get(from.toString()) == null) {
- channelMap.put(from.toString(), session);
- sessionMap.put(session.getId(), from.toString());
- }
- }
- } catch (Exception je) {
- logger.error(EELFLoggerDelegate.errorLogger, "Failed to read value" + je.getMessage());
- }
-
- try {
- Object to = jsonObject.get("to");
- if (to == null)
- return;
- Object toSessionObj = channelMap.get(to);
- /*if (toSessionObj != null) {
- Session toSession = null;
- toSession = (Session) toSessionObj;
- toSession.getBasicRemote().sendText(message);
- }
-*/
- } catch (Exception ex) {
- logger.error(EELFLoggerDelegate.errorLogger, "Failed to send text" + ex.getMessage());
- }
-
+ save(jsonObject, session);
} catch (Exception ex) {
logger.error(EELFLoggerDelegate.errorLogger, "Failed" + ex.getMessage());
}
-
}
@OnOpen
@@ -122,5 +91,13 @@ public class PeerBroadcastSocket {
logger.info(EELFLoggerDelegate.debugLogger, "Channel closed");
}
+ private void save(Map<String, Object> jsonObject, Session session) {
+ final Optional<String> from = Optional.of(jsonObject.get("from").toString());
+ if (from.isPresent() && channelMap.get(from.get()) == null) {
+ this.channelMap.put(from.toString(), session);
+ this.sessionMap.put(session.getId(), from.toString());
+ }
+ }
+
}