From d378c37fbd1ecec7b43394926f1ca32a695e07de Mon Sep 17 00:00:00 2001 From: vasraz Date: Mon, 22 Mar 2021 15:33:06 +0000 Subject: Reformat openecomp-be Signed-off-by: Vasyl Razinkov Issue-ID: SDC-3449 Change-Id: I13e02322f8e00820cc5a1d85752caaeda9bf10d1 --- .../websocket/NotificationWebsocketHandler.java | 109 +++++++++------------ 1 file changed, 47 insertions(+), 62 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 1e424631f7..7f21dee9a2 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 @@ -17,10 +17,14 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.notification.websocket; import com.google.gson.Gson; +import java.io.IOException; +import java.util.Objects; +import java.util.Optional; +import java.util.UUID; +import java.util.function.Consumer; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.notification.types.NotificationsStatusDto; @@ -31,100 +35,81 @@ import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.handler.TextWebSocketHandler; -import java.io.IOException; -import java.util.Objects; -import java.util.Optional; -import java.util.UUID; -import java.util.function.Consumer; - public class NotificationWebsocketHandler extends TextWebSocketHandler { - private static final String USER_ID_HEADER_PARAM = "USER_ID"; - private static final String LAST_DELIVERED_QUERY_PARAM = "LAST_DELIVERED_EVENT_ID"; - private static final String COOKIE = "Cookie"; - private static Logger LOGGER = LoggerFactory.getLogger(NotificationWebsocketHandler.class); - private NotificationWorker worker; + private static final String USER_ID_HEADER_PARAM = "USER_ID"; + private static final String LAST_DELIVERED_QUERY_PARAM = "LAST_DELIVERED_EVENT_ID"; + private static final String COOKIE = "Cookie"; + private static Logger LOGGER = LoggerFactory.getLogger(NotificationWebsocketHandler.class); + private NotificationWorker worker; - public NotificationWebsocketHandler(NotificationWorker worker) { - super(); - this.worker = Objects.requireNonNull(worker, "NotificationWorker object is not initialized."); - } + public NotificationWebsocketHandler(NotificationWorker worker) { + super(); + this.worker = Objects.requireNonNull(worker, "NotificationWorker object is not initialized."); + } - @Override + @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { super.afterConnectionEstablished(session); - - String ownerId = getOwnerId(session); - if (ownerId == null) { - return; - } - UUID lastDelivered = getLastEventId(session); - - Consumer notesProcessor = (notes) -> notifyReceiver(session, notes); - - worker.register(ownerId, lastDelivered, notesProcessor); + String ownerId = getOwnerId(session); + if (ownerId == null) { + return; + } + UUID lastDelivered = getLastEventId(session); + Consumer notesProcessor = (notes) -> notifyReceiver(session, notes); + worker.register(ownerId, lastDelivered, notesProcessor); } - @Override + @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { - String ownerId = getOwnerId(session); - if (ownerId != null) { - worker.unregister(ownerId); - } - super.afterConnectionClosed(session, status); + String ownerId = getOwnerId(session); + if (ownerId != null) { + worker.unregister(ownerId); + } + super.afterConnectionClosed(session, status); } - private void notifyReceiver(WebSocketSession session, NotificationsStatusDto notificationsStatusDto) { - - try { - session.sendMessage(new TextMessage(new Gson().toJson(notificationsStatusDto))); - } catch (IOException e) { - LOGGER.error("IO Exception during Receiver notification.", e); - } - } + private void notifyReceiver(WebSocketSession session, NotificationsStatusDto notificationsStatusDto) { + try { + session.sendMessage(new TextMessage(new Gson().toJson(notificationsStatusDto))); + } catch (IOException e) { + LOGGER.error("IO Exception during Receiver notification.", e); + } + } 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(); - } - } - - LOGGER.error("No " + USER_ID_HEADER_PARAM + " specified in the session cookies."); - return null; + String[] cookies = handshakeHeaders.get(COOKIE).get(0).split("; "); + Optional cookie = extractValue(cookies, 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(); if (uriQuery != null) { - String[] queries = uriQuery.split("; "); Optional paramValue = extractValue(queries, LAST_DELIVERED_QUERY_PARAM); if (paramValue.isPresent()) { return UUID.fromString(paramValue.get()); } } - LOGGER.warn("No " + LAST_DELIVERED_QUERY_PARAM + " specified in the request URI."); return null; } - private Optional extractValue(String[] pairs, String name) { - - for (String nameValuePair : pairs) { - + private Optional extractValue(String[] pairs, String name) { + for (String nameValuePair : pairs) { String[] value = nameValuePair.split("="); if (value[0].equals(name)) { return Optional.of(value[1]); } } - - return Optional.empty(); - } - + return Optional.empty(); + } } -- cgit 1.2.3-korg