package fr.synchrotron.soleil.ica.proxy.midlleware.response;

import fr.synchrotron.soleil.ica.proxy.midlleware.MiddlewareContext;
import fr.synchrotron.soleil.ica.proxy.midlleware.ProxyService;
import io.netty.handler.codec.http.HttpMethod;
import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpClient;
import org.vertx.java.core.http.HttpClientResponse;
import org.vertx.java.core.http.HttpHeaders;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.logging.Logger;
import org.vertx.java.core.logging.impl.LoggerFactory;
import org.vertx.java.core.streams.Pump;

/**
 * @author Gregory Boissinot
 */
public class DefaultClientResponseHandler implements ClientResponseHandler {

    private static final Logger LOG = LoggerFactory.getLogger(DefaultClientResponseHandler.class);

    protected MiddlewareContext context;

    public DefaultClientResponseHandler(MiddlewareContext context) {
        this.context = context;
    }

    public Handler<HttpClientResponse> get() {

        final HttpServerRequest request = context.getHttpServerRequest();
        return new Handler<HttpClientResponse>() {
            @Override
            public void handle(HttpClientResponse clientResponse) {

                final int statusCode = clientResponse.statusCode();
                if (LOG.isDebugEnabled()) {
                    LOG.debug(String.format("Returned endpoint status code: %s", statusCode));
                }

                switch (statusCode) {
                    case 404:
                        sendWithoutTransferEncodingResponse(clientResponse);
                        break;
                    default:
                        sendPassThroughResponse(clientResponse);
                        break;
                }
            }

            private void sendWithoutTransferEncodingResponse(final HttpClientResponse clientResponse) {
                final HttpServerRequest request = context.getHttpServerRequest();
                request.response().setStatusCode(clientResponse.statusCode());
                request.response().setStatusMessage(clientResponse.statusMessage());
                request.response().headers().set(clientResponse.headers());
                request.response().headers().remove(HttpHeaders.TRANSFER_ENCODING);
                ProxyService proxyService = new ProxyService();
                proxyService.fixWarningCookieDomain(context, clientResponse);
                endRequest();
            }

            private void sendPassThroughResponse(final HttpClientResponse clientResponse) {
                clientResponse.pause();
                request.response().setStatusCode(clientResponse.statusCode());
                request.response().setStatusMessage(clientResponse.statusMessage());
                request.response().headers().set(clientResponse.headers());
                ProxyService proxyService = new ProxyService();
                proxyService.fixWarningCookieDomain(context, clientResponse);
                clientResponse.endHandler(new Handler<Void>() {
                    public void handle(Void event) {
                        endRequest();
                    }
                });

                if (HttpMethod.GET.name().equals(request.method())) {
                    request.response().setChunked(true);
                    Pump.createPump(clientResponse, request.response()).start();
                }

                clientResponse.resume();
            }
        };
    }

    protected void endRequest() {
        final HttpServerRequest request = context.getHttpServerRequest();
        final HttpClient httpClient = context.getHttpClient();
        request.response().end();
        if (httpClient != null) {
            httpClient.close();
        }
    }


}

@@ -267,24 +267,106 @@
 re.Handler;%0A
+import org.vertx.java.core.VoidHandler;%0Aimport org.vertx.java.core.buffer.Buffer;%0A
 import org.v
@@ -2534,32 +2534,34 @@
                 
+//
 endRequest();%0A  
@@ -2550,32 +2550,646 @@
 //endRequest();%0A
+                final Buffer clientRepsonseBody = new Buffer();%0A                clientResponse.dataHandler(new Handler%3CBuffer%3E() %7B%0A                    @Override%0A                    public void handle(final Buffer data) %7B%0A                        clientRepsonseBody.appendBuffer(data);%0A                    %7D%0A                %7D);%0A                clientResponse.endHandler(new VoidHandler() %7B%0A                    @Override%0A                    protected void handle() %7B%0A                        request.response().write(clientRepsonseBody);%0A                        endRequest();%0A                    %7D%0A                %7D);%0A
             %7D%0A%0A 

Fix Response 404 with write content