openvidu-java-client: removed static properties

pull/375/head
pabloFuente 2019-06-03 12:04:49 +02:00
parent fb663b87ad
commit 79334d9acd
2 changed files with 52 additions and 48 deletions

View File

@ -64,9 +64,9 @@ public class OpenVidu {
private static final Logger log = LoggerFactory.getLogger(OpenVidu.class); private static final Logger log = LoggerFactory.getLogger(OpenVidu.class);
private String secret; private String secret;
protected static String urlOpenViduServer; protected String hostname;
protected static HttpClient httpClient; protected HttpClient httpClient;
protected static Map<String, Session> activeSessions = new ConcurrentHashMap<>(); protected Map<String, Session> activeSessions = new ConcurrentHashMap<>();
protected final static String API_SESSIONS = "api/sessions"; protected final static String API_SESSIONS = "api/sessions";
protected final static String API_TOKENS = "api/tokens"; protected final static String API_TOKENS = "api/tokens";
@ -79,12 +79,12 @@ public class OpenVidu {
* Server is up an running * Server is up an running
* @param secret Secret used on OpenVidu Server initialization * @param secret Secret used on OpenVidu Server initialization
*/ */
public OpenVidu(String urlOpenViduServer, String secret) { public OpenVidu(String hostname, String secret) {
OpenVidu.urlOpenViduServer = urlOpenViduServer; this.hostname = hostname;
if (!OpenVidu.urlOpenViduServer.endsWith("/")) { if (!this.hostname.endsWith("/")) {
OpenVidu.urlOpenViduServer += "/"; this.hostname += "/";
} }
this.secret = secret; this.secret = secret;
@ -112,7 +112,7 @@ public class OpenVidu {
requestBuilder = requestBuilder.setConnectTimeout(30000); requestBuilder = requestBuilder.setConnectTimeout(30000);
requestBuilder = requestBuilder.setConnectionRequestTimeout(30000); requestBuilder = requestBuilder.setConnectionRequestTimeout(30000);
OpenVidu.httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestBuilder.build()) this.httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestBuilder.build())
.setConnectionTimeToLive(30, TimeUnit.SECONDS).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) .setConnectionTimeToLive(30, TimeUnit.SECONDS).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.setSSLContext(sslContext).setDefaultCredentialsProvider(provider).build(); .setSSLContext(sslContext).setDefaultCredentialsProvider(provider).build();
} }
@ -126,8 +126,8 @@ public class OpenVidu {
* @throws OpenViduHttpException * @throws OpenViduHttpException
*/ */
public Session createSession() throws OpenViduJavaClientException, OpenViduHttpException { public Session createSession() throws OpenViduJavaClientException, OpenViduHttpException {
Session s = new Session(); Session s = new Session(this);
OpenVidu.activeSessions.put(s.getSessionId(), s); this.activeSessions.put(s.getSessionId(), s);
return s; return s;
} }
@ -150,8 +150,8 @@ public class OpenVidu {
*/ */
public Session createSession(SessionProperties properties) public Session createSession(SessionProperties properties)
throws OpenViduJavaClientException, OpenViduHttpException { throws OpenViduJavaClientException, OpenViduHttpException {
Session s = new Session(properties); Session s = new Session(this, properties);
OpenVidu.activeSessions.put(s.getSessionId(), s); this.activeSessions.put(s.getSessionId(), s);
return s; return s;
} }
@ -191,7 +191,7 @@ public class OpenVidu {
public Recording startRecording(String sessionId, RecordingProperties properties) public Recording startRecording(String sessionId, RecordingProperties properties)
throws OpenViduJavaClientException, OpenViduHttpException { throws OpenViduJavaClientException, OpenViduHttpException {
HttpPost request = new HttpPost(OpenVidu.urlOpenViduServer + API_RECORDINGS + API_RECORDINGS_START); HttpPost request = new HttpPost(this.hostname + API_RECORDINGS + API_RECORDINGS_START);
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("session", sessionId); json.put("session", sessionId);
@ -221,7 +221,7 @@ public class OpenVidu {
HttpResponse response; HttpResponse response;
try { try {
response = OpenVidu.httpClient.execute(request); response = this.httpClient.execute(request);
} catch (IOException e2) { } catch (IOException e2) {
throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause()); throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause());
} }
@ -230,7 +230,7 @@ public class OpenVidu {
int statusCode = response.getStatusLine().getStatusCode(); int statusCode = response.getStatusLine().getStatusCode();
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) { if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
Recording r = new Recording(httpResponseToJson(response)); Recording r = new Recording(httpResponseToJson(response));
Session activeSession = OpenVidu.activeSessions.get(r.getSessionId()); Session activeSession = this.activeSessions.get(r.getSessionId());
if (activeSession != null) { if (activeSession != null) {
activeSession.setIsBeingRecorded(true); activeSession.setIsBeingRecorded(true);
} else { } else {
@ -350,11 +350,10 @@ public class OpenVidu {
* </ul> * </ul>
*/ */
public Recording stopRecording(String recordingId) throws OpenViduJavaClientException, OpenViduHttpException { public Recording stopRecording(String recordingId) throws OpenViduJavaClientException, OpenViduHttpException {
HttpPost request = new HttpPost( HttpPost request = new HttpPost(this.hostname + API_RECORDINGS + API_RECORDINGS_STOP + "/" + recordingId);
OpenVidu.urlOpenViduServer + API_RECORDINGS + API_RECORDINGS_STOP + "/" + recordingId);
HttpResponse response; HttpResponse response;
try { try {
response = OpenVidu.httpClient.execute(request); response = this.httpClient.execute(request);
} catch (IOException e) { } catch (IOException e) {
throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
} }
@ -363,7 +362,7 @@ public class OpenVidu {
int statusCode = response.getStatusLine().getStatusCode(); int statusCode = response.getStatusLine().getStatusCode();
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) { if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
Recording r = new Recording(httpResponseToJson(response)); Recording r = new Recording(httpResponseToJson(response));
Session activeSession = OpenVidu.activeSessions.get(r.getSessionId()); Session activeSession = this.activeSessions.get(r.getSessionId());
if (activeSession != null) { if (activeSession != null) {
activeSession.setIsBeingRecorded(false); activeSession.setIsBeingRecorded(false);
} else { } else {
@ -393,10 +392,10 @@ public class OpenVidu {
* </ul> * </ul>
*/ */
public Recording getRecording(String recordingId) throws OpenViduJavaClientException, OpenViduHttpException { public Recording getRecording(String recordingId) throws OpenViduJavaClientException, OpenViduHttpException {
HttpGet request = new HttpGet(OpenVidu.urlOpenViduServer + API_RECORDINGS + "/" + recordingId); HttpGet request = new HttpGet(this.hostname + API_RECORDINGS + "/" + recordingId);
HttpResponse response; HttpResponse response;
try { try {
response = OpenVidu.httpClient.execute(request); response = this.httpClient.execute(request);
} catch (IOException e) { } catch (IOException e) {
throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
} }
@ -423,10 +422,10 @@ public class OpenVidu {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public List<Recording> listRecordings() throws OpenViduJavaClientException, OpenViduHttpException { public List<Recording> listRecordings() throws OpenViduJavaClientException, OpenViduHttpException {
HttpGet request = new HttpGet(OpenVidu.urlOpenViduServer + API_RECORDINGS); HttpGet request = new HttpGet(this.hostname + API_RECORDINGS);
HttpResponse response; HttpResponse response;
try { try {
response = OpenVidu.httpClient.execute(request); response = this.httpClient.execute(request);
} catch (IOException e) { } catch (IOException e) {
throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
} }
@ -468,10 +467,10 @@ public class OpenVidu {
* </ul> * </ul>
*/ */
public void deleteRecording(String recordingId) throws OpenViduJavaClientException, OpenViduHttpException { public void deleteRecording(String recordingId) throws OpenViduJavaClientException, OpenViduHttpException {
HttpDelete request = new HttpDelete(OpenVidu.urlOpenViduServer + API_RECORDINGS + "/" + recordingId); HttpDelete request = new HttpDelete(this.hostname + API_RECORDINGS + "/" + recordingId);
HttpResponse response; HttpResponse response;
try { try {
response = OpenVidu.httpClient.execute(request); response = this.httpClient.execute(request);
} catch (IOException e) { } catch (IOException e) {
throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
} }
@ -513,7 +512,7 @@ public class OpenVidu {
* {@link io.openvidu.java.client.OpenVidu#getActiveSessions()} * {@link io.openvidu.java.client.OpenVidu#getActiveSessions()}
*/ */
public List<Session> getActiveSessions() { public List<Session> getActiveSessions() {
return new ArrayList<>(OpenVidu.activeSessions.values()); return new ArrayList<>(this.activeSessions.values());
} }
/** /**
@ -531,11 +530,11 @@ public class OpenVidu {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public boolean fetch() throws OpenViduJavaClientException, OpenViduHttpException { public boolean fetch() throws OpenViduJavaClientException, OpenViduHttpException {
HttpGet request = new HttpGet(OpenVidu.urlOpenViduServer + API_SESSIONS); HttpGet request = new HttpGet(this.hostname + API_SESSIONS);
HttpResponse response; HttpResponse response;
try { try {
response = OpenVidu.httpClient.execute(request); response = this.httpClient.execute(request);
} catch (IOException e) { } catch (IOException e) {
throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
} }
@ -553,7 +552,7 @@ public class OpenVidu {
jsonArraySessions.forEach(session -> { jsonArraySessions.forEach(session -> {
String sessionId = (String) ((JSONObject) session).get("sessionId"); String sessionId = (String) ((JSONObject) session).get("sessionId");
fetchedSessionIds.add(sessionId); fetchedSessionIds.add(sessionId);
OpenVidu.activeSessions.computeIfPresent(sessionId, (sId, s) -> { this.activeSessions.computeIfPresent(sessionId, (sId, s) -> {
String beforeJSON = s.toJson(); String beforeJSON = s.toJson();
s = s.resetSessionWithJson((JSONObject) session); s = s.resetSessionWithJson((JSONObject) session);
String afterJSON = s.toJson(); String afterJSON = s.toJson();
@ -562,7 +561,7 @@ public class OpenVidu {
log.info("Available session '{}' info fetched. Any change: {}", sessionId, changed); log.info("Available session '{}' info fetched. Any change: {}", sessionId, changed);
return s; return s;
}); });
OpenVidu.activeSessions.computeIfAbsent(sessionId, sId -> { this.activeSessions.computeIfAbsent(sessionId, sId -> {
log.info("New session '{}' fetched", sessionId); log.info("New session '{}' fetched", sessionId);
hasChanged[0] = true; hasChanged[0] = true;
return new Session((JSONObject) session); return new Session((JSONObject) session);
@ -570,7 +569,7 @@ public class OpenVidu {
}); });
// Remove closed sessions from activeSessions map // Remove closed sessions from activeSessions map
OpenVidu.activeSessions = OpenVidu.activeSessions.entrySet().stream().filter(entry -> { this.activeSessions = this.activeSessions.entrySet().stream().filter(entry -> {
if (fetchedSessionIds.contains(entry.getKey())) { if (fetchedSessionIds.contains(entry.getKey())) {
return true; return true;
} else { } else {
@ -579,7 +578,7 @@ public class OpenVidu {
return false; return false;
} }
}).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())); }).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
log.info("Active sessions info fetched: {}", OpenVidu.activeSessions.keySet()); log.info("Active sessions info fetched: {}", this.activeSessions.keySet());
return hasChanged[0]; return hasChanged[0];
} else { } else {
throw new OpenViduHttpException(statusCode); throw new OpenViduHttpException(statusCode);

View File

@ -27,6 +27,7 @@ import java.util.stream.Collectors;
import org.apache.http.HttpHeaders; import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
@ -45,16 +46,20 @@ public class Session {
private String sessionId; private String sessionId;
private long createdAt; private long createdAt;
private OpenVidu openVidu;
private SessionProperties properties; private SessionProperties properties;
private Map<String, Connection> activeConnections = new ConcurrentHashMap<>(); private Map<String, Connection> activeConnections = new ConcurrentHashMap<>();
private boolean recording = false; private boolean recording = false;
protected Session() throws OpenViduJavaClientException, OpenViduHttpException { protected Session(OpenVidu openVidu) throws OpenViduJavaClientException, OpenViduHttpException {
this.openVidu = openVidu;
this.properties = new SessionProperties.Builder().build(); this.properties = new SessionProperties.Builder().build();
this.getSessionIdHttp(); this.getSessionIdHttp();
} }
protected Session(SessionProperties properties) throws OpenViduJavaClientException, OpenViduHttpException { protected Session(OpenVidu openVidu, SessionProperties properties)
throws OpenViduJavaClientException, OpenViduHttpException {
this.openVidu = openVidu;
this.properties = properties; this.properties = properties;
this.getSessionIdHttp(); this.getSessionIdHttp();
} }
@ -111,7 +116,7 @@ public class Session {
this.getSessionId(); this.getSessionId();
} }
HttpPost request = new HttpPost(OpenVidu.urlOpenViduServer + OpenVidu.API_TOKENS); HttpPost request = new HttpPost(this.openVidu.hostname + OpenVidu.API_TOKENS);
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("session", this.sessionId); json.put("session", this.sessionId);
@ -156,7 +161,7 @@ public class Session {
HttpResponse response; HttpResponse response;
try { try {
response = OpenVidu.httpClient.execute(request); response = this.openVidu.httpClient.execute(request);
} catch (IOException e2) { } catch (IOException e2) {
throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause()); throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause());
} }
@ -183,12 +188,12 @@ public class Session {
* @throws OpenViduHttpException * @throws OpenViduHttpException
*/ */
public void close() throws OpenViduJavaClientException, OpenViduHttpException { public void close() throws OpenViduJavaClientException, OpenViduHttpException {
HttpDelete request = new HttpDelete(OpenVidu.urlOpenViduServer + OpenVidu.API_SESSIONS + "/" + this.sessionId); HttpDelete request = new HttpDelete(this.openVidu.hostname + OpenVidu.API_SESSIONS + "/" + this.sessionId);
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded"); request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
HttpResponse response; HttpResponse response;
try { try {
response = OpenVidu.httpClient.execute(request); response = this.openVidu.httpClient.execute(request);
} catch (IOException e) { } catch (IOException e) {
throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
} }
@ -196,7 +201,7 @@ public class Session {
try { try {
int statusCode = response.getStatusLine().getStatusCode(); int statusCode = response.getStatusLine().getStatusCode();
if ((statusCode == org.apache.http.HttpStatus.SC_NO_CONTENT)) { if ((statusCode == org.apache.http.HttpStatus.SC_NO_CONTENT)) {
OpenVidu.activeSessions.remove(this.sessionId); this.openVidu.activeSessions.remove(this.sessionId);
log.info("Session {} closed", this.sessionId); log.info("Session {} closed", this.sessionId);
} else { } else {
throw new OpenViduHttpException(statusCode); throw new OpenViduHttpException(statusCode);
@ -227,12 +232,12 @@ public class Session {
*/ */
public boolean fetch() throws OpenViduJavaClientException, OpenViduHttpException { public boolean fetch() throws OpenViduJavaClientException, OpenViduHttpException {
String beforeJSON = this.toJson(); String beforeJSON = this.toJson();
HttpGet request = new HttpGet(OpenVidu.urlOpenViduServer + OpenVidu.API_SESSIONS + "/" + this.sessionId); HttpGet request = new HttpGet(this.openVidu.hostname + OpenVidu.API_SESSIONS + "/" + this.sessionId);
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded"); request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
HttpResponse response; HttpResponse response;
try { try {
response = OpenVidu.httpClient.execute(request); response = this.openVidu.httpClient.execute(request);
} catch (IOException e) { } catch (IOException e) {
throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
} }
@ -287,13 +292,13 @@ public class Session {
* @throws OpenViduHttpException * @throws OpenViduHttpException
*/ */
public void forceDisconnect(String connectionId) throws OpenViduJavaClientException, OpenViduHttpException { public void forceDisconnect(String connectionId) throws OpenViduJavaClientException, OpenViduHttpException {
HttpDelete request = new HttpDelete(OpenVidu.urlOpenViduServer + OpenVidu.API_SESSIONS + "/" + this.sessionId HttpDelete request = new HttpDelete(
+ "/connection/" + connectionId); this.openVidu.hostname + OpenVidu.API_SESSIONS + "/" + this.sessionId + "/connection/" + connectionId);
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded"); request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
HttpResponse response = null; HttpResponse response = null;
try { try {
response = OpenVidu.httpClient.execute(request); response = this.openVidu.httpClient.execute(request);
} catch (IOException e) { } catch (IOException e) {
throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
} }
@ -366,12 +371,12 @@ public class Session {
*/ */
public void forceUnpublish(String streamId) throws OpenViduJavaClientException, OpenViduHttpException { public void forceUnpublish(String streamId) throws OpenViduJavaClientException, OpenViduHttpException {
HttpDelete request = new HttpDelete( HttpDelete request = new HttpDelete(
OpenVidu.urlOpenViduServer + OpenVidu.API_SESSIONS + "/" + this.sessionId + "/stream/" + streamId); this.openVidu.hostname + OpenVidu.API_SESSIONS + "/" + this.sessionId + "/stream/" + streamId);
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded"); request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
HttpResponse response; HttpResponse response;
try { try {
response = OpenVidu.httpClient.execute(request); response = this.openVidu.httpClient.execute(request);
} catch (IOException e) { } catch (IOException e) {
throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
} }
@ -445,7 +450,7 @@ public class Session {
return; return;
} }
HttpPost request = new HttpPost(OpenVidu.urlOpenViduServer + OpenVidu.API_SESSIONS); HttpPost request = new HttpPost(this.openVidu.hostname + OpenVidu.API_SESSIONS);
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("mediaMode", properties.mediaMode().name()); json.put("mediaMode", properties.mediaMode().name());
@ -466,7 +471,7 @@ public class Session {
HttpResponse response; HttpResponse response;
try { try {
response = OpenVidu.httpClient.execute(request); response = this.openVidu.httpClient.execute(request);
} catch (IOException e2) { } catch (IOException e2) {
throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause()); throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause());
} }