package alien4cloud.webconfiguration;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import alien4cloud.exception.InitializationException;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Slf4j
@Configuration
public class StaticResourcesConfiguration extends WebMvcConfigurerAdapter {
    @Value("${directories.alien}/${directories.csar_repository}/")
    private String toscaRepo;
    @Value("${directories.alien}/editor/")
    private String editorRepo;
    @Value("${directories.alien}/work/plugins/ui/")
    private String pluginsUi;

    public final static String PLUGIN_STATIC_ENDPOINT = "/static/plugins/";

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String prefix = "file:///";
        // resource locations must be full path and not relatives
        String absToscaRepo = prefix.concat(safeGetRealPath(toscaRepo)).concat("/");
        String absEditorRepo = prefix.concat(safeGetRealPath(editorRepo)).concat("/");
        String absPluginUi = prefix.concat(safeGetRealPath(pluginsUi)).concat("/");

        log.info("Serving {} as tosca repo content.", absToscaRepo);
        log.info("Serving {} as plugin ui content.", absPluginUi);
        registry.addResourceHandler("/static/tosca/**").addResourceLocations(absToscaRepo);
        registry.addResourceHandler("/static/editor/**").addResourceLocations(absEditorRepo);
        registry.addResourceHandler(PLUGIN_STATIC_ENDPOINT + "**").addResourceLocations(absPluginUi);
    }

    private String safeGetRealPath(String pathStr) {
        Path path = Paths.get(pathStr);
        try {
            if (!Files.exists(path)) {
                Files.createDirectories(path);
            }
            return path.toRealPath().toString();
        } catch (IOException e) {
            throw new InitializationException("Unable to initialize alien as specified directories cannot be created.", e);
        }
    }
}
@@ -142,16 +142,61 @@
 e.Paths;
+%0Aimport java.util.List;%0Aimport java.util.Map;
 %0A%0Aimport
@@ -278,16 +278,67 @@
 Slf4j;%0A%0A
+import org.alien4cloud.tosca.editor.EditorService;%0A
 import o
@@ -480,143 +480,586 @@
 ork.
-web.servlet.config.annotation.ResourceHandlerRegistry;%0Aimport org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
+core.io.Resource;%0Aimport org.springframework.web.context.request.NativeWebRequest;%0Aimport org.springframework.web.context.request.ServletWebRequest;%0Aimport org.springframework.web.servlet.HandlerMapping;%0Aimport org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;%0Aimport org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;%0Aimport org.springframework.web.servlet.resource.ResourceResolver;%0Aimport org.springframework.web.servlet.resource.ResourceResolverChain;%0A%0Aimport javax.inject.Inject;%0Aimport javax.servlet.http.HttpServletRequest
 ;%0A%0A@
@@ -1155,16 +1155,69 @@
 apter %7B%0A
+    @Inject%0A    private EditorService editorService;%0A
     @Val
@@ -2264,16 +2264,29 @@
 /editor/
+%7BtopologyId%7D/
 **%22).add
@@ -2313,24 +2313,1310 @@
 sEditorRepo)
+.resourceChain(false)%0A                .addResolver(new ResourceResolver() %7B%0A                    @Override%0A                    public Resource resolveResource(HttpServletRequest request, String requestPath, List%3C? extends Resource%3E locations,%0A                            ResourceResolverChain chain) %7B%0A                        log.debug(%22Resolving editor resource%22);%0A                        // check security for the requested topology file.%0A                        ServletWebRequest webRequest = new ServletWebRequest(request);%0A                        Map uriTemplateVars = (Map) webRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, 0);%0A                        String topologyId = (String) uriTemplateVars.get(%22topologyId%22);%0A                        editorService.checkAuthorization(topologyId);%0A                        // let the usual resolving%0A                        return chain.resolveResource(request, topologyId + %22/%22 + requestPath, locations);%0A                    %7D%0A%0A                    @Override%0A                    public String resolveUrlPath(String resourceUrlPath, List%3C? extends Resource%3E locations, ResourceResolverChain chain) %7B%0A                        return chain.resolveUrlPath(resourceUrlPath, locations);%0A%0A                    %7D%0A                %7D)
 ;%0A        re

Secure static resources from the editor.