mirror of https://github.com/OpenVidu/openvidu.git
openvidu-java-client: removed static properties
parent
fb663b87ad
commit
79334d9acd
|
@ -64,9 +64,9 @@ public class OpenVidu {
|
|||
private static final Logger log = LoggerFactory.getLogger(OpenVidu.class);
|
||||
|
||||
private String secret;
|
||||
protected static String urlOpenViduServer;
|
||||
protected static HttpClient httpClient;
|
||||
protected static Map<String, Session> activeSessions = new ConcurrentHashMap<>();
|
||||
protected String hostname;
|
||||
protected HttpClient httpClient;
|
||||
protected Map<String, Session> activeSessions = new ConcurrentHashMap<>();
|
||||
|
||||
protected final static String API_SESSIONS = "api/sessions";
|
||||
protected final static String API_TOKENS = "api/tokens";
|
||||
|
@ -79,12 +79,12 @@ public class OpenVidu {
|
|||
* Server is up an running
|
||||
* @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("/")) {
|
||||
OpenVidu.urlOpenViduServer += "/";
|
||||
if (!this.hostname.endsWith("/")) {
|
||||
this.hostname += "/";
|
||||
}
|
||||
|
||||
this.secret = secret;
|
||||
|
@ -112,7 +112,7 @@ public class OpenVidu {
|
|||
requestBuilder = requestBuilder.setConnectTimeout(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)
|
||||
.setSSLContext(sslContext).setDefaultCredentialsProvider(provider).build();
|
||||
}
|
||||
|
@ -126,8 +126,8 @@ public class OpenVidu {
|
|||
* @throws OpenViduHttpException
|
||||
*/
|
||||
public Session createSession() throws OpenViduJavaClientException, OpenViduHttpException {
|
||||
Session s = new Session();
|
||||
OpenVidu.activeSessions.put(s.getSessionId(), s);
|
||||
Session s = new Session(this);
|
||||
this.activeSessions.put(s.getSessionId(), s);
|
||||
return s;
|
||||
}
|
||||
|
||||
|
@ -150,8 +150,8 @@ public class OpenVidu {
|
|||
*/
|
||||
public Session createSession(SessionProperties properties)
|
||||
throws OpenViduJavaClientException, OpenViduHttpException {
|
||||
Session s = new Session(properties);
|
||||
OpenVidu.activeSessions.put(s.getSessionId(), s);
|
||||
Session s = new Session(this, properties);
|
||||
this.activeSessions.put(s.getSessionId(), s);
|
||||
return s;
|
||||
}
|
||||
|
||||
|
@ -191,7 +191,7 @@ public class OpenVidu {
|
|||
public Recording startRecording(String sessionId, RecordingProperties properties)
|
||||
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();
|
||||
json.put("session", sessionId);
|
||||
|
@ -221,7 +221,7 @@ public class OpenVidu {
|
|||
|
||||
HttpResponse response;
|
||||
try {
|
||||
response = OpenVidu.httpClient.execute(request);
|
||||
response = this.httpClient.execute(request);
|
||||
} catch (IOException e2) {
|
||||
throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause());
|
||||
}
|
||||
|
@ -230,7 +230,7 @@ public class OpenVidu {
|
|||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
Recording r = new Recording(httpResponseToJson(response));
|
||||
Session activeSession = OpenVidu.activeSessions.get(r.getSessionId());
|
||||
Session activeSession = this.activeSessions.get(r.getSessionId());
|
||||
if (activeSession != null) {
|
||||
activeSession.setIsBeingRecorded(true);
|
||||
} else {
|
||||
|
@ -350,11 +350,10 @@ public class OpenVidu {
|
|||
* </ul>
|
||||
*/
|
||||
public Recording stopRecording(String recordingId) throws OpenViduJavaClientException, OpenViduHttpException {
|
||||
HttpPost request = new HttpPost(
|
||||
OpenVidu.urlOpenViduServer + API_RECORDINGS + API_RECORDINGS_STOP + "/" + recordingId);
|
||||
HttpPost request = new HttpPost(this.hostname + API_RECORDINGS + API_RECORDINGS_STOP + "/" + recordingId);
|
||||
HttpResponse response;
|
||||
try {
|
||||
response = OpenVidu.httpClient.execute(request);
|
||||
response = this.httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
@ -363,7 +362,7 @@ public class OpenVidu {
|
|||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)) {
|
||||
Recording r = new Recording(httpResponseToJson(response));
|
||||
Session activeSession = OpenVidu.activeSessions.get(r.getSessionId());
|
||||
Session activeSession = this.activeSessions.get(r.getSessionId());
|
||||
if (activeSession != null) {
|
||||
activeSession.setIsBeingRecorded(false);
|
||||
} else {
|
||||
|
@ -393,10 +392,10 @@ public class OpenVidu {
|
|||
* </ul>
|
||||
*/
|
||||
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;
|
||||
try {
|
||||
response = OpenVidu.httpClient.execute(request);
|
||||
response = this.httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
@ -423,10 +422,10 @@ public class OpenVidu {
|
|||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
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;
|
||||
try {
|
||||
response = OpenVidu.httpClient.execute(request);
|
||||
response = this.httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
@ -468,10 +467,10 @@ public class OpenVidu {
|
|||
* </ul>
|
||||
*/
|
||||
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;
|
||||
try {
|
||||
response = OpenVidu.httpClient.execute(request);
|
||||
response = this.httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
@ -513,7 +512,7 @@ public class OpenVidu {
|
|||
* {@link io.openvidu.java.client.OpenVidu#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")
|
||||
public boolean fetch() throws OpenViduJavaClientException, OpenViduHttpException {
|
||||
HttpGet request = new HttpGet(OpenVidu.urlOpenViduServer + API_SESSIONS);
|
||||
HttpGet request = new HttpGet(this.hostname + API_SESSIONS);
|
||||
|
||||
HttpResponse response;
|
||||
try {
|
||||
response = OpenVidu.httpClient.execute(request);
|
||||
response = this.httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
@ -553,7 +552,7 @@ public class OpenVidu {
|
|||
jsonArraySessions.forEach(session -> {
|
||||
String sessionId = (String) ((JSONObject) session).get("sessionId");
|
||||
fetchedSessionIds.add(sessionId);
|
||||
OpenVidu.activeSessions.computeIfPresent(sessionId, (sId, s) -> {
|
||||
this.activeSessions.computeIfPresent(sessionId, (sId, s) -> {
|
||||
String beforeJSON = s.toJson();
|
||||
s = s.resetSessionWithJson((JSONObject) session);
|
||||
String afterJSON = s.toJson();
|
||||
|
@ -562,7 +561,7 @@ public class OpenVidu {
|
|||
log.info("Available session '{}' info fetched. Any change: {}", sessionId, changed);
|
||||
return s;
|
||||
});
|
||||
OpenVidu.activeSessions.computeIfAbsent(sessionId, sId -> {
|
||||
this.activeSessions.computeIfAbsent(sessionId, sId -> {
|
||||
log.info("New session '{}' fetched", sessionId);
|
||||
hasChanged[0] = true;
|
||||
return new Session((JSONObject) session);
|
||||
|
@ -570,7 +569,7 @@ public class OpenVidu {
|
|||
});
|
||||
|
||||
// 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())) {
|
||||
return true;
|
||||
} else {
|
||||
|
@ -579,7 +578,7 @@ public class OpenVidu {
|
|||
return false;
|
||||
}
|
||||
}).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];
|
||||
} else {
|
||||
throw new OpenViduHttpException(statusCode);
|
||||
|
|
|
@ -27,6 +27,7 @@ import java.util.stream.Collectors;
|
|||
|
||||
import org.apache.http.HttpHeaders;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpDelete;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
|
@ -45,16 +46,20 @@ public class Session {
|
|||
|
||||
private String sessionId;
|
||||
private long createdAt;
|
||||
private OpenVidu openVidu;
|
||||
private SessionProperties properties;
|
||||
private Map<String, Connection> activeConnections = new ConcurrentHashMap<>();
|
||||
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.getSessionIdHttp();
|
||||
}
|
||||
|
||||
protected Session(SessionProperties properties) throws OpenViduJavaClientException, OpenViduHttpException {
|
||||
protected Session(OpenVidu openVidu, SessionProperties properties)
|
||||
throws OpenViduJavaClientException, OpenViduHttpException {
|
||||
this.openVidu = openVidu;
|
||||
this.properties = properties;
|
||||
this.getSessionIdHttp();
|
||||
}
|
||||
|
@ -111,7 +116,7 @@ public class Session {
|
|||
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();
|
||||
json.put("session", this.sessionId);
|
||||
|
@ -156,7 +161,7 @@ public class Session {
|
|||
|
||||
HttpResponse response;
|
||||
try {
|
||||
response = OpenVidu.httpClient.execute(request);
|
||||
response = this.openVidu.httpClient.execute(request);
|
||||
} catch (IOException e2) {
|
||||
throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause());
|
||||
}
|
||||
|
@ -183,12 +188,12 @@ public class Session {
|
|||
* @throws 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");
|
||||
|
||||
HttpResponse response;
|
||||
try {
|
||||
response = OpenVidu.httpClient.execute(request);
|
||||
response = this.openVidu.httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
@ -196,7 +201,7 @@ public class Session {
|
|||
try {
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
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);
|
||||
} else {
|
||||
throw new OpenViduHttpException(statusCode);
|
||||
|
@ -227,12 +232,12 @@ public class Session {
|
|||
*/
|
||||
public boolean fetch() throws OpenViduJavaClientException, OpenViduHttpException {
|
||||
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");
|
||||
|
||||
HttpResponse response;
|
||||
try {
|
||||
response = OpenVidu.httpClient.execute(request);
|
||||
response = this.openVidu.httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
@ -287,13 +292,13 @@ public class Session {
|
|||
* @throws OpenViduHttpException
|
||||
*/
|
||||
public void forceDisconnect(String connectionId) throws OpenViduJavaClientException, OpenViduHttpException {
|
||||
HttpDelete request = new HttpDelete(OpenVidu.urlOpenViduServer + OpenVidu.API_SESSIONS + "/" + this.sessionId
|
||||
+ "/connection/" + connectionId);
|
||||
HttpDelete request = new HttpDelete(
|
||||
this.openVidu.hostname + OpenVidu.API_SESSIONS + "/" + this.sessionId + "/connection/" + connectionId);
|
||||
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
|
||||
|
||||
HttpResponse response = null;
|
||||
try {
|
||||
response = OpenVidu.httpClient.execute(request);
|
||||
response = this.openVidu.httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
@ -366,12 +371,12 @@ public class Session {
|
|||
*/
|
||||
public void forceUnpublish(String streamId) throws OpenViduJavaClientException, OpenViduHttpException {
|
||||
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");
|
||||
|
||||
HttpResponse response;
|
||||
try {
|
||||
response = OpenVidu.httpClient.execute(request);
|
||||
response = this.openVidu.httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
|
||||
}
|
||||
|
@ -445,7 +450,7 @@ public class Session {
|
|||
return;
|
||||
}
|
||||
|
||||
HttpPost request = new HttpPost(OpenVidu.urlOpenViduServer + OpenVidu.API_SESSIONS);
|
||||
HttpPost request = new HttpPost(this.openVidu.hostname + OpenVidu.API_SESSIONS);
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("mediaMode", properties.mediaMode().name());
|
||||
|
@ -466,7 +471,7 @@ public class Session {
|
|||
|
||||
HttpResponse response;
|
||||
try {
|
||||
response = OpenVidu.httpClient.execute(request);
|
||||
response = this.openVidu.httpClient.execute(request);
|
||||
} catch (IOException e2) {
|
||||
throw new OpenViduJavaClientException(e2.getMessage(), e2.getCause());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue