From 16684e8f06b8d4baa8911c4efcd37b4657c48487 Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Tue, 4 Apr 2017 10:36:54 +0200 Subject: [PATCH] Users connected to lessons are stored (removing sessionID possible) --- .../session_manager/SessionController.java | 59 ++++++++++++++++--- .../video-session/video-session.component.ts | 7 +++ .../src/app/services/video-session.service.ts | 9 +++ 3 files changed, 68 insertions(+), 7 deletions(-) diff --git a/openvidu-sample-app/backend/openvidu-sample-app/src/main/java/openvidu/openvidu_sample_app/session_manager/SessionController.java b/openvidu-sample-app/backend/openvidu-sample-app/src/main/java/openvidu/openvidu_sample_app/session_manager/SessionController.java index ec93c4ec..7c7fc5cc 100644 --- a/openvidu-sample-app/backend/openvidu-sample-app/src/main/java/openvidu/openvidu_sample_app/session_manager/SessionController.java +++ b/openvidu-sample-app/backend/openvidu-sample-app/src/main/java/openvidu/openvidu_sample_app/session_manager/SessionController.java @@ -6,6 +6,7 @@ import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.util.Collection; +import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -46,7 +47,8 @@ public class SessionController { @Autowired private UserComponent user; - private Map sessionIdMap = new ConcurrentHashMap<>(); + private Map lessonIdSessionId = new ConcurrentHashMap<>(); + private Map> sessionIdUserIdToken = new ConcurrentHashMap<>(); private final String OPENVIDU_URL = "https://localhost:8443/"; private final String SECRET ="MY_SECRET"; @@ -95,10 +97,10 @@ public class SessionController { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } - if(this.sessionIdMap.get(id_lesson) != null) { + if(this.lessonIdSessionId.get(id_lesson) != null) { // If there's already a valid sessionId for this lesson, not necessary to ask for a new one - return new ResponseEntity<>(this.sessionIdMap.get(id_lesson), HttpStatus.OK); + return new ResponseEntity<>(this.lessonIdSessionId.get(id_lesson), HttpStatus.OK); } else { @@ -114,7 +116,8 @@ public class SessionController { BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String sessionId = br.readLine(); - this.sessionIdMap.put(id_lesson, sessionId); + this.lessonIdSessionId.put(id_lesson, sessionId); + this.sessionIdUserIdToken.put(sessionId, new HashMap<>()); return new ResponseEntity<>(sessionId, HttpStatus.OK); } else { @@ -144,7 +147,7 @@ public class SessionController { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } - if (this.sessionIdMap.get(id_lesson) == null){ + if (this.lessonIdSessionId.get(id_lesson) == null){ return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } @@ -155,7 +158,7 @@ public class SessionController { */ JSONObject json = new JSONObject(); - json.put(0, this.sessionIdMap.get(id_lesson)); + json.put(0, this.lessonIdSessionId.get(id_lesson)); json.put(1, role); HttpPost request = new HttpPost(OPENVIDU_URL + "newToken"); @@ -171,8 +174,10 @@ public class SessionController { BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String token = br.readLine(); + this.sessionIdUserIdToken.get(this.lessonIdSessionId.get(id_lesson)).put(this.user.getLoggedUser().getId(), token); + JSONObject responseJson = new JSONObject(); - responseJson.put(0, this.sessionIdMap.get(id_lesson)); + responseJson.put(0, this.lessonIdSessionId.get(id_lesson)); responseJson.put(1, token); return new ResponseEntity<>(responseJson, HttpStatus.OK); @@ -183,6 +188,46 @@ public class SessionController { } + @RequestMapping(value = "/remove-user", method = RequestMethod.POST) + public ResponseEntity removeUser(@RequestBody String lessonId) throws Exception { + + if (!this.userIsLogged()) { + return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + } + + long id_lesson = -1; + try{ + id_lesson = Long.parseLong(lessonId); + }catch(NumberFormatException e){ + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + + Lesson c = lessonRepository.findOne(id_lesson); + + if (!checkAuthorizationUsers(c, c.getAttenders())){ + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + + if (this.lessonIdSessionId.get(id_lesson) == null){ + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + + String sessionId = this.lessonIdSessionId.get(id_lesson); + if (this.sessionIdUserIdToken.get(sessionId).remove(this.user.getLoggedUser().getId()) != null){ + // This user has left the lesson + if(this.sessionIdUserIdToken.get(sessionId).isEmpty()){ + // The last user has left the lesson + this.lessonIdSessionId.remove(id_lesson); + this.sessionIdUserIdToken.remove(sessionId); + } + return new ResponseEntity<>(HttpStatus.OK); + } else { + System.out.println("Problems in the app server: the user didn't have a valid token"); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + private boolean userIsLogged(){ if (!user.isLoggedUser()) { System.out.println("Not user logged"); diff --git a/openvidu-sample-app/frontend/src/app/components/video-session/video-session.component.ts b/openvidu-sample-app/frontend/src/app/components/video-session/video-session.component.ts index 29e07e3b..079b6b73 100644 --- a/openvidu-sample-app/frontend/src/app/components/video-session/video-session.component.ts +++ b/openvidu-sample-app/frontend/src/app/components/video-session/video-session.component.ts @@ -167,6 +167,13 @@ export class VideoSessionComponent implements OnInit { } ngOnDestroy() { + this.videoSessionService.removeUser(this.lesson.id).subscribe( + response => { + console.warn("You have succesfully left the lesson"); + }, + error => { + console.log(error); + }); this.toggleScrollPage("auto"); this.exitFullScreen(); if (this.OV) this.OV.close(false); diff --git a/openvidu-sample-app/frontend/src/app/services/video-session.service.ts b/openvidu-sample-app/frontend/src/app/services/video-session.service.ts index 58d9fd24..661646d9 100644 --- a/openvidu-sample-app/frontend/src/app/services/video-session.service.ts +++ b/openvidu-sample-app/frontend/src/app/services/video-session.service.ts @@ -34,6 +34,15 @@ export class VideoSessionService { .catch(error => this.handleError(error)); } + removeUser(lessonId: number) { + let body = JSON.stringify(lessonId); + let headers = new Headers({ 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + this.authenticationService.token }); + let options = new RequestOptions({ headers }); + return this.http.post('/api-sessions/remove-user', body, options) + .map(response => response) + .catch(error => this.handleError(error)); + } + private handleError(error: any) { console.error(error); return Observable.throw('Server error (' + error.status + '): ' + error.text())