2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.notification.websocket;
22 import com.google.gson.Gson;
23 import java.io.IOException;
24 import java.util.Objects;
25 import java.util.Optional;
26 import java.util.UUID;
27 import java.util.function.Consumer;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30 import org.openecomp.sdc.notification.types.NotificationsStatusDto;
31 import org.openecomp.sdc.notification.workers.NotificationWorker;
32 import org.springframework.http.HttpHeaders;
33 import org.springframework.web.socket.CloseStatus;
34 import org.springframework.web.socket.TextMessage;
35 import org.springframework.web.socket.WebSocketSession;
36 import org.springframework.web.socket.handler.TextWebSocketHandler;
38 public class NotificationWebsocketHandler extends TextWebSocketHandler {
40 private static final String USER_ID_HEADER_PARAM = "USER_ID";
41 private static final String LAST_DELIVERED_QUERY_PARAM = "LAST_DELIVERED_EVENT_ID";
42 private static final String COOKIE = "Cookie";
43 private static Logger LOGGER = LoggerFactory.getLogger(NotificationWebsocketHandler.class);
44 private NotificationWorker worker;
46 public NotificationWebsocketHandler(NotificationWorker worker) {
48 this.worker = Objects.requireNonNull(worker, "NotificationWorker object is not initialized.");
52 public void afterConnectionEstablished(WebSocketSession session) throws Exception {
53 super.afterConnectionEstablished(session);
54 String ownerId = getOwnerId(session);
55 if (ownerId == null) {
58 UUID lastDelivered = getLastEventId(session);
59 Consumer<NotificationsStatusDto> notesProcessor = (notes) -> notifyReceiver(session, notes);
60 worker.register(ownerId, lastDelivered, notesProcessor);
64 public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
65 String ownerId = getOwnerId(session);
66 if (ownerId != null) {
67 worker.unregister(ownerId);
69 super.afterConnectionClosed(session, status);
72 private void notifyReceiver(WebSocketSession session, NotificationsStatusDto notificationsStatusDto) {
74 session.sendMessage(new TextMessage(new Gson().toJson(notificationsStatusDto)));
75 } catch (IOException e) {
76 LOGGER.error("IO Exception during Receiver notification.", e);
80 private String getOwnerId(WebSocketSession session) {
81 HttpHeaders handshakeHeaders = session.getHandshakeHeaders();
82 if (handshakeHeaders.containsKey(COOKIE)) {
83 String[] cookies = handshakeHeaders.get(COOKIE).get(0).split("; ");
84 Optional<String> cookie = extractValue(cookies, USER_ID_HEADER_PARAM);
85 if (cookie.isPresent()) {
89 LOGGER.error("No " + USER_ID_HEADER_PARAM + " specified in the session cookies.");
93 private UUID getLastEventId(WebSocketSession session) {
94 String uriQuery = session.getUri().getQuery();
95 if (uriQuery != null) {
96 String[] queries = uriQuery.split("; ");
97 Optional<String> paramValue = extractValue(queries, LAST_DELIVERED_QUERY_PARAM);
98 if (paramValue.isPresent()) {
99 return UUID.fromString(paramValue.get());
102 LOGGER.warn("No " + LAST_DELIVERED_QUERY_PARAM + " specified in the request URI.");
106 private Optional<String> extractValue(String[] pairs, String name) {
107 for (String nameValuePair : pairs) {
108 String[] value = nameValuePair.split("=");
109 if (value[0].equals(name)) {
110 return Optional.of(value[1]);
113 return Optional.empty();