mirror of https://github.com/OpenVidu/openvidu.git
openvidu-java-client: improve exception handling and Entity consumption
parent
85b6496ba0
commit
af6a1a3693
|
@ -203,14 +203,10 @@ public class OpenVidu {
|
|||
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||
request.setEntity(params);
|
||||
|
||||
HttpResponse response;
|
||||
HttpResponse response = null;
|
||||
try {
|
||||
response = this.httpClient.execute(request);
|
||||
} catch (IOException e2) {
|
||||
throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause());
|
||||
}
|
||||
|
||||
try {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
Recording r = new Recording(httpResponseToJson(response));
|
||||
|
@ -226,8 +222,13 @@ public class OpenVidu {
|
|||
} else {
|
||||
throw new OpenViduHttpException(statusCode);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
} finally {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
if (response != null) {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -334,14 +335,10 @@ public class OpenVidu {
|
|||
*/
|
||||
public Recording stopRecording(String recordingId) throws OpenViduJavaClientException, OpenViduHttpException {
|
||||
HttpPost request = new HttpPost(this.hostname + API_RECORDINGS_STOP + "/" + recordingId);
|
||||
HttpResponse response;
|
||||
HttpResponse response = null;
|
||||
try {
|
||||
response = this.httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
||||
try {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
Recording r = new Recording(httpResponseToJson(response));
|
||||
|
@ -357,8 +354,12 @@ public class OpenVidu {
|
|||
} else {
|
||||
throw new OpenViduHttpException(statusCode);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
} finally {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
if (response != null) {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -377,22 +378,23 @@ public class OpenVidu {
|
|||
*/
|
||||
public Recording getRecording(String recordingId) throws OpenViduJavaClientException, OpenViduHttpException {
|
||||
HttpGet request = new HttpGet(this.hostname + API_RECORDINGS + "/" + recordingId);
|
||||
HttpResponse response;
|
||||
HttpResponse response = null;
|
||||
try {
|
||||
response = this.httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
||||
try {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
return new Recording(httpResponseToJson(response));
|
||||
} else {
|
||||
throw new OpenViduHttpException(statusCode);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
} finally {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
if (response != null) {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -406,14 +408,10 @@ public class OpenVidu {
|
|||
*/
|
||||
public List<Recording> listRecordings() throws OpenViduJavaClientException, OpenViduHttpException {
|
||||
HttpGet request = new HttpGet(this.hostname + API_RECORDINGS);
|
||||
HttpResponse response;
|
||||
HttpResponse response = null;
|
||||
try {
|
||||
response = this.httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
||||
try {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
List<Recording> recordings = new ArrayList<>();
|
||||
|
@ -426,8 +424,13 @@ public class OpenVidu {
|
|||
} else {
|
||||
throw new OpenViduHttpException(statusCode);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
} finally {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
if (response != null) {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -452,20 +455,21 @@ public class OpenVidu {
|
|||
*/
|
||||
public void deleteRecording(String recordingId) throws OpenViduJavaClientException, OpenViduHttpException {
|
||||
HttpDelete request = new HttpDelete(this.hostname + API_RECORDINGS + "/" + recordingId);
|
||||
HttpResponse response;
|
||||
HttpResponse response = null;
|
||||
try {
|
||||
response = this.httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
||||
try {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (!(statusCode == org.apache.http.HttpStatus.SC_NO_CONTENT)) {
|
||||
throw new OpenViduHttpException(statusCode);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
} finally {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
if (response != null) {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -519,14 +523,10 @@ public class OpenVidu {
|
|||
public boolean fetch() throws OpenViduJavaClientException, OpenViduHttpException {
|
||||
HttpGet request = new HttpGet(this.hostname + API_SESSIONS + "?pendingConnections=true");
|
||||
|
||||
HttpResponse response;
|
||||
HttpResponse response = null;
|
||||
try {
|
||||
response = this.httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
||||
try {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
|
||||
|
@ -581,14 +581,20 @@ public class OpenVidu {
|
|||
} else {
|
||||
throw new OpenViduHttpException(statusCode);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
} finally {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
if (response != null) {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private JsonObject httpResponseToJson(HttpResponse response) throws OpenViduJavaClientException {
|
||||
try {
|
||||
JsonObject json = new Gson().fromJson(EntityUtils.toString(response.getEntity(), "UTF-8"), JsonObject.class);
|
||||
JsonObject json = new Gson().fromJson(EntityUtils.toString(response.getEntity(), "UTF-8"),
|
||||
JsonObject.class);
|
||||
return json;
|
||||
} catch (JsonSyntaxException | ParseException | IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
|
|
|
@ -18,7 +18,8 @@
|
|||
package io.openvidu.java.client;
|
||||
|
||||
/**
|
||||
* Defines error responses from OpenVidu Server
|
||||
* Defines error responses from OpenVidu Server. See error codes at
|
||||
* https://docs.openvidu.io/en/stable/reference-docs/REST-API/
|
||||
*/
|
||||
public class OpenViduHttpException extends OpenViduException {
|
||||
|
||||
|
|
|
@ -126,14 +126,10 @@ public class Session {
|
|||
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||
request.setEntity(params);
|
||||
|
||||
HttpResponse response;
|
||||
HttpResponse response = null;
|
||||
try {
|
||||
response = this.openVidu.httpClient.execute(request);
|
||||
} catch (IOException e2) {
|
||||
throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause());
|
||||
}
|
||||
|
||||
try {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
String token = httpResponseToJson(response).get("id").getAsString();
|
||||
|
@ -142,8 +138,13 @@ public class Session {
|
|||
} else {
|
||||
throw new OpenViduHttpException(statusCode);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
} finally {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
if (response != null) {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -189,14 +190,10 @@ public class Session {
|
|||
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||
request.setEntity(params);
|
||||
|
||||
HttpResponse response;
|
||||
HttpResponse response = null;
|
||||
try {
|
||||
response = this.openVidu.httpClient.execute(request);
|
||||
} catch (IOException e2) {
|
||||
throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause());
|
||||
}
|
||||
|
||||
try {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
Connection connection = new Connection(httpResponseToJson(response));
|
||||
|
@ -205,8 +202,13 @@ public class Session {
|
|||
} else {
|
||||
throw new OpenViduHttpException(statusCode);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
} finally {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
if (response != null) {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -221,14 +223,10 @@ public class Session {
|
|||
HttpDelete request = new HttpDelete(this.openVidu.hostname + OpenVidu.API_SESSIONS + "/" + this.sessionId);
|
||||
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
|
||||
|
||||
HttpResponse response;
|
||||
HttpResponse response = null;
|
||||
try {
|
||||
response = this.openVidu.httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
||||
try {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_NO_CONTENT)) {
|
||||
this.openVidu.activeSessions.remove(this.sessionId);
|
||||
|
@ -236,8 +234,13 @@ public class Session {
|
|||
} else {
|
||||
throw new OpenViduHttpException(statusCode);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
} finally {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
if (response != null) {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -268,14 +271,10 @@ public class Session {
|
|||
this.openVidu.hostname + OpenVidu.API_SESSIONS + "/" + this.sessionId + "?pendingConnections=true");
|
||||
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
|
||||
|
||||
HttpResponse response;
|
||||
HttpResponse response = null;
|
||||
try {
|
||||
response = this.openVidu.httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
||||
try {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
this.resetWithJson(httpResponseToJson(response));
|
||||
|
@ -286,8 +285,13 @@ public class Session {
|
|||
} else {
|
||||
throw new OpenViduHttpException(statusCode);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
} finally {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
if (response != null) {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -343,11 +347,7 @@ public class Session {
|
|||
HttpResponse response = null;
|
||||
try {
|
||||
response = this.openVidu.httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
||||
try {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_NO_CONTENT)) {
|
||||
// Remove connection from activeConnections map
|
||||
|
@ -370,10 +370,13 @@ public class Session {
|
|||
} else {
|
||||
throw new OpenViduHttpException(statusCode);
|
||||
}
|
||||
} finally
|
||||
|
||||
{
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
} finally {
|
||||
if (response != null) {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -423,14 +426,10 @@ public class Session {
|
|||
this.openVidu.hostname + OpenVidu.API_SESSIONS + "/" + this.sessionId + "/stream/" + streamId);
|
||||
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
|
||||
|
||||
HttpResponse response;
|
||||
HttpResponse response = null;
|
||||
try {
|
||||
response = this.openVidu.httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
||||
try {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_NO_CONTENT)) {
|
||||
for (Connection connection : this.connections.values()) {
|
||||
|
@ -445,17 +444,22 @@ public class Session {
|
|||
} else {
|
||||
throw new OpenViduHttpException(statusCode);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
} finally {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
if (response != null) {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="https://docs.openvidu.io/en/stable/openvidu-pro/"
|
||||
* style="display: inline-block; background-color: rgb(0, 136, 170); color:
|
||||
* white; font-weight: bold; padding: 0px 5px; margin-right: 5px; border-radius:
|
||||
* 3px; font-size: 13px; line-height:21px; font-family: Montserrat,
|
||||
* sans-serif">PRO</a> Updates the properties of a Connection with a
|
||||
* <a href="https://docs.openvidu.io/en/stable/openvidu-pro/" style="display:
|
||||
* inline-block; background-color: rgb(0, 136, 170); color: white; font-weight:
|
||||
* bold; padding: 0px 5px; margin-right: 5px; border-radius: 3px; font-size:
|
||||
* 13px; line-height:21px; font-family: Montserrat, sans-serif">PRO</a> Updates
|
||||
* the properties of a Connection with a
|
||||
* {@link io.openvidu.java.client.ConnectionProperties} object. Only these
|
||||
* properties can be updated:
|
||||
* <ul>
|
||||
|
@ -474,10 +478,9 @@ public class Session {
|
|||
* objects.<br>
|
||||
* <br>
|
||||
*
|
||||
* The affected client will trigger one
|
||||
* <a href="/en/stable/api/openvidu-browser/classes/ConnectionPropertyChangedEvent.html">
|
||||
* ConnectionPropertyChangedEvent
|
||||
* </a> for each modified property.
|
||||
* The affected client will trigger one <a href=
|
||||
* "/en/stable/api/openvidu-browser/classes/ConnectionPropertyChangedEvent.html">
|
||||
* ConnectionPropertyChangedEvent </a> for each modified property.
|
||||
*
|
||||
* @param connectionId The Connection to modify
|
||||
* @param connectionProperties A ConnectionProperties object with the new values
|
||||
|
@ -499,14 +502,10 @@ public class Session {
|
|||
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||
request.setEntity(params);
|
||||
|
||||
HttpResponse response;
|
||||
HttpResponse response = null;
|
||||
try {
|
||||
response = this.openVidu.httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
||||
try {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
log.info("Connection {} updated", connectionId);
|
||||
|
@ -531,8 +530,12 @@ public class Session {
|
|||
return existingConnection;
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
} finally {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
if (response != null) {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -649,20 +652,18 @@ public class Session {
|
|||
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||
request.setEntity(params);
|
||||
|
||||
HttpResponse response;
|
||||
HttpResponse response = null;
|
||||
try {
|
||||
response = this.openVidu.httpClient.execute(request);
|
||||
} catch (IOException e2) {
|
||||
throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause());
|
||||
}
|
||||
try {
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
JsonObject responseJson = httpResponseToJson(response);
|
||||
this.sessionId = responseJson.get("id").getAsString();
|
||||
this.createdAt = responseJson.get("createdAt").getAsLong();
|
||||
|
||||
// Values that get filled by OpenVidu Server from its global or per-session configuration
|
||||
// Values that get filled by OpenVidu Server from its global or per-session
|
||||
// configuration
|
||||
VideoCodec forcedVideoCodec = VideoCodec.valueOf(responseJson.get("forcedVideoCodec").getAsString());
|
||||
VideoCodec forcedVideoCodecResolved = VideoCodec
|
||||
.valueOf(responseJson.get("forcedVideoCodecResolved").getAsString());
|
||||
|
@ -677,14 +678,16 @@ public class Session {
|
|||
|
||||
this.properties = responseProperties;
|
||||
log.info("Session '{}' created", this.sessionId);
|
||||
} else if (statusCode == org.apache.http.HttpStatus.SC_CONFLICT) {
|
||||
// 'customSessionId' already existed
|
||||
this.sessionId = properties.customSessionId();
|
||||
} else {
|
||||
throw new OpenViduHttpException(statusCode);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
} finally {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
if (response != null) {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue