openvidu-java-client : change to lambda function

pull/821/head
최종명 2023-10-16 17:57:22 +09:00
parent a14f8d997d
commit b24d89c884
1 changed files with 108 additions and 129 deletions

View File

@ -123,17 +123,14 @@ public class Session {
this.getSessionId(); this.getSessionId();
} }
final HttpClientResponseHandler<String> responseHandler = new HttpClientResponseHandler<String>() { final HttpClientResponseHandler<String> responseHandler = response -> {
@Override final int status = response.getCode();
public String handleResponse(final ClassicHttpResponse response) throws IOException, HttpException { if (status == HttpStatus.SC_OK) {
final int status = response.getCode(); String token = OpenVidu.httpResponseEntityToJson(response.getEntity()).get("id").getAsString();
if (status == HttpStatus.SC_OK) { log.info("Returning a TOKEN: {}", token);
String token = OpenVidu.httpResponseEntityToJson(response.getEntity()).get("id").getAsString(); return token;
log.info("Returning a TOKEN: {}", token); } else {
return token; throw OpenVidu.openViduHttpException(status);
} else {
throw OpenVidu.openViduHttpException(status);
}
} }
}; };
@ -224,18 +221,15 @@ public class Session {
*/ */
public void close() throws OpenViduJavaClientException, OpenViduHttpException { public void close() throws OpenViduJavaClientException, OpenViduHttpException {
final HttpClientResponseHandler<Void> responseHandler = new HttpClientResponseHandler<Void>() { final HttpClientResponseHandler<Void> responseHandler = response -> {
@Override final int status = response.getCode();
public Void handleResponse(final ClassicHttpResponse response) throws IOException, HttpException { if (status == HttpStatus.SC_NO_CONTENT) {
final int status = response.getCode(); openVidu.activeSessions.remove(sessionId);
if (status == HttpStatus.SC_NO_CONTENT) { log.info("Session {} closed", sessionId);
openVidu.activeSessions.remove(sessionId); } else {
log.info("Session {} closed", sessionId); throw OpenVidu.openViduHttpException(status);
} else {
throw OpenVidu.openViduHttpException(status);
}
return null;
} }
return null;
}; };
HttpDelete request = new HttpDelete(this.openVidu.hostname + OpenVidu.API_SESSIONS + "/" + this.sessionId); HttpDelete request = new HttpDelete(this.openVidu.hostname + OpenVidu.API_SESSIONS + "/" + this.sessionId);
@ -272,19 +266,16 @@ public class Session {
final String beforeJSON = this.toJson(); final String beforeJSON = this.toJson();
final HttpClientResponseHandler<Boolean> responseHandler = new HttpClientResponseHandler<Boolean>() { final HttpClientResponseHandler<Boolean> responseHandler = response -> {
@Override final int status = response.getCode();
public Boolean handleResponse(final ClassicHttpResponse response) throws IOException, HttpException { if (status == HttpStatus.SC_OK) {
final int status = response.getCode(); resetWithJson(OpenVidu.httpResponseEntityToJson(response.getEntity()));
if (status == HttpStatus.SC_OK) { final String afterJSON = toJson();
resetWithJson(OpenVidu.httpResponseEntityToJson(response.getEntity())); boolean hasChanged = !beforeJSON.equals(afterJSON);
final String afterJSON = toJson(); log.info("Session info fetched for session '{}'. Any change: {}", sessionId, hasChanged);
boolean hasChanged = !beforeJSON.equals(afterJSON); return hasChanged;
log.info("Session info fetched for session '{}'. Any change: {}", sessionId, hasChanged); } else {
return hasChanged; throw OpenVidu.openViduHttpException(status);
} else {
throw OpenVidu.openViduHttpException(status);
}
} }
}; };
@ -344,34 +335,31 @@ public class Session {
*/ */
public void forceDisconnect(String connectionId) throws OpenViduJavaClientException, OpenViduHttpException { public void forceDisconnect(String connectionId) throws OpenViduJavaClientException, OpenViduHttpException {
final HttpClientResponseHandler<Void> responseHandler = new HttpClientResponseHandler<Void>() { final HttpClientResponseHandler<Void> responseHandler = response -> {
@Override final int status = response.getCode();
public Void handleResponse(final ClassicHttpResponse response) throws IOException, HttpException { if (status == HttpStatus.SC_NO_CONTENT) {
final int status = response.getCode(); // Remove connection from activeConnections map
if (status == HttpStatus.SC_NO_CONTENT) { Connection connectionClosed = connections.remove(connectionId);
// Remove connection from activeConnections map // Remove every Publisher of the closed connection from every subscriber list of
Connection connectionClosed = connections.remove(connectionId); // other connections
// Remove every Publisher of the closed connection from every subscriber list of if (connectionClosed != null) {
// other connections for (Publisher publisher : connectionClosed.getPublishers()) {
if (connectionClosed != null) { String streamId = publisher.getStreamId();
for (Publisher publisher : connectionClosed.getPublishers()) { for (Connection connection : connections.values()) {
String streamId = publisher.getStreamId(); connection.setSubscribers(connection.getSubscribers().stream()
for (Connection connection : connections.values()) { .filter(subscriber -> !streamId.equals(subscriber))
connection.setSubscribers(connection.getSubscribers().stream() .collect(Collectors.toList()));
.filter(subscriber -> !streamId.equals(subscriber))
.collect(Collectors.toList()));
}
} }
} else {
log.warn(
"The closed connection wasn't fetched in OpenVidu Java Client. No changes in the collection of active connections of the Session");
} }
log.info("Connection {} closed", connectionId);
} else { } else {
throw OpenVidu.openViduHttpException(status); log.warn(
"The closed connection wasn't fetched in OpenVidu Java Client. No changes in the collection of active connections of the Session");
} }
return null; log.info("Connection {} closed", connectionId);
} else {
throw OpenVidu.openViduHttpException(status);
} }
return null;
}; };
HttpDelete request = new HttpDelete( HttpDelete request = new HttpDelete(
@ -427,25 +415,22 @@ public class Session {
*/ */
public void forceUnpublish(String streamId) throws OpenViduJavaClientException, OpenViduHttpException { public void forceUnpublish(String streamId) throws OpenViduJavaClientException, OpenViduHttpException {
final HttpClientResponseHandler<Void> responseHandler = new HttpClientResponseHandler<Void>() { final HttpClientResponseHandler<Void> responseHandler = response -> {
@Override final int status = response.getCode();
public Void handleResponse(final ClassicHttpResponse response) throws IOException, HttpException { if (status == HttpStatus.SC_NO_CONTENT) {
final int status = response.getCode(); for (Connection connection : connections.values()) {
if (status == HttpStatus.SC_NO_CONTENT) { // Try to remove the Publisher from the Connection publishers collection
for (Connection connection : connections.values()) { if (connection.publishers.remove(streamId) != null) {
// Try to remove the Publisher from the Connection publishers collection continue;
if (connection.publishers.remove(streamId) != null) {
continue;
}
// Try to remove the Publisher from the Connection subscribers collection
connection.subscribers.remove(streamId);
} }
log.info("Stream {} unpublished", streamId); // Try to remove the Publisher from the Connection subscribers collection
} else { connection.subscribers.remove(streamId);
throw OpenVidu.openViduHttpException(status);
} }
return null; log.info("Stream {} unpublished", streamId);
} else {
throw OpenVidu.openViduHttpException(status);
} }
return null;
}; };
HttpDelete request = new HttpDelete( HttpDelete request = new HttpDelete(
@ -499,32 +484,29 @@ public class Session {
public Connection updateConnection(String connectionId, ConnectionProperties connectionProperties) public Connection updateConnection(String connectionId, ConnectionProperties connectionProperties)
throws OpenViduJavaClientException, OpenViduHttpException { throws OpenViduJavaClientException, OpenViduHttpException {
final HttpClientResponseHandler<Connection> responseHandler = new HttpClientResponseHandler<Connection>() { final HttpClientResponseHandler<Connection> responseHandler = response -> {
@Override final int status = response.getCode();
public Connection handleResponse(final ClassicHttpResponse response) throws IOException, HttpException { if (status == HttpStatus.SC_OK) {
final int status = response.getCode(); log.info("Connection {} updated", connectionId);
if (status == HttpStatus.SC_OK) { } else if (status == HttpStatus.SC_NO_CONTENT) {
log.info("Connection {} updated", connectionId); log.info("Properties of Connection {} remain the same", connectionId);
} else if (status == HttpStatus.SC_NO_CONTENT) { } else {
log.info("Properties of Connection {} remain the same", connectionId); throw OpenVidu.openViduHttpException(status);
} else { }
throw OpenVidu.openViduHttpException(status); JsonObject json = OpenVidu.httpResponseEntityToJson(response.getEntity());
}
JsonObject json = OpenVidu.httpResponseEntityToJson(response.getEntity());
// Update the actual Connection object with the new options // Update the actual Connection object with the new options
Connection existingConnection = connections.get(connectionId); Connection existingConnection = connections.get(connectionId);
if (existingConnection == null) { if (existingConnection == null) {
// The updated Connection is not available in local map // The updated Connection is not available in local map
Connection newConnection = new Connection(json); Connection newConnection = new Connection(json);
connections.put(connectionId, newConnection); connections.put(connectionId, newConnection);
return newConnection; return newConnection;
} else { } else {
// The updated Connection was available in local map // The updated Connection was available in local map
existingConnection.overrideConnectionProperties(connectionProperties); existingConnection.overrideConnectionProperties(connectionProperties);
return existingConnection; return existingConnection;
}
} }
}; };
@ -657,42 +639,39 @@ public class Session {
return; return;
} }
final HttpClientResponseHandler<Void> responseHandler = new HttpClientResponseHandler<Void>() { final HttpClientResponseHandler<Void> responseHandler = response -> {
@Override final int status = response.getCode();
public Void handleResponse(final ClassicHttpResponse response) throws IOException, HttpException { if (status == HttpStatus.SC_OK) {
final int status = response.getCode(); JsonObject responseJson = OpenVidu.httpResponseEntityToJson(response.getEntity());
if (status == HttpStatus.SC_OK) { sessionId = responseJson.get("id").getAsString();
JsonObject responseJson = OpenVidu.httpResponseEntityToJson(response.getEntity()); createdAt = responseJson.get("createdAt").getAsLong();
sessionId = responseJson.get("id").getAsString();
createdAt = responseJson.get("createdAt").getAsLong();
// Values that get filled by OpenVidu Server from its global or per-session // Values that get filled by OpenVidu Server from its global or per-session
// configuration // configuration
VideoCodec forcedVideoCodec = VideoCodec VideoCodec forcedVideoCodec = VideoCodec
.valueOf(responseJson.get("forcedVideoCodec").getAsString()); .valueOf(responseJson.get("forcedVideoCodec").getAsString());
Boolean allowTranscoding = responseJson.get("allowTranscoding").getAsBoolean(); Boolean allowTranscoding = responseJson.get("allowTranscoding").getAsBoolean();
SessionProperties responseProperties = new SessionProperties.Builder() SessionProperties responseProperties = new SessionProperties.Builder()
.mediaMode(properties.mediaMode()).recordingMode(properties.recordingMode()) .mediaMode(properties.mediaMode()).recordingMode(properties.recordingMode())
.defaultRecordingProperties(properties.defaultRecordingProperties()) .defaultRecordingProperties(properties.defaultRecordingProperties())
.customSessionId(properties.customSessionId()).mediaNode(properties.mediaNode()) .customSessionId(properties.customSessionId()).mediaNode(properties.mediaNode())
.forcedVideoCodec(forcedVideoCodec).allowTranscoding(allowTranscoding).build(); .forcedVideoCodec(forcedVideoCodec).allowTranscoding(allowTranscoding).build();
properties = responseProperties; properties = responseProperties;
log.info("Session '{}' created", sessionId); log.info("Session '{}' created", sessionId);
} else if (status == HttpStatus.SC_CONFLICT) { } else if (status == HttpStatus.SC_CONFLICT) {
// 'customSessionId' already existed // 'customSessionId' already existed
sessionId = properties.customSessionId(); sessionId = properties.customSessionId();
try { try {
fetch(); fetch();
} catch (OpenViduJavaClientException | OpenViduHttpException e) { } catch (OpenViduJavaClientException | OpenViduHttpException e) {
throw OpenVidu.openViduException(e); throw OpenVidu.openViduException(e);
}
} else {
throw OpenVidu.openViduHttpException(status);
} }
return null; } else {
throw OpenVidu.openViduHttpException(status);
} }
return null;
}; };
JsonObject json = properties.toJson(); JsonObject json = properties.toJson();