package com.google.sps.servlets;

import java.io.IOException;
import java.io.BufferedReader;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import java.security.GeneralSecurityException;
import com.google.sps.data.User;
import com.google.sps.data.Config;
import com.google.sps.data.RequestParser;
import com.google.sps.service.DatabaseService;
import java.util.Collections;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private static final String NAME_PROPERTY_KEY = "name";

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {    
        try {
            GoogleIdToken idToken = tokenVerifier().verify(RequestParser.parseStringFromRequest(request));

            if (idToken == null) {
                response.setContentType("text/html");
                response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                return;
            }
            
            String userId = idToken.getPayload().getSubject();
            String name = (String) idToken.getPayload().get(LoginServlet.NAME_PROPERTY_KEY);

            DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

            Filter userFilter = new FilterPredicate(User.USER_ID_PROPERTY_KEY, FilterOperator.EQUAL, userId);
            Query query = new Query(User.USER_ENTITY_NAME).setFilter(userFilter);
            PreparedQuery results = datastore.prepare(query);

            if (results.countEntities() == 0) {
                DatabaseService.save(new User(userId, name).getUserEntity());
            }

            response.setContentType("text/html");
            response.setStatus(HttpServletResponse.SC_OK); 
        } catch (GeneralSecurityException e){
            System.out.println("Cannot verify token: " + e);
        }
    }

    public GoogleIdTokenVerifier tokenVerifier() {
        return new GoogleIdTokenVerifier.Builder(new NetHttpTransport(), new JacksonFactory())
            .setAudience(Collections.singletonList(Config.CLIENT_ID))
            .build();
    }
}
@@ -1605,18 +1605,55 @@
 tion %7B  
+%0A
   
+      GoogleIdToken idToken = null;%0A
 %0A       
@@ -1666,39 +1666,24 @@
 %0A           
- GoogleIdToken 
 idToken = to
@@ -1751,21 +1751,134 @@
 uest));%0A
-%0A
     
+    %7D catch (GeneralSecurityException e)%7B%0A            System.out.println(%22Cannot verify token: %22 + e);%0A        %7D%0A%0A
         
@@ -1896,28 +1896,24 @@
  == null) %7B%0A
-    
             
@@ -1942,36 +1942,32 @@
 e(%22text/html%22);%0A
-    
             resp
@@ -2027,28 +2027,24 @@
             
-    
 return;%0A    
@@ -2051,25 +2051,18 @@
     
-    %7D%0A   
+%7D%0A
         
  %0A  
@@ -2057,22 +2057,17 @@
         
- %0A    
+%0A
         
@@ -2117,20 +2117,16 @@
 ject();%0A
-    
         
@@ -2211,28 +2211,24 @@
 );%0A%0A        
-  
-  
 DatastoreSer
@@ -2288,28 +2288,24 @@
 Service();%0A%0A
-    
         Filt
@@ -2406,20 +2406,16 @@
         
-    
 Query qu
@@ -2484,20 +2484,16 @@
         
- 
-   
 Prepared
@@ -2539,28 +2539,24 @@
 );%0A%0A        
-    
 if (results.
@@ -2575,28 +2575,24 @@
 s() == 0) %7B%0A
-    
             
@@ -2657,35 +2657,27 @@
 ));%0A        
-    %7D%0A%0A    
+%7D%0A%0A
         resp
@@ -2710,36 +2710,32 @@
 html%22);%0A        
-   
- 
 response.setStat
@@ -2770,125 +2770,8 @@
 ); %0A
-        %7D catch (GeneralSecurityException e)%7B%0A            System.out.println(%22Cannot verify token: %22 + e);%0A        %7D%0A
     

Refactor try block