package com.github.onsdigital.florence.filter;

import com.github.davidcarboni.restolino.framework.Filter;
import com.github.onsdigital.florence.configuration.Configuration;
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 zebedeeToken = "/zebedee";

    private static final String tredegarBaseUrl = Configuration.getTredegarUrl();
    private static final String zebedeeBaseUrl = Configuration.getZebedeeUrl();

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

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

        String requestUri = request.getRequestURI();
        String requestQueryString = request.getQueryString() != null ? request.getQueryString() : "";

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

            String requestBaseUrl = tredegarBaseUrl; // proxy to tredegar by default.

            if (requestUri.startsWith(zebedeeToken)) {
                requestUri = requestUri.replace(zebedeeToken, "");
                requestBaseUrl = zebedeeBaseUrl;
            }

            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(requestBaseUrl + requestUri + "?" + requestQueryString);

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

//            if (requestBaseUrl == zebedeeBaseUrl)
//            {
//                httpGet.addHeader("X-Florence-Token", );
//            }

            CloseableHttpResponse proxyResponse = httpClient.execute(httpGet);

            try {
                HttpEntity responseEntity = proxyResponse.getEntity();

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

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

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

        } catch (IOException e) {
            return true;
        }

        return false;
    }
}
@@ -205,16 +205,61 @@
 OUtils;%0A
+import org.apache.commons.lang3.StringUtils;%0A
 import o
@@ -2309,24 +2309,63 @@
 derNames();%0A
+%0A            String accessToken = %22%22;%0A%0A
             
@@ -2556,16 +2556,141 @@
 rName));
+%0A%0A                if (headerName.equals(%22access_token%22))%0A                    accessToken = request.getHeader(%22access_token%22);
 %0A       
@@ -2697,18 +2697,16 @@
      %7D%0A%0A
-//
         
@@ -2749,12 +2749,49 @@
 eUrl
+ && StringUtils.isNotEmpty(accessToken)
 )%0A
-//
     
@@ -2800,18 +2800,16 @@
       %7B%0A
-//
         
@@ -2858,13 +2858,22 @@
 n%22, 
+accessToken
 );%0A
-//
     

Add authentication header to zebedee requests if the access_token cookie is set.