package de.jeha.j7.resources;

import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author jenshadlich@googlemail.com
 */
@RestController
public class ProxyResource {

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

    @Autowired
    private CloseableHttpClient httpClient;

    @RequestMapping(value = "/**", method = RequestMethod.GET)
    public void proxy(HttpServletRequest request, HttpServletResponse response) throws IOException {
        LOG.info("proxy request '{}', '{}'", request.getRequestURI(), request.getQueryString());

        String url = "http://" + chooseBackendInstance() + request.getRequestURI();
        if (!StringUtils.isEmpty(request.getQueryString())) {
            url += request.getQueryString();
        }

        HttpGet delegate = new HttpGet(url);

        final CloseableHttpResponse backendResponse;
        try {
            backendResponse = httpClient.execute(delegate);
        } catch (IOException e) {
            LOG.warn("502 Bad Gateway '{}'", e.getMessage());
            response.setContentType("text/plain");
            response.getOutputStream().print("502 Bad Gateway");
            response.setStatus(HttpServletResponse.SC_BAD_GATEWAY);
            response.flushBuffer();
            return;
        }

        try {
            // copy headers to response
            for (Header header : backendResponse.getAllHeaders()) {
                if ("Connection".equals(header.getName())) {
                    continue;
                }
                response.setHeader(header.getName(), header.getValue());
            }
            if (backendResponse.getEntity().isStreaming()) {
                IOUtils.copy(backendResponse.getEntity().getContent(), response.getOutputStream());
                response.flushBuffer();
            }
        } finally {
            backendResponse.close();
            response.getOutputStream().close();
        }
    }

    private String chooseBackendInstance() {
        return "localhost:8888";
    }

}

@@ -97,60 +97,8 @@
 er;%0A
-import org.apache.http.client.config.RequestConfig;%0A
 impo
@@ -228,180 +228,77 @@
 ttp.
-entity.BufferedHttpEntity;%0Aimport org.apache.http.impl.client.CloseableHttpClient;%0Aimport org.apache.http.impl.client.HttpClientBuilder;%0Aimport org.apache.http.impl.client.
+client.methods.HttpHead;%0Aimport org.apache.http.impl.client.Closeable
 Http
@@ -303,17 +303,16 @@
 tpClient
-s
 ;%0Aimport
@@ -1102,16 +1102,19 @@
 id proxy
+Get
 (HttpSer
@@ -1209,16 +1209,20 @@
 (%22proxy 
+GET 
 request 
@@ -2660,24 +2660,1460 @@
    %7D%0A    %7D%0A%0A
+    @RequestMapping(value = %22/**%22, method = RequestMethod.HEAD)%0A    public void proxyHead(HttpServletRequest request, HttpServletResponse response) throws IOException %7B%0A        LOG.info(%22proxy HEAD request '%7B%7D', '%7B%7D'%22, request.getRequestURI(), request.getQueryString());%0A%0A        String url = %22http://%22 + chooseBackendInstance() + request.getRequestURI();%0A        if (!StringUtils.isEmpty(request.getQueryString())) %7B%0A            url += request.getQueryString();%0A        %7D%0A%0A        HttpHead delegate = new HttpHead(url);%0A%0A        final CloseableHttpResponse backendResponse;%0A        try %7B%0A            backendResponse = httpClient.execute(delegate);%0A        %7D catch (IOException e) %7B%0A            LOG.warn(%22502 Bad Gateway '%7B%7D'%22, e.getMessage());%0A            response.setContentType(%22text/plain%22);%0A            response.getOutputStream().print(%22502 Bad Gateway%22);%0A            response.setStatus(HttpServletResponse.SC_BAD_GATEWAY);%0A            response.flushBuffer();%0A            return;%0A        %7D%0A%0A        try %7B%0A            // copy headers to response%0A            for (Header header : backendResponse.getAllHeaders()) %7B%0A                if (%22Connection%22.equals(header.getName())) %7B%0A                    continue;%0A                %7D%0A                response.setHeader(header.getName(), header.getValue());%0A            %7D%0A        %7D finally %7B%0A            backendResponse.close();%0A            response.getOutputStream().close();%0A        %7D%0A    %7D%0A%0A
     private 

implement HEAD