package org.mb.http.basic;

import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;
import org.eclipse.jetty.server.handler.AbstractHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

/**
 * Created by Dmitriy Dzhevaga on 17.06.2015.
 */
public class JettyServer implements Server {
    private org.eclipse.jetty.server.Server jettyServer;

    public static Server newInstance(int port) {
        return new JettyServer(port);
    }

    private JettyServer(int port) {
        jettyServer = new org.eclipse.jetty.server.Server(port);
    }

    @Override
    public void start() throws Exception {
        if(jettyServer.getHandler() == null) {
            throw new IllegalStateException("Handler is not set");
        }
        jettyServer.start();
        jettyServer.join();
    }

    @Override
    public void setHandler(final Handler handler) {
        jettyServer.setHandler(new AbstractHandler() {
            @Override
            public void handle(String s, org.eclipse.jetty.server.Request httpRequest, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
                try {
                    Request request = readRequest(httpServletRequest);
                    Response response = handler.handle(request);
                    writeResponse(response, httpServletResponse);
                    httpRequest.setHandled(true);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    private Request readRequest(HttpServletRequest srcRequest) throws IllegalArgumentException, IOException {
        Request.Builder builder = Request.newBuilder(srcRequest.getRequestURI(), Method.of(srcRequest.getMethod()));

        Enumeration<String> headerNames = srcRequest.getHeaderNames();
        while(headerNames.hasMoreElements()){
            String headerName = headerNames.nextElement();
            String headerValue = srcRequest.getHeader(headerName);
            builder.addHeader(headerName, headerValue);
        }

        for(Map.Entry<String, String[]> parameter : srcRequest.getParameterMap().entrySet()) {
            String parameterName = parameter.getKey();
            List<String> parameterValues = Arrays.asList(parameter.getValue());
            builder.addQueryParameters(parameterName, parameterValues);
        }

        String encoding = srcRequest.getCharacterEncoding();
        if(Strings.isNullOrEmpty(encoding)){
            encoding = Charsets.UTF_8.toString();
        }

        String content = CharStreams.toString(new InputStreamReader(srcRequest.getInputStream(), encoding));
        builder.setContent(content);

        return builder.build();
    }

    private void writeResponse(Response srcResponse, HttpServletResponse dstResponse) throws IOException {
        dstResponse.setStatus(srcResponse.getStatusCode());

        for(Map.Entry<String, String> header : srcResponse.getHeaders().entrySet()) {
            dstResponse.setHeader(header.getKey(), header.getValue());
        }

        srcResponse.getContent().writeTo(dstResponse.getOutputStream());
    }
}

@@ -136,12 +136,12 @@
 .io.
-Byte
+Char
 Stre
@@ -157,40 +157,31 @@
 ort 
-com.google.common.io.CharStreams
+org.apache.log4j.Logger
 ;%0D%0Ai
@@ -243,48 +243,8 @@
 %0D%0A%0D%0A
-import javax.servlet.ServletException;%0D%0A
 impo
@@ -433,16 +433,16 @@
 .%0D%0A */%0D%0A
-
 public c
@@ -475,24 +475,187 @@
 s Server %7B%0D%0A
+    private static final String LOG_NO_RESPONSE_ERROR = %22No response will be sent%22;%0D%0A    private static final Logger Log = Logger.getLogger(JettyServer.class);%0D%0A%0D%0A
     private 
@@ -1799,24 +1799,167 @@
 ption e) %7B%0D%0A
+                    if(httpServletResponse.isCommitted()) %7B%0D%0A                        Log.error(LOG_NO_RESPONSE_ERROR);%0D%0A                    %7D%0D%0A
             

Add request processing error logging for case when it can not be sent in response