package com.handson.infra;


import io.netty.buffer.ByteBuf;
import io.reactivex.netty.RxNetty;
import io.reactivex.netty.protocol.http.server.HttpServer;
import io.reactivex.netty.protocol.http.server.RequestHandlerWithErrorMapper;
import io.reactivex.netty.protocol.http.server.file.FileErrorResponseMapper;
import io.reactivex.netty.protocol.http.server.file.FileRequestHandler;

import java.io.File;
import java.io.IOException;
import java.net.URI;

public class StaticServer {

    private final String rootDir;
    private final int port;

    public StaticServer(String rootDir, int port) {
        this.rootDir = rootDir;
        this.port = port;
    }

    public HttpServer<ByteBuf, ByteBuf> createServer() {
        File rootDirectory = new File(rootDir);
        return RxNetty.createHttpServer(8000,
                RequestHandlerWithErrorMapper.from(
                        new LocalDirectoryRequestHandler(rootDirectory),
                        new FileErrorResponseMapper()));
    }

    public static class LocalDirectoryRequestHandler extends FileRequestHandler {

        private final File directory;

        public LocalDirectoryRequestHandler(File directory) {
            this.directory =  directory;
        }

        @Override
        protected URI resolveUri(String path) {
            if (File.separator.equals(path)) {
                path = "index.html";
            }
            File file = new File(directory, path);
            try {
                if (!file.getCanonicalPath().startsWith(directory.getCanonicalPath())) {
                    System.out.println("Forbiden path");
                    return null;
                }
                if (!file.exists()) {
                    System.out.println("file not found " + path);
                    return null;
                }
                return file.toURI();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }
}

@@ -721,20 +721,328 @@
         
-File
+try %7B%0A            final String ideBasePath = new File(%22.%22).getCanonicalPath();%0A            final File rootDirectory;%0A            if (ideBasePath.contains(rootDir)) %7B%0A                // for eclipse%0A                rootDirectory = new File(%22.%22);%0A            %7D else %7B%0A                // for intellij%0A               
  rootDir
@@ -1069,16 +1069,34 @@
 otDir);%0A
+            %7D%0A    
         
@@ -1149,16 +1149,20 @@
         
+    
 RequestH
@@ -1209,24 +1209,28 @@
             
+   
+ 
 new LocalDir
@@ -1286,24 +1286,28 @@
             
+    
 new FileErro
@@ -1327,16 +1327,103 @@
 er()));%0A
+        %7D catch (IOException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A
     %7D%0A%0A 

Fix static file server within eclipse