package com.kodcu.controller;

import com.kodcu.other.Current;
import com.kodcu.service.DirectoryService;
import com.kodcu.service.ThreadService;
import com.kodcu.service.ui.TabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.file.Path;
import java.util.Optional;

/**
 * Created by usta on 05.09.2015.
 */
@Component
public class WebWorkerResource {

    private final Current current;
    private final TabService tabService;
    private final DirectoryService directoryService;
    private final FileService fileService;
    private final ThreadService threadService;
    private final ApplicationController controller;
    private final DataUriController dataUriService;

    @Autowired
    public WebWorkerResource(Current current, TabService tabService, DirectoryService directoryService, FileService fileService, ThreadService threadService, ApplicationController controller, DataUriController dataUriService) {
        this.current = current;
        this.tabService = tabService;
        this.directoryService = directoryService;
        this.fileService = fileService;
        this.threadService = threadService;
        this.controller = controller;
        this.dataUriService = dataUriService;
    }


    public void executeWorkerResource(AllController.Payload payload) {

        Optional.ofNullable(payload.getRequestURI())
                .filter(e -> e.endsWith("resource.afx"))
                .ifPresent(e -> {
                    payload.setFinalURI(payload.param("path"));
                });

        String finalURI = payload.getFinalURI();
        if (finalURI.matches(".*\\.(asc|asciidoc|ad|adoc|md|markdown)$")) {

            if (controller.getIncludeAsciidocResource()) {
                payload.write(String.format("link:%s[]", finalURI));
                return;
            }

            Path path = directoryService.findPathInConfigOrCurrentOrWorkDir(finalURI);
            fileService.processFile(payload, path);

        } else if (payload.getRequestURI().endsWith("webworker.js")) {
            Path path = directoryService.findPathInConfigOrCurrentOrWorkDir("js/webworker.js");
            fileService.processFile(payload, path);
        } else if (payload.getRequestURI().endsWith("asciidoctor-default.css")) {
            final String stylesheet = controller.readDefaultStylesheet();
            payload.write(stylesheet);
        } else {
            Path path = directoryService.findPathInConfigOrCurrentOrWorkDir(finalURI);
            fileService.processFile(payload, path);
        }
    }
}

@@ -175,24 +175,81 @@
 TabService;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import org.s
@@ -347,16 +347,68 @@
 mponent;
+%0Aimport org.springframework.web.client.RestTemplate;
 %0A%0Aimport
@@ -865,16 +865,140 @@
 Service;
+%0A    private final RestTemplate restTemplate;%0A%0A    private Logger logger = LoggerFactory.getLogger(WebWorkerResource.class);
 %0A%0A    @A
@@ -1230,16 +1230,43 @@
 iService
+, RestTemplate restTemplate
 ) %7B%0A    
@@ -1545,24 +1545,66 @@
 UriService;%0A
+        this.restTemplate = restTemplate;%0A
     %7D%0A%0A%0A    
@@ -2193,16 +2193,581 @@
      %7D%0A%0A
+            if (finalURI.startsWith(%22//%22)) %7B%0A                finalURI = finalURI.replace(%22//%22, %22http://%22);%0A            %7D%0A%0A            if (finalURI.startsWith(%22http://%22) %7C%7C finalURI.startsWith(%22https://%22)) %7B%0A%0A                String data = %22%22;%0A%0A                try %7B%0A                    data = restTemplate.getForObject(finalURI, String.class);%0A                %7D catch (Exception ex) %7B%0A                    logger.warn(%22resource not found or not readable: %7B%7D%22, finalURI);%0A                %7D%0A%0A                payload.write(data);%0A%0A                return;%0A            %7D%0A%0A
         

Fix Include Content from a URI #228