package com.google.sps.servlets;

import com.google.firebase.FirebaseApp;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.sps.firebase.FirebaseAppManager;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/retrieveFile")
public class FileRetriever extends HttpServlet {

  public FileRetriever() throws IOException {}

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    FirebaseApp app = FirebaseAppManager.getApp();
    FirebaseDatabase db = FirebaseDatabase.getInstance(app);
    db.goOnline();
    String refPath = RequestUtils.getParameter(req, "path", "");

    DatabaseReference ref = db.getReference(refPath);

    ref.child("t").setValueAsync("t");

    CompletableFuture<String> future = new CompletableFuture<>();
    ref.addListenerForSingleValueEvent(
        new ValueEventListener() {

          @Override
          public void onDataChange(DataSnapshot snapshot) {
            ref.removeEventListener(this);

            // WorkspaceFile file = new WorkspaceFile(snapshot);
            future.complete(Thread.currentThread().getName());

            // try {
            // future.complete(file.getContents().get());
            // } catch (InterruptedException | ExecutionException e) {
            // future.completeExceptionally(e);
            // }
          }

          @Override
          public void onCancelled(DatabaseError error) {
            // TODO Auto-generated method stub
            future.completeExceptionally(error.toException());
          }
        });

    try {
      resp.getWriter().println(future.get());
      resp.getWriter().println(Thread.currentThread().getName());
    } catch (InterruptedException | ExecutionException e) {
      resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getStackTrace().toString());
    }
  }
}

@@ -380,24 +380,71 @@
 AppManager;%0A
+import com.google.sps.workspace.WorkspaceFile;%0A
 import java.
@@ -451,32 +451,32 @@
 io.IOException;%0A
-
 import java.util
@@ -1278,22 +1278,29 @@
 eFuture%3C
-String
+WorkspaceFile
 %3E future
@@ -1540,19 +1540,16 @@
         
- //
  Workspa
@@ -1623,258 +1623,14 @@
 ete(
-Thread.currentThread().getName());%0A%0A            // try %7B%0A            // future.complete(file.getContents().get());%0A            // %7D catch (InterruptedException %7C ExecutionException e) %7B%0A            // future.completeExceptionally(e);%0A            // %7D
+file);
 %0A   
@@ -1904,16 +1904,36 @@
 re.get()
+.getContents().get()
 );%0A     
@@ -2049,24 +2049,24 @@
 eption e) %7B%0A
+
       resp.s
@@ -2126,24 +2126,8 @@
 , e.
-getStackTrace().
 toSt

Remove commented out code.