11package urjc .ovteaching ;
22
3+ import java .io .IOException ;
4+ import java .io .UnsupportedEncodingException ;
5+ import java .security .KeyManagementException ;
6+ import java .security .KeyStoreException ;
7+ import java .security .NoSuchAlgorithmException ;
8+ import java .security .cert .CertificateException ;
9+ import java .security .cert .X509Certificate ;
310import java .util .Collection ;
411import java .util .HashMap ;
512import java .util .HashSet ;
613import java .util .Map ;
714import java .util .Optional ;
815import java .util .Set ;
916import java .util .concurrent .ConcurrentHashMap ;
17+ import java .util .concurrent .TimeUnit ;
1018
19+ import javax .net .ssl .SSLContext ;
20+
21+ import org .apache .http .HttpHeaders ;
22+ import org .apache .http .HttpResponse ;
23+ import org .apache .http .auth .AuthScope ;
24+ import org .apache .http .auth .UsernamePasswordCredentials ;
25+ import org .apache .http .client .CredentialsProvider ;
26+ import org .apache .http .client .HttpClient ;
27+ import org .apache .http .client .config .RequestConfig ;
28+ import org .apache .http .client .methods .HttpPost ;
29+ import org .apache .http .conn .ssl .NoopHostnameVerifier ;
30+ import org .apache .http .entity .StringEntity ;
31+ import org .apache .http .impl .client .BasicCredentialsProvider ;
32+ import org .apache .http .impl .client .HttpClientBuilder ;
33+ import org .apache .http .ssl .SSLContextBuilder ;
34+ import org .apache .http .ssl .TrustStrategy ;
35+ import org .apache .http .util .EntityUtils ;
1136import org .springframework .beans .factory .annotation .Autowired ;
1237import org .springframework .beans .factory .annotation .Value ;
1338import org .springframework .stereotype .Component ;
1439
40+ import com .google .gson .Gson ;
41+ import com .google .gson .JsonObject ;
42+
1543import io .openvidu .java .client .OpenVidu ;
1644import io .openvidu .java .client .OpenViduHttpException ;
1745import io .openvidu .java .client .OpenViduJavaClientException ;
1846import io .openvidu .java .client .OpenViduRole ;
1947import io .openvidu .java .client .Recording ;
48+ import io .openvidu .java .client .RecordingLayout ;
49+ import io .openvidu .java .client .RecordingProperties ;
2050import io .openvidu .java .client .Session ;
2151import io .openvidu .java .client .SessionProperties ;
2252import io .openvidu .java .client .TokenOptions ;
@@ -37,6 +67,8 @@ public class OpenViduComponent {
3767 private OpenVidu openVidu ;
3868 private String OPENVIDU_URL ;
3969 private String SECRET ;
70+
71+ private HttpClient httpClient ;
4072
4173 private Map <Long , Session > roomIdSession ;
4274 private Map <String , Map <Long , String []>> sessionIdUserIdToken ;
@@ -47,6 +79,28 @@ public OpenViduComponent(@Value("${openvidu.secret}") String secret, @Value("${o
4779 this .openVidu = new OpenVidu (OPENVIDU_URL , SECRET );
4880 this .roomIdSession = new ConcurrentHashMap <>();
4981 this .sessionIdUserIdToken = new ConcurrentHashMap <>();
82+
83+ TrustStrategy trustStrategy = new TrustStrategy () {
84+ @ Override
85+ public boolean isTrusted (X509Certificate [] chain , String authType ) throws CertificateException {
86+ return true ;
87+ }
88+ };
89+ CredentialsProvider provider = new BasicCredentialsProvider ();
90+ UsernamePasswordCredentials credentials = new UsernamePasswordCredentials ("OPENVIDUAPP" , this .SECRET );
91+ provider .setCredentials (AuthScope .ANY , credentials );
92+ SSLContext sslContext ;
93+ try {
94+ sslContext = new SSLContextBuilder ().loadTrustMaterial (null , trustStrategy ).build ();
95+ } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e ) {
96+ throw new RuntimeException (e );
97+ }
98+ RequestConfig .Builder requestBuilder = RequestConfig .custom ();
99+ requestBuilder = requestBuilder .setConnectTimeout (30000 );
100+ requestBuilder = requestBuilder .setConnectionRequestTimeout (30000 );
101+ this .httpClient = HttpClientBuilder .create ().setDefaultRequestConfig (requestBuilder .build ())
102+ .setConnectionTimeToLive (30 , TimeUnit .SECONDS ).setSSLHostnameVerifier (NoopHostnameVerifier .INSTANCE )
103+ .setSSLContext (sslContext ).setDefaultCredentialsProvider (provider ).build ();
50104 }
51105
52106 public boolean isSessionCreated (Room room ) {
@@ -160,4 +214,31 @@ public String stopRecording(Room room) {
160214 }
161215 return null ;
162216 }
217+
218+ public void sendSignalToEveryone (Room room , String type , String data ) throws UnsupportedEncodingException , IOException {
219+ Session session = this .roomIdSession .get (room .getId ());
220+
221+ HttpPost request = new HttpPost (this .OPENVIDU_URL + "/api/signal" );
222+
223+ JsonObject json = new JsonObject ();
224+ json .addProperty ("session" , session .getSessionId ());
225+ json .addProperty ("type" , "signal:" + type );
226+ json .addProperty ("data" , data );
227+
228+ StringEntity params = new StringEntity (json .toString ());
229+ request .setHeader (HttpHeaders .CONTENT_TYPE , "application/json" );
230+ request .setEntity (params );
231+
232+ HttpResponse response = this .httpClient .execute (request );
233+ try {
234+ int statusCode = response .getStatusLine ().getStatusCode ();
235+ if ((statusCode == org .apache .http .HttpStatus .SC_OK )) {
236+ return ;
237+ } else {
238+ throw new IOException ("Openvidu error: " + statusCode );
239+ }
240+ } finally {
241+ EntityUtils .consumeQuietly (response .getEntity ());
242+ }
243+ }
163244}
0 commit comments