/*
 * 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
 *
 *      http://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.codahale.grpcproxy;

import com.codahale.grpcproxy.helloworld.HelloReply;
import com.codahale.grpcproxy.helloworld.HelloRequest;
import io.airlift.airline.Command;
import io.airlift.airline.Option;
import java.io.IOException;
import java.util.concurrent.Executors;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.util.thread.ExecutorThreadPool;
import org.eclipse.jetty.util.thread.ThreadPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * An HTTP/1.1 server which parses protobuf messages in request bodies and emits protobuf messages
 * in response bodies. Implements, in its own way, the {@code helloworld.Greeter} service.
 */
class LegacyHttpServer extends AbstractHandler {

  private static final Logger LOGGER = LoggerFactory.getLogger(LegacyHttpServer.class);

  @Override
  public void handle(String target, Request baseRequest, HttpServletRequest request,
      HttpServletResponse response) throws IOException, ServletException {
    final String method = baseRequest.getParameter("method");
    if ("helloworld.Greeter/SayHello".equals(method)) {
      baseRequest.setHandled(true);
      sayHello(baseRequest, response);
    }
  }

  private void sayHello(Request request, HttpServletResponse response)
      throws IOException {
    final HelloRequest req = HelloRequest.parseFrom(request.getInputStream());
    final String greeting = "Hello " + req.getName();
    final HelloReply resp = HelloReply.newBuilder().setMessage(greeting).build();
    resp.writeTo(response.getOutputStream());
  }

  @Command(name = "http", description = "Run a legacy HTTP/Protobuf HelloWorld service.")
  public static class Cmd implements Runnable {

    @Option(name = {"-p", "--port"}, description = "the port to listen on")
    private int port = 8080;

    @Override
    public void run() {
      try {
        final ThreadPool threadPool = new ExecutorThreadPool(Executors.newCachedThreadPool());
        final Server server = new Server(threadPool);
        server.setHandler(new LegacyHttpServer());

        final ServerConnector connector = new ServerConnector(server);
        connector.setPort(port);
        server.addConnector(connector);

        server.start();
      } catch (Exception e) {
        LOGGER.error("Error running command", e);
      }
    }
  }
}

@@ -799,47 +799,8 @@
 on;%0A
-import java.util.concurrent.Executors;%0A
 impo
@@ -1155,65 +1155,14 @@
 ead.
-ExecutorThreadPool;%0Aimport org.eclipse.jetty.util.thread.
+Queued
 Thre
@@ -2552,153 +2552,182 @@
 80;%0A
-%0A
     @O
-verride%0A    public void run() %7B%0A      try %7B%0A        final ThreadPool threadPool = new ExecutorThreadPool(Executors.newCachedThreadPool());
+ption(name = %7B%22-c%22, %22--threads%22%7D, description = %22the number of worker threads to use%22)%0A    private int threads = 100;%0A%0A    @Override%0A    public void run() %7B%0A      try %7B
 %0A   
@@ -2768,18 +2768,37 @@
 ver(
-threadPool
+new QueuedThreadPool(threads)
 );%0A 

Use queued thread pool for Jetty.