Spotbug: split message processing to reduce complexity

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
Andy Scherzinger 2022-12-28 13:47:51 +01:00
parent 7b86b02c21
commit c5067b7a60
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B

View file

@ -54,6 +54,7 @@ import java.util.Map;
import javax.inject.Inject; import javax.inject.Inject;
import androidx.annotation.NonNull;
import autodagger.AutoInjector; import autodagger.AutoInjector;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request; import okhttp3.Request;
@ -163,15 +164,141 @@ public class MagicWebSocketInstance extends WebSocketListener {
} }
@Override @Override
public void onMessage(WebSocket webSocket, String text) { public void onMessage(@NonNull WebSocket webSocket, @NonNull String text) {
if (webSocket == internalWebSocket) { if (webSocket == internalWebSocket) {
Log.d(TAG, "Receiving : " + webSocket + " " + text); Log.d(TAG, "Receiving : " + webSocket + " " + text);
try { try {
BaseWebSocketMessage baseWebSocketMessage = LoganSquare.parse(text, BaseWebSocketMessage.class); BaseWebSocketMessage baseWebSocketMessage = LoganSquare.parse(text, BaseWebSocketMessage.class);
String messageType = baseWebSocketMessage.getType(); String messageType = baseWebSocketMessage.getType();
if (messageType != null) {
switch (messageType) { switch (messageType) {
case "hello": case "hello":
processHelloMessage(webSocket, text);
break;
case "error":
processErrorMessage(webSocket, text);
break;
case "room":
processJoinedRoomMessage(text);
break;
case "event":
processEventMessage(text);
break;
case "message":
processMessage(text);
break;
case "bye":
connected = false;
resumeId = "";
break;
default:
break;
}
} else {
Log.e(TAG, "Received message with type: null");
}
} catch (IOException e) {
Log.e(TAG, "Failed to recognize WebSocket message", e);
}
}
}
private void processMessage(String text) throws IOException {
CallOverallWebSocketMessage callOverallWebSocketMessage = LoganSquare.parse(text, CallOverallWebSocketMessage.class);
NCSignalingMessage ncSignalingMessage = callOverallWebSocketMessage.getCallWebSocketMessage().getNcSignalingMessage();
if (TextUtils.isEmpty(ncSignalingMessage.getFrom()) && callOverallWebSocketMessage.getCallWebSocketMessage().getSenderWebSocketMessage() != null) {
ncSignalingMessage.setFrom(callOverallWebSocketMessage.getCallWebSocketMessage().getSenderWebSocketMessage().getSessionId());
}
signalingMessageReceiver.process(ncSignalingMessage);
}
private void processEventMessage(String text) throws IOException {
EventOverallWebSocketMessage eventOverallWebSocketMessage = LoganSquare.parse(text, EventOverallWebSocketMessage.class);
if (eventOverallWebSocketMessage.getEventMap() != null) {
String target = (String) eventOverallWebSocketMessage.getEventMap().get("target");
switch (target) {
case "room":
if (eventOverallWebSocketMessage.getEventMap().get("type").equals("message")) {
processRoomMessageMessage(eventOverallWebSocketMessage);
} else if (eventOverallWebSocketMessage.getEventMap().get("type").equals("join")) {
processRoomJoinMessage(eventOverallWebSocketMessage);
}
break;
case TARGET_PARTICIPANTS:
signalingMessageReceiver.process(eventOverallWebSocketMessage.getEventMap());
break;
}
}
}
private void processRoomMessageMessage(EventOverallWebSocketMessage eventOverallWebSocketMessage) {
Map<String, Object> messageHashMap = (Map<String, Object>) eventOverallWebSocketMessage
.getEventMap()
.get("message");
if (messageHashMap != null && messageHashMap.containsKey("data")) {
Map<String, Object> dataHashMap = (Map<String, Object>) messageHashMap.get("data");
if (dataHashMap != null && dataHashMap.containsKey("chat")) {
Map<String, Object> chatMap = (Map<String, Object>) dataHashMap.get("chat");
if (chatMap != null && chatMap.containsKey("refresh") && (boolean) chatMap.get("refresh")) {
HashMap<String, String> refreshChatHashMap = new HashMap<>();
refreshChatHashMap.put(BundleKeys.KEY_ROOM_TOKEN, (String) messageHashMap.get("roomid"));
refreshChatHashMap.put(BundleKeys.KEY_INTERNAL_USER_ID, Long.toString(conversationUser.getId()));
eventBus.post(new WebSocketCommunicationEvent("refreshChat", refreshChatHashMap));
}
}
}
}
private void processRoomJoinMessage(EventOverallWebSocketMessage eventOverallWebSocketMessage) {
List<HashMap<String, Object>> joinEventList = (List<HashMap<String, Object>>) eventOverallWebSocketMessage
.getEventMap()
.get("join");
HashMap<String, Object> internalHashMap;
Participant participant;
for (int i = 0; i < joinEventList.size(); i++) {
internalHashMap = joinEventList.get(i);
HashMap<String, Object> userMap = (HashMap<String, Object>) internalHashMap.get("user");
participant = new Participant();
String userId = (String) internalHashMap.get("userid");
if (userId != null) {
participant.setActorType(USERS);
participant.setActorId(userId);
} else {
participant.setActorType(GUESTS);
// FIXME seems to be not given by the HPB: participant.setActorId();
}
if (userMap != null) {
// There is no "user" attribute for guest participants.
participant.setDisplayName((String) userMap.get("displayname"));
}
usersHashMap.put((String) internalHashMap.get("sessionid"), participant);
}
}
private void processJoinedRoomMessage(String text) throws IOException {
JoinedRoomOverallWebSocketMessage joinedRoomOverallWebSocketMessage = LoganSquare.parse(text, JoinedRoomOverallWebSocketMessage.class);
currentRoomToken = joinedRoomOverallWebSocketMessage.getRoomWebSocketMessage().getRoomId();
if (joinedRoomOverallWebSocketMessage.getRoomWebSocketMessage().getRoomPropertiesWebSocketMessage() != null && !TextUtils.isEmpty(currentRoomToken)) {
sendRoomJoinedEvent();
}
}
private void processErrorMessage(WebSocket webSocket, String text) throws IOException {
Log.e(TAG, "Received error: " + text);
ErrorOverallWebSocketMessage errorOverallWebSocketMessage = LoganSquare.parse(text, ErrorOverallWebSocketMessage.class);
if ("no_such_session".equals(errorOverallWebSocketMessage.getErrorWebSocketMessage().getCode())) {
Log.d(TAG, "WebSocket " + webSocket.hashCode() + " resumeID " + resumeId + " expired");
resumeId = "";
currentRoomToken = "";
restartWebSocket();
} else if ("hello_expected".equals(errorOverallWebSocketMessage.getErrorWebSocketMessage().getCode())) {
restartWebSocket();
}
}
private void processHelloMessage(WebSocket webSocket, String text) throws IOException {
connected = true; connected = true;
reconnecting = false; reconnecting = false;
restartCount = 0; restartCount = 0;
@ -197,102 +324,6 @@ public class MagicWebSocketInstance extends WebSocketListener {
helloHasHap.put(ROOM_TOKEN, currentRoomToken); helloHasHap.put(ROOM_TOKEN, currentRoomToken);
} }
eventBus.post(new WebSocketCommunicationEvent("hello", helloHasHap)); eventBus.post(new WebSocketCommunicationEvent("hello", helloHasHap));
break;
case "error":
Log.e(TAG, "Received error: " + text);
ErrorOverallWebSocketMessage errorOverallWebSocketMessage = LoganSquare.parse(text, ErrorOverallWebSocketMessage.class);
if (("no_such_session").equals(errorOverallWebSocketMessage.getErrorWebSocketMessage().getCode())) {
Log.d(TAG, "WebSocket " + webSocket.hashCode() + " resumeID " + resumeId + " expired");
resumeId = "";
currentRoomToken = "";
restartWebSocket();
} else if (("hello_expected").equals(errorOverallWebSocketMessage.getErrorWebSocketMessage().getCode())) {
restartWebSocket();
}
break;
case "room":
JoinedRoomOverallWebSocketMessage joinedRoomOverallWebSocketMessage = LoganSquare.parse(text, JoinedRoomOverallWebSocketMessage.class);
currentRoomToken = joinedRoomOverallWebSocketMessage.getRoomWebSocketMessage().getRoomId();
if (joinedRoomOverallWebSocketMessage.getRoomWebSocketMessage().getRoomPropertiesWebSocketMessage() != null && !TextUtils.isEmpty(currentRoomToken)) {
sendRoomJoinedEvent();
}
break;
case "event":
EventOverallWebSocketMessage eventOverallWebSocketMessage = LoganSquare.parse(text, EventOverallWebSocketMessage.class);
if (eventOverallWebSocketMessage.getEventMap() != null) {
String target = (String) eventOverallWebSocketMessage.getEventMap().get("target");
switch (target) {
case "room":
if (eventOverallWebSocketMessage.getEventMap().get("type").equals("message")) {
Map<String, Object> messageHashMap =
(Map<String, Object>) eventOverallWebSocketMessage.getEventMap().get("message");
if (messageHashMap.containsKey("data")) {
Map<String, Object> dataHashMap = (Map<String, Object>) messageHashMap.get(
"data");
if (dataHashMap.containsKey("chat")) {
boolean shouldRefreshChat;
Map<String, Object> chatMap = (Map<String, Object>) dataHashMap.get("chat");
if (chatMap.containsKey("refresh")) {
shouldRefreshChat = (boolean) chatMap.get("refresh");
if (shouldRefreshChat) {
HashMap<String, String> refreshChatHashMap = new HashMap<>();
refreshChatHashMap.put(BundleKeys.KEY_ROOM_TOKEN, (String) messageHashMap.get("roomid"));
refreshChatHashMap.put(BundleKeys.KEY_INTERNAL_USER_ID, Long.toString(conversationUser.getId()));
eventBus.post(new WebSocketCommunicationEvent("refreshChat", refreshChatHashMap));
}
}
}
}
} else if (eventOverallWebSocketMessage.getEventMap().get("type").equals("join")) {
List<HashMap<String, Object>> joinEventList = (List<HashMap<String, Object>>) eventOverallWebSocketMessage.getEventMap().get("join");
HashMap<String, Object> internalHashMap;
Participant participant;
for (int i = 0; i < joinEventList.size(); i++) {
internalHashMap = joinEventList.get(i);
HashMap<String, Object> userMap = (HashMap<String, Object>) internalHashMap.get("user");
participant = new Participant();
String userId = (String) internalHashMap.get("userid");
if (userId != null) {
participant.setActorType(USERS);
participant.setActorId(userId);
} else {
participant.setActorType(GUESTS);
// FIXME seems to be not given by the HPB: participant.setActorId();
}
if (userMap != null) {
// There is no "user" attribute for guest participants.
participant.setDisplayName((String) userMap.get("displayname"));
}
usersHashMap.put((String) internalHashMap.get("sessionid"), participant);
}
}
break;
case TARGET_PARTICIPANTS:
signalingMessageReceiver.process(eventOverallWebSocketMessage.getEventMap());
break;
}
}
break;
case "message":
CallOverallWebSocketMessage callOverallWebSocketMessage = LoganSquare.parse(text, CallOverallWebSocketMessage.class);
NCSignalingMessage ncSignalingMessage = callOverallWebSocketMessage.getCallWebSocketMessage().getNcSignalingMessage();
if (TextUtils.isEmpty(ncSignalingMessage.getFrom()) && callOverallWebSocketMessage.getCallWebSocketMessage().getSenderWebSocketMessage() != null) {
ncSignalingMessage.setFrom(callOverallWebSocketMessage.getCallWebSocketMessage().getSenderWebSocketMessage().getSessionId());
}
signalingMessageReceiver.process(ncSignalingMessage);
break;
case "bye":
connected = false;
resumeId = "";
default:
break;
}
} catch (IOException e) {
Log.e(TAG, "Failed to recognize WebSocket message", e);
}
}
} }
private void sendRoomJoinedEvent() { private void sendRoomJoinedEvent() {