// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package servlets;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

@WebServlet("/Login")
public class LoginServlet extends HttpServlet {
  private static final String OAUTH_URL = "/OAuth";
  private static final String LOG_PAGE_URL = "/login.html";
  private static final String TEXT_TYPE = "text/html";

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType(TEXT_TYPE);
    UserService userService = UserServiceFactory.getUserService();
    HashMap<String, String> loginObject = new HashMap<String, String>();
    String loginURL = "";
    String logoutURL = "";
    String loginStatus = "";

    if(userService.isUserLoggedIn()) {
      logoutURL = userService.createLogoutURL(LOG_PAGE_URL); 
      loginStatus = "true"; 
    } else {
      loginURL = userService.createLoginURL(OAUTH_URL);
      loginStatus = "false";
    }

    loginObject.put("loginURL", loginURL);
    loginObject.put("logoutURL", logoutURL);
    loginObject.put("loginStatus", loginStatus);

    // Convert the URL to a JSON String.
    String jsonMessage = messageListAsJson(loginObject);

    // Send the JSON message as the response.
    response.setContentType(TEXT_TYPE);
    response.getWriter().println(jsonMessage);
  }

  /**
  * Converts a Java HashMap into a JSON string using Gson.  
  */
  private String messageListAsJson(HashMap<String, String> output) {
    return new Gson().toJson(output);
  }
}

@@ -1233,16 +1233,243 @@
 t/html%22;
+%0A  private final String LOGOUT_URL = %22logoutURL%22;%0A  private final String LOGIN_URL = %22loginURL%22;%0A  private final String LOGIN_STATUS = %22loginStatus%22;%0A  private final String TRUE = %22true%22;%0A  private final String FALSE = %22false%22;
 %0A%0A  @Ove
@@ -1756,90 +1756,8 @@
 %3E();
-%0A    String loginURL = %22%22;%0A    String logoutURL = %22%22;%0A    String loginStatus = %22%22;
 %0A%0A  
@@ -1806,16 +1806,32 @@
  log
-outURL =
+inObject.put(LOGOUT_URL,
  use
@@ -1868,16 +1868,17 @@
 AGE_URL)
+)
 ; %0A     
@@ -1887,23 +1887,38 @@
 ogin
-Status = %22true%22
+Object.put(LOGIN_STATUS, TRUE)
 ; %0A 
@@ -1940,21 +1940,37 @@
    login
+Object.put(LOGIN_
 URL
- =
+,
  userSer
@@ -2003,182 +2003,61 @@
 URL)
-;%0A      loginStatus = %22false%22;%0A    %7D%0A%0A    loginObject.put(%22loginURL%22, loginURL);%0A    loginObject.put(%22logoutURL%22, logoutURL
+);%0A      loginObject.put(LOGIN_STATUS, FALSE
 );
+ 
 %0A    
-loginObject.put(%22loginStatus%22, loginStatus);
+%7D
 %0A%0A  

replace String literals with constants