mirror of https://github.com/OpenVidu/openvidu.git
Move SessionProperties#fromJson from openvidu-server to openvidu-java-client
parent
caa9accf02
commit
a49e1818c0
|
@ -489,8 +489,10 @@ public class RecordingProperties {
|
||||||
}
|
}
|
||||||
if (json.has("outputMode")) {
|
if (json.has("outputMode")) {
|
||||||
outputModeAux = OutputMode.valueOf(json.get("outputMode").getAsString());
|
outputModeAux = OutputMode.valueOf(json.get("outputMode").getAsString());
|
||||||
builder.outputMode(outputModeAux);
|
} else {
|
||||||
|
outputModeAux = DefaultValues.outputMode;
|
||||||
}
|
}
|
||||||
|
builder.outputMode(outputModeAux);
|
||||||
if ((OutputMode.COMPOSED.equals(outputModeAux) || OutputMode.COMPOSED_QUICK_START.equals(outputModeAux))
|
if ((OutputMode.COMPOSED.equals(outputModeAux) || OutputMode.COMPOSED_QUICK_START.equals(outputModeAux))
|
||||||
&& hasVideoAux) {
|
&& hasVideoAux) {
|
||||||
if (json.has("recordingLayout")) {
|
if (json.has("recordingLayout")) {
|
||||||
|
|
|
@ -17,7 +17,11 @@
|
||||||
|
|
||||||
package io.openvidu.java.client;
|
package io.openvidu.java.client;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* See {@link io.openvidu.java.client.OpenVidu#createSession(SessionProperties)}
|
* See {@link io.openvidu.java.client.OpenVidu#createSession(SessionProperties)}
|
||||||
|
@ -290,4 +294,139 @@ public class SessionProperties {
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtain a {@link SessionProperties.Builder} directly from a JSON object in the
|
||||||
|
* form of a Map
|
||||||
|
*
|
||||||
|
* @return A {@link SessionProperties.Builder}
|
||||||
|
* @throws IllegalArgumentException If some parameter has a wrong type or an
|
||||||
|
* invalid value
|
||||||
|
*/
|
||||||
|
public static SessionProperties.Builder fromJson(Map<String, ?> params) throws IllegalArgumentException {
|
||||||
|
|
||||||
|
SessionProperties.Builder builder = new SessionProperties.Builder();
|
||||||
|
String customSessionId = null;
|
||||||
|
|
||||||
|
if (params != null) {
|
||||||
|
|
||||||
|
// Obtain primitive values from the params map
|
||||||
|
String mediaModeString;
|
||||||
|
String recordingModeString;
|
||||||
|
String forcedVideoCodecStr;
|
||||||
|
Boolean allowTranscoding;
|
||||||
|
try {
|
||||||
|
mediaModeString = (String) params.get("mediaMode");
|
||||||
|
recordingModeString = (String) params.get("recordingMode");
|
||||||
|
customSessionId = (String) params.get("customSessionId");
|
||||||
|
forcedVideoCodecStr = (String) params.get("forcedVideoCodec");
|
||||||
|
allowTranscoding = (Boolean) params.get("allowTranscoding");
|
||||||
|
} catch (ClassCastException e) {
|
||||||
|
throw new IllegalArgumentException("Type error in some parameter: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse obtained values into actual types
|
||||||
|
VideoCodec forcedVideoCodec = null;
|
||||||
|
try {
|
||||||
|
forcedVideoCodec = VideoCodec.valueOf(forcedVideoCodecStr);
|
||||||
|
} catch (NullPointerException e) {
|
||||||
|
// Not an error: "forcedVideoCodec" was not provided in params.
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
throw new IllegalArgumentException("Invalid value for parameter 'forcedVideoCodec': " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Safe parameter retrieval. Default values if not defined
|
||||||
|
if (recordingModeString != null) {
|
||||||
|
RecordingMode recordingMode = RecordingMode.valueOf(recordingModeString);
|
||||||
|
builder = builder.recordingMode(recordingMode);
|
||||||
|
} else {
|
||||||
|
builder = builder.recordingMode(RecordingMode.MANUAL);
|
||||||
|
}
|
||||||
|
if (mediaModeString != null) {
|
||||||
|
MediaMode mediaMode = MediaMode.valueOf(mediaModeString);
|
||||||
|
builder = builder.mediaMode(mediaMode);
|
||||||
|
} else {
|
||||||
|
builder = builder.mediaMode(MediaMode.ROUTED);
|
||||||
|
}
|
||||||
|
if (customSessionId != null && !customSessionId.isEmpty()) {
|
||||||
|
if (!isValidCustomSessionId(customSessionId)) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Parameter 'customSessionId' is wrong. Must be an alphanumeric string [a-zA-Z0-9_-]");
|
||||||
|
}
|
||||||
|
builder = builder.customSessionId(customSessionId);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (forcedVideoCodec != null) {
|
||||||
|
builder = builder.forcedVideoCodec(forcedVideoCodec);
|
||||||
|
builder = builder.forcedVideoCodecResolved(forcedVideoCodec);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (allowTranscoding != null) {
|
||||||
|
builder = builder.allowTranscoding(allowTranscoding);
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonObject defaultRecordingPropertiesJson = null;
|
||||||
|
if (params.get("defaultRecordingProperties") != null) {
|
||||||
|
try {
|
||||||
|
defaultRecordingPropertiesJson = new Gson()
|
||||||
|
.toJsonTree(params.get("defaultRecordingProperties"), Map.class).getAsJsonObject();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Error in parameter 'defaultRecordingProperties'. It is not a valid JSON object");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (defaultRecordingPropertiesJson != null) {
|
||||||
|
try {
|
||||||
|
RecordingProperties defaultRecordingProperties = RecordingProperties
|
||||||
|
.fromJson(defaultRecordingPropertiesJson);
|
||||||
|
builder = builder.defaultRecordingProperties(defaultRecordingProperties);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Parameter 'defaultRecordingProperties' is not valid: " + e.getMessage());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
builder.defaultRecordingProperties(new RecordingProperties.Builder().build());
|
||||||
|
}
|
||||||
|
|
||||||
|
String mediaNode = getMediaNodeProperty(params);
|
||||||
|
if (mediaNode != null) {
|
||||||
|
builder = builder.mediaNode(mediaNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
throw new IllegalArgumentException("Some parameter is not valid. " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @hidden
|
||||||
|
*/
|
||||||
|
public static String getMediaNodeProperty(Map<?, ?> params) throws IllegalArgumentException {
|
||||||
|
if (params.containsKey("mediaNode") && params.get("mediaNode") != null) {
|
||||||
|
JsonObject mediaNodeJson;
|
||||||
|
try {
|
||||||
|
mediaNodeJson = JsonParser.parseString(params.get("mediaNode").toString()).getAsJsonObject();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IllegalArgumentException("Error in parameter 'mediaNode'. It is not a valid JSON object");
|
||||||
|
}
|
||||||
|
if (!mediaNodeJson.has("id")) {
|
||||||
|
throw new IllegalArgumentException("Error in parameter 'mediaNode'. Property 'id' not found");
|
||||||
|
}
|
||||||
|
String mediaNode;
|
||||||
|
try {
|
||||||
|
mediaNode = mediaNodeJson.get("id").getAsString();
|
||||||
|
} catch (ClassCastException e) {
|
||||||
|
throw new IllegalArgumentException("Type error in parameter 'mediaNode.id': " + e.getMessage());
|
||||||
|
}
|
||||||
|
return mediaNode;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isValidCustomSessionId(String customSessionId) {
|
||||||
|
return customSessionId.matches("[a-zA-Z0-9_-]+");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,32 +7,27 @@ import junit.framework.TestSuite;
|
||||||
/**
|
/**
|
||||||
* Unit test for simple App.
|
* Unit test for simple App.
|
||||||
*/
|
*/
|
||||||
public class OpenViduTest
|
public class OpenViduTest extends TestCase {
|
||||||
extends TestCase
|
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* Create the test case
|
* Create the test case
|
||||||
*
|
*
|
||||||
* @param testName name of the test case
|
* @param testName name of the test case
|
||||||
*/
|
*/
|
||||||
public OpenViduTest( String testName )
|
public OpenViduTest(String testName) {
|
||||||
{
|
super(testName);
|
||||||
super( testName );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the suite of tests being tested
|
* @return the suite of tests being tested
|
||||||
*/
|
*/
|
||||||
public static Test suite()
|
public static Test suite() {
|
||||||
{
|
return new TestSuite(OpenViduTest.class);
|
||||||
return new TestSuite( OpenViduTest.class );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rigourous Test :-)
|
* Rigourous Test :-)
|
||||||
*/
|
*/
|
||||||
public void testApp()
|
public void testApp() {
|
||||||
{
|
assertTrue(true);
|
||||||
assertTrue( true );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
package io.openvidu.java.client.test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertThrows;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
import io.openvidu.java.client.SessionProperties;
|
||||||
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
public class SessionPropertiesTest extends TestCase {
|
||||||
|
|
||||||
|
public SessionPropertiesTest(String testName) {
|
||||||
|
super(testName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Test suite() {
|
||||||
|
return new TestSuite(SessionPropertiesTest.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testFromJsonSuccess() {
|
||||||
|
String jsonString = "{'customSessionId':'MY_CUSTOM_STRING','mediaMode':'ROUTED','recordingMode':'ALWAYS','defaultRecordingProperties':{'name':'MY_CUSTOM_STRING','hasAudio':false,'hasVideo':true,'outputMode':'COMPOSED_QUICK_START','recordingLayout':'BEST_FIT','resolution':'1920x1000','frameRate':25,'shmSize':536870911},'forcedVideoCodec':'VP8','allowTranscoding':true,'mediaNode':{'id':'MY_CUSTOM_STRING'}}";
|
||||||
|
JsonObject originalJson = new Gson().fromJson(jsonString, JsonObject.class);
|
||||||
|
Map<String, ?> map = mapFromJsonString(jsonString);
|
||||||
|
SessionProperties.Builder builder = SessionProperties.fromJson(map);
|
||||||
|
SessionProperties props = builder.build();
|
||||||
|
JsonObject finalJson = props.toJson();
|
||||||
|
assertEquals(originalJson, finalJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testFromJsonError() {
|
||||||
|
Map<String, ?> map = mapFromJsonString("{'mediaNode':'MY_CUSTOM_STRING'}");
|
||||||
|
assertException(map, "Error in parameter 'mediaNode'");
|
||||||
|
|
||||||
|
map = mapFromJsonString("{'customSessionId':'WRONG_CUSTOM_SESSION_ID?'}");
|
||||||
|
assertException(map, "Parameter 'customSessionId' is wrong");
|
||||||
|
|
||||||
|
// map = mapFromJsonString("{'defaultRecordingProperties':{'resolution':'2000x1000'}}");
|
||||||
|
// assertException(map, "Parameter 'customSessionId' is wrong");
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, ?> mapFromJsonString(String json) {
|
||||||
|
return new Gson().fromJson(json, Map.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertException(Map<String, ?> params, String containsError) {
|
||||||
|
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
|
||||||
|
() -> SessionProperties.fromJson(params));
|
||||||
|
assertTrue(exception.getMessage().contains(containsError));
|
||||||
|
}
|
||||||
|
}
|
|
@ -61,7 +61,6 @@ import io.openvidu.java.client.MediaMode;
|
||||||
import io.openvidu.java.client.OpenViduRole;
|
import io.openvidu.java.client.OpenViduRole;
|
||||||
import io.openvidu.java.client.Recording.OutputMode;
|
import io.openvidu.java.client.Recording.OutputMode;
|
||||||
import io.openvidu.java.client.RecordingLayout;
|
import io.openvidu.java.client.RecordingLayout;
|
||||||
import io.openvidu.java.client.RecordingMode;
|
|
||||||
import io.openvidu.java.client.RecordingProperties;
|
import io.openvidu.java.client.RecordingProperties;
|
||||||
import io.openvidu.java.client.SessionProperties;
|
import io.openvidu.java.client.SessionProperties;
|
||||||
import io.openvidu.java.client.VideoCodec;
|
import io.openvidu.java.client.VideoCodec;
|
||||||
|
@ -106,7 +105,7 @@ public class SessionRestController {
|
||||||
|
|
||||||
SessionProperties sessionProperties;
|
SessionProperties sessionProperties;
|
||||||
try {
|
try {
|
||||||
sessionProperties = getSessionPropertiesFromParams(params).build();
|
sessionProperties = getSessionPropertiesFromParams((Map<String, ?>) params).build();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return this.generateErrorResponse(e.getMessage(), "/sessions", HttpStatus.BAD_REQUEST);
|
return this.generateErrorResponse(e.getMessage(), "/sessions", HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
@ -780,22 +779,15 @@ public class SessionRestController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected SessionProperties.Builder getSessionPropertiesFromParams(Map<?, ?> params) throws Exception {
|
protected SessionProperties.Builder getSessionPropertiesFromParams(Map<String, ?> params) throws Exception {
|
||||||
|
|
||||||
SessionProperties.Builder builder = new SessionProperties.Builder();
|
SessionProperties.Builder builder = SessionProperties.fromJson(params);
|
||||||
String customSessionId = null;
|
|
||||||
|
|
||||||
if (params != null) {
|
if (params != null) {
|
||||||
|
|
||||||
// Obtain primitive values from the params map
|
|
||||||
String mediaModeString;
|
|
||||||
String recordingModeString;
|
|
||||||
String forcedVideoCodecStr;
|
String forcedVideoCodecStr;
|
||||||
Boolean allowTranscoding;
|
Boolean allowTranscoding;
|
||||||
try {
|
try {
|
||||||
mediaModeString = (String) params.get("mediaMode");
|
|
||||||
recordingModeString = (String) params.get("recordingMode");
|
|
||||||
customSessionId = (String) params.get("customSessionId");
|
|
||||||
forcedVideoCodecStr = (String) params.get("forcedVideoCodec");
|
forcedVideoCodecStr = (String) params.get("forcedVideoCodec");
|
||||||
allowTranscoding = (Boolean) params.get("allowTranscoding");
|
allowTranscoding = (Boolean) params.get("allowTranscoding");
|
||||||
} catch (ClassCastException e) {
|
} catch (ClassCastException e) {
|
||||||
|
@ -807,32 +799,12 @@ public class SessionRestController {
|
||||||
try {
|
try {
|
||||||
forcedVideoCodec = VideoCodec.valueOf(forcedVideoCodecStr);
|
forcedVideoCodec = VideoCodec.valueOf(forcedVideoCodecStr);
|
||||||
} catch (NullPointerException e) {
|
} catch (NullPointerException e) {
|
||||||
// Not an error: "forcedVideoCodec" was not provided in params.
|
// Not an error: "forcedVideoCodec" was not provided in params
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
throw new Exception("Invalid value for parameter 'forcedVideoCodec': " + e.getMessage());
|
throw new Exception("Invalid value for parameter 'forcedVideoCodec': " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Safe parameter retrieval. Default values if not defined
|
|
||||||
if (recordingModeString != null) {
|
|
||||||
RecordingMode recordingMode = RecordingMode.valueOf(recordingModeString);
|
|
||||||
builder = builder.recordingMode(recordingMode);
|
|
||||||
} else {
|
|
||||||
builder = builder.recordingMode(RecordingMode.MANUAL);
|
|
||||||
}
|
|
||||||
if (mediaModeString != null) {
|
|
||||||
MediaMode mediaMode = MediaMode.valueOf(mediaModeString);
|
|
||||||
builder = builder.mediaMode(mediaMode);
|
|
||||||
} else {
|
|
||||||
builder = builder.mediaMode(MediaMode.ROUTED);
|
|
||||||
}
|
|
||||||
if (customSessionId != null && !customSessionId.isEmpty()) {
|
|
||||||
if (!sessionManager.formatChecker.isValidCustomSessionId(customSessionId)) {
|
|
||||||
throw new Exception(
|
|
||||||
"Parameter 'customSessionId' is wrong. Must be an alphanumeric string [a-zA-Z0-9_-]");
|
|
||||||
}
|
|
||||||
builder = builder.customSessionId(customSessionId);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (forcedVideoCodec == null) {
|
if (forcedVideoCodec == null) {
|
||||||
forcedVideoCodec = openviduConfig.getOpenviduForcedCodec();
|
forcedVideoCodec = openviduConfig.getOpenviduForcedCodec();
|
||||||
|
@ -858,28 +830,6 @@ public class SessionRestController {
|
||||||
builder = builder.allowTranscoding(openviduConfig.isOpenviduAllowingTranscoding());
|
builder = builder.allowTranscoding(openviduConfig.isOpenviduAllowingTranscoding());
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObject defaultRecordingPropertiesJson = null;
|
|
||||||
if (params.get("defaultRecordingProperties") != null) {
|
|
||||||
try {
|
|
||||||
defaultRecordingPropertiesJson = new Gson()
|
|
||||||
.toJsonTree(params.get("defaultRecordingProperties"), Map.class).getAsJsonObject();
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new Exception(
|
|
||||||
"Error in parameter 'defaultRecordingProperties'. It is not a valid JSON object");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (defaultRecordingPropertiesJson != null) {
|
|
||||||
try {
|
|
||||||
RecordingProperties defaultRecordingProperties = RecordingProperties
|
|
||||||
.fromJson(defaultRecordingPropertiesJson);
|
|
||||||
builder = builder.defaultRecordingProperties(defaultRecordingProperties);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new Exception("Parameter 'defaultRecordingProperties' is not valid: " + e.getMessage());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
builder.defaultRecordingProperties(new RecordingProperties.Builder().build());
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
throw new Exception("Some parameter is not valid. " + e.getMessage());
|
throw new Exception("Some parameter is not valid. " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue