From 620816da08c9fd46d778d2e8d7b7c74cb5a520e1 Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Mon, 16 Sep 2019 10:56:39 +0200 Subject: [PATCH] openvidu-server: POST /api/signal --- .../io/openvidu/client/OpenViduException.java | 1 - .../server/core/SessionEventsHandler.java | 28 +++++-- .../openvidu/server/core/SessionManager.java | 23 +++++- .../kurento/core/KurentoSessionManager.java | 13 ---- .../server/rest/SessionRestController.java | 77 ++++++++++++++++++- 5 files changed, 118 insertions(+), 24 deletions(-) diff --git a/openvidu-client/src/main/java/io/openvidu/client/OpenViduException.java b/openvidu-client/src/main/java/io/openvidu/client/OpenViduException.java index f795bc30..80029510 100644 --- a/openvidu-client/src/main/java/io/openvidu/client/OpenViduException.java +++ b/openvidu-client/src/main/java/io/openvidu/client/OpenViduException.java @@ -46,7 +46,6 @@ public class OpenViduException extends JsonRpcErrorException { USER_METADATA_FORMAT_INVALID_ERROR_CODE(500), SIGNAL_FORMAT_INVALID_ERROR_CODE(600), SIGNAL_TO_INVALID_ERROR_CODE(601), - SIGNAL_MESSAGE_INVALID_ERROR_CODE(602), DOCKER_NOT_FOUND(709), RECORDING_PATH_NOT_VALID(708), RECORDING_FILE_EMPTY_ERROR(707), RECORDING_DELETE_ERROR_CODE(706), RECORDING_LIST_ERROR_CODE(705), RECORDING_STOP_ERROR_CODE(704), diff --git a/openvidu-server/src/main/java/io/openvidu/server/core/SessionEventsHandler.java b/openvidu-server/src/main/java/io/openvidu/server/core/SessionEventsHandler.java index c200e47e..61b19430 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/core/SessionEventsHandler.java +++ b/openvidu-server/src/main/java/io/openvidu/server/core/SessionEventsHandler.java @@ -312,15 +312,27 @@ public class SessionEventsHandler { public void onSendMessage(Participant participant, JsonObject message, Set participants, Integer transactionId, OpenViduException error) { - if (error != null) { - rpcNotificationService.sendErrorResponse(participant.getParticipantPrivateId(), transactionId, null, error); - return; + + boolean isRpcCall = transactionId != null; + if (isRpcCall) { + if (error != null) { + rpcNotificationService.sendErrorResponse(participant.getParticipantPrivateId(), transactionId, null, + error); + return; + } } JsonObject params = new JsonObject(); - params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_DATA_PARAM, message.get("data").getAsString()); - params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_FROM_PARAM, participant.getParticipantPublicId()); - params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_TYPE_PARAM, message.get("type").getAsString()); + if (message.has("data")) { + params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_DATA_PARAM, message.get("data").getAsString()); + } + if (message.has("type")) { + params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_TYPE_PARAM, message.get("type").getAsString()); + } + if (participant != null) { + params.addProperty(ProtocolElements.PARTICIPANTSENDMESSAGE_FROM_PARAM, + participant.getParticipantPublicId()); + } Set toSet = new HashSet(); @@ -357,7 +369,9 @@ public class SessionEventsHandler { } } - rpcNotificationService.sendResponse(participant.getParticipantPrivateId(), transactionId, new JsonObject()); + if (isRpcCall) { + rpcNotificationService.sendResponse(participant.getParticipantPrivateId(), transactionId, new JsonObject()); + } } public void onStreamPropertyChanged(Participant participant, Integer transactionId, Set participants, diff --git a/openvidu-server/src/main/java/io/openvidu/server/core/SessionManager.java b/openvidu-server/src/main/java/io/openvidu/server/core/SessionManager.java index 9f886d29..1d4ed946 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/core/SessionManager.java +++ b/openvidu-server/src/main/java/io/openvidu/server/core/SessionManager.java @@ -36,6 +36,8 @@ import org.springframework.beans.factory.annotation.Autowired; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.JsonSyntaxException; import io.openvidu.client.OpenViduException; import io.openvidu.client.OpenViduException.Code; @@ -94,7 +96,26 @@ public abstract class SessionManager { public abstract void unsubscribe(Participant participant, String senderName, Integer transactionId); - public abstract void sendMessage(Participant participant, String message, Integer transactionId); + public void sendMessage(String message, String sessionId) { + try { + JsonObject messageJson = new JsonParser().parse(message).getAsJsonObject(); + sessionEventsHandler.onSendMessage(null, messageJson, getParticipants(sessionId), null, null); + } catch (JsonSyntaxException | IllegalStateException e) { + throw new OpenViduException(Code.SIGNAL_FORMAT_INVALID_ERROR_CODE, + "Provided signal object '" + message + "' has not a valid JSON format"); + } + } + + public void sendMessage(Participant participant, String message, Integer transactionId) { + try { + JsonObject messageJson = new JsonParser().parse(message).getAsJsonObject(); + sessionEventsHandler.onSendMessage(participant, messageJson, getParticipants(participant.getSessionId()), + transactionId, null); + } catch (JsonSyntaxException | IllegalStateException e) { + throw new OpenViduException(Code.SIGNAL_FORMAT_INVALID_ERROR_CODE, + "Provided signal object '" + message + "' has not a valid JSON format"); + } + } public abstract void streamPropertyChanged(Participant participant, Integer transactionId, String streamId, String property, JsonElement newValue, String changeReason); diff --git a/openvidu-server/src/main/java/io/openvidu/server/kurento/core/KurentoSessionManager.java b/openvidu-server/src/main/java/io/openvidu/server/kurento/core/KurentoSessionManager.java index b4fa4114..4878e674 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/kurento/core/KurentoSessionManager.java +++ b/openvidu-server/src/main/java/io/openvidu/server/kurento/core/KurentoSessionManager.java @@ -435,19 +435,6 @@ public class KurentoSessionManager extends SessionManager { sessionEventsHandler.onUnsubscribe(participant, transactionId, null); } - @Override - public void sendMessage(Participant participant, String message, Integer transactionId) { - try { - JsonObject messageJson = new JsonParser().parse(message).getAsJsonObject(); - KurentoParticipant kParticipant = (KurentoParticipant) participant; - sessionEventsHandler.onSendMessage(participant, messageJson, - getParticipants(kParticipant.getSession().getSessionId()), transactionId, null); - } catch (JsonSyntaxException | IllegalStateException e) { - throw new OpenViduException(Code.SIGNAL_FORMAT_INVALID_ERROR_CODE, - "Provided signal object '" + message + "' has not a valid JSON format"); - } - } - @Override public void streamPropertyChanged(Participant participant, Integer transactionId, String streamId, String property, JsonElement newValue, String reason) { diff --git a/openvidu-server/src/main/java/io/openvidu/server/rest/SessionRestController.java b/openvidu-server/src/main/java/io/openvidu/server/rest/SessionRestController.java index 71222b91..c886bc94 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/rest/SessionRestController.java +++ b/openvidu-server/src/main/java/io/openvidu/server/rest/SessionRestController.java @@ -17,6 +17,7 @@ package io.openvidu.server.rest; +import java.util.ArrayList; import java.util.Collection; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -37,6 +38,8 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -460,7 +463,7 @@ public class SessionRestController { // Session is not in ROUTED MediMode or it is already being recorded return new ResponseEntity<>(HttpStatus.CONFLICT); } else { - // Session is not active + // Session is not active (no connected participants) return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE); } } @@ -473,7 +476,7 @@ public class SessionRestController { return new ResponseEntity<>(HttpStatus.CONFLICT); } if (session.getParticipants().isEmpty()) { - // Session has no participants + // Session is not active (no connected participants) return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE); } @@ -582,6 +585,76 @@ public class SessionRestController { return new ResponseEntity<>(this.recordingManager.deleteRecordingFromHost(recordingId, false)); } + @RequestMapping(value = "/signal", method = RequestMethod.POST) + public ResponseEntity signal(@RequestBody Map params) { + + if (params == null) { + return this.generateErrorResponse("Error in body parameters. Cannot be empty", "/api/signal", + HttpStatus.BAD_REQUEST); + } + + log.info("REST API: POST /api/signal {}", params.toString()); + + String sessionId; + String type; + ArrayList to; + String data; + try { + sessionId = (String) params.get("session"); + to = (ArrayList) params.get("to"); + type = (String) params.get("type"); + data = (String) params.get("data"); + } catch (ClassCastException e) { + return this.generateErrorResponse("Type error in some parameter", "/api/signal", HttpStatus.BAD_REQUEST); + } + + JsonObject completeMessage = new JsonObject(); + + if (sessionId == null) { + // "session" parameter not found + return this.generateErrorResponse("\"session\" parameter is mandatory", "/api/signal", + HttpStatus.BAD_REQUEST); + } + Session session = sessionManager.getSession(sessionId); + if (session == null) { + session = sessionManager.getSessionNotActive(sessionId); + if (session != null) { + // Session is not active (no connected participants) + return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE); + } + // Session does not exist + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + if (type != null) { + completeMessage.addProperty("type", type); + } + + if (data != null) { + completeMessage.addProperty("data", data); + } + + if (to != null) { + try { + Gson gson = new GsonBuilder().create(); + JsonArray toArray = gson.toJsonTree(to).getAsJsonArray(); + completeMessage.add("to", toArray); + } catch (IllegalStateException exception) { + return this.generateErrorResponse("\"to\" parameter is not a valid JSON array", "/api/signal", + HttpStatus.BAD_REQUEST); + } + } + + try { + sessionManager.sendMessage(completeMessage.toString(), sessionId); + } catch (OpenViduException e) { + return this.generateErrorResponse("\"to\" array has no valid connection identifiers", "/api/signal", + HttpStatus.NOT_ACCEPTABLE); + } + + return new ResponseEntity<>(HttpStatus.OK); + } + private ResponseEntity generateErrorResponse(String errorMessage, String path, HttpStatus status) { JsonObject responseJson = new JsonObject(); responseJson.addProperty("timestamp", System.currentTimeMillis());