openvidu-server: generateErrorResponse as a static method of SessionRestController

pull/771/head
pabloFuente 2022-12-13 12:02:34 +01:00
parent f66d642c2a
commit ea724a8db0
3 changed files with 35 additions and 26 deletions

View File

@ -207,6 +207,10 @@ public class Session implements SessionInterface {
public String getMediaNodeId() { public String getMediaNodeId() {
return null; return null;
} }
public String getMediaNodeIp() {
return null;
}
protected void checkClosed() { protected void checkClosed() {
if (isClosed()) { if (isClosed()) {

View File

@ -162,6 +162,11 @@ public class KurentoSession extends Session {
return this.kms.getId(); return this.kms.getId();
} }
@Override
public String getMediaNodeIp() {
return this.kms.getIp();
}
public void sendIceCandidate(String participantPrivateId, String senderPublicId, String endpointName, public void sendIceCandidate(String participantPrivateId, String senderPublicId, String endpointName,
IceCandidate candidate) { IceCandidate candidate) {
this.kurentoSessionHandler.onIceCandidate(sessionId, participantPrivateId, senderPublicId, endpointName, this.kurentoSessionHandler.onIceCandidate(sessionId, participantPrivateId, senderPublicId, endpointName,

View File

@ -101,7 +101,7 @@ public class SessionRestController {
try { try {
sessionProperties = getSessionPropertiesFromParams(params).build(); sessionProperties = getSessionPropertiesFromParams(params).build();
} catch (Exception e) { } catch (Exception e) {
return this.generateErrorResponse(e.getMessage(), "/sessions", HttpStatus.BAD_REQUEST); return SessionRestController.generateErrorResponse(e.getMessage(), "/sessions", HttpStatus.BAD_REQUEST);
} }
String sessionId; String sessionId;
@ -254,13 +254,13 @@ public class SessionRestController {
+ " closing lock to be available for closing from DELETE " + RequestMappings.API + " closing lock to be available for closing from DELETE " + RequestMappings.API
+ "/sessions"; + "/sessions";
log.error(errorMsg); log.error(errorMsg);
return this.generateErrorResponse(errorMsg, "/sessions", HttpStatus.BAD_REQUEST); return SessionRestController.generateErrorResponse(errorMsg, "/sessions", HttpStatus.BAD_REQUEST);
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
String errorMsg = "InterruptedException while waiting for Session " + sessionId String errorMsg = "InterruptedException while waiting for Session " + sessionId
+ " closing lock to be available for closing from DELETE " + RequestMappings.API + "/sessions"; + " closing lock to be available for closing from DELETE " + RequestMappings.API + "/sessions";
log.error(errorMsg); log.error(errorMsg);
return this.generateErrorResponse(errorMsg, "/sessions", HttpStatus.BAD_REQUEST); return SessionRestController.generateErrorResponse(errorMsg, "/sessions", HttpStatus.BAD_REQUEST);
} }
} else { } else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND); return new ResponseEntity<>(HttpStatus.NOT_FOUND);
@ -283,7 +283,7 @@ public class SessionRestController {
try { try {
connectionProperties = getConnectionPropertiesFromParams(params).build(); connectionProperties = getConnectionPropertiesFromParams(params).build();
} catch (Exception e) { } catch (Exception e) {
return this.generateErrorResponse(e.getMessage(), "/sessions/" + sessionId + "/connection", return SessionRestController.generateErrorResponse(e.getMessage(), "/sessions/" + sessionId + "/connection",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
switch (connectionProperties.getType()) { switch (connectionProperties.getType()) {
@ -292,7 +292,7 @@ public class SessionRestController {
case IPCAM: case IPCAM:
return this.newIpcamConnection(session, connectionProperties); return this.newIpcamConnection(session, connectionProperties);
default: default:
return this.generateErrorResponse("Wrong type parameter", "/sessions/" + sessionId + "/connection", return SessionRestController.generateErrorResponse("Wrong type parameter", "/sessions/" + sessionId + "/connection",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
} }
@ -371,7 +371,7 @@ public class SessionRestController {
public ResponseEntity<?> startRecording(@RequestBody Map<String, ?> params) { public ResponseEntity<?> startRecording(@RequestBody Map<String, ?> params) {
if (params == null) { if (params == null) {
return this.generateErrorResponse("Error in body parameters. Cannot be empty", "/recordings/start", return SessionRestController.generateErrorResponse("Error in body parameters. Cannot be empty", "/recordings/start",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
@ -386,13 +386,13 @@ public class SessionRestController {
try { try {
sessionId = (String) params.get("session"); sessionId = (String) params.get("session");
} catch (Exception e) { } catch (Exception e) {
return this.generateErrorResponse("Type error in parameter \"session\"", "/recordings/start", return SessionRestController.generateErrorResponse("Type error in parameter \"session\"", "/recordings/start",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
if (sessionId == null) { if (sessionId == null) {
// "session" parameter not found // "session" parameter not found
return this.generateErrorResponse("\"session\" parameter is mandatory", "/recordings/start", return SessionRestController.generateErrorResponse("\"session\" parameter is mandatory", "/recordings/start",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
@ -426,9 +426,9 @@ public class SessionRestController {
try { try {
recordingProperties = getRecordingPropertiesFromParams(params, session).build(); recordingProperties = getRecordingPropertiesFromParams(params, session).build();
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
return this.generateErrorResponse(e.getMessage(), "/sessions", HttpStatus.UNPROCESSABLE_ENTITY); return SessionRestController.generateErrorResponse(e.getMessage(), "/sessions", HttpStatus.UNPROCESSABLE_ENTITY);
} catch (RuntimeException e) { } catch (RuntimeException e) {
return this.generateErrorResponse(e.getMessage(), "/sessions", HttpStatus.BAD_REQUEST); return SessionRestController.generateErrorResponse(e.getMessage(), "/sessions", HttpStatus.BAD_REQUEST);
} }
try { try {
@ -556,7 +556,7 @@ public class SessionRestController {
public ResponseEntity<String> newToken(@RequestBody Map<String, ?> params) { public ResponseEntity<String> newToken(@RequestBody Map<String, ?> params) {
if (params == null) { if (params == null) {
return this.generateErrorResponse("Error in body parameters. Cannot be empty", "/tokens", return SessionRestController.generateErrorResponse("Error in body parameters. Cannot be empty", "/tokens",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
@ -566,11 +566,11 @@ public class SessionRestController {
try { try {
sessionId = (String) params.get("session"); sessionId = (String) params.get("session");
} catch (ClassCastException e) { } catch (ClassCastException e) {
return this.generateErrorResponse("Type error in some parameter", "/tokens", HttpStatus.BAD_REQUEST); return SessionRestController.generateErrorResponse("Type error in some parameter", "/tokens", HttpStatus.BAD_REQUEST);
} }
if (sessionId == null) { if (sessionId == null) {
return this.generateErrorResponse("\"session\" parameter is mandatory", "/tokens", HttpStatus.BAD_REQUEST); return SessionRestController.generateErrorResponse("\"session\" parameter is mandatory", "/tokens", HttpStatus.BAD_REQUEST);
} }
log.warn("Token API is deprecated. Use Connection API instead (POST {}/sessions/{}/connection)", log.warn("Token API is deprecated. Use Connection API instead (POST {}/sessions/{}/connection)",
@ -578,7 +578,7 @@ public class SessionRestController {
final Session session = this.sessionManager.getSessionWithNotActive(sessionId); final Session session = this.sessionManager.getSessionWithNotActive(sessionId);
if (session == null) { if (session == null) {
return this.generateErrorResponse("Session " + sessionId + " not found", "/tokens", HttpStatus.NOT_FOUND); return SessionRestController.generateErrorResponse("Session " + sessionId + " not found", "/tokens", HttpStatus.NOT_FOUND);
} }
ConnectionProperties connectionProperties; ConnectionProperties connectionProperties;
@ -586,14 +586,14 @@ public class SessionRestController {
try { try {
connectionProperties = getConnectionPropertiesFromParams(params).build(); connectionProperties = getConnectionPropertiesFromParams(params).build();
} catch (Exception e) { } catch (Exception e) {
return this.generateErrorResponse(e.getMessage(), "/sessions/" + sessionId + "/connection", return SessionRestController.generateErrorResponse(e.getMessage(), "/sessions/" + sessionId + "/connection",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
ResponseEntity<?> entity = this.newWebrtcConnection(session, connectionProperties); ResponseEntity<?> entity = this.newWebrtcConnection(session, connectionProperties);
JsonObject jsonResponse = JsonParser.parseString(entity.getBody().toString()).getAsJsonObject(); JsonObject jsonResponse = JsonParser.parseString(entity.getBody().toString()).getAsJsonObject();
if (jsonResponse.has("error")) { if (jsonResponse.has("error")) {
return this.generateErrorResponse(jsonResponse.get("message").getAsString(), "/tokens", return SessionRestController.generateErrorResponse(jsonResponse.get("message").getAsString(), "/tokens",
HttpStatus.valueOf(jsonResponse.get("status").getAsInt())); HttpStatus.valueOf(jsonResponse.get("status").getAsInt()));
} else { } else {
String connectionId = jsonResponse.get("id").getAsString(); String connectionId = jsonResponse.get("id").getAsString();
@ -639,7 +639,7 @@ public class SessionRestController {
public ResponseEntity<?> signal(@RequestBody Map<String, ?> params) { public ResponseEntity<?> signal(@RequestBody Map<String, ?> params) {
if (params == null) { if (params == null) {
return this.generateErrorResponse("Error in body parameters. Cannot be empty", "/signal", return SessionRestController.generateErrorResponse("Error in body parameters. Cannot be empty", "/signal",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
@ -655,14 +655,14 @@ public class SessionRestController {
type = (String) params.get("type"); type = (String) params.get("type");
data = (String) params.get("data"); data = (String) params.get("data");
} catch (ClassCastException e) { } catch (ClassCastException e) {
return this.generateErrorResponse("Type error in some parameter", "/signal", HttpStatus.BAD_REQUEST); return SessionRestController.generateErrorResponse("Type error in some parameter", "/signal", HttpStatus.BAD_REQUEST);
} }
JsonObject completeMessage = new JsonObject(); JsonObject completeMessage = new JsonObject();
if (sessionId == null) { if (sessionId == null) {
// "session" parameter not found // "session" parameter not found
return this.generateErrorResponse("\"session\" parameter is mandatory", "/signal", HttpStatus.BAD_REQUEST); return SessionRestController.generateErrorResponse("\"session\" parameter is mandatory", "/signal", HttpStatus.BAD_REQUEST);
} }
Session session = sessionManager.getSession(sessionId); Session session = sessionManager.getSession(sessionId);
if (session == null) { if (session == null) {
@ -689,7 +689,7 @@ public class SessionRestController {
JsonArray toArray = gson.toJsonTree(to).getAsJsonArray(); JsonArray toArray = gson.toJsonTree(to).getAsJsonArray();
completeMessage.add("to", toArray); completeMessage.add("to", toArray);
} catch (IllegalStateException exception) { } catch (IllegalStateException exception) {
return this.generateErrorResponse("\"to\" parameter is not a valid JSON array", "/signal", return SessionRestController.generateErrorResponse("\"to\" parameter is not a valid JSON array", "/signal",
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
} }
@ -697,7 +697,7 @@ public class SessionRestController {
try { try {
sessionManager.sendMessage(completeMessage.toString(), session); sessionManager.sendMessage(completeMessage.toString(), session);
} catch (OpenViduException e) { } catch (OpenViduException e) {
return this.generateErrorResponse("\"to\" array has no valid connection identifiers", "/signal", return SessionRestController.generateErrorResponse("\"to\" array has no valid connection identifiers", "/signal",
HttpStatus.NOT_ACCEPTABLE); HttpStatus.NOT_ACCEPTABLE);
} }
@ -720,7 +720,7 @@ public class SessionRestController {
return new ResponseEntity<>(token.toJsonAsParticipant().toString(), RestUtils.getResponseHeaders(), return new ResponseEntity<>(token.toJsonAsParticipant().toString(), RestUtils.getResponseHeaders(),
HttpStatus.OK); HttpStatus.OK);
} catch (Exception e) { } catch (Exception e) {
return this.generateErrorResponse( return SessionRestController.generateErrorResponse(
"Error creating Connection for session " + session.getSessionId() + ": " + e.getMessage(), "Error creating Connection for session " + session.getSessionId() + ": " + e.getMessage(),
REQUEST_PATH, HttpStatus.INTERNAL_SERVER_ERROR); REQUEST_PATH, HttpStatus.INTERNAL_SERVER_ERROR);
} finally { } finally {
@ -729,7 +729,7 @@ public class SessionRestController {
} else { } else {
log.error("Session {} is in the process of closing. Connection couldn't be created", log.error("Session {} is in the process of closing. Connection couldn't be created",
session.getSessionId()); session.getSessionId());
return this.generateErrorResponse("Session " + session.getSessionId() + " not found", REQUEST_PATH, return SessionRestController.generateErrorResponse("Session " + session.getSessionId() + " not found", REQUEST_PATH,
HttpStatus.NOT_FOUND); HttpStatus.NOT_FOUND);
} }
} }
@ -761,10 +761,10 @@ public class SessionRestController {
return new ResponseEntity<>(ipcamParticipant.toJson().toString(), RestUtils.getResponseHeaders(), return new ResponseEntity<>(ipcamParticipant.toJson().toString(), RestUtils.getResponseHeaders(),
HttpStatus.OK); HttpStatus.OK);
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
return this.generateErrorResponse("\"rtspUri\" parameter is not a valid rtsp uri", REQUEST_PATH, return SessionRestController.generateErrorResponse("\"rtspUri\" parameter is not a valid rtsp uri", REQUEST_PATH,
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} catch (Exception e) { } catch (Exception e) {
return this.generateErrorResponse(e.getMessage(), REQUEST_PATH, HttpStatus.INTERNAL_SERVER_ERROR); return SessionRestController.generateErrorResponse(e.getMessage(), REQUEST_PATH, HttpStatus.INTERNAL_SERVER_ERROR);
} finally { } finally {
session.closingLock.readLock().unlock(); session.closingLock.readLock().unlock();
} }
@ -872,7 +872,7 @@ public class SessionRestController {
return token; return token;
} }
protected ResponseEntity<String> generateErrorResponse(String errorMessage, String path, HttpStatus status) { public static ResponseEntity<String> generateErrorResponse(String errorMessage, String path, HttpStatus status) {
JsonObject responseJson = new JsonObject(); JsonObject responseJson = new JsonObject();
responseJson.addProperty("timestamp", System.currentTimeMillis()); responseJson.addProperty("timestamp", System.currentTimeMillis());
responseJson.addProperty("status", status.value()); responseJson.addProperty("status", status.value());