package com.github.onsdigital.florence.api;

import com.github.davidcarboni.restolino.framework.Filter;
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;

/**
* Routes all traffic to Tredegar, Unless it is recognised as a florence file being requested.
*/
public class TredegarProxy implements Filter {


    private static final String florenceToken = "/florence";
    private static final String tredegarBaseUrl = "http://localhost:8080";

    private static final List<String> florencePaths = Arrays.asList("/");

    @Override
    public boolean filter(HttpServletRequest request, HttpServletResponse response) {

        try {
            if (florencePaths.contains(request.getRequestURI())
                    || request.getRequestURI().startsWith(florenceToken)) {
                return true; // carry on and serve the file from florence
            }

            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(tredegarBaseUrl + request.getRequestURI());

            // copy the request headers.
            Enumeration<String> headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements())
            {
                String headerName = headerNames.nextElement();
                httpGet.addHeader(headerName, request.getHeader(headerName));
            }

            CloseableHttpResponse tredegarResponse = httpClient.execute(httpGet);

            try {
                HttpEntity responseEntity = tredegarResponse.getEntity();

                // copy headers from the response
                for (Header header : tredegarResponse.getAllHeaders()) {
                    response.setHeader(header.getName(), header.getValue());
                }

                IOUtils.copy(responseEntity.getContent(), response.getOutputStream());
                EntityUtils.consume(responseEntity);

            } catch (IOException e) {
                return true;
            } finally {
                tredegarResponse.close();
            }

        } catch (IOException e) {
            return true;
        }

        return false;
    }
}
@@ -902,48 +902,182 @@
 ing 
-tredegarBaseUrl = %22http://localhost:8080
+zebedeeToken = %22/zebedee%22;%0A%0A    private static final String tredegarBaseUrl = %22http://localhost:8080%22;%0A    private static final String zebedeeBaseUrl = %22http://localhost:8082
 %22;%0A%0A
@@ -1252,16 +1252,172 @@
 nse) %7B%0A%0A
+        String requestUri = request.getRequestURI();%0A        String requestQueryString = request.getQueryString() != null ? request.getQueryString() : %22%22;%0A%0A
         
@@ -1468,32 +1468,19 @@
 (request
-.getRequestURI()
+Uri
 )%0A      
@@ -1503,32 +1503,19 @@
  request
-.getRequestURI()
+Uri
 .startsW
@@ -1617,32 +1617,305 @@
 %0A            %7D%0A%0A
+            String requestBaseUrl = tredegarBaseUrl; // proxy to tredegar by default.%0A%0A            if (requestUri.startsWith(zebedeeToken)) %7B%0A                requestUri = requestUri.replace(zebedeeToken, %22%22);%0A                requestBaseUrl = zebedeeBaseUrl;%0A            %7D%0A%0A
             Clos
@@ -2014,24 +2014,23 @@
 HttpGet(
-t
 re
-degar
+quest
 BaseUrl 
@@ -2038,32 +2038,46 @@
  request
-.getRequestURI()
+Uri + %22?%22 + requestQueryString
 );%0A%0A    
@@ -2439,24 +2439,21 @@
 esponse 
-tredegar
+proxy
 Response
@@ -2547,24 +2547,21 @@
 ntity = 
-tredegar
+proxy
 Response
@@ -2662,24 +2662,21 @@
 eader : 
-tredegar
+proxy
 Response
@@ -3039,24 +3039,21 @@
         
-tredegar
+proxy
 Response

Copy query string in proxy. Added zebedee token to proxy allowing the api to handle the url used at runtime.