src/main/java/zoot/tube/App.java
/**
     * Adds message handlers to the server.
     */
Adds message handlers to the server.
Adds message handlers to the server .
private void addMessageHandlers() {
        this.server.addMessageHandler(Server.createDefaultGreeting(this.server, "Hello "));
        this.server.addMessageHandler((message) -> {
            try {
                ApiRequest request = this.gson.fromJson(message, ApiRequest.class);
                if (request.getHeader().equals("getMyPlaylists")) {
                    List<Playlist> myPlaylists = youtubeAPI.getMyPlaylists();
                    String response = this.wrapIntoJsonObjectDataRaw("playlists", gson.toJson(myPlaylists));
                    System.out.println("Sending playlists");
                    this.server.sendMessage(response);
                }
                if (request.getHeader().equals("signIn")) {
                    String user = "user";
                    Credential credential = authenticator.authorizeAndGetNewCredential(null);
                    youtubeAPI.setCredential(credential);
                    if (credential != null) {
                        String usersEmail = GoogleUtil.getUserInfo(credential).getEmail();
                        System.out.println(usersEmail);
                        RefreshTokenSaver.saveRefreshToken(usersEmail, credential.getRefreshToken());
                        String jSONEmail = this.wrapIntoJsonObject("successfulSignIn", usersEmail);
                        this.server.sendMessage(jSONEmail);
                    }
                }
                if (request.getHeader().equals("signOut")) {
                    youtubeAPI.setCredential(null);
                    String signOut = this.wrapIntoJsonObject("successfulSignOut", null);
                    this.server.sendMessage(signOut);
                }
                if (request.getHeader().equals("isSignedIn")) {
                    if (this.getYouTubeAPICredential() != null) {
                        String usersEmail = GoogleUtil.getUserInfo(this.getYouTubeAPICredential()).getEmail();
                        String credentialStatus
                                = this.wrapIntoJsonObject("signedIn", usersEmail);
                        this.server.sendMessage(credentialStatus);
                    } else {
                        String credentialStatus
                                = this.wrapIntoJsonObject("signedOut", null);
                        this.server.sendMessage(credentialStatus);
                    }
                }
            } catch (Exception e) {
                System.err.println("Not a standard request");
            }
            try {
                ApiRequestTimes request = this.gson.fromJson(message, ApiRequestTimes.class);
                if (request.getHeader().equals("scheduleLists")) {
                    String[] ids = gson.fromJson(request.getData().getIds(), String[].class);
                    String user = GoogleUtil.getUserInfo(youtubeAPI.getCredential()).getEmail();
                    for (String id : ids) {
                        long add = 5 * 60 * 60 * 1000;
                        Date start = new Date(request.getData().getStart() + add);
                        Date expire = new Date(request.getData().getExpire() + add);
                        scheduler.scheduleMakeVideosInPlaylistPrivate(user, start, expire, id);
                    }
                }
            } catch (Exception e) {
                System.err.println("Not a timer request");
            }
        });
    }
private void addMessageHandlers ( ) { this . server . addMessageHandler ( Server . createDefaultGreeting ( this . server , "Hello " ) ) ; this . server . addMessageHandler ( ( message ) -> { try { ApiRequest request = this . gson . fromJson ( message , ApiRequest . class ) ; if ( request . getHeader ( ) . equals ( "getMyPlaylists" ) ) { List < Playlist > myPlaylists = youtubeAPI . getMyPlaylists ( ) ; String response = this . wrapIntoJsonObjectDataRaw ( "playlists" , gson . toJson ( myPlaylists ) ) ; System . out . println ( "Sending playlists" ) ; this . server . sendMessage ( response ) ; } if ( request . getHeader ( ) . equals ( "signIn" ) ) { String user = "user" ; Credential credential = authenticator . authorizeAndGetNewCredential ( null ) ; youtubeAPI . setCredential ( credential ) ; if ( credential != null ) { String usersEmail = GoogleUtil . getUserInfo ( credential ) . getEmail ( ) ; System . out . println ( usersEmail ) ; RefreshTokenSaver . saveRefreshToken ( usersEmail , credential . getRefreshToken ( ) ) ; String jSONEmail = this . wrapIntoJsonObject ( "successfulSignIn" , usersEmail ) ; this . server . sendMessage ( jSONEmail ) ; } } if ( request . getHeader ( ) . equals ( "signOut" ) ) { youtubeAPI . setCredential ( null ) ; String signOut = this . wrapIntoJsonObject ( "successfulSignOut" , null ) ; this . server . sendMessage ( signOut ) ; } if ( request . getHeader ( ) . equals ( "isSignedIn" ) ) { if ( this . getYouTubeAPICredential ( ) != null ) { String usersEmail = GoogleUtil . getUserInfo ( this . getYouTubeAPICredential ( ) ) . getEmail ( ) ; String credentialStatus = this . wrapIntoJsonObject ( "signedIn" , usersEmail ) ; this . server . sendMessage ( credentialStatus ) ; } else { String credentialStatus = this . wrapIntoJsonObject ( "signedOut" , null ) ; this . server . sendMessage ( credentialStatus ) ; } } } catch ( Exception e ) { System . err . println ( "Not a standard request" ) ; } try { ApiRequestTimes request = this . gson . fromJson ( message , ApiRequestTimes . class ) ; if ( request . getHeader ( ) . equals ( "scheduleLists" ) ) { String [ ] ids = gson . fromJson ( request . getData ( ) . getIds ( ) , String [ ] . class ) ; String user = GoogleUtil . getUserInfo ( youtubeAPI . getCredential ( ) ) . getEmail ( ) ; for ( String id : ids ) { long add = 5 * 60 * 60 * 1000 ; Date start = new Date ( request . getData ( ) . getStart ( ) + add ) ; Date expire = new Date ( request . getData ( ) . getExpire ( ) + add ) ; scheduler . scheduleMakeVideosInPlaylistPrivate ( user , start , expire , id ) ; } } } catch ( Exception e ) { System . err . println ( "Not a timer request" ) ; } } ) ; }
Adds message handlers to the server.
Adds message handlers to the server .
// Add a greeting handler. // Add a handler for requesting the user's playlists. // Parse the message. // Make sure the message is asking for the user's playlists. // Get the playlists. // Package up the playlists into a response. //                if(RefreshTokenSaver.loadRefreshToken(user).length() > 0){ //                    authenticator.authorizeUsingRefreshToken(RefreshTokenSaver.loadRefreshToken(user)); //                }else { // Credential credential = this.getCredential(user); // Create the YouTubeAPI //String response = this.wrapIntoJsonObjectDataRaw("email", jSONEmail);