/**
 * Copyright 2015 Google Inc. All Rights Reserved.
 * <p/>
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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 com.example.managedvms.gettingstartedjava.auth;

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

// [START example]
@WebServlet(name = "login", value = "/login")
@SuppressWarnings("serial")
public class LoginServlet extends HttpServlet {

  private Logger logger = Logger.getLogger(this.getClass().getName());
  private static final Collection<String> SCOPES = Arrays.asList("email", "profile");
  private static final JsonFactory JSON_FACTORY = new JacksonFactory();
  private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();

  private GoogleAuthorizationCodeFlow flow;

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws IOException, ServletException {
    flow = new GoogleAuthorizationCodeFlow.Builder(
        HTTP_TRANSPORT,
        JSON_FACTORY,
        getServletContext().getInitParameter("bookshelf.clientID"),
        getServletContext().getInitParameter("bookshelf.clientSecret"),
        SCOPES)
        .build();
    String state = new BigInteger(130, new SecureRandom()).toString(32);
    req.getSession().setAttribute("state", state);
    if (req.getAttribute("loginDestination") != null) {
      req
          .getSession()
          .setAttribute("loginDestination", (String) req.getAttribute("loginDestination"));
      logger.log(
          Level.INFO, "logging destination " + (String) req.getAttribute("loginDestination"));
    } else {
      req.getSession().setAttribute("loginDestination", "/books");
      logger.log(Level.INFO, "logging destination /books");
    }

    // Callback url should be the one registered in Google Developers Console
    String url =
        flow.newAuthorizationUrl()
            .setRedirectUri(getServletContext().getInitParameter("bookshelf.callback"))
            .setState(state)
            .build();
    resp.sendRedirect(url);
  }
}
// [END example]

@@ -2031,279 +2031,8 @@
 n %7B%0A
-    flow = new GoogleAuthorizationCodeFlow.Builder(%0A        HTTP_TRANSPORT,%0A        JSON_FACTORY,%0A        getServletContext().getInitParameter(%22bookshelf.clientID%22),%0A        getServletContext().getInitParameter(%22bookshelf.clientSecret%22),%0A        SCOPES)%0A        .build();
 %0A   
@@ -2594,16 +2594,289 @@
 %0A    %7D%0A%0A
+    flow = new GoogleAuthorizationCodeFlow.Builder(%0A        HTTP_TRANSPORT,%0A        JSON_FACTORY,%0A        getServletContext().getInitParameter(%22bookshelf.clientID%22),%0A        getServletContext().getInitParameter(%22bookshelf.clientSecret%22),%0A        SCOPES)%0A        .build();%0A%0A
     // C

move Flow closer to where it's used.