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";
    static final String QUEUE_NAME = QueueName.of(PROJECT_ID, LOCATION_ID, QUEUE_ID).toString();

    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);
        }

        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.
            client.createTask(QUEUE_NAME, task);
        }

        response.setContentType("text/plain;");
        response.getWriter().println("Task has been added to queue.");
    }
}
@@ -988,24 +988,26 @@
 queue%22;%0A    
+//
 static final
@@ -1576,34 +1576,44 @@
 onse);%0A        %7D
-%0A%0A
+ else %7B%0A    
         try (Clo
@@ -1668,32 +1668,36 @@
 ) %7B%0A            
+    
 // Construct the
@@ -1726,16 +1726,20 @@
  task).%0A
+    
         
@@ -1827,24 +1827,28 @@
             
+   
+ 
 .setRelative
@@ -1886,24 +1886,28 @@
             
+    
 .setHttpMeth
@@ -1946,16 +1946,20 @@
         
+ 
+   
 .putHead
@@ -2020,24 +2020,28 @@
             
+   
+ 
 .setBody(Byt
@@ -2096,32 +2096,36 @@
                 
+    
 .build();%0A%0A     
@@ -2111,32 +2111,36 @@
      .build();%0A%0A
+    
             // C
@@ -2175,16 +2175,20 @@
         
+  
+  
 Task tas
@@ -2201,32 +2201,36 @@
 sk.newBuilder()%0A
+    
                 
@@ -2283,32 +2283,36 @@
                 
+    
 .build();%0A%0A     
@@ -2318,16 +2318,20 @@
         
+  
+  
 // Add t
@@ -2352,16 +2352,116 @@
  queue.%0A
+                String QUEUE_NAME = QueueName.of(PROJECT_ID, LOCATION_ID, QUEUE_ID).toString();%0A    
         
@@ -2513,11 +2513,19 @@
     
-%7D%0A%0A
+    %7D%0A%0A    
     
@@ -2568,32 +2568,36 @@
 ain;%22);%0A        
+   
+ 
 response.getWrit
@@ -2639,19 +2639,29 @@
 o queue.%22);%0A
+        %7D%0A
     %7D%0A%7D

fix localhost bag.