package com.google.research.bleth.servlets;

import com.google.cloud.tasks.v2.AppEngineHttpRequest;
import com.google.cloud.tasks.v2.CloudTasksClient;
import com.google.cloud.tasks.v2.HttpMethod;
import com.google.cloud.tasks.v2.QueueName;
import com.google.cloud.tasks.v2.Task;
import com.google.protobuf.ByteString;

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

/**
 * A servlet used for enqueuing a task targeted at endpoint '/new-simulation',
 * in order to create and run a new simulation.
 */
@WebServlet("/enqueue-simulation")
public class EnqueueSimulationServlet extends HttpServlet {

    static final String PROJECT_ID = "bleth-2020";
    static final String LOCATION_ID = "europe-west1";
    static final String QUEUE_ID = "simulations-queue";

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        // If servlet runs on localhost, forward request without creating and enqueuing a task.
        if (request.getServerName().equals("localhost") && request.getServerPort() == 8080) {
            RequestDispatcher dispatcher = getServletContext()
                    .getRequestDispatcher("/new-simulation");
            dispatcher.forward(request, response);
        } else {
            try (CloudTasksClient client = CloudTasksClient.create()) {
                // Construct the HTTP request (for the task).
                AppEngineHttpRequest httpRequest = AppEngineHttpRequest.newBuilder()
                        .setRelativeUri("/new-simulation")
                        .setHttpMethod(HttpMethod.POST)
                        .putHeaders("Content-Type", request.getContentType())
                        .setBody(ByteString.readFrom(request.getInputStream()))
                        .build();

                // Construct the task body.
                Task task = Task.newBuilder()
                        .setAppEngineHttpRequest(httpRequest)
                        .build();

                // Add the task to the queue.
                String QUEUE_NAME = QueueName.of(PROJECT_ID, LOCATION_ID, QUEUE_ID).toString();
                client.createTask(QUEUE_NAME, task);
            }

            response.setContentType("text/plain;");
            response.getWriter().println("Task has been added to queue.");
        }
    }
}
@@ -2276,26 +2276,25 @@
  String 
-QUEUE_NAME
+queueName
  = Queue
@@ -2386,18 +2386,17 @@
 ask(
-QUEUE_NAME
+queueName
 , ta

change variable name.