Skip to content

Commit

Permalink
openvidu-java-client: async call from getSessionId to createSession. …
Browse files Browse the repository at this point in the history
…OpenViduException simplified
  • Loading branch information
pabloFuente committed Apr 25, 2018
1 parent ac09441 commit 5fd6db8
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 242 deletions.
219 changes: 131 additions & 88 deletions openvidu-java-client/src/main/java/io/openvidu/java/client/OpenVidu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,8 +50,6 @@
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;
Expand Down Expand Up @@ -102,12 +101,29 @@ public boolean isTrusted(X509Certificate[] chain, String authType) throws Certif

}

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;
}
Expand All @@ -125,34 +141,42 @@ public Session createSession(SessionProperties properties) throws OpenViduExcept
* 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);

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);

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());
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 = myHttpClient.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)) {
return new Recording(httpResponseToJson(response));
} else {
throw new OpenViduJavaClientException("Unexpected response from OpenVidu Server: " + statusCode);
}
}

Expand All @@ -169,8 +193,10 @@ public Recording startRecording(String sessionId, RecordingProperties properties
* @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 = "";
}
Expand All @@ -186,8 +212,10 @@ public Recording startRecording(String sessionId, String name) throws OpenViduEx
* @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, "");
}

Expand All @@ -198,22 +226,24 @@ public Recording startRecording(String sessionId) throws OpenViduException {
* 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);

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());
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 OpenViduJavaClientException("Unexpected response from OpenVidu Server: " + statusCode);
}
}

Expand All @@ -222,49 +252,54 @@ public Recording stopRecording(String recordingId) throws OpenViduException {
*
* @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);

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());
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 OpenViduJavaClientException("Unexpected response from OpenVidu Server: " + statusCode);
}
}

/**
* 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);

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());
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 OpenViduJavaClientException("Unexpected response from OpenVidu Server: " + statusCode);
}
}

Expand All @@ -274,25 +309,33 @@ public List<Recording> listRecordings() throws OpenViduException {
* {@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;
}

}

This file was deleted.

Loading

0 comments on commit 5fd6db8

Please sign in to comment.