/*
 * 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.gprcproxy;

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.Handler;
import org.eclipse.jetty.server.NCSARequestLog;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.RequestLogHandler;

/**
 * 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 HttpServer {

  public static void main(String[] args) throws Exception {
    final Server server = new Server(8080);
    final AbstractHandler handler = new AbstractHandler() {
      @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);
          final HelloRequest req = HelloRequest.parseFrom(request.getInputStream());
          final HelloReply resp = HelloReply.newBuilder().setMessage("Hello " + req.getName())
                                            .build();
          response.setStatus(HttpServletResponse.SC_OK);
          resp.writeTo(response.getOutputStream());
        }
      }
    };

    final HandlerCollection handlers = new HandlerCollection();
    final RequestLogHandler requestLogHandler = new RequestLogHandler();
    requestLogHandler.setRequestLog(new NCSARequestLog());
    handlers.setHandlers(new Handler[]{handler, requestLogHandler});
    server.setHandler(handlers);
    server.start();
    server.join();
  }
}

@@ -862,97 +862,8 @@
 se;%0A
-import org.eclipse.jetty.server.Handler;%0Aimport org.eclipse.jetty.server.NCSARequestLog;%0A
 impo
@@ -999,126 +999,8 @@
 ler;
-%0Aimport org.eclipse.jetty.server.handler.HandlerCollection;%0Aimport org.eclipse.jetty.server.handler.RequestLogHandler;
 %0A%0A/*
@@ -2107,274 +2107,8 @@
   %7D;
-%0A%0A    final HandlerCollection handlers = new HandlerCollection();%0A    final RequestLogHandler requestLogHandler = new RequestLogHandler();%0A    requestLogHandler.setRequestLog(new NCSARequestLog());%0A    handlers.setHandlers(new Handler%5B%5D%7Bhandler, requestLogHandler%7D);
 %0A   
@@ -2133,17 +2133,16 @@
 (handler
-s
 );%0A    s

Drop the Jetty request log.