openvidu-server: Session and Token objects REST API

pull/550/head
pabloFuente 2020-10-08 14:33:14 +02:00
parent 3499b97301
commit c24606aca7
9 changed files with 75 additions and 60 deletions

View File

@ -232,6 +232,7 @@ public class Participant {
public JsonObject toJson() {
JsonObject json = new JsonObject();
json.addProperty("connectionId", this.participantPublicId);
json.addProperty("session", this.sessionId);
json.addProperty("createdAt", this.createdAt);
json.addProperty("location", this.location != null ? this.location.toString() : "unknown");
json.addProperty("platform", this.platform);

View File

@ -175,7 +175,7 @@ public class Session implements SessionInterface {
Iterator<Entry<String, Token>> iterator = this.tokens.entrySet().iterator();
while (iterator.hasNext() && !deleted) {
Entry<String, Token> entry = iterator.next();
if (connectionId.equals(entry.getValue().getConnetionId())) {
if (connectionId.equals(entry.getValue().getConnectionId())) {
iterator.remove();
deleted = true;
}
@ -225,6 +225,7 @@ public class Session implements SessionInterface {
private JsonObject sharedJson(Function<KurentoParticipant, JsonObject> toJsonFunction) {
JsonObject json = new JsonObject();
json.addProperty("sessionId", this.sessionId);
json.addProperty("id", this.sessionId); // TODO: deprecated. Better use only "sessionId"
json.addProperty("createdAt", this.startTime);
json.addProperty("mediaMode", this.sessionProperties.mediaMode().name());
json.addProperty("recordingMode", this.sessionProperties.recordingMode().name());

View File

@ -308,7 +308,8 @@ public abstract class SessionManager {
}
public Token newTokenForInsecureUser(Session session, String token, String serverMetadata) throws Exception {
Token tokenObj = new Token(token, OpenViduRole.PUBLISHER, serverMetadata != null ? serverMetadata : "", true,
Token tokenObj = new Token(token, session.getSessionId(), OpenViduRole.PUBLISHER,
serverMetadata != null ? serverMetadata : "", true,
this.openviduConfig.isTurnadminAvailable() ? this.coturnCredentialsService.createUser() : null, null);
session.storeToken(tokenObj);
session.showTokens("Token created for insecure user");
@ -357,7 +358,7 @@ public abstract class SessionManager {
if (this.sessionidParticipantpublicidParticipant.get(sessionId) != null) {
Participant p = new Participant(finalUserId, participantPrivatetId, token.getConnetionId(), sessionId,
Participant p = new Participant(finalUserId, participantPrivatetId, token.getConnectionId(), sessionId,
token, clientMetadata, location, platform, EndpointType.WEBRTC_ENDPOINT, null);
this.sessionidParticipantpublicidParticipant.get(sessionId).put(p.getParticipantPublicId(), p);

View File

@ -19,6 +19,8 @@ package io.openvidu.server.core;
import org.apache.commons.lang3.RandomStringUtils;
import com.google.gson.JsonObject;
import io.openvidu.java.client.OpenViduRole;
import io.openvidu.server.coturn.TurnCredentials;
import io.openvidu.server.kurento.core.KurentoTokenOptions;
@ -26,6 +28,7 @@ import io.openvidu.server.kurento.core.KurentoTokenOptions;
public class Token {
private String token;
private String sessionId;
private OpenViduRole role;
private String serverMetadata = "";
private boolean record;
@ -35,9 +38,10 @@ public class Token {
private final String connectionId = IdentifierPrefixes.PARTICIPANT_PUBLIC_ID
+ RandomStringUtils.randomAlphabetic(1).toUpperCase() + RandomStringUtils.randomAlphanumeric(9);
public Token(String token, OpenViduRole role, String serverMetadata, boolean record,
public Token(String token, String sessionId, OpenViduRole role, String serverMetadata, boolean record,
TurnCredentials turnCredentials, KurentoTokenOptions kurentoTokenOptions) {
this.token = token;
this.sessionId = sessionId;
this.role = role;
this.serverMetadata = serverMetadata;
this.record = record;
@ -73,7 +77,7 @@ public class Token {
return kurentoTokenOptions;
}
public String getConnetionId() {
public String getConnectionId() {
return connectionId;
}
@ -85,6 +89,21 @@ public class Token {
this.record = record;
}
public JsonObject toJson() {
JsonObject json = new JsonObject();
json.addProperty("token", this.getToken());
json.addProperty("id", this.getToken());
json.addProperty("connectionId", this.getConnectionId());
json.addProperty("session", this.sessionId);
json.addProperty("role", this.getRole().toString());
json.addProperty("data", this.getServerMetadata());
json.addProperty("record", this.record());
if (this.getKurentoTokenOptions() != null) {
json.add("kurentoOptions", this.getKurentoTokenOptions().toJson());
}
return json;
}
@Override
public String toString() {
if (this.role != null)

View File

@ -56,6 +56,6 @@ public class TokenGenerator {
token += "&turnCredential=" + turnCredentials.getCredential();
}
}
return new Token(token, role, serverMetadata, record, turnCredentials, kurentoTokenOptions);
return new Token(token, sessionId, role, serverMetadata, record, turnCredentials, kurentoTokenOptions);
}
}

View File

@ -79,4 +79,29 @@ public class KurentoTokenOptions {
return this.allowedFilters.containsKey(filterType);
}
public JsonObject toJson() {
JsonObject json = new JsonObject();
if (this.getVideoMaxRecvBandwidth() != null) {
json.addProperty("videoMaxRecvBandwidth", this.getVideoMaxRecvBandwidth());
}
if (this.getVideoMinRecvBandwidth() != null) {
json.addProperty("videoMinRecvBandwidth", this.getVideoMinRecvBandwidth());
}
if (this.getVideoMaxSendBandwidth() != null) {
json.addProperty("videoMaxSendBandwidth", this.getVideoMaxSendBandwidth());
}
if (this.getVideoMinSendBandwidth() != null) {
json.addProperty("videoMinSendBandwidth", this.getVideoMinSendBandwidth());
}
if (this.getAllowedFilters().length > 0) {
JsonArray filtersJson = new JsonArray();
String[] filters = this.getAllowedFilters();
for (String filter : filters) {
filtersJson.add(filter);
}
json.add("allowedFilters", filtersJson);
}
return json;
}
}

View File

@ -176,11 +176,9 @@ public class SessionRestController {
Session sessionNotActive = sessionManager.storeSessionNotActive(sessionId, sessionProperties);
log.info("New session {} initialized {}", sessionId, this.sessionManager.getSessionsWithNotActive().stream()
.map(Session::getSessionId).collect(Collectors.toList()).toString());
JsonObject responseJson = new JsonObject();
responseJson.addProperty("id", sessionNotActive.getSessionId());
responseJson.addProperty("createdAt", sessionNotActive.getStartTime());
return new ResponseEntity<>(responseJson.toString(), RestUtils.getResponseHeaders(), HttpStatus.OK);
return new ResponseEntity<>(sessionNotActive.toJson().toString(), RestUtils.getResponseHeaders(),
HttpStatus.OK);
}
@RequestMapping(value = "/sessions/{sessionId}", method = RequestMethod.GET)
@ -396,44 +394,7 @@ public class SessionRestController {
if (session.closingLock.readLock().tryLock()) {
try {
Token token = sessionManager.newToken(session, role, metadata, record, kurentoTokenOptions);
JsonObject responseJson = new JsonObject();
responseJson.addProperty("id", token.getToken());
responseJson.addProperty("connectionId", token.getConnetionId());
responseJson.addProperty("session", sessionId);
responseJson.addProperty("role", role.toString());
responseJson.addProperty("data", metadata);
responseJson.addProperty("record", record);
responseJson.addProperty("token", token.getToken());
if (kurentoOptions != null) {
JsonObject kurentoOptsResponse = new JsonObject();
if (kurentoTokenOptions.getVideoMaxRecvBandwidth() != null) {
kurentoOptsResponse.addProperty("videoMaxRecvBandwidth",
kurentoTokenOptions.getVideoMaxRecvBandwidth());
}
if (kurentoTokenOptions.getVideoMinRecvBandwidth() != null) {
kurentoOptsResponse.addProperty("videoMinRecvBandwidth",
kurentoTokenOptions.getVideoMinRecvBandwidth());
}
if (kurentoTokenOptions.getVideoMaxSendBandwidth() != null) {
kurentoOptsResponse.addProperty("videoMaxSendBandwidth",
kurentoTokenOptions.getVideoMaxSendBandwidth());
}
if (kurentoTokenOptions.getVideoMinSendBandwidth() != null) {
kurentoOptsResponse.addProperty("videoMinSendBandwidth",
kurentoTokenOptions.getVideoMinSendBandwidth());
}
if (kurentoTokenOptions.getAllowedFilters().length > 0) {
JsonArray filters = new JsonArray();
for (String filter : kurentoTokenOptions.getAllowedFilters()) {
filters.add(filter);
}
kurentoOptsResponse.add("allowedFilters", filters);
}
responseJson.add("kurentoOptions", kurentoOptsResponse);
}
return new ResponseEntity<>(responseJson.toString(), RestUtils.getResponseHeaders(), HttpStatus.OK);
return new ResponseEntity<>(token.toJson().toString(), RestUtils.getResponseHeaders(), HttpStatus.OK);
} catch (Exception e) {
return this.generateErrorResponse(
"Error generating token for session " + sessionId + ": " + e.getMessage(), "/tokens",

View File

@ -115,7 +115,7 @@ public class SessionGarbageCollectorIntegrationTest {
}
private void joinParticipant(String sessionId, String token) {
Token t = new Token(token, OpenViduRole.PUBLISHER, "SERVER_METADATA", true, null, null);
Token t = new Token(token, sessionId, OpenViduRole.PUBLISHER, "SERVER_METADATA", true, null, null);
String uuid = UUID.randomUUID().toString();
String participantPrivateId = "PARTICIPANT_PRIVATE_ID_" + uuid;
String finalUserId = "FINAL_USER_ID_" + uuid;

View File

@ -2520,6 +2520,8 @@ public class OpenViduTestAppE2eTest extends AbstractOpenViduTestAppE2eTest {
log.info("REST API test");
final String DEFAULT_JSON_SESSION = "{'sessionId':'STR','id':'STR','createdAt':0,'mediaMode':'STR','recordingMode':'STR','defaultOutputMode':'STR','defaultRecordingLayout':'STR','customSessionId':'STR','connections':{'numberOfElements':0,'content':[]},'recording':false}";
CustomHttpClient restClient = new CustomHttpClient(OPENVIDU_URL, "OPENVIDUAPP", OPENVIDU_SECRET);
// 401
@ -2550,11 +2552,10 @@ public class OpenViduTestAppE2eTest extends AbstractOpenViduTestAppE2eTest {
// 200
body = "{'mediaMode': 'ROUTED', 'recordingMode': 'MANUAL', 'customSessionId': 'CUSTOM_SESSION_ID', 'defaultOutputMode': 'COMPOSED', 'defaultRecordingLayout': 'BEST_FIT'}";
restClient.rest(HttpMethod.POST, "/openvidu/api/sessions", body, HttpStatus.SC_OK, true,
"{'id': 'STR', 'createdAt': 0}");
restClient.rest(HttpMethod.POST, "/openvidu/api/sessions", body, HttpStatus.SC_OK, true, DEFAULT_JSON_SESSION);
// Default values
JsonObject res = restClient.rest(HttpMethod.POST, "/openvidu/api/sessions", "{}", HttpStatus.SC_OK, true,
"{'id': 'STR', 'createdAt': 0}");
DEFAULT_JSON_SESSION);
restClient.rest(HttpMethod.DELETE, "/openvidu/api/sessions/" + res.get("id").getAsString(),
HttpStatus.SC_NO_CONTENT);
@ -2564,7 +2565,7 @@ public class OpenViduTestAppE2eTest extends AbstractOpenViduTestAppE2eTest {
/** GET /openvidu/api/sessions (after session created) **/
restClient.rest(HttpMethod.GET, "/openvidu/api/sessions/CUSTOM_SESSION_ID", null, HttpStatus.SC_OK, true,
"{'sessionId':'STR','createdAt':0,'mediaMode':'STR','recordingMode':'STR','defaultOutputMode':'STR','defaultRecordingLayout':'STR','customSessionId':'STR','connections':{'numberOfElements':0,'content':[]},'recording':true}");
DEFAULT_JSON_SESSION);
restClient.rest(HttpMethod.GET, "/openvidu/api/sessions", null, HttpStatus.SC_OK, true,
ImmutableMap.of("numberOfElements", new Integer(1), "content", new JsonArray()));
@ -2643,7 +2644,7 @@ public class OpenViduTestAppE2eTest extends AbstractOpenViduTestAppE2eTest {
// 409 (RELAYED media mode)
res = restClient.rest(HttpMethod.POST, "/openvidu/api/sessions", "{'mediaMode':'RELAYED'}", HttpStatus.SC_OK,
true, "{'id': 'STR', 'createdAt': 0}");
true, DEFAULT_JSON_SESSION);
body = "{'session':'" + res.get("id").getAsString() + "'}";
restClient.rest(HttpMethod.POST, "/openvidu/api/recordings/start", body, HttpStatus.SC_CONFLICT);
restClient.rest(HttpMethod.DELETE, "/openvidu/api/sessions/" + res.get("id").getAsString(),
@ -2737,7 +2738,7 @@ public class OpenViduTestAppE2eTest extends AbstractOpenViduTestAppE2eTest {
restClient.rest(HttpMethod.DELETE, "/openvidu/api/sessions/CUSTOM_SESSION_ID/stream/NOT_EXISTS",
HttpStatus.SC_NOT_FOUND);
res = restClient.rest(HttpMethod.GET, "/openvidu/api/sessions/CUSTOM_SESSION_ID", null, HttpStatus.SC_OK, true,
"{'sessionId':'STR','createdAt':0,'mediaMode':'STR','recordingMode':'STR','defaultOutputMode':'STR','defaultRecordingLayout':'STR','customSessionId':'STR','connections':{'numberOfElements':2,'content'"
"{'sessionId':'STR','id':'STR','createdAt':0,'mediaMode':'STR','recordingMode':'STR','defaultOutputMode':'STR','defaultRecordingLayout':'STR','customSessionId':'STR','connections':{'numberOfElements':2,'content'"
+ ":[{'connectionId':'STR','createdAt':0,'location':'STR','platform':'STR','token':'STR','role':'STR','serverData':'STR','clientData':'STR','publishers':["
+ "{'createdAt':0,'streamId':'STR','mediaOptions':{'hasAudio':false,'audioActive':false,'hasVideo':false,'videoActive':false,'typeOfVideo':'STR','frameRate':0,"
+ "'videoDimensions':'STR','filter':{}}}],'subscribers':[{'createdAt':0,'streamId':'STR','publisher':'STR'}]},{'connectionId':'STR','createdAt':0,'location':'STR',"
@ -2800,16 +2801,25 @@ public class OpenViduTestAppE2eTest extends AbstractOpenViduTestAppE2eTest {
final String tokenA = res.get("token").getAsString();
res = restClient.rest(HttpMethod.POST, "/openvidu/api/tokens", body, HttpStatus.SC_OK);
final String tokenBConnectionId = res.get("connectionId").getAsString();
final String tokenB = res.get("token").getAsString();
user.getDriver().findElement(By.id("one2one-btn")).click();
user.getDriver().findElement(By.id("session-settings-btn-0")).click();
Thread.sleep(1000);
// Set token 1
user.getDriver().findElement(By.id("session-settings-btn-0")).click();
Thread.sleep(1000);
tokenInput = user.getDriver().findElement(By.cssSelector("#custom-token-div input"));
tokenInput.clear();
tokenInput.sendKeys(tokenA);
user.getDriver().findElement(By.id("save-btn")).click();
Thread.sleep(1000);
// Set token 2
user.getDriver().findElement(By.id("session-settings-btn-1")).click();
Thread.sleep(1000);
tokenInput = user.getDriver().findElement(By.cssSelector("#custom-token-div input"));
tokenInput.clear();
tokenInput.sendKeys(tokenB);
user.getDriver().findElement(By.id("save-btn")).click();
Thread.sleep(1000);
@ -2835,9 +2845,6 @@ public class OpenViduTestAppE2eTest extends AbstractOpenViduTestAppE2eTest {
user.getDriver().findElement(By.cssSelector("#openvidu-instance-1 .join-btn")).sendKeys(Keys.ENTER);
user.getEventManager().waitUntilEventReaches("connectionCreated", 1);
user.getEventManager().waitUntilEventReaches("accessAllowed", 1);
user.getEventManager().waitUntilEventReaches("streamCreated", 1);
user.getEventManager().waitUntilEventReaches("streamPlaying", 1);
// connectionId should be equal to the one brought by the token
Assert.assertEquals("Wrong connectionId", tokenBConnectionId,