From b24d89c8847bf1162e9bb277eabda8b5e8b045df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A2=85=EB=AA=85?= Date: Mon, 16 Oct 2023 17:57:22 +0900 Subject: [PATCH 1/2] openvidu-java-client : change to lambda function --- .../java/io/openvidu/java/client/Session.java | 247 ++++++++---------- 1 file changed, 113 insertions(+), 134 deletions(-) diff --git a/openvidu-java-client/src/main/java/io/openvidu/java/client/Session.java b/openvidu-java-client/src/main/java/io/openvidu/java/client/Session.java index 054ddfdb18..a3eb4481e8 100644 --- a/openvidu-java-client/src/main/java/io/openvidu/java/client/Session.java +++ b/openvidu-java-client/src/main/java/io/openvidu/java/client/Session.java @@ -123,17 +123,14 @@ public String generateToken(TokenOptions tokenOptions) throws OpenViduJavaClient this.getSessionId(); } - final HttpClientResponseHandler responseHandler = new HttpClientResponseHandler() { - @Override - public String handleResponse(final ClassicHttpResponse response) throws IOException, HttpException { - final int status = response.getCode(); - if (status == HttpStatus.SC_OK) { - String token = OpenVidu.httpResponseEntityToJson(response.getEntity()).get("id").getAsString(); - log.info("Returning a TOKEN: {}", token); - return token; - } else { - throw OpenVidu.openViduHttpException(status); - } + final HttpClientResponseHandler responseHandler = response -> { + final int status = response.getCode(); + if (status == HttpStatus.SC_OK) { + String token = OpenVidu.httpResponseEntityToJson(response.getEntity()).get("id").getAsString(); + log.info("Returning a TOKEN: {}", token); + return token; + } else { + throw OpenVidu.openViduHttpException(status); } }; @@ -224,18 +221,15 @@ public Connection handleResponse(final ClassicHttpResponse response) throws IOEx */ public void close() throws OpenViduJavaClientException, OpenViduHttpException { - final HttpClientResponseHandler responseHandler = new HttpClientResponseHandler() { - @Override - public Void handleResponse(final ClassicHttpResponse response) throws IOException, HttpException { - final int status = response.getCode(); - if (status == HttpStatus.SC_NO_CONTENT) { - openVidu.activeSessions.remove(sessionId); - log.info("Session {} closed", sessionId); - } else { - throw OpenVidu.openViduHttpException(status); - } - return null; + final HttpClientResponseHandler responseHandler = response -> { + final int status = response.getCode(); + if (status == HttpStatus.SC_NO_CONTENT) { + openVidu.activeSessions.remove(sessionId); + log.info("Session {} closed", sessionId); + } else { + throw OpenVidu.openViduHttpException(status); } + return null; }; HttpDelete request = new HttpDelete(this.openVidu.hostname + OpenVidu.API_SESSIONS + "/" + this.sessionId); @@ -272,19 +266,16 @@ public boolean fetch() throws OpenViduJavaClientException, OpenViduHttpException final String beforeJSON = this.toJson(); - final HttpClientResponseHandler responseHandler = new HttpClientResponseHandler() { - @Override - public Boolean handleResponse(final ClassicHttpResponse response) throws IOException, HttpException { - final int status = response.getCode(); - if (status == HttpStatus.SC_OK) { - resetWithJson(OpenVidu.httpResponseEntityToJson(response.getEntity())); - final String afterJSON = toJson(); - boolean hasChanged = !beforeJSON.equals(afterJSON); - log.info("Session info fetched for session '{}'. Any change: {}", sessionId, hasChanged); - return hasChanged; - } else { - throw OpenVidu.openViduHttpException(status); - } + final HttpClientResponseHandler responseHandler = response -> { + final int status = response.getCode(); + if (status == HttpStatus.SC_OK) { + resetWithJson(OpenVidu.httpResponseEntityToJson(response.getEntity())); + final String afterJSON = toJson(); + boolean hasChanged = !beforeJSON.equals(afterJSON); + log.info("Session info fetched for session '{}'. Any change: {}", sessionId, hasChanged); + return hasChanged; + } else { + throw OpenVidu.openViduHttpException(status); } }; @@ -344,34 +335,31 @@ public void forceDisconnect(Connection connection) throws OpenViduJavaClientExce */ public void forceDisconnect(String connectionId) throws OpenViduJavaClientException, OpenViduHttpException { - final HttpClientResponseHandler responseHandler = new HttpClientResponseHandler() { - @Override - public Void handleResponse(final ClassicHttpResponse response) throws IOException, HttpException { - final int status = response.getCode(); - if (status == HttpStatus.SC_NO_CONTENT) { - // Remove connection from activeConnections map - Connection connectionClosed = connections.remove(connectionId); - // Remove every Publisher of the closed connection from every subscriber list of - // other connections - if (connectionClosed != null) { - for (Publisher publisher : connectionClosed.getPublishers()) { - String streamId = publisher.getStreamId(); - for (Connection connection : connections.values()) { - connection.setSubscribers(connection.getSubscribers().stream() - .filter(subscriber -> !streamId.equals(subscriber)) - .collect(Collectors.toList())); - } + final HttpClientResponseHandler responseHandler = response -> { + final int status = response.getCode(); + if (status == HttpStatus.SC_NO_CONTENT) { + // Remove connection from activeConnections map + Connection connectionClosed = connections.remove(connectionId); + // Remove every Publisher of the closed connection from every subscriber list of + // other connections + if (connectionClosed != null) { + for (Publisher publisher : connectionClosed.getPublishers()) { + String streamId = publisher.getStreamId(); + for (Connection connection : connections.values()) { + connection.setSubscribers(connection.getSubscribers().stream() + .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 { - 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( @@ -427,25 +415,22 @@ public void forceUnpublish(Publisher publisher) throws OpenViduJavaClientExcepti */ public void forceUnpublish(String streamId) throws OpenViduJavaClientException, OpenViduHttpException { - final HttpClientResponseHandler responseHandler = new HttpClientResponseHandler() { - @Override - public Void handleResponse(final ClassicHttpResponse response) throws IOException, HttpException { - final int status = response.getCode(); - if (status == HttpStatus.SC_NO_CONTENT) { - for (Connection connection : connections.values()) { - // Try to remove the Publisher from the Connection publishers collection - if (connection.publishers.remove(streamId) != null) { - continue; - } - // Try to remove the Publisher from the Connection subscribers collection - connection.subscribers.remove(streamId); + final HttpClientResponseHandler responseHandler = response -> { + final int status = response.getCode(); + if (status == HttpStatus.SC_NO_CONTENT) { + for (Connection connection : connections.values()) { + // Try to remove the Publisher from the Connection publishers collection + if (connection.publishers.remove(streamId) != null) { + continue; } - log.info("Stream {} unpublished", streamId); - } else { - throw OpenVidu.openViduHttpException(status); + // Try to remove the Publisher from the Connection subscribers collection + connection.subscribers.remove(streamId); } - return null; + log.info("Stream {} unpublished", streamId); + } else { + throw OpenVidu.openViduHttpException(status); } + return null; }; HttpDelete request = new HttpDelete( @@ -499,32 +484,29 @@ public Void handleResponse(final ClassicHttpResponse response) throws IOExceptio public Connection updateConnection(String connectionId, ConnectionProperties connectionProperties) throws OpenViduJavaClientException, OpenViduHttpException { - final HttpClientResponseHandler responseHandler = new HttpClientResponseHandler() { - @Override - public Connection handleResponse(final ClassicHttpResponse response) throws IOException, HttpException { - final int status = response.getCode(); - if (status == HttpStatus.SC_OK) { - log.info("Connection {} updated", connectionId); - } else if (status == HttpStatus.SC_NO_CONTENT) { - log.info("Properties of Connection {} remain the same", connectionId); - } else { - throw OpenVidu.openViduHttpException(status); - } - JsonObject json = OpenVidu.httpResponseEntityToJson(response.getEntity()); - - // Update the actual Connection object with the new options - Connection existingConnection = connections.get(connectionId); - - if (existingConnection == null) { - // The updated Connection is not available in local map - Connection newConnection = new Connection(json); - connections.put(connectionId, newConnection); - return newConnection; - } else { - // The updated Connection was available in local map - existingConnection.overrideConnectionProperties(connectionProperties); - return existingConnection; - } + final HttpClientResponseHandler responseHandler = response -> { + final int status = response.getCode(); + if (status == HttpStatus.SC_OK) { + log.info("Connection {} updated", connectionId); + } else if (status == HttpStatus.SC_NO_CONTENT) { + log.info("Properties of Connection {} remain the same", connectionId); + } else { + throw OpenVidu.openViduHttpException(status); + } + JsonObject json = OpenVidu.httpResponseEntityToJson(response.getEntity()); + + // Update the actual Connection object with the new options + Connection existingConnection = connections.get(connectionId); + + if (existingConnection == null) { + // The updated Connection is not available in local map + Connection newConnection = new Connection(json); + connections.put(connectionId, newConnection); + return newConnection; + } else { + // The updated Connection was available in local map + existingConnection.overrideConnectionProperties(connectionProperties); + return existingConnection; } }; @@ -657,42 +639,39 @@ private void getSessionHttp() throws OpenViduJavaClientException, OpenViduHttpEx return; } - final HttpClientResponseHandler responseHandler = new HttpClientResponseHandler() { - @Override - public Void handleResponse(final ClassicHttpResponse response) throws IOException, HttpException { - final int status = response.getCode(); - if (status == HttpStatus.SC_OK) { - JsonObject responseJson = OpenVidu.httpResponseEntityToJson(response.getEntity()); - sessionId = responseJson.get("id").getAsString(); - createdAt = responseJson.get("createdAt").getAsLong(); - - // Values that get filled by OpenVidu Server from its global or per-session - // configuration - VideoCodec forcedVideoCodec = VideoCodec - .valueOf(responseJson.get("forcedVideoCodec").getAsString()); - Boolean allowTranscoding = responseJson.get("allowTranscoding").getAsBoolean(); - - SessionProperties responseProperties = new SessionProperties.Builder() - .mediaMode(properties.mediaMode()).recordingMode(properties.recordingMode()) - .defaultRecordingProperties(properties.defaultRecordingProperties()) - .customSessionId(properties.customSessionId()).mediaNode(properties.mediaNode()) - .forcedVideoCodec(forcedVideoCodec).allowTranscoding(allowTranscoding).build(); - - properties = responseProperties; - log.info("Session '{}' created", sessionId); - } else if (status == HttpStatus.SC_CONFLICT) { - // 'customSessionId' already existed - sessionId = properties.customSessionId(); - try { - fetch(); - } catch (OpenViduJavaClientException | OpenViduHttpException e) { - throw OpenVidu.openViduException(e); - } - } else { - throw OpenVidu.openViduHttpException(status); + final HttpClientResponseHandler responseHandler = response -> { + final int status = response.getCode(); + if (status == HttpStatus.SC_OK) { + JsonObject responseJson = OpenVidu.httpResponseEntityToJson(response.getEntity()); + sessionId = responseJson.get("id").getAsString(); + createdAt = responseJson.get("createdAt").getAsLong(); + + // Values that get filled by OpenVidu Server from its global or per-session + // configuration + VideoCodec forcedVideoCodec = VideoCodec + .valueOf(responseJson.get("forcedVideoCodec").getAsString()); + Boolean allowTranscoding = responseJson.get("allowTranscoding").getAsBoolean(); + + SessionProperties responseProperties = new SessionProperties.Builder() + .mediaMode(properties.mediaMode()).recordingMode(properties.recordingMode()) + .defaultRecordingProperties(properties.defaultRecordingProperties()) + .customSessionId(properties.customSessionId()).mediaNode(properties.mediaNode()) + .forcedVideoCodec(forcedVideoCodec).allowTranscoding(allowTranscoding).build(); + + properties = responseProperties; + log.info("Session '{}' created", sessionId); + } else if (status == HttpStatus.SC_CONFLICT) { + // 'customSessionId' already existed + sessionId = properties.customSessionId(); + try { + fetch(); + } catch (OpenViduJavaClientException | OpenViduHttpException e) { + throw OpenVidu.openViduException(e); } - return null; + } else { + throw OpenVidu.openViduHttpException(status); } + return null; }; JsonObject json = properties.toJson(); From 406289cf57049721e78e3b3c48d71a8e8f8dd5bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A2=85=EB=AA=85?= Date: Mon, 16 Oct 2023 17:57:59 +0900 Subject: [PATCH 2/2] openvidu-java-client : change to lambda function --- .../java/io/openvidu/java/client/Session.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/openvidu-java-client/src/main/java/io/openvidu/java/client/Session.java b/openvidu-java-client/src/main/java/io/openvidu/java/client/Session.java index a3eb4481e8..51d138a0ae 100644 --- a/openvidu-java-client/src/main/java/io/openvidu/java/client/Session.java +++ b/openvidu-java-client/src/main/java/io/openvidu/java/client/Session.java @@ -183,17 +183,14 @@ public Connection createConnection(ConnectionProperties connectionProperties) this.getSessionId(); } - final HttpClientResponseHandler responseHandler = new HttpClientResponseHandler() { - @Override - public Connection handleResponse(final ClassicHttpResponse response) throws IOException, HttpException { - final int status = response.getCode(); - if (status == HttpStatus.SC_OK) { - Connection connection = new Connection(OpenVidu.httpResponseEntityToJson(response.getEntity())); - connections.put(connection.getConnectionId(), connection); - return connection; - } else { - throw OpenVidu.openViduHttpException(status); - } + final HttpClientResponseHandler responseHandler = response -> { + final int status = response.getCode(); + if (status == HttpStatus.SC_OK) { + Connection connection = new Connection(OpenVidu.httpResponseEntityToJson(response.getEntity())); + connections.put(connection.getConnectionId(), connection); + return connection; + } else { + throw OpenVidu.openViduHttpException(status); } };