Commit a39b7baf authored by Jörg Richter's avatar Jörg Richter

Fix: process REST API request when no websocket connection is open (#215)

parent 79aba1ce
Pipeline #6654 passed with stages
in 3 minutes and 6 seconds
......@@ -3,6 +3,7 @@ package systems.dmx.core.impl;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
......@@ -16,6 +17,8 @@ class WebSocketConnectionPool {
*/
private Map<String, Map<String, WebSocketConnection>> pool = new ConcurrentHashMap();
private Logger logger = Logger.getLogger(getClass().getName());
// ----------------------------------------------------------------------------------------------------- Constructor
WebSocketConnectionPool() {
......@@ -34,12 +37,13 @@ class WebSocketConnectionPool {
WebSocketConnection getConnection(String pluginUri, String sessionId) {
Map<String, WebSocketConnection> connections = pool.get(pluginUri);
if (connections == null) {
throw new RuntimeException("No WebSocket connection open for plugin \"" + pluginUri + "\"");
logger.warning("No WebSocket connection open for plugin \"" + pluginUri + "\"");
return null;
}
WebSocketConnection connection = connections.get(sessionId);
if (connection == null) {
throw new RuntimeException("No WebSocket connection open for session \"" + sessionId + "\" (plugin \"" +
pluginUri + "\")");
logger.warning("No WebSocket connection open for session \"" + sessionId + "\" (plugin \"" + pluginUri +
"\")");
}
return connection;
}
......
......@@ -59,18 +59,15 @@ class WebSocketsServiceImpl implements WebSocketsService {
@Override
public void messageToAllButOne(HttpServletRequest request, String pluginUri, String message) {
if (request == null) {
throw new IllegalArgumentException("request must not be null");
}
WebSocketConnection connection = getConnection(request, pluginUri);
if (connection != null) {
broadcast(pluginUri, message, connection);
throw new IllegalArgumentException("request is null");
}
broadcast(pluginUri, message, getConnection(request, pluginUri));
}
@Override
public void messageToOne(HttpServletRequest request, String pluginUri, String message) {
if (request == null) {
throw new IllegalArgumentException("request must not be null");
throw new IllegalArgumentException("request is null");
}
WebSocketConnection connection = getConnection(request, pluginUri);
if (connection != null) {
......@@ -118,7 +115,7 @@ class WebSocketsServiceImpl implements WebSocketsService {
/**
* @return the WebSocket connection that is associated with the given request (based on its session cookie),
* or null if called outside request scope (e.g. while system startup).
* or null if no such session exists or if called outside request scope (e.g. while system startup).
*
* @throws RuntimeException if no valid session is associated with the request.
*/
......@@ -126,7 +123,7 @@ class WebSocketsServiceImpl implements WebSocketsService {
try {
HttpSession session = request.getSession(false);
if (session == null) {
throw new RuntimeException("No valid session is associated with the request");
throw new RuntimeException("No valid session associated with the request");
}
return pool.getConnection(pluginUri, session.getId());
} catch (IllegalStateException e) {
......@@ -135,6 +132,9 @@ class WebSocketsServiceImpl implements WebSocketsService {
}
}
/**
* @param exclude may be null
*/
private void broadcast(String pluginUri, String message, WebSocketConnection exclude) {
Collection<WebSocketConnection> connections = pool.getConnections(pluginUri);
if (connections != null) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment