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;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
@ -49,8 +50,6 @@ import org.json.simple.JSONObject;
|
|||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import io.openvidu.java.client.OpenViduException.Code;
|
||||
|
||||
public class OpenVidu {
|
||||
|
||||
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);
|
||||
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);
|
||||
return s;
|
||||
}
|
||||
|
@ -125,34 +141,42 @@ public class OpenVidu {
|
|||
* The configuration for this recording
|
||||
*
|
||||
* @return The new created session
|
||||
*
|
||||
* @throws OpenViduJavaClientException
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public Recording startRecording(String sessionId, RecordingProperties properties) throws OpenViduException {
|
||||
public Recording startRecording(String sessionId, RecordingProperties properties)
|
||||
throws OpenViduJavaClientException {
|
||||
|
||||
HttpPost request = new HttpPost(this.urlOpenViduServer + API_RECORDINGS + API_RECORDINGS_START);
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("session", sessionId);
|
||||
json.put("name", properties.name());
|
||||
json.put("recordingLayout", (properties.recordingLayout() != null) ? properties.recordingLayout().name() : "");
|
||||
json.put("customLayout", (properties.customLayout() != null) ? properties.customLayout() : "");
|
||||
StringEntity params = null;
|
||||
try {
|
||||
HttpPost request = new HttpPost(this.urlOpenViduServer + API_RECORDINGS + API_RECORDINGS_START);
|
||||
params = new StringEntity(json.toString());
|
||||
} catch (UnsupportedEncodingException e1) {
|
||||
throw new OpenViduJavaClientException(e1.getMessage(), e1.getCause());
|
||||
}
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("session", sessionId);
|
||||
json.put("name", properties.name());
|
||||
json.put("recordingLayout",
|
||||
(properties.recordingLayout() != null) ? properties.recordingLayout().name() : "");
|
||||
json.put("customLayout", (properties.customLayout() != null) ? properties.customLayout() : "");
|
||||
StringEntity params = new StringEntity(json.toString());
|
||||
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||
request.setEntity(params);
|
||||
|
||||
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||
request.setEntity(params);
|
||||
HttpResponse response;
|
||||
try {
|
||||
response = myHttpClient.execute(request);
|
||||
} catch (IOException e2) {
|
||||
throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause());
|
||||
}
|
||||
|
||||
HttpResponse response = myHttpClient.execute(request);
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
return new Recording(httpResponseToJson(response));
|
||||
} else {
|
||||
throw new OpenViduException(Code.RECORDING_START_ERROR_CODE, Integer.toString(statusCode));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new OpenViduException(Code.RECORDING_START_ERROR_CODE,
|
||||
"Unable to start recording for session '" + sessionId + "': " + e.getMessage());
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
return new Recording(httpResponseToJson(response));
|
||||
} else {
|
||||
throw new OpenViduJavaClientException("Unexpected response from OpenVidu Server: " + statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -169,8 +193,10 @@ public class OpenVidu {
|
|||
* @return The started recording. If this method successfully returns the
|
||||
* Recording object it means that the recording can be stopped with
|
||||
* guarantees
|
||||
*
|
||||
* @throws OpenViduJavaClientException
|
||||
*/
|
||||
public Recording startRecording(String sessionId, String name) throws OpenViduException {
|
||||
public Recording startRecording(String sessionId, String name) throws OpenViduJavaClientException {
|
||||
if (name == null) {
|
||||
name = "";
|
||||
}
|
||||
|
@ -186,8 +212,10 @@ public class OpenVidu {
|
|||
* @return The started recording. If this method successfully returns the
|
||||
* Recording object it means that the recording can be stopped with
|
||||
* guarantees
|
||||
*
|
||||
* @throws OpenViduJavaClientException
|
||||
*/
|
||||
public Recording startRecording(String sessionId) throws OpenViduException {
|
||||
public Recording startRecording(String sessionId) throws OpenViduJavaClientException {
|
||||
return this.startRecording(sessionId, "");
|
||||
}
|
||||
|
||||
|
@ -198,22 +226,24 @@ public class OpenVidu {
|
|||
* The id property of the recording you want to stop
|
||||
*
|
||||
* @return The stopped recording
|
||||
*
|
||||
* @throws OpenViduJavaClientException
|
||||
*/
|
||||
public Recording stopRecording(String recordingId) throws OpenViduException {
|
||||
public Recording stopRecording(String recordingId) throws OpenViduJavaClientException {
|
||||
HttpPost request = new HttpPost(
|
||||
this.urlOpenViduServer + API_RECORDINGS + API_RECORDINGS_STOP + "/" + recordingId);
|
||||
HttpResponse response;
|
||||
try {
|
||||
HttpPost request = new HttpPost(
|
||||
this.urlOpenViduServer + API_RECORDINGS + API_RECORDINGS_STOP + "/" + recordingId);
|
||||
HttpResponse response = myHttpClient.execute(request);
|
||||
response = myHttpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
return new Recording(httpResponseToJson(response));
|
||||
} else {
|
||||
throw new OpenViduException(Code.RECORDING_STOP_ERROR_CODE, Integer.toString(statusCode));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new OpenViduException(Code.RECORDING_STOP_ERROR_CODE,
|
||||
"Unable to stop recording '" + recordingId + "': " + e.getMessage());
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
return new Recording(httpResponseToJson(response));
|
||||
} else {
|
||||
throw new OpenViduJavaClientException("Unexpected response from OpenVidu Server: " + statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -222,21 +252,23 @@ public class OpenVidu {
|
|||
*
|
||||
* @param recordingId
|
||||
* 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 {
|
||||
HttpGet request = new HttpGet(this.urlOpenViduServer + API_RECORDINGS + "/" + recordingId);
|
||||
HttpResponse response;
|
||||
try {
|
||||
HttpGet request = new HttpGet(this.urlOpenViduServer + API_RECORDINGS + "/" + recordingId);
|
||||
HttpResponse response = myHttpClient.execute(request);
|
||||
response = myHttpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
return new Recording(httpResponseToJson(response));
|
||||
} else {
|
||||
throw new OpenViduException(Code.RECORDING_LIST_ERROR_CODE, Integer.toString(statusCode));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new OpenViduException(Code.RECORDING_LIST_ERROR_CODE,
|
||||
"Unable to get recording '" + recordingId + "': " + e.getMessage());
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
return new Recording(httpResponseToJson(response));
|
||||
} else {
|
||||
throw new OpenViduJavaClientException("Unexpected response from OpenVidu Server: " + statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -244,27 +276,30 @@ public class OpenVidu {
|
|||
* Lists all existing recordings
|
||||
*
|
||||
* @return A {@link java.util.List} with all existing recordings
|
||||
*
|
||||
* @throws OpenViduJavaClientException
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Recording> listRecordings() throws OpenViduException {
|
||||
public List<Recording> listRecordings() throws OpenViduJavaClientException {
|
||||
HttpGet request = new HttpGet(this.urlOpenViduServer + API_RECORDINGS);
|
||||
HttpResponse response;
|
||||
try {
|
||||
HttpGet request = new HttpGet(this.urlOpenViduServer + API_RECORDINGS);
|
||||
HttpResponse response = myHttpClient.execute(request);
|
||||
response = myHttpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
List<Recording> recordings = new ArrayList<>();
|
||||
JSONObject json = httpResponseToJson(response);
|
||||
JSONArray array = (JSONArray) json.get("items");
|
||||
array.forEach(item -> {
|
||||
recordings.add(new Recording((JSONObject) item));
|
||||
});
|
||||
return recordings;
|
||||
} else {
|
||||
throw new OpenViduException(Code.RECORDING_LIST_ERROR_CODE, Integer.toString(statusCode));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new OpenViduException(Code.RECORDING_LIST_ERROR_CODE, "Unable to list recordings: " + e.getMessage());
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
List<Recording> recordings = new ArrayList<>();
|
||||
JSONObject json = httpResponseToJson(response);
|
||||
JSONArray array = (JSONArray) json.get("items");
|
||||
array.forEach(item -> {
|
||||
recordings.add(new Recording((JSONObject) item));
|
||||
});
|
||||
return recordings;
|
||||
} else {
|
||||
throw new OpenViduJavaClientException("Unexpected response from OpenVidu Server: " + statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -274,25 +309,33 @@ public class OpenVidu {
|
|||
* {@link io.openvidu.java.client.Recording.Status#available}
|
||||
*
|
||||
* @param recordingId
|
||||
*
|
||||
* @throws OpenViduJavaClientException
|
||||
*/
|
||||
public void deleteRecording(String recordingId) throws OpenViduException {
|
||||
public void deleteRecording(String recordingId) throws OpenViduJavaClientException {
|
||||
HttpDelete request = new HttpDelete(this.urlOpenViduServer + API_RECORDINGS + "/" + recordingId);
|
||||
HttpResponse response;
|
||||
try {
|
||||
HttpDelete request = new HttpDelete(this.urlOpenViduServer + API_RECORDINGS + "/" + recordingId);
|
||||
HttpResponse response = myHttpClient.execute(request);
|
||||
response = myHttpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (!(statusCode == org.apache.http.HttpStatus.SC_NO_CONTENT)) {
|
||||
throw new OpenViduException(Code.RECORDING_DELETE_ERROR_CODE, Integer.toString(statusCode));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new OpenViduException(Code.RECORDING_DELETE_ERROR_CODE,
|
||||
"Unable to delete recording '" + recordingId + "': " + e.getMessage());
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (!(statusCode == org.apache.http.HttpStatus.SC_NO_CONTENT)) {
|
||||
throw new OpenViduJavaClientException("Unexpected response from OpenVidu Server: " + statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
private JSONObject httpResponseToJson(HttpResponse response) throws ParseException, IOException {
|
||||
private JSONObject httpResponseToJson(HttpResponse response) throws OpenViduJavaClientException {
|
||||
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;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import org.apache.http.HttpHeaders;
|
||||
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.ParseException;
|
||||
|
||||
import io.openvidu.java.client.OpenViduException.Code;
|
||||
|
||||
public class Session {
|
||||
|
||||
private HttpClient httpClient;
|
||||
|
@ -41,63 +40,28 @@ public class Session {
|
|||
final static String API_SESSIONS = "api/sessions";
|
||||
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.urlOpenViduServer = urlOpenViduServer;
|
||||
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.urlOpenViduServer = urlOpenViduServer;
|
||||
this.properties = properties;
|
||||
this.sessionId = this.getSessionId();
|
||||
this.getSessionIdHttp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the unique identifier of the Session. This translates into a new request
|
||||
* to OpenVidu Server if this Session has no <code>sessionId</code> yet or
|
||||
* simply returns the existing value if it has already been retrieved
|
||||
* Gets the unique identifier of the Session
|
||||
*
|
||||
* @return The sessionId
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public String getSessionId() throws OpenViduException {
|
||||
|
||||
if (this.hasSessionId()) {
|
||||
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());
|
||||
}
|
||||
|
||||
public String getSessionId() {
|
||||
return this.sessionId;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -106,8 +70,10 @@ public class Session {
|
|||
* new request to OpenVidu Server
|
||||
*
|
||||
* @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());
|
||||
}
|
||||
|
||||
|
@ -117,39 +83,45 @@ public class Session {
|
|||
* OpenVidu Server
|
||||
*
|
||||
* @returns The generated token
|
||||
*
|
||||
* @throws OpenViduJavaClientException
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public String generateToken(TokenOptions tokenOptions) throws OpenViduException {
|
||||
public String generateToken(TokenOptions tokenOptions) throws OpenViduJavaClientException {
|
||||
|
||||
if (!this.hasSessionId()) {
|
||||
this.getSessionId();
|
||||
}
|
||||
|
||||
HttpPost request = new HttpPost(this.urlOpenViduServer + API_TOKENS);
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("session", this.sessionId);
|
||||
json.put("role", tokenOptions.getRole().name());
|
||||
json.put("data", tokenOptions.getData());
|
||||
StringEntity params;
|
||||
try {
|
||||
HttpPost request = new HttpPost(this.urlOpenViduServer + API_TOKENS);
|
||||
params = new StringEntity(json.toString());
|
||||
} catch (UnsupportedEncodingException e1) {
|
||||
throw new OpenViduJavaClientException(e1.getMessage(), e1.getCause());
|
||||
}
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("session", this.sessionId);
|
||||
json.put("role", tokenOptions.getRole().name());
|
||||
json.put("data", tokenOptions.getData());
|
||||
StringEntity params = new StringEntity(json.toString());
|
||||
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||
request.setEntity(params);
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
HttpResponse response = httpClient.execute(request);
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
System.out.println("Returning a TOKEN");
|
||||
return (String) httpResponseToJson(response).get("id");
|
||||
} else {
|
||||
throw new OpenViduException(Code.TOKEN_CANNOT_BE_CREATED_ERROR_CODE, Integer.toString(statusCode));
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new OpenViduException(Code.TOKEN_CANNOT_BE_CREATED_ERROR_CODE,
|
||||
"Unable to generate a token: " + e.getMessage());
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
System.out.println("Returning a TOKEN");
|
||||
return (String) httpResponseToJson(response).get("id");
|
||||
} else {
|
||||
throw new OpenViduJavaClientException("Unexpected response from OpenVidu Server: " + statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -169,9 +141,54 @@ public class Session {
|
|||
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();
|
||||
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