2

Is there an easy way to show the directory listing of my SPRING BOOT (v 2.1) resources/static folder?

The files are located under resources/static and I can access them separately, but I want to have a listing of all files and open them by clicking on the title like shown in the picture.

I want to "expose" the Log Files under resources/static/logs. If possible answer the question in Kotlin.

I found a similar question on SO but it didn't help: Spring boot Tomcat – Enable/disable directory listing

Directory Listing

  • 1
    You are aware that you are basically browsing the classpath and you are opening up your application for some nice exploits. – M. Deinum Apr 21 '20 at 14:35
  • @M.Deinum is right and why are there log files in resources/static/logs? There is an actuator endpoint for logfiles called logfile curl 'http://localhost:8080/actuator/logfile' -i -X GET – Simon Martinelli Apr 21 '20 at 14:46

1 Answers1

-1

Try this. It detects new files and folders (register new folder watcher) and performs some logic.

Somewhere in config class...

@Bean(name = "storageWatchService")
public WatchService createWatchService() throws IOException {
    return FileSystems.getDefault().newWatchService();
}

@Component
public class StorageWatcher implements ApplicationRunner {

    private static final Logger LOG = LoggerFactory.getLogger(StorageWatcher.class);

    private static final WatchEvent.Kind<Path>[] WATCH_EVENTS_KINDS = new WatchEvent.Kind[] {StandardWatchEventKinds.ENTRY_CREATE};

    private static final Map<WatchKey, Path> KEY_PATH_MAP = new HashMap<>();

    @Resource
    private PPAFacade ppaFacade;

    @Resource
    private WatchService storageWatchService;

    @Resource
    private Environment environment;

    @Override
    public void run(ApplicationArguments args) {
        try {
            registerDir(Paths.get(environment.getProperty(RINEX_FOLDER)), storageWatchService);

            while (true) {
                final WatchKey key = storageWatchService.take();
                for (WatchEvent<?> event : key.pollEvents()) {
                    if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE && event.context() instanceof Path) {
                        final String fullPath = KEY_PATH_MAP.get(key) + "\\" + event.context().toString();
                        final File file = new File(fullPath);

                        if (file.isDirectory()) {
                            registerDir(file.toPath(), storageWatchService);
                        } else {
                            ppaFacade.process(file);
                        }
                    }
                }
                if (!key.reset()) {
                    KEY_PATH_MAP.remove(key);
                }
                if (KEY_PATH_MAP.isEmpty()) {
                    break;
                }
            }
        } catch (InterruptedException e) {
            LOG.error("StorageWatcher has been interrupted. No new files will be detected and processed.");
        }
    }

    private static void registerDir(Path path, WatchService watchService) {

        if (!Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) {
            return;
        }
        try {
            LOG.info("registering: " + path);
            final WatchKey key = path.register(watchService, WATCH_EVENTS_KINDS);
            KEY_PATH_MAP.putIfAbsent(key, path);
            Arrays.stream(path.toFile().listFiles()).forEach(f -> registerDir(f.toPath(), watchService));
        } catch (IOException e) {
            LOG.error(MessageFormat.format("Can not register file watcher for {0}", path), e);
        }
    }
}
Evgeniy
  • 146
  • 3
  • 12