From ca71611f42c00eef619cc867737e67de2d45aa5e Mon Sep 17 00:00:00 2001
From: Brice Copy <brice.copy@users.noreply.github.com>
Date: Mon, 15 Jul 2019 17:23:11 +0200
Subject: [PATCH] Fix issues with empty param lists

---
 .../atmosphere/C2MonBroadcastService.java     | 71 ++++++++++---------
 .../SubscriptionUpdateTagIdentifier.java      |  4 ++
 2 files changed, 41 insertions(+), 34 deletions(-)

diff --git a/src/main/java/cern/c2mon/client/atmosphere/C2MonBroadcastService.java b/src/main/java/cern/c2mon/client/atmosphere/C2MonBroadcastService.java
index e523e80..176822f 100644
--- a/src/main/java/cern/c2mon/client/atmosphere/C2MonBroadcastService.java
+++ b/src/main/java/cern/c2mon/client/atmosphere/C2MonBroadcastService.java
@@ -85,22 +85,22 @@ public class C2MonBroadcastService {
 	@Get
 	public void onGetSubscribe(AtmosphereResource resource) {
 		obtainBroadcasterFactory(resource);
-		String[] subscriptionsIdToAdd = resource.getRequest().getParameterValues("id");
-		
-        List<SubscriptionUpdateTagIdentifier> tagIdentifiersById = Arrays.asList(subscriptionsIdToAdd).stream()
-                .map(id -> SubscriptionUpdateTagIdentifier.builder().id(Long.valueOf(id)).build())
-                .collect(Collectors.toList());
-        
-        String[] subscriptionsNamesToAdd = resource.getRequest().getParameterValues("name");
-        
-        List<SubscriptionUpdateTagIdentifier> tagIdentifiersByName = Arrays.asList(subscriptionsNamesToAdd).stream()
-                .map(name -> SubscriptionUpdateTagIdentifier.builder().name(name).build())
-                .collect(Collectors.toList());
-        
-        
-        tagIdentifiersById.addAll(tagIdentifiersByName);
-        resource.suspend();
-        subscribeTagsByTagIdentifier(resource, tagIdentifiersById);
+//		String[] subscriptionsIdToAdd = resource.getRequest().getParameterValues("id");
+//		
+//        List<SubscriptionUpdateTagIdentifier> tagIdentifiersById = Arrays.asList(subscriptionsIdToAdd).stream()
+//                .map(id -> SubscriptionUpdateTagIdentifier.builder().id(Long.valueOf(id)).build())
+//                .collect(Collectors.toList());
+//        
+//        String[] subscriptionsNamesToAdd = resource.getRequest().getParameterValues("name");
+//        
+//        List<SubscriptionUpdateTagIdentifier> tagIdentifiersByName = Arrays.asList(subscriptionsNamesToAdd).stream()
+//                .map(name -> SubscriptionUpdateTagIdentifier.builder().name(name).build())
+//                .collect(Collectors.toList());
+//        
+//        
+//        tagIdentifiersById.addAll(tagIdentifiersByName);
+//        resource.suspend();
+//        subscribeTagsByTagIdentifier(resource, tagIdentifiersById);
 	}
 
     public Set<Long> subscribeTagsByTagIdentifier(AtmosphereResource resource, List<SubscriptionUpdateTagIdentifier> subscriptionsToAdd) {
@@ -108,42 +108,45 @@ public class C2MonBroadcastService {
         Set<String> tagNamesList = subscriptionsToAdd.stream().map(SubscriptionUpdateTagIdentifier::getName).collect(Collectors.toSet());
 
         Set<Long> validTagIds = new HashSet<>();
-		if(!tagIdsList.isEmpty()) {
+		if(!tagIdsList.isEmpty() && tagIdsList.iterator().next() != null) {
           this.logger.debug("Subscribing to tags IDs {}", tagIdsList.stream().map( Object::toString ).collect(Collectors.joining(",")));
 		}
-		if(!tagNamesList.isEmpty()) {
+		if(!tagNamesList.isEmpty() && tagNamesList.iterator().next() != null) {
 	          this.logger.debug("Subscribing to tags Names {}", tagNamesList.stream().collect(Collectors.joining(",")));
 	    }
         try {
 			// TODO : We should validate the tags before subscribing
 			for (Long tagToAdd : tagIdsList) {
-			    if(this.logger.isDebugEnabled()) {
-				  this.logger.debug("Associating res {} with tag id {} ", resource.uuid(), tagToAdd);
-			    }
-				m_broadcasterFactory.lookup(tagToAdd, true).addAtmosphereResource(resource);
-				validTagIds.add(tagToAdd);
+                if (tagToAdd != null) {
+                    if (this.logger.isDebugEnabled()) {
+                        this.logger.debug("Associating res {} with tag id {} ", resource.uuid(), tagToAdd);
+                    }
+                    m_broadcasterFactory.lookup(tagToAdd, true).addAtmosphereResource(resource);
+                    validTagIds.add(tagToAdd);
+                }
 			}
 			// TODO : Only subscribe to valid tags and return only those in the return result.
-			c2monTagService.subscribe(tagIdsList, broadcastingTagListener);
+			c2monTagService.subscribe(validTagIds, broadcastingTagListener);
 		} catch (Exception e) {
 			this.logger.warn("Failed tag subscription by ID : ", e);
 		}
         try {
+            Set<Long> newTagNameIds = new HashSet<>();
             // TODO : We should validate the tags before subscribing
             for (String tagNameToAdd : tagNamesList) {
-                
-                Collection<Tag> tags = c2monTagService.findByName(tagNameToAdd);
-                for (Tag tag: tags) {
-                  if(this.logger.isDebugEnabled()) {
-                        this.logger.debug("Associating res {} with tag id {} (tag name {}) ", resource.uuid(), tag.getId(), tag.getName());
-                  }
-                  m_broadcasterFactory.lookup(tag.getId(), true).addAtmosphereResource(resource);
-                  validTagIds.add(tag.getId());
+                if(tagNameToAdd!= null){
+                    Collection<Tag> tags = c2monTagService.findByName(tagNameToAdd);
+                    for (Tag tag: tags) {
+                      if(this.logger.isDebugEnabled()) {
+                            this.logger.debug("Associating res {} with tag id {} (tag name {}) ", resource.uuid(), tag.getId(), tag.getName());
+                      }
+                      m_broadcasterFactory.lookup(tag.getId(), true).addAtmosphereResource(resource);
+                      newTagNameIds.add(tag.getId());
+                    }
                 }
-                
             }
             // TODO : Only subscribe to valid tags and return only those in the return result.
-            c2monTagService.subscribe(tagIdsList, broadcastingTagListener);
+            c2monTagService.subscribe(newTagNameIds, broadcastingTagListener);
         } catch (Exception e) {
             this.logger.warn("Failed tag subscription: ", e);
         }
diff --git a/src/main/java/cern/c2mon/client/atmosphere/SubscriptionUpdateTagIdentifier.java b/src/main/java/cern/c2mon/client/atmosphere/SubscriptionUpdateTagIdentifier.java
index d3c40c1..f5523d4 100644
--- a/src/main/java/cern/c2mon/client/atmosphere/SubscriptionUpdateTagIdentifier.java
+++ b/src/main/java/cern/c2mon/client/atmosphere/SubscriptionUpdateTagIdentifier.java
@@ -1,10 +1,14 @@
 package cern.c2mon.client.atmosphere;
 
+import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Data;
+import lombok.NoArgsConstructor;
 
 @Data
 @Builder
+@NoArgsConstructor
+@AllArgsConstructor
 public class SubscriptionUpdateTagIdentifier {
    Long id;
    String name;
-- 
GitLab