// Copyright (C) 2009 The Android Open Source Project
//
// 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
//
// http://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 com.google.gerrit.server.http;

import com.google.gerrit.client.reviewdb.Account;
import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gerrit.server.config.CanonicalWebUrl;
import com.google.gerrit.server.config.Nullable;
import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.client.SchemaFactory;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
@Singleton
public class BecomeAnyAccountLoginServlet extends HttpServlet {
  private final SchemaFactory<ReviewDb> schema;
  private final Provider<WebSession> webSession;
  private final Provider<String> urlProvider;
  private final byte[] raw;

  @Inject
  BecomeAnyAccountLoginServlet(final Provider<WebSession> ws,
      final SchemaFactory<ReviewDb> sf,
      final @CanonicalWebUrl @Nullable Provider<String> up,
      final ServletContext servletContext) throws IOException {
    webSession = ws;
    schema = sf;
    urlProvider = up;

    final String hostPageName = "WEB-INF/BecomeAnyAccount.html";
    final String doc = HtmlDomUtil.readFile(servletContext, "/" + hostPageName);
    if (doc == null) {
      throw new FileNotFoundException("No " + hostPageName + " in webapp");
    }

    raw = doc.getBytes(HtmlDomUtil.ENC);
  }

  @Override
  protected void doGet(final HttpServletRequest req,
      final HttpServletResponse rsp) throws IOException {
    doPost(req, rsp);
  }

  @Override
  protected void doPost(final HttpServletRequest req,
      final HttpServletResponse rsp) throws IOException {
    final List<Account> accounts;
    if (req.getParameter("ssh_user_name") != null) {
      accounts = bySshUserName(rsp, req.getParameter("ssh_user_name"));

    } else if (req.getParameter("preferred_email") != null) {
      accounts = byPreferredEmail(rsp, req.getParameter("preferred_email"));

    } else if (req.getParameter("account_id") != null) {
      accounts = byAccountId(rsp, req.getParameter("account_id"));

    } else {
      rsp.setContentType("text/html");
      rsp.setCharacterEncoding(HtmlDomUtil.ENC);
      rsp.setContentLength(raw.length);
      final OutputStream out = rsp.getOutputStream();
      try {
        out.write(raw);
      } finally {
        out.close();
      }
      return;
    }

    if (accounts.size() == 1) {
      final Account account = accounts.get(0);
      rsp.setHeader("Expires", "Fri, 01 Jan 1980 00:00:00 GMT");
      rsp.setHeader("Pragma", "no-cache");
      rsp.setHeader("Cache-Control", "no-cache, must-revalidate");
      webSession.get().login(account.getId(), false);
      rsp.sendRedirect(urlProvider.get());

    } else {
      rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
  }

  private List<Account> bySshUserName(final HttpServletResponse rsp,
      final String userName) {
    try {
      final ReviewDb db = schema.open();
      try {
        final Account account = db.accounts().bySshUserName(userName);
        return account != null ? Collections.<Account> singletonList(account)
            : Collections.<Account> emptyList();
      } finally {
        db.close();
      }
    } catch (OrmException e) {
      getServletContext().log("cannot query database", e);
      return Collections.<Account> emptyList();
    }
  }

  private List<Account> byPreferredEmail(final HttpServletResponse rsp,
      final String email) {
    try {
      final ReviewDb db = schema.open();
      try {
        return db.accounts().byPreferredEmail(email).toList();
      } finally {
        db.close();
      }
    } catch (OrmException e) {
      getServletContext().log("cannot query database", e);
      return Collections.<Account> emptyList();
    }
  }

  private List<Account> byAccountId(final HttpServletResponse rsp,
      final String idStr) {
    final Account.Id id;
    try {
      id = Account.Id.parse(idStr);
    } catch (NumberFormatException nfe) {
      return Collections.<Account> emptyList();
    }
    try {
      final ReviewDb db = schema.open();
      try {
        final Account account = db.accounts().get(id);
        return account != null ? Collections.<Account> singletonList(account)
            : Collections.<Account> emptyList();
      } finally {
        db.close();
      }
    } catch (OrmException e) {
      getServletContext().log("cannot query database", e);
      return Collections.<Account> emptyList();
    }
  }
}

@@ -1141,16 +1141,39 @@
 Stream;%0A
+import java.io.Writer;%0A
 import j
@@ -2528,24 +2528,194 @@
 Exception %7B%0A
+    rsp.setHeader(%22Expires%22, %22Fri, 01 Jan 1980 00:00:00 GMT%22);%0A    rsp.setHeader(%22Pragma%22, %22no-cache%22);%0A    rsp.setHeader(%22Cache-Control%22, %22no-cache, must-revalidate%22);%0A%0A
     final Li
@@ -3515,340 +3515,416 @@
     
-rsp.setHeader(%22Expires%22, %22Fri, 01 Jan 1980 00:00:00 GMT%22);%0A      rsp.setHeader(%22Pragma%22, %22no-cache%22);%0A      rsp.setHeader(%22Cache-Control%22, %22no-cache, must-revalidate%22);%0A      webSession.get().login(account.getId(), false);%0A      rsp.sendRedirect(urlProvider.get()
+webSession.get().login(account.getId(), false);%0A      rsp.sendRedirect(urlProvider.get());%0A%0A    %7D else %7B%0A      rsp.setContentType(%22text/html%22);%0A      rsp.setCharacterEncoding(HtmlDomUtil.ENC);%0A      final Writer out = rsp.getWriter();%0A      out.write(%22%3Chtml%3E%22);%0A      out.write(%22%3Cbody%3E%22);%0A      out.write(%22%3Ch1%3EAccount Not Found%3C/h1%3E%22);%0A      out.write(%22%3C/body%3E%22
 );%0A
-%0A
     
-%7D else %7B%0A      rsp.sendError(HttpServletResponse.SC_NOT_FOUND
+  out.write(%22%3C/html%3E%22);%0A      out.close(
 );%0A 

Clarify become any account servlet errors