/*
 * 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 java.io.IOException;
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.handler.AbstractHandler;
import org.slf4j.bridge.SLF4JBridgeHandler;

/**
 * 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.
 */
public class LegacyHttpService extends AbstractHandler {

  public static void main(String[] args) throws Exception {
    SLF4JBridgeHandler.removeHandlersForRootLogger();
    SLF4JBridgeHandler.install();
    final Server server = new Server(8080);
    server.setHandler(new LegacyHttpService());
    server.start();
  }

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

@@ -718,32 +718,71 @@
 io.IOException;%0A
+import java.util.concurrent.Executors;%0A
 import javax.ser
@@ -796,32 +796,32 @@
 rvletException;%0A
-
 import javax.ser
@@ -974,24 +974,73 @@
 ver.Server;%0A
+import org.eclipse.jetty.server.ServerConnector;%0A
 import org.e
@@ -1080,24 +1080,130 @@
 actHandler;%0A
+import org.eclipse.jetty.util.thread.ExecutorThreadPool;%0Aimport org.eclipse.jetty.util.thread.ThreadPool;%0A
 import org.s
@@ -1638,16 +1638,108 @@
 stall();
+%0A%0A    final ThreadPool threadPool = new ExecutorThreadPool(Executors.newCachedThreadPool());
 %0A    fin
@@ -1772,12 +1772,18 @@
 ver(
-8080
+threadPool
 );%0A 
@@ -1825,24 +1825,158 @@
 Service());%0A
+%0A    final ServerConnector connector = new ServerConnector(server);%0A    connector.setPort(8080);%0A    server.addConnector(connector);%0A%0A
     server.s

Use a cached thread pool for Jetty.