diff --git a/openvidu-backend-client/src/main/java/org/openvidu/client/OpenVidu.java b/openvidu-backend-client/src/main/java/org/openvidu/client/OpenVidu.java index a43777af..c46128ac 100644 --- a/openvidu-backend-client/src/main/java/org/openvidu/client/OpenVidu.java +++ b/openvidu-backend-client/src/main/java/org/openvidu/client/OpenVidu.java @@ -10,7 +10,6 @@ import java.security.NoSuchAlgorithmException; import org.apache.http.HttpResponse; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.ClientProtocolException; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; @@ -22,6 +21,8 @@ import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContextBuilder; import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; import org.openvidu.client.OpenViduException.Code; public class OpenVidu { @@ -59,23 +60,20 @@ public class OpenVidu { } } - public String createSession() throws OpenViduException, ClientProtocolException, IOException { + public JSONObject createSession() throws IOException, ParseException { HttpResponse response = myHttpClient.execute(new HttpGet(this.urlOpenViduServer + "getSessionId")); - + int statusCode = response.getStatusLine().getStatusCode(); - if ((statusCode == org.apache.http.HttpStatus.SC_OK) && (response.getEntity().getContentLength() > 0)){ + if ((statusCode == org.apache.http.HttpStatus.SC_OK)){ System.out.println("Returning a SESSIONID"); - BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); - String sessionId = br.readLine(); - - return sessionId; + return this.httpResponseToJsonObject(response); } else { - throw new OpenViduException(Code.TRANSPORT_REQUEST_ERROR_CODE, "Unable to generate a sessionID"); + throw new OpenViduException(Code.SESSIONID_CANNOT_BE_CREATED_ERROR_CODE, "Unable to generate a sessionID"); } } - public String generateToken(String sessionId, String role) throws OpenViduException, ClientProtocolException, IOException { + public JSONObject generateToken(String sessionId, String role) throws IOException, ParseException { JSONObject json = new JSONObject(); json.put(0, sessionId); json.put(1, role); @@ -86,16 +84,24 @@ public class OpenVidu { request.setEntity(params); HttpResponse response = myHttpClient.execute(request); - + int statusCode = response.getStatusLine().getStatusCode(); - if ((statusCode == org.apache.http.HttpStatus.SC_OK) && (response.getEntity().getContentLength() > 0)){ + if ((statusCode == org.apache.http.HttpStatus.SC_OK)){ System.out.println("Returning a TOKEN"); - BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); - String token = br.readLine(); - - return token; + return this.httpResponseToJsonObject(response); } else { - throw new OpenViduException(Code.TRANSPORT_REQUEST_ERROR_CODE, "Unable to generate a token"); + throw new OpenViduException(Code.TOKEN_CANNOT_BE_CREATED_ERROR_CODE, "Unable to generate a token"); } } + + private JSONObject httpResponseToJsonObject(HttpResponse response) throws ParseException, UnsupportedOperationException, IOException{ + BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); + StringBuffer buf = new StringBuffer(); + String line = ""; + while ((line = rd.readLine()) != null) { + buf.append(line); + } + JSONParser parser = new JSONParser(); + return ((JSONObject) parser.parse(buf.toString())); + } } diff --git a/openvidu-backend-client/src/main/java/org/openvidu/client/OpenViduException.java b/openvidu-backend-client/src/main/java/org/openvidu/client/OpenViduException.java index d11e1a0c..55209515 100644 --- a/openvidu-backend-client/src/main/java/org/openvidu/client/OpenViduException.java +++ b/openvidu-backend-client/src/main/java/org/openvidu/client/OpenViduException.java @@ -38,7 +38,8 @@ public class OpenViduException extends RuntimeException { 104), USER_CLOSED_ERROR_CODE( 103), USER_NOT_FOUND_ERROR_CODE(102), USER_GENERIC_ERROR_CODE(101), - USER_UNAUTHORIZED(401); + USER_UNAUTHORIZED_ERROR_CODE(401), ROLE_NOT_FOUND_ERROR_CODE(402), + SESSIONID_CANNOT_BE_CREATED_ERROR_CODE(403), TOKEN_CANNOT_BE_CREATED_ERROR_CODE(404); private int value; diff --git a/openvidu-client/src/main/java/org/openvidu/client/OpenViduException.java b/openvidu-client/src/main/java/org/openvidu/client/OpenViduException.java index d11e1a0c..55209515 100644 --- a/openvidu-client/src/main/java/org/openvidu/client/OpenViduException.java +++ b/openvidu-client/src/main/java/org/openvidu/client/OpenViduException.java @@ -38,7 +38,8 @@ public class OpenViduException extends RuntimeException { 104), USER_CLOSED_ERROR_CODE( 103), USER_NOT_FOUND_ERROR_CODE(102), USER_GENERIC_ERROR_CODE(101), - USER_UNAUTHORIZED(401); + USER_UNAUTHORIZED_ERROR_CODE(401), ROLE_NOT_FOUND_ERROR_CODE(402), + SESSIONID_CANNOT_BE_CREATED_ERROR_CODE(403), TOKEN_CANNOT_BE_CREATED_ERROR_CODE(404); private int value; diff --git a/openvidu-sample-app/src/main/java/openvidu/openvidu_sample_app/session_manager/SessionController.java b/openvidu-sample-app/src/main/java/openvidu/openvidu_sample_app/session_manager/SessionController.java index cb179012..fe7925bc 100644 --- a/openvidu-sample-app/src/main/java/openvidu/openvidu_sample_app/session_manager/SessionController.java +++ b/openvidu-sample-app/src/main/java/openvidu/openvidu_sample_app/session_manager/SessionController.java @@ -5,9 +5,7 @@ import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import org.apache.http.client.HttpClient; import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; import org.openvidu.client.OpenVidu; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -48,7 +46,7 @@ public class SessionController { } @RequestMapping(value = "/create-session", method = RequestMethod.POST) - public ResponseEntity createSession(@RequestBody String lessonId) { + public ResponseEntity createSession(@RequestBody String lessonId) { if (!this.userIsLogged()) { return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); @@ -71,27 +69,33 @@ public class SessionController { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } + JSONObject responseJson = new JSONObject(); + 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.lessonIdSessionId.get(id_lesson), HttpStatus.OK); + responseJson.put(0, this.lessonIdSessionId.get(id_lesson)); + return new ResponseEntity<>(responseJson, HttpStatus.OK); } else { try { - String sessionId = this.openVidu.createSession(); + JSONObject json = this.openVidu.createSession(); + String sessionId = (String) json.get("0"); this.lessonIdSessionId.put(id_lesson, sessionId); this.sessionIdUserIdToken.put(sessionId, new HashMap<>()); showMap(); - return new ResponseEntity<>(sessionId, HttpStatus.OK); + responseJson.put(0, sessionId); + + return new ResponseEntity<>(responseJson, HttpStatus.OK); } catch (Exception e) { - return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + return getErrorResponse(e); } } } @RequestMapping(value = "/generate-token", method = RequestMethod.POST) - public ResponseEntity generateToken(@RequestBody String lessonId) throws Exception { + public ResponseEntity generateToken(@RequestBody String lessonId) { if (!this.userIsLogged()) { return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); @@ -119,12 +123,12 @@ public class SessionController { String sessionId = this.lessonIdSessionId.get(id_lesson); String role = user.hasRoleTeacher() ? "PUBLISHER" : "SUBSCRIBER"; + JSONObject responseJson = new JSONObject(); + try { - - String token = this.openVidu.generateToken(sessionId, role); + String token = (String) this.openVidu.generateToken(sessionId, role).get("0"); this.sessionIdUserIdToken.get(sessionId).put(this.user.getLoggedUser().getId(), token); - JSONObject responseJson = new JSONObject(); responseJson.put(0, sessionId); responseJson.put(1, token); @@ -132,9 +136,7 @@ public class SessionController { return new ResponseEntity<>(responseJson, HttpStatus.OK); } catch (Exception e) { - JSONParser parser = new JSONParser(); - JSONObject json = (JSONObject) parser.parse(e.getMessage()); - return new ResponseEntity<>(json, HttpStatus.INTERNAL_SERVER_ERROR); + return getErrorResponse(e); } } @@ -198,6 +200,14 @@ public class SessionController { return true; } + private ResponseEntity getErrorResponse(Exception e){ + JSONObject json = new JSONObject(); + json.put("cause", e.getCause()); + json.put("error", e.getMessage()); + json.put("exception", e.getClass()); + return new ResponseEntity<>(json, HttpStatus.INTERNAL_SERVER_ERROR); + } + // Authorization checking for creating or joining a certain lesson private boolean checkAuthorization(Object o, User u){ return !(o == null || !this.user.getLoggedUser().equals(u)); diff --git a/openvidu-sample-app/src/main/resources/frontend/src/app/components/video-session/video-session.component.ts b/openvidu-sample-app/src/main/resources/frontend/src/app/components/video-session/video-session.component.ts index e20fb360..3f962598 100644 --- a/openvidu-sample-app/src/main/resources/frontend/src/app/components/video-session/video-session.component.ts +++ b/openvidu-sample-app/src/main/resources/frontend/src/app/components/video-session/video-session.component.ts @@ -84,10 +84,12 @@ export class VideoSessionComponent implements OnInit { // If the user is the teacher: creates the session and gets a token (with PUBLISHER role) this.videoSessionService.createSession(this.lesson.id).subscribe( - sessionId => { - this.sessionId = sessionId; + sessionId => { // {0: sessionId} + console.warn(sessionId); + this.sessionId = sessionId[0]; this.videoSessionService.generateToken(this.lesson.id).subscribe( sessionIdAndToken => { + console.warn(sessionIdAndToken); this.token = sessionIdAndToken[1]; console.warn("Token: " + this.token); console.warn("SessionId: " + this.sessionId); @@ -106,7 +108,7 @@ export class VideoSessionComponent implements OnInit { // If the user is a student: gets a token (with SUBSCRIBER role) this.videoSessionService.generateToken(this.lesson.id).subscribe( - sessionIdAndToken => { + sessionIdAndToken => { // {0: sessionId, 1: token} this.sessionId = sessionIdAndToken[0]; this.token = sessionIdAndToken[1]; console.warn("Token: " + this.token); diff --git a/openvidu-sample-app/src/main/resources/frontend/src/app/services/video-session.service.ts b/openvidu-sample-app/src/main/resources/frontend/src/app/services/video-session.service.ts index 661646d9..cbbf7bb9 100644 --- a/openvidu-sample-app/src/main/resources/frontend/src/app/services/video-session.service.ts +++ b/openvidu-sample-app/src/main/resources/frontend/src/app/services/video-session.service.ts @@ -14,13 +14,13 @@ export class VideoSessionService { constructor(private http: Http, private authenticationService: AuthenticationService) { } - // Returns "sessionId" + // Returns {0: sessionId} createSession(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/create-session', body, options) - .map(response => response.text()) + .map(response => response.json()) .catch(error => this.handleError(error)); } diff --git a/openvidu-server/pom.xml b/openvidu-server/pom.xml index b18a0872..40467de4 100644 --- a/openvidu-server/pom.xml +++ b/openvidu-server/pom.xml @@ -155,6 +155,10 @@ org.springframework.boot spring-boot-starter-security + + com.googlecode.json-simple + json-simple +