This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new ce8738801e67 CAMEL-27926: camel-atmosphere-websocket: 
NullPointerException when a connection key has no open connection and the 
endpoint has no consumer (#31232)
ce8738801e67 is described below

commit ce8738801e67ea84d89581b176bbaf0db790e590
Author: Guillaume Nodet <[email protected]>
AuthorDate: Wed Dec 2 11:53:34 2026 +0100

    CAMEL-27926: camel-atmosphere-websocket: NullPointerException when a 
connection key has no open connection and the endpoint has no consumer (#31232)
    
    WebsocketProducer called 
getWebsocketConsumer().sendNotDeliveredMessage(...) without a
    null check, so a producer-only endpoint threw a NullPointerException when a 
connection
    key had no open connection. Without a consumer it now logs a WARN naming the
    undelivered connection keys.
    
    Hermes Agent (Claude Sonnet 4.6) on behalf of Guillaume Nodet.
    
    Co-authored-by: Claude Sonnet 4.6 <[email protected]>
---
 .../atmosphere/websocket/WebsocketProducer.java    |   9 +-
 .../websocket/WebsocketProducerNoConsumerTest.java | 127 +++++++++++++++++++++
 2 files changed, 135 insertions(+), 1 deletion(-)

diff --git 
a/components/camel-atmosphere-websocket/src/main/java/org/apache/camel/component/atmosphere/websocket/WebsocketProducer.java
 
b/components/camel-atmosphere-websocket/src/main/java/org/apache/camel/component/atmosphere/websocket/WebsocketProducer.java
index 637d24de84b7..30402193f076 100644
--- 
a/components/camel-atmosphere-websocket/src/main/java/org/apache/camel/component/atmosphere/websocket/WebsocketProducer.java
+++ 
b/components/camel-atmosphere-websocket/src/main/java/org/apache/camel/component/atmosphere/websocket/WebsocketProducer.java
@@ -121,7 +121,14 @@ public class WebsocketProducer extends DefaultProducer {
 
         if (!notValidConnectionKeys.isEmpty()) {
             LOG.debug("Some connections have not received the message {}", 
message);
-            
getEndpoint().getWebsocketConsumer().sendNotDeliveredMessage(notValidConnectionKeys,
 message);
+            WebsocketConsumer consumer = getEndpoint().getWebsocketConsumer();
+            if (consumer != null) {
+                consumer.sendNotDeliveredMessage(notValidConnectionKeys, 
message);
+            } else {
+                LOG.warn(
+                        "Cannot report undelivered message(s) for connection 
key(s) {} - no consumer configured on this endpoint",
+                        notValidConnectionKeys);
+            }
         }
     }
 
diff --git 
a/components/camel-atmosphere-websocket/src/test/java/org/apache/camel/component/atmosphere/websocket/WebsocketProducerNoConsumerTest.java
 
b/components/camel-atmosphere-websocket/src/test/java/org/apache/camel/component/atmosphere/websocket/WebsocketProducerNoConsumerTest.java
new file mode 100644
index 000000000000..3a55dbcc5349
--- /dev/null
+++ 
b/components/camel-atmosphere-websocket/src/test/java/org/apache/camel/component/atmosphere/websocket/WebsocketProducerNoConsumerTest.java
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.atmosphere.websocket;
+
+import java.util.UUID;
+import java.util.concurrent.ExecutorService;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.spi.ExecutorServiceManager;
+import org.atmosphere.websocket.WebSocket;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * Regression test for CAMEL-27926: NullPointerException when a connection key 
has no open connection and the endpoint
+ * has no consumer (producer-only endpoint, websocketConsumer == null).
+ */
+class WebsocketProducerNoConsumerTest {
+
+    private WebsocketEndpoint endpoint;
+    private WebSocketStore store;
+    private WebsocketProducer producer;
+    private Exchange exchange;
+    private Message message;
+
+    @BeforeEach
+    void setUp() throws Exception {
+        endpoint = mock(WebsocketEndpoint.class);
+        store = mock(WebSocketStore.class);
+        CamelContext camelContext = mock(CamelContext.class);
+        ExecutorServiceManager executorServiceManager = 
mock(ExecutorServiceManager.class);
+        ExecutorService executor = mock(ExecutorService.class);
+        exchange = mock(Exchange.class);
+        message = mock(Message.class);
+
+        when(endpoint.getWebSocketStore()).thenReturn(store);
+        when(endpoint.getCamelContext()).thenReturn(camelContext);
+        
when(camelContext.getExecutorServiceManager()).thenReturn(executorServiceManager);
+        when(executorServiceManager.newSingleThreadExecutor(any(), 
anyString())).thenReturn(executor);
+        when(endpoint.isSendToAll()).thenReturn(false);
+        when(exchange.getIn()).thenReturn(message);
+        when(message.getBody()).thenReturn("hello");
+        
when(message.getHeader(WebsocketConstants.CONNECTION_KEY_LIST)).thenReturn(null);
+
+        producer = new WebsocketProducer(endpoint);
+        producer.doStart();
+    }
+
+    /**
+     * CAMEL-27926: when the endpoint has no consumer (producer-only) and the 
connection key is not found in the store,
+     * the producer must not throw a NullPointerException.
+     */
+    @Test
+    void testNoNpeWhenConsumerIsNullAndConnectionKeyIsInvalid() {
+        String invalidKey = UUID.randomUUID().toString();
+
+        when(store.getWebSocket(invalidKey)).thenReturn(null);
+        when(message.getHeader(WebsocketConstants.CONNECTION_KEY, 
String.class)).thenReturn(invalidKey);
+        // Producer-only endpoint: no consumer configured
+        when(endpoint.getWebsocketConsumer()).thenReturn(null);
+
+        // Must not throw NullPointerException (CAMEL-27926)
+        assertDoesNotThrow(() -> producer.process(exchange));
+    }
+
+    /**
+     * When a consumer IS present, undelivered messages must still be 
forwarded to it.
+     */
+    @Test
+    void testConsumerReceivesNotDeliveredMessageWhenPresent() throws Exception 
{
+        String invalidKey = UUID.randomUUID().toString();
+        WebsocketConsumer consumer = mock(WebsocketConsumer.class);
+
+        when(store.getWebSocket(invalidKey)).thenReturn(null);
+        when(message.getHeader(WebsocketConstants.CONNECTION_KEY, 
String.class)).thenReturn(invalidKey);
+        when(endpoint.getWebsocketConsumer()).thenReturn(consumer);
+
+        producer.process(exchange);
+
+        // Consumer must be notified of the undelivered message
+        verify(consumer).sendNotDeliveredMessage(any(), any());
+    }
+
+    /**
+     * When all connection keys are valid, sendNotDeliveredMessage must NOT be 
called even if no consumer is present.
+     */
+    @Test
+    void testNoCallToConsumerWhenAllKeysAreValid() throws Exception {
+        String validKey = UUID.randomUUID().toString();
+        WebSocket webSocket = mock(WebSocket.class);
+        WebsocketConsumer consumer = mock(WebsocketConsumer.class);
+
+        when(store.getWebSocket(validKey)).thenReturn(webSocket);
+        when(message.getHeader(WebsocketConstants.CONNECTION_KEY, 
String.class)).thenReturn(validKey);
+        // Consumer is present but should never be called when all keys are 
valid
+        when(endpoint.getWebsocketConsumer()).thenReturn(consumer);
+
+        producer.process(exchange);
+
+        // No undelivered keys → consumer must not be touched
+        verify(consumer, never()).sendNotDeliveredMessage(any(), any());
+    }
+}

Reply via email to