From 01addff060bd2ecaa75ca497c91c732e6a35c9ec Mon Sep 17 00:00:00 2001 From: Hiroki Terashima Date: Wed, 8 Nov 2017 23:07:45 -0800 Subject: [PATCH] Added null check for run id before sending message via websocket. NPE will no longer be thrown when authors were connected via web socket and closed session. Fixes #898. --- .../websocket/WISETextWebSocketHandler.java | 81 ++++++++++--------- 1 file changed, 42 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/wise/portal/service/websocket/WISETextWebSocketHandler.java b/src/main/java/org/wise/portal/service/websocket/WISETextWebSocketHandler.java index f9a2763090..5859ff2a5d 100644 --- a/src/main/java/org/wise/portal/service/websocket/WISETextWebSocketHandler.java +++ b/src/main/java/org/wise/portal/service/websocket/WISETextWebSocketHandler.java @@ -357,22 +357,23 @@ private void handleUserDisconnecting(WISEWebSocketSession wiseWebSocketSession) String userName = wiseWebSocketSession.getUserName(); Long runId = wiseWebSocketSession.getRunId(); - // add the user name and run id - messageJSON.put("userName", userName); - messageJSON.put("runId", runId); - - String message = messageJSON.toString(); // get the message as a string + if (runId != null) { + // add the user name and run id + messageJSON.put("userName", userName); + messageJSON.put("runId", runId); - // get all the teachers that are currently connected for this run - Set teacherConnectionsForRun = getTeacherConnectionsForRun(runId); + String message = messageJSON.toString(); // get the message as a string - // send the message to all the teachers currently connected for this run - sendMessageToConnections(message, teacherConnectionsForRun); + // get all the teachers that are currently connected for this run + Set teacherConnectionsForRun = getTeacherConnectionsForRun(runId); - // also send updated studentsOnline list to all the teacher currently connected for this run - JSONObject studentsOnlineMessage = createStudentsOnlineMessage(wiseWebSocketSession); - sendMessageToConnections(studentsOnlineMessage.toString(), teacherConnectionsForRun); + // send the message to all the teachers currently connected for this run + sendMessageToConnections(message, teacherConnectionsForRun); + // also send updated studentsOnline list to all the teacher currently connected for this run + JSONObject studentsOnlineMessage = createStudentsOnlineMessage(wiseWebSocketSession); + sendMessageToConnections(studentsOnlineMessage.toString(), teacherConnectionsForRun); + } } catch (JSONException e) { e.printStackTrace(); } @@ -497,37 +498,39 @@ private void addSession(WISEWebSocketSession wiseWebSocketSession) { * @param wiseWebSocketSession the WISEWebSocketSession to remove from our collections */ private void removeSession(WISEWebSocketSession wiseWebSocketSession) { + removeWebSocketSessionFromRunConnections(wiseWebSocketSession); + removeWebSocketSessionfromSessions(wiseWebSocketSession); + removeWebSocketSessionFromUsers(wiseWebSocketSession); + } - if (wiseWebSocketSession != null) { - - WebSocketSession session = wiseWebSocketSession.getSession(); // get the session - - User user = wiseWebSocketSession.getUser(); //get the user - - if (session != null) { - - Long runId = getValueFromSession(session, "runId"); //get the run id - - if (wiseWebSocketSession.isTeacher()) { - // user is a teacher so we will get the set of teacher connections for the run - Set teacherConnectionsForRun = runToTeacherConnections.get(runId); + private void removeWebSocketSessionFromUsers(WISEWebSocketSession wiseWebSocketSession) { + User user = wiseWebSocketSession.getUser(); + userToWISEWebSocketSession.remove(user); + } - if (teacherConnectionsForRun != null) { - // remove the teacher session from the set of teacher connections for the run - teacherConnectionsForRun.remove(wiseWebSocketSession); - } - } else { - // user is a student so we will get the set of student connections for the run - Set studentConnectionsForRun = runToStudentConnections.get(runId); + private void removeWebSocketSessionfromSessions(WISEWebSocketSession wiseWebSocketSession) { + WebSocketSession session = wiseWebSocketSession.getSession(); + sessionToWISEWebSocketSession.remove(session); + } - if (studentConnectionsForRun != null) { - // remove the student session from the set of student connections for the run - studentConnectionsForRun.remove(wiseWebSocketSession); - } + private void removeWebSocketSessionFromRunConnections(WISEWebSocketSession wiseWebSocketSession) { + WebSocketSession session = wiseWebSocketSession.getSession(); + Long runId = getValueFromSession(session, "runId"); + if (runId != null) { + if (wiseWebSocketSession.isTeacher()) { + // user is a teacher so we will get the set of teacher connections for the run + Set teacherConnectionsForRun = runToTeacherConnections.get(runId); + if (teacherConnectionsForRun != null) { + // remove the teacher session from the set of teacher connections for the run + teacherConnectionsForRun.remove(wiseWebSocketSession); + } + } else { + // user is a student so we will get the set of student connections for the run + Set studentConnectionsForRun = runToStudentConnections.get(runId); + if (studentConnectionsForRun != null) { + // remove the student session from the set of student connections for the run + studentConnectionsForRun.remove(wiseWebSocketSession); } - - sessionToWISEWebSocketSession.remove(session); - userToWISEWebSocketSession.remove(user); } } }