mirror of https://github.com/OpenVidu/openvidu.git
openvidu-java-client: async call from getSessionId to createSession. OpenViduException simplified
parent
ac09441c1d
commit
5fd6db8994
|
@ -18,6 +18,7 @@
|
||||||
package io.openvidu.java.client;
|
package io.openvidu.java.client;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.security.KeyManagementException;
|
import java.security.KeyManagementException;
|
||||||
import java.security.KeyStoreException;
|
import java.security.KeyStoreException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
@ -49,8 +50,6 @@ import org.json.simple.JSONObject;
|
||||||
import org.json.simple.parser.JSONParser;
|
import org.json.simple.parser.JSONParser;
|
||||||
import org.json.simple.parser.ParseException;
|
import org.json.simple.parser.ParseException;
|
||||||
|
|
||||||
import io.openvidu.java.client.OpenViduException.Code;
|
|
||||||
|
|
||||||
public class OpenVidu {
|
public class OpenVidu {
|
||||||
|
|
||||||
private String urlOpenViduServer;
|
private String urlOpenViduServer;
|
||||||
|
@ -102,12 +101,29 @@ public class OpenVidu {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Session createSession() throws OpenViduException {
|
/**
|
||||||
|
* Creates an OpenVidu session with the default settings
|
||||||
|
*
|
||||||
|
* @return The created session
|
||||||
|
*
|
||||||
|
* @throws OpenViduJavaClientException
|
||||||
|
*/
|
||||||
|
public Session createSession() throws OpenViduJavaClientException {
|
||||||
Session s = new Session(myHttpClient, urlOpenViduServer);
|
Session s = new Session(myHttpClient, urlOpenViduServer);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Session createSession(SessionProperties properties) throws OpenViduException {
|
/**
|
||||||
|
* Creates an OpenVidu session
|
||||||
|
*
|
||||||
|
* @param properties
|
||||||
|
* The specific configuration for this session
|
||||||
|
*
|
||||||
|
* @return The created session
|
||||||
|
*
|
||||||
|
* @throws OpenViduJavaClientException
|
||||||
|
*/
|
||||||
|
public Session createSession(SessionProperties properties) throws OpenViduJavaClientException {
|
||||||
Session s = new Session(myHttpClient, urlOpenViduServer, properties);
|
Session s = new Session(myHttpClient, urlOpenViduServer, properties);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
@ -125,34 +141,42 @@ public class OpenVidu {
|
||||||
* The configuration for this recording
|
* The configuration for this recording
|
||||||
*
|
*
|
||||||
* @return The new created session
|
* @return The new created session
|
||||||
|
*
|
||||||
|
* @throws OpenViduJavaClientException
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public Recording startRecording(String sessionId, RecordingProperties properties) throws OpenViduException {
|
public Recording startRecording(String sessionId, RecordingProperties properties)
|
||||||
try {
|
throws OpenViduJavaClientException {
|
||||||
|
|
||||||
HttpPost request = new HttpPost(this.urlOpenViduServer + API_RECORDINGS + API_RECORDINGS_START);
|
HttpPost request = new HttpPost(this.urlOpenViduServer + API_RECORDINGS + API_RECORDINGS_START);
|
||||||
|
|
||||||
JSONObject json = new JSONObject();
|
JSONObject json = new JSONObject();
|
||||||
json.put("session", sessionId);
|
json.put("session", sessionId);
|
||||||
json.put("name", properties.name());
|
json.put("name", properties.name());
|
||||||
json.put("recordingLayout",
|
json.put("recordingLayout", (properties.recordingLayout() != null) ? properties.recordingLayout().name() : "");
|
||||||
(properties.recordingLayout() != null) ? properties.recordingLayout().name() : "");
|
|
||||||
json.put("customLayout", (properties.customLayout() != null) ? properties.customLayout() : "");
|
json.put("customLayout", (properties.customLayout() != null) ? properties.customLayout() : "");
|
||||||
StringEntity params = new StringEntity(json.toString());
|
StringEntity params = null;
|
||||||
|
try {
|
||||||
|
params = new StringEntity(json.toString());
|
||||||
|
} catch (UnsupportedEncodingException e1) {
|
||||||
|
throw new OpenViduJavaClientException(e1.getMessage(), e1.getCause());
|
||||||
|
}
|
||||||
|
|
||||||
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||||
request.setEntity(params);
|
request.setEntity(params);
|
||||||
|
|
||||||
HttpResponse response = myHttpClient.execute(request);
|
HttpResponse response;
|
||||||
|
try {
|
||||||
|
response = myHttpClient.execute(request);
|
||||||
|
} catch (IOException e2) {
|
||||||
|
throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause());
|
||||||
|
}
|
||||||
|
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||||
return new Recording(httpResponseToJson(response));
|
return new Recording(httpResponseToJson(response));
|
||||||
} else {
|
} else {
|
||||||
throw new OpenViduException(Code.RECORDING_START_ERROR_CODE, Integer.toString(statusCode));
|
throw new OpenViduJavaClientException("Unexpected response from OpenVidu Server: " + statusCode);
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new OpenViduException(Code.RECORDING_START_ERROR_CODE,
|
|
||||||
"Unable to start recording for session '" + sessionId + "': " + e.getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,8 +193,10 @@ public class OpenVidu {
|
||||||
* @return The started recording. If this method successfully returns the
|
* @return The started recording. If this method successfully returns the
|
||||||
* Recording object it means that the recording can be stopped with
|
* Recording object it means that the recording can be stopped with
|
||||||
* guarantees
|
* guarantees
|
||||||
|
*
|
||||||
|
* @throws OpenViduJavaClientException
|
||||||
*/
|
*/
|
||||||
public Recording startRecording(String sessionId, String name) throws OpenViduException {
|
public Recording startRecording(String sessionId, String name) throws OpenViduJavaClientException {
|
||||||
if (name == null) {
|
if (name == null) {
|
||||||
name = "";
|
name = "";
|
||||||
}
|
}
|
||||||
|
@ -186,8 +212,10 @@ public class OpenVidu {
|
||||||
* @return The started recording. If this method successfully returns the
|
* @return The started recording. If this method successfully returns the
|
||||||
* Recording object it means that the recording can be stopped with
|
* Recording object it means that the recording can be stopped with
|
||||||
* guarantees
|
* guarantees
|
||||||
|
*
|
||||||
|
* @throws OpenViduJavaClientException
|
||||||
*/
|
*/
|
||||||
public Recording startRecording(String sessionId) throws OpenViduException {
|
public Recording startRecording(String sessionId) throws OpenViduJavaClientException {
|
||||||
return this.startRecording(sessionId, "");
|
return this.startRecording(sessionId, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -198,22 +226,24 @@ public class OpenVidu {
|
||||||
* The id property of the recording you want to stop
|
* The id property of the recording you want to stop
|
||||||
*
|
*
|
||||||
* @return The stopped recording
|
* @return The stopped recording
|
||||||
|
*
|
||||||
|
* @throws OpenViduJavaClientException
|
||||||
*/
|
*/
|
||||||
public Recording stopRecording(String recordingId) throws OpenViduException {
|
public Recording stopRecording(String recordingId) throws OpenViduJavaClientException {
|
||||||
try {
|
|
||||||
HttpPost request = new HttpPost(
|
HttpPost request = new HttpPost(
|
||||||
this.urlOpenViduServer + API_RECORDINGS + API_RECORDINGS_STOP + "/" + recordingId);
|
this.urlOpenViduServer + API_RECORDINGS + API_RECORDINGS_STOP + "/" + recordingId);
|
||||||
HttpResponse response = myHttpClient.execute(request);
|
HttpResponse response;
|
||||||
|
try {
|
||||||
|
response = myHttpClient.execute(request);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||||
|
}
|
||||||
|
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||||
return new Recording(httpResponseToJson(response));
|
return new Recording(httpResponseToJson(response));
|
||||||
} else {
|
} else {
|
||||||
throw new OpenViduException(Code.RECORDING_STOP_ERROR_CODE, Integer.toString(statusCode));
|
throw new OpenViduJavaClientException("Unexpected response from OpenVidu Server: " + statusCode);
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new OpenViduException(Code.RECORDING_STOP_ERROR_CODE,
|
|
||||||
"Unable to stop recording '" + recordingId + "': " + e.getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -222,21 +252,23 @@ public class OpenVidu {
|
||||||
*
|
*
|
||||||
* @param recordingId
|
* @param recordingId
|
||||||
* The id property of the recording you want to retrieve
|
* The id property of the recording you want to retrieve
|
||||||
|
*
|
||||||
|
* @throws OpenViduJavaClientException
|
||||||
*/
|
*/
|
||||||
public Recording getRecording(String recordingId) throws OpenViduException {
|
public Recording getRecording(String recordingId) throws OpenViduJavaClientException {
|
||||||
try {
|
|
||||||
HttpGet request = new HttpGet(this.urlOpenViduServer + API_RECORDINGS + "/" + recordingId);
|
HttpGet request = new HttpGet(this.urlOpenViduServer + API_RECORDINGS + "/" + recordingId);
|
||||||
HttpResponse response = myHttpClient.execute(request);
|
HttpResponse response;
|
||||||
|
try {
|
||||||
|
response = myHttpClient.execute(request);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||||
|
}
|
||||||
|
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||||
return new Recording(httpResponseToJson(response));
|
return new Recording(httpResponseToJson(response));
|
||||||
} else {
|
} else {
|
||||||
throw new OpenViduException(Code.RECORDING_LIST_ERROR_CODE, Integer.toString(statusCode));
|
throw new OpenViduJavaClientException("Unexpected response from OpenVidu Server: " + statusCode);
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new OpenViduException(Code.RECORDING_LIST_ERROR_CODE,
|
|
||||||
"Unable to get recording '" + recordingId + "': " + e.getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -244,12 +276,18 @@ public class OpenVidu {
|
||||||
* Lists all existing recordings
|
* Lists all existing recordings
|
||||||
*
|
*
|
||||||
* @return A {@link java.util.List} with all existing recordings
|
* @return A {@link java.util.List} with all existing recordings
|
||||||
|
*
|
||||||
|
* @throws OpenViduJavaClientException
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public List<Recording> listRecordings() throws OpenViduException {
|
public List<Recording> listRecordings() throws OpenViduJavaClientException {
|
||||||
try {
|
|
||||||
HttpGet request = new HttpGet(this.urlOpenViduServer + API_RECORDINGS);
|
HttpGet request = new HttpGet(this.urlOpenViduServer + API_RECORDINGS);
|
||||||
HttpResponse response = myHttpClient.execute(request);
|
HttpResponse response;
|
||||||
|
try {
|
||||||
|
response = myHttpClient.execute(request);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||||
|
}
|
||||||
|
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||||
|
@ -261,10 +299,7 @@ public class OpenVidu {
|
||||||
});
|
});
|
||||||
return recordings;
|
return recordings;
|
||||||
} else {
|
} else {
|
||||||
throw new OpenViduException(Code.RECORDING_LIST_ERROR_CODE, Integer.toString(statusCode));
|
throw new OpenViduJavaClientException("Unexpected response from OpenVidu Server: " + statusCode);
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new OpenViduException(Code.RECORDING_LIST_ERROR_CODE, "Unable to list recordings: " + e.getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -274,25 +309,33 @@ public class OpenVidu {
|
||||||
* {@link io.openvidu.java.client.Recording.Status#available}
|
* {@link io.openvidu.java.client.Recording.Status#available}
|
||||||
*
|
*
|
||||||
* @param recordingId
|
* @param recordingId
|
||||||
|
*
|
||||||
|
* @throws OpenViduJavaClientException
|
||||||
*/
|
*/
|
||||||
public void deleteRecording(String recordingId) throws OpenViduException {
|
public void deleteRecording(String recordingId) throws OpenViduJavaClientException {
|
||||||
try {
|
|
||||||
HttpDelete request = new HttpDelete(this.urlOpenViduServer + API_RECORDINGS + "/" + recordingId);
|
HttpDelete request = new HttpDelete(this.urlOpenViduServer + API_RECORDINGS + "/" + recordingId);
|
||||||
HttpResponse response = myHttpClient.execute(request);
|
HttpResponse response;
|
||||||
|
try {
|
||||||
|
response = myHttpClient.execute(request);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||||
|
}
|
||||||
|
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
if (!(statusCode == org.apache.http.HttpStatus.SC_NO_CONTENT)) {
|
if (!(statusCode == org.apache.http.HttpStatus.SC_NO_CONTENT)) {
|
||||||
throw new OpenViduException(Code.RECORDING_DELETE_ERROR_CODE, Integer.toString(statusCode));
|
throw new OpenViduJavaClientException("Unexpected response from OpenVidu Server: " + statusCode);
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new OpenViduException(Code.RECORDING_DELETE_ERROR_CODE,
|
|
||||||
"Unable to delete recording '" + recordingId + "': " + e.getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private JSONObject httpResponseToJson(HttpResponse response) throws ParseException, IOException {
|
private JSONObject httpResponseToJson(HttpResponse response) throws OpenViduJavaClientException {
|
||||||
JSONParser parser = new JSONParser();
|
JSONParser parser = new JSONParser();
|
||||||
return (JSONObject) parser.parse(EntityUtils.toString(response.getEntity()));
|
JSONObject json;
|
||||||
|
try {
|
||||||
|
json = (JSONObject) parser.parse(EntityUtils.toString(response.getEntity()));
|
||||||
|
} catch (org.apache.http.ParseException | ParseException | IOException e) {
|
||||||
|
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||||
|
}
|
||||||
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,81 +0,0 @@
|
||||||
/*
|
|
||||||
* (C) Copyright 2017-2018 OpenVidu (http://openvidu.io/)
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
package io.openvidu.java.client;
|
|
||||||
|
|
||||||
public class OpenViduException extends RuntimeException {
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
public static enum Code {
|
|
||||||
GENERIC_ERROR_CODE(999),
|
|
||||||
|
|
||||||
TRANSPORT_ERROR_CODE(803), TRANSPORT_RESPONSE_ERROR_CODE(802), TRANSPORT_REQUEST_ERROR_CODE(801),
|
|
||||||
|
|
||||||
MEDIA_MUTE_ERROR_CODE(307), MEDIA_NOT_A_WEB_ENDPOINT_ERROR_CODE(306), MEDIA_RTP_ENDPOINT_ERROR_CODE(
|
|
||||||
305), MEDIA_WEBRTC_ENDPOINT_ERROR_CODE(
|
|
||||||
304), MEDIA_ENDPOINT_ERROR_CODE(303), MEDIA_SDP_ERROR_CODE(302), MEDIA_GENERIC_ERROR_CODE(301),
|
|
||||||
|
|
||||||
ROOM_CANNOT_BE_CREATED_ERROR_CODE(204), ROOM_CLOSED_ERROR_CODE(203), ROOM_NOT_FOUND_ERROR_CODE(
|
|
||||||
202), ROOM_GENERIC_ERROR_CODE(201),
|
|
||||||
|
|
||||||
USER_NOT_STREAMING_ERROR_CODE(105), EXISTING_USER_IN_ROOM_ERROR_CODE(104), USER_CLOSED_ERROR_CODE(
|
|
||||||
103), USER_NOT_FOUND_ERROR_CODE(102), USER_GENERIC_ERROR_CODE(101),
|
|
||||||
|
|
||||||
USER_UNAUTHORIZED_ERROR_CODE(401), ROLE_NOT_FOUND_ERROR_CODE(402), SESSIONID_CANNOT_BE_CREATED_ERROR_CODE(
|
|
||||||
403), TOKEN_CANNOT_BE_CREATED_ERROR_CODE(404),
|
|
||||||
|
|
||||||
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),
|
|
||||||
|
|
||||||
RECORDING_FILE_EMPTY_ERROR(707), RECORDING_DELETE_ERROR_CODE(706), RECORDING_LIST_ERROR_CODE(
|
|
||||||
705), RECORDING_STOP_ERROR_CODE(704), RECORDING_START_ERROR_CODE(
|
|
||||||
703), RECORDING_REPORT_ERROR_CODE(702), RECORDING_COMPLETION_ERROR_CODE(701);
|
|
||||||
|
|
||||||
private int value;
|
|
||||||
|
|
||||||
private Code(int value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getValue() {
|
|
||||||
return this.value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Code code = Code.GENERIC_ERROR_CODE;
|
|
||||||
|
|
||||||
public OpenViduException(Code code, String message) {
|
|
||||||
super(message);
|
|
||||||
this.code = code;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Code getCode() {
|
|
||||||
return code;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getCodeValue() {
|
|
||||||
return code.getValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Code: " + getCodeValue() + " " + super.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
/*
|
||||||
|
* (C) Copyright 2017-2018 OpenVidu (http://openvidu.io/)
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.openvidu.java.client;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines unexpected internal errors in OpenVidu Java Client
|
||||||
|
*/
|
||||||
|
public class OpenViduJavaClientException extends Exception {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public OpenViduJavaClientException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public OpenViduJavaClientException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -18,6 +18,7 @@
|
||||||
package io.openvidu.java.client;
|
package io.openvidu.java.client;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
import org.apache.http.HttpHeaders;
|
import org.apache.http.HttpHeaders;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
|
@ -29,8 +30,6 @@ import org.json.simple.JSONObject;
|
||||||
import org.json.simple.parser.JSONParser;
|
import org.json.simple.parser.JSONParser;
|
||||||
import org.json.simple.parser.ParseException;
|
import org.json.simple.parser.ParseException;
|
||||||
|
|
||||||
import io.openvidu.java.client.OpenViduException.Code;
|
|
||||||
|
|
||||||
public class Session {
|
public class Session {
|
||||||
|
|
||||||
private HttpClient httpClient;
|
private HttpClient httpClient;
|
||||||
|
@ -41,73 +40,40 @@ public class Session {
|
||||||
final static String API_SESSIONS = "api/sessions";
|
final static String API_SESSIONS = "api/sessions";
|
||||||
final static String API_TOKENS = "api/tokens";
|
final static String API_TOKENS = "api/tokens";
|
||||||
|
|
||||||
protected Session(HttpClient httpClient, String urlOpenViduServer) throws OpenViduException {
|
protected Session(HttpClient httpClient, String urlOpenViduServer) throws OpenViduJavaClientException {
|
||||||
this.httpClient = httpClient;
|
this.httpClient = httpClient;
|
||||||
this.urlOpenViduServer = urlOpenViduServer;
|
this.urlOpenViduServer = urlOpenViduServer;
|
||||||
this.properties = new SessionProperties();
|
this.properties = new SessionProperties();
|
||||||
this.sessionId = this.getSessionId();
|
this.getSessionIdHttp();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Session(HttpClient httpClient, String urlOpenViduServer, SessionProperties properties) {
|
protected Session(HttpClient httpClient, String urlOpenViduServer, SessionProperties properties)
|
||||||
|
throws OpenViduJavaClientException {
|
||||||
this.httpClient = httpClient;
|
this.httpClient = httpClient;
|
||||||
this.urlOpenViduServer = urlOpenViduServer;
|
this.urlOpenViduServer = urlOpenViduServer;
|
||||||
this.properties = properties;
|
this.properties = properties;
|
||||||
this.sessionId = this.getSessionId();
|
this.getSessionIdHttp();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the unique identifier of the Session. This translates into a new request
|
* Gets the unique identifier of the Session
|
||||||
* to OpenVidu Server if this Session has no <code>sessionId</code> yet or
|
|
||||||
* simply returns the existing value if it has already been retrieved
|
|
||||||
*
|
*
|
||||||
* @return The sessionId
|
* @return The sessionId
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
public String getSessionId() {
|
||||||
public String getSessionId() throws OpenViduException {
|
|
||||||
|
|
||||||
if (this.hasSessionId()) {
|
|
||||||
return this.sessionId;
|
return this.sessionId;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
HttpPost request = new HttpPost(this.urlOpenViduServer + API_SESSIONS);
|
|
||||||
|
|
||||||
JSONObject json = new JSONObject();
|
|
||||||
json.put("mediaMode", properties.mediaMode().name());
|
|
||||||
json.put("recordingMode", properties.recordingMode().name());
|
|
||||||
json.put("defaultRecordingLayout", properties.defaultRecordingLayout().name());
|
|
||||||
json.put("defaultCustomLayout", properties.defaultCustomLayout());
|
|
||||||
StringEntity params = new StringEntity(json.toString());
|
|
||||||
|
|
||||||
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
|
||||||
request.setEntity(params);
|
|
||||||
|
|
||||||
HttpResponse response = httpClient.execute(request);
|
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
|
||||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
|
||||||
System.out.println("Returning a SESSIONID");
|
|
||||||
String id = "";
|
|
||||||
id = (String) httpResponseToJson(response).get("id");
|
|
||||||
this.sessionId = id;
|
|
||||||
return id;
|
|
||||||
} else {
|
|
||||||
throw new OpenViduException(Code.SESSIONID_CANNOT_BE_CREATED_ERROR_CODE, Integer.toString(statusCode));
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new OpenViduException(Code.SESSIONID_CANNOT_BE_CREATED_ERROR_CODE,
|
|
||||||
"Unable to generate a sessionId: " + e.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a new token associated to Session object with default values for
|
* Gets a new token associated to Session object with default values for
|
||||||
* {@link io.openvidu.java.client.TokenOptions}. This always translates into a
|
* {@link io.openvidu.java.client.TokenOptions}. This always translates into a
|
||||||
* new request to OpenVidu Server
|
* new request to OpenVidu Server
|
||||||
*
|
*
|
||||||
* @returns The generated token
|
* @returns The generated token
|
||||||
|
*
|
||||||
|
* @throws OpenViduJavaClientException
|
||||||
*/
|
*/
|
||||||
public String generateToken() throws OpenViduException {
|
public String generateToken() throws OpenViduJavaClientException {
|
||||||
return this.generateToken(new TokenOptions.Builder().role(OpenViduRole.PUBLISHER).build());
|
return this.generateToken(new TokenOptions.Builder().role(OpenViduRole.PUBLISHER).build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,39 +83,45 @@ public class Session {
|
||||||
* OpenVidu Server
|
* OpenVidu Server
|
||||||
*
|
*
|
||||||
* @returns The generated token
|
* @returns The generated token
|
||||||
|
*
|
||||||
|
* @throws OpenViduJavaClientException
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public String generateToken(TokenOptions tokenOptions) throws OpenViduException {
|
public String generateToken(TokenOptions tokenOptions) throws OpenViduJavaClientException {
|
||||||
|
|
||||||
if (!this.hasSessionId()) {
|
if (!this.hasSessionId()) {
|
||||||
this.getSessionId();
|
this.getSessionId();
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
HttpPost request = new HttpPost(this.urlOpenViduServer + API_TOKENS);
|
HttpPost request = new HttpPost(this.urlOpenViduServer + API_TOKENS);
|
||||||
|
|
||||||
JSONObject json = new JSONObject();
|
JSONObject json = new JSONObject();
|
||||||
json.put("session", this.sessionId);
|
json.put("session", this.sessionId);
|
||||||
json.put("role", tokenOptions.getRole().name());
|
json.put("role", tokenOptions.getRole().name());
|
||||||
json.put("data", tokenOptions.getData());
|
json.put("data", tokenOptions.getData());
|
||||||
StringEntity params = new StringEntity(json.toString());
|
StringEntity params;
|
||||||
|
try {
|
||||||
|
params = new StringEntity(json.toString());
|
||||||
|
} catch (UnsupportedEncodingException e1) {
|
||||||
|
throw new OpenViduJavaClientException(e1.getMessage(), e1.getCause());
|
||||||
|
}
|
||||||
|
|
||||||
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||||
request.setEntity(params);
|
request.setEntity(params);
|
||||||
|
|
||||||
HttpResponse response = httpClient.execute(request);
|
HttpResponse response;
|
||||||
|
try {
|
||||||
|
response = httpClient.execute(request);
|
||||||
|
} catch (IOException e2) {
|
||||||
|
throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause());
|
||||||
|
}
|
||||||
|
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||||
System.out.println("Returning a TOKEN");
|
System.out.println("Returning a TOKEN");
|
||||||
return (String) httpResponseToJson(response).get("id");
|
return (String) httpResponseToJson(response).get("id");
|
||||||
} else {
|
} else {
|
||||||
throw new OpenViduException(Code.TOKEN_CANNOT_BE_CREATED_ERROR_CODE, Integer.toString(statusCode));
|
throw new OpenViduJavaClientException("Unexpected response from OpenVidu Server: " + statusCode);
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new OpenViduException(Code.TOKEN_CANNOT_BE_CREATED_ERROR_CODE,
|
|
||||||
"Unable to generate a token: " + e.getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,9 +141,54 @@ public class Session {
|
||||||
return (this.sessionId != null && !this.sessionId.isEmpty());
|
return (this.sessionId != null && !this.sessionId.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
private JSONObject httpResponseToJson(HttpResponse response) throws ParseException, IOException {
|
@SuppressWarnings("unchecked")
|
||||||
|
private void getSessionIdHttp() throws OpenViduJavaClientException {
|
||||||
|
if (this.hasSessionId()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpPost request = new HttpPost(this.urlOpenViduServer + API_SESSIONS);
|
||||||
|
|
||||||
|
JSONObject json = new JSONObject();
|
||||||
|
json.put("mediaMode", properties.mediaMode().name());
|
||||||
|
json.put("recordingMode", properties.recordingMode().name());
|
||||||
|
json.put("defaultRecordingLayout", properties.defaultRecordingLayout().name());
|
||||||
|
json.put("defaultCustomLayout", properties.defaultCustomLayout());
|
||||||
|
StringEntity params = null;
|
||||||
|
try {
|
||||||
|
params = new StringEntity(json.toString());
|
||||||
|
} catch (UnsupportedEncodingException e1) {
|
||||||
|
throw new OpenViduJavaClientException(e1.getMessage(), e1.getCause());
|
||||||
|
}
|
||||||
|
|
||||||
|
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||||
|
request.setEntity(params);
|
||||||
|
|
||||||
|
HttpResponse response;
|
||||||
|
try {
|
||||||
|
response = httpClient.execute(request);
|
||||||
|
} catch (IOException e2) {
|
||||||
|
throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause());
|
||||||
|
}
|
||||||
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
|
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||||
|
System.out.println("Returning a SESSIONID");
|
||||||
|
String id = (String) httpResponseToJson(response).get("id");
|
||||||
|
this.sessionId = id;
|
||||||
|
} else {
|
||||||
|
throw new OpenViduJavaClientException("Unexpected response from OpenVidu Server: " + statusCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private JSONObject httpResponseToJson(HttpResponse response) throws OpenViduJavaClientException {
|
||||||
JSONParser parser = new JSONParser();
|
JSONParser parser = new JSONParser();
|
||||||
return (JSONObject) parser.parse(EntityUtils.toString(response.getEntity()));
|
JSONObject json;
|
||||||
|
try {
|
||||||
|
json = (JSONObject) parser.parse(EntityUtils.toString(response.getEntity()));
|
||||||
|
} catch (org.apache.http.ParseException | ParseException | IOException e) {
|
||||||
|
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||||
|
}
|
||||||
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue