From 7b00f9af119e560d7974aab8aa405a6decbaa398 Mon Sep 17 00:00:00 2001 From: shikha0203 Date: Tue, 21 Feb 2023 13:56:55 +0000 Subject: Fix NotificationWebsocketHandler- Add null test before using nullable values Issue-ID: SDC-4408 Signed-off-by: shikha0203 Change-Id: I06d386c1e4234351461f792354091b6f1c472010 --- .../websocket/NotificationWebsocketHandler.java | 24 +++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-websocket') diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-websocket/src/main/java/org/openecomp/sdc/notification/websocket/NotificationWebsocketHandler.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-websocket/src/main/java/org/openecomp/sdc/notification/websocket/NotificationWebsocketHandler.java index 7f21dee9a2..c5b9dab73c 100644 --- a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-websocket/src/main/java/org/openecomp/sdc/notification/websocket/NotificationWebsocketHandler.java +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-websocket/src/main/java/org/openecomp/sdc/notification/websocket/NotificationWebsocketHandler.java @@ -21,10 +21,13 @@ package org.openecomp.sdc.notification.websocket; import com.google.gson.Gson; import java.io.IOException; +import java.net.URI; +import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.UUID; import java.util.function.Consumer; +import org.apache.commons.collections.CollectionUtils; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.notification.types.NotificationsStatusDto; @@ -79,19 +82,26 @@ public class NotificationWebsocketHandler extends TextWebSocketHandler { private String getOwnerId(WebSocketSession session) { HttpHeaders handshakeHeaders = session.getHandshakeHeaders(); - if (handshakeHeaders.containsKey(COOKIE)) { - String[] cookies = handshakeHeaders.get(COOKIE).get(0).split("; "); - Optional cookie = extractValue(cookies, USER_ID_HEADER_PARAM); - if (cookie.isPresent()) { - return cookie.get(); + if (handshakeHeaders.containsKey(COOKIE)) { + final List handshakeHeadersOrEmpty = handshakeHeaders.getOrEmpty(COOKIE); + if (CollectionUtils.isNotEmpty(handshakeHeadersOrEmpty)) { + final Optional cookie = extractValue(handshakeHeadersOrEmpty.get(0).split("; "), USER_ID_HEADER_PARAM); + if (cookie.isPresent()) { + return cookie.get(); + } + } } - } LOGGER.error("No " + USER_ID_HEADER_PARAM + " specified in the session cookies."); return null; } private UUID getLastEventId(WebSocketSession session) { - String uriQuery = session.getUri().getQuery(); + URI uri = session.getUri(); + if (uri == null) { + LOGGER.warn("No " + LAST_DELIVERED_QUERY_PARAM + " specified in the request URI."); + return null; + } + String uriQuery = uri.getQuery(); if (uriQuery != null) { String[] queries = uriQuery.split("; "); Optional paramValue = extractValue(queries, LAST_DELIVERED_QUERY_PARAM); -- cgit 1.2.3-korg