package com.google.sps.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.google.gson.Gson;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Given a tweet or sentence, the servlet will extract salient terms from those
 * and store them in a list.
 */
@WebServlet("/keyword")
public class KeywordServlet extends HttpServlet {

  private static final List<String> tweets = new ArrayList<>();
  private static final List<String> terms = Arrays.asList("Black Lives Matter", "COVID-19");

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // TODO: Extract saliency
    Gson gson = new Gson();
    String json = gson.toJson(terms);
    response.getWriter().println(json);
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    final String tweet = request.getParameter("keyword-sentence");
    if (tweet != null && !tweet.equals("")) {
      tweets.add(request.getParameter("keyword-sentence"));
    }
    response.sendRedirect("/index.html");
  }

}


@@ -129,41 +129,41 @@
 til.
-List;%0Aimport com.google.gson.Gson
+Collection;%0Aimport java.util.List
 ;%0Aim
@@ -836,61 +836,41 @@
     
-Gson gson = new Gson();%0A    String json = gson.toJson
+String json = mustacheJsonBuilder
 (ter
@@ -1210,16 +1210,16 @@
 ;%0A    %7D%0A
-
     resp
@@ -1257,11 +1257,402 @@
 );%0A  %7D%0A%0A
+  /**%0A   * Generates and returns a JSON usable by Mustache.%0A   */%0A  private String mustacheJsonBuilder(Collection%3CString%3E toJson) %7B%0A    String builder = %22%7B%5C%22keywords%5C%22: %5B%22;%0A    for (String term : toJson) %7B%0A      builder += %22%7B%5C%22term%5C%22: %5C%22%22 + term + %22%5C%22%7D,%22;%0A    %7D%0A%0A    // Remove extra comma at the end.%0A    builder = builder.substring(0, builder.length() - 1) + %22%5D%7D%22;%0A    return builder;%0A  %7D%0A%0A
 %7D%0A%0A

Change get's output to format Mustache can use