public RequestResponse requestSong(String spotifyId, String token, int locationId, String code) throws SpotifyApiException, UpdateQRCodeException { if (!data.hasSong(spotifyId)) { // Fetch song info from spotify if neccessary SpotifyLookupContainer song = requestSpotifySong(spotifyId); logger.debug( song.getTrack().getName() + " - Fetched from spotify, length: " + song.getTrack().getLength()); long length = (long) song.getTrack().getLength() * 1000; data.saveSong( spotifyId, song.getTrack().getName(), song.getTrack().concatArtistNames(), length); } int qrValidationCode = qrService.verify(code, locationId); if (!data.getSession(locationId).isOpen()) { // session is not open return new RequestResponse( "Stemming er ikke mulig. Sesjonen er ikke aktiv", false, qrValidationCode); } else if (qrValidationCode == QR_IS_NOT_VALID) { // Code is not valid return new RequestResponse("QR-koden er ikke gyldig", false, qrValidationCode); } else if (qrValidationCode == QR_HAS_UNLIMITED_USES) { // Unlimited votes so check token int userId = verifyToken(token); // throws exception if code is not valid or is null if (data.userCanRequestSong(spotifyId, locationId, userId)) { data.requestSongOneActiveVote(spotifyId, locationId, userId); } else { logger.debug( "User " + userId + " could not vote for song " + spotifyId + " at location " + locationId); return new RequestResponse( "Denne brukeren har allerede stemt opp denne sangen", false, qrValidationCode); } } else if (qrValidationCode == QR_HAS_LIMITED_USES) { // Limited votes, so token not neccessary int userId = authService.verify(token); if (userId == -1) { // Token null or not valid userId = -1337; // The fake spotify user } boolean success = data.requestSongManyActiveVotes( spotifyId, locationId, userId); // No need to update qrcodes and such.//Jo? Have to decrement the uses. if (success) { if (!qrService.codeIsUsed(code)) { logger.debug("Could not update QRcode"); } } else { return new RequestResponse("Kunne ikke oppdatere databasen", false, qrValidationCode); } } return new RequestResponse("Stemmen ble registrert", true, qrValidationCode); }
private int verifyToken(String token) throws InvalidTokenException { int tokenUserId = authService.verify(token); if (tokenUserId == -1) throw new InvalidTokenException("Invalid access token"); return tokenUserId; }