// Copyright 2019 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 com.google.slurp.servlets;

import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet that converts a given POST request of user UIDs to the corresponding user emails, sending
 * this back as the response.
 */
@WebServlet("/api/v1/convert-uids-to-emails")
public class ConvertUidsToEmailsServlet extends HttpServlet {

  /**
   * {@inheritDoc}
   */
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

  }
}

@@ -646,16 +646,178 @@
 eption;%0A
+import java.io.Reader;%0Aimport java.lang.reflect.Type;%0Aimport java.util.List;%0Aimport java.util.concurrent.ExecutionException;%0Aimport java.util.stream.Collectors;%0A%0A
 import j
@@ -985,16 +985,277 @@
 ponse;%0A%0A
+import com.google.firebase.auth.FirebaseAuth;%0Aimport com.google.firebase.auth.GetUsersResult;%0Aimport com.google.firebase.auth.UidIdentifier;%0Aimport com.google.firebase.auth.UserIdentifier;%0Aimport com.google.gson.Gson;%0Aimport com.google.gson.reflect.TypeToken;%0A%0A
 /**%0A * S
@@ -1329,16 +1329,19 @@
 sponding
+%0A *
  user em
@@ -1353,19 +1353,16 @@
  sending
-%0A *
  this ba
@@ -1497,152 +1497,1809 @@
 t %7B%0A
-%0A  /**%0A   * %7B@inheritDoc%7D%0A   */%0A  @Override%0A  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException %7B%0A
+  /**%0A   * Constructs a list of %3Ccode%3EUserIdentifier%3C/code%3E using the given JSON array supplied by the%0A   * POST request.%0A   *%0A   * @param json The body of the POST request, expected to be String array of user UIDs.%0A   * @return A list of UserIdentifier objects identified by user UID. Can then be used in a%0A   * %3Ccode%3EFirebaseAuth%3C/code%3E instance's %3Ccode%3EgetUsers%3C/code%3E or %3Ccode%3EgetUsersAsync%3C/code%3E%0A   * functions.%0A   */%0A  private List%3CUserIdentifier%3E getUserIdentifiers(Reader json) %7B%0A    Type typeOfListString = new TypeToken%3CList%3CString%3E%3E() %7B%7D.getType();%0A    List%3CString%3E userUids = new Gson().fromJson(json, typeOfListString);%0A    return userUids.stream()%0A                   .map(uid -%3E new UidIdentifier(uid))%0A                   .collect(Collectors.toList());%0A  %7D%0A%0A  /**%0A   * %7B@inheritDoc%7D%0A   *%0A   * Given a JSON array of user UIDs, sends back to the response a JSON array of the corresponding%0A   * user emails. On error, sends an empty response.%0A   */%0A  @Override%0A  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException %7B%0A    List%3CUserIdentifier%3E userIdentifiers = getUserIdentifiers(request.getReader());%0A%0A    GetUsersResult result;%0A    try %7B%0A      result = FirebaseAuth.getInstance().getUsersAsync(userIdentifiers).get();%0A    %7D catch (InterruptedException %7C ExecutionException error) %7B%0A      System.err.println(%22Error when asynchronously getting users:%22);%0A      error.printStackTrace();%0A      return;%0A    %7D%0A    List%3CString%3E userEmails = result.getUsers()%0A                                    .stream()%0A                                    .map(user -%3E user.getEmail())%0A                                    .collect(Collectors.toList());%0A%0A    response.setContentType(%22application/json%22);%0A    response.getWriter().println(new Gson().toJson(userEmails));
 %0A  %7D

Implement converting from UIDs to emails