package com.cgi.eoss.fstep.orchestrator.service;

import com.cgi.eoss.fstep.rpc.Job;
import com.cgi.eoss.fstep.rpc.worker.Binding;
import com.cgi.eoss.fstep.rpc.worker.FstepWorkerGrpc;
import com.cgi.eoss.fstep.rpc.worker.PortBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * <p>Service for more specific interaction with graphical application-type FS-TEP services.</p>
 */
@Component
public class FstepGuiServiceManager {

    static final String GUACAMOLE_PORT = "8080/tcp";

    @Value("${fstep.orchestrator.gui.urlPattern:/gui/:__PORT__/}")
    private String guiUrlPattern;

    @Autowired
    public FstepGuiServiceManager() {
    }

    /**
     * @return The string representation of a URL suitable for accessing the GUI application represented by the given
     * Job running on the given worker.
     */
    // TODO Expose the GUI URL via a reverse proxy with FS-TEP access controls
    public String getGuiUrl(FstepWorkerGrpc.FstepWorkerBlockingStub worker, Job rpcJob) {
        PortBinding portBinding = worker.getPortBindings(rpcJob).getBindingsList().stream()
                .filter(b -> b.getPortDef().equals(GUACAMOLE_PORT))
                .findFirst()
                .orElseThrow(() -> new ServiceExecutionException("Could not find GUI port on docker container for job: " + rpcJob.getId()));

        Binding binding = portBinding.getBinding();

        return guiUrlPattern.replaceAll("__PORT__", String.valueOf(binding.getPort()));
    }

}

@@ -43,16 +43,187 @@
 rvice;%0A%0A
+import org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.beans.factory.annotation.Value;%0Aimport org.springframework.stereotype.Component;%0A
 import c
@@ -402,179 +402,8 @@
 ing;
-%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.beans.factory.annotation.Value;%0Aimport org.springframework.stereotype.Component;
 %0A%0A/*
@@ -1497,25 +1497,24 @@
 ding();%0A
-%0A
         
 return g
@@ -1505,16 +1505,193 @@
         
+String guiUrl = guiUrlPattern;%0A        if (guiUrl.contains(%22__HOST__%22)) %7B%0A            guiUrl = guiUrl.replaceAll(%22__HOST__%22, String.valueOf(binding.getIp()));%0A        %7D%0A        
 return g
@@ -1695,23 +1695,16 @@
 n guiUrl
-Pattern
 .replace

Support host replacing in gui pattern