package de.digitalcollections.iiif.serverdemo.interceptor;

import de.digitalcollections.iiif.serverdemo.service.DemoPresentationServiceImpl;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class ServerUrlInterceptor extends HandlerInterceptorAdapter {

  @Autowired
  DemoPresentationServiceImpl demoPresentationServiceImpl;

  //before the actual handler will be executed
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    String requestUrl = request.getRequestURL().toString();
    if (requestUrl.endsWith("/manifest")) {
      String contextPath = request.getContextPath();
      String requestUri = request.getRequestURI();
      String serverUrl = retrieveServerUrl(requestUrl, requestUri, contextPath);
      demoPresentationServiceImpl.setServerUrl(serverUrl);
    }
    return true;
  }

  protected String retrieveServerUrl(String requestUrl, String requestUri, String contextPath) {
    String requestUriWithoutContextPath = requestUri.substring(contextPath.length());
    String serverUrl = requestUrl.substring(0, requestUrl.indexOf(requestUriWithoutContextPath));
    return serverUrl;
  }
}
@@ -1043,16 +1043,76 @@
 ;%0A    %7D%0A
+    response.addHeader(%22Access-Control-Allow-Origin%22, %22*%22);%0A
     retu
@@ -1433,8 +1433,9 @@
 l;%0A  %7D%0A%7D
+%0A

Add cors header