package ee.tuleva.onboarding.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CORSFilter extends GenericFilterBean {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "https://pension.tuleva.ee");
        response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Authorization");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("P3P", "CP=\"ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI\"");

        List<String> allowedHeaders = Arrays.asList(
                "x-requested-with",
                "x-statistics-identifier",
                HttpHeaders.CONTENT_TYPE,
                HttpHeaders.AUTHORIZATION,
                HttpHeaders.USER_AGENT,
                HttpHeaders.ORIGIN,
                HttpHeaders.ACCEPT
                );
        response.setHeader("Access-Control-Allow-Headers",  String.join(", ", allowedHeaders));

        HttpServletRequest request = ((HttpServletRequest) req);

        if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
            response.setStatus(HttpServletResponse.SC_OK);
        } else {
            chain.doFilter(req, res);
        }
    }
}

@@ -732,16 +732,184 @@
 Bean %7B%0A%0A
+    String DEFAULT_ALLOWED_ORIGIN = %22https://pension.tuleva.ee%22;%0A    private List%3CString%3E allowedOrigins = Arrays.asList(DEFAULT_ALLOWED_ORIGIN, %22https://tuleva.ee%22);%0A%0A
     @Ove
@@ -914,16 +914,16 @@
 verride%0A
-
     publ
@@ -1104,16 +1104,238 @@
 e) res;%0A
+        HttpServletRequest request = (HttpServletRequest) req;%0A        String origin = request.getHeader(%22Origin%22);%0A        String allowedOriginResponse = allowedOrigins.contains(origin) ? origin : DEFAULT_ALLOWED_ORIGIN;%0A
         
@@ -1388,35 +1388,29 @@
 n%22, 
-%22https://pension.tuleva.ee%22
+allowedOriginResponse
 );%0A 
@@ -2314,74 +2314,8 @@
 );%0A%0A
-        HttpServletRequest request = ((HttpServletRequest) req);%0A%0A
     

Add tuleva.ee as cors origin