All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.datakernel.loader.SimpleStaticLoaderAsync Maven / Gradle / Ivy

package io.datakernel.loader;

import io.datakernel.async.Stage;
import io.datakernel.bytebuf.ByteBuf;
import io.datakernel.eventloop.Eventloop;
import io.datakernel.file.AsyncFile;
import io.datakernel.http.HttpException;

import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.concurrent.ExecutorService;

class SimpleStaticLoaderAsync implements StaticLoader {
	private final ExecutorService executorService;
	private final Path root;

	public SimpleStaticLoaderAsync(Eventloop eventloop, ExecutorService executorService, Path root) {
		this.executorService = executorService;
		this.root = root;
	}

	@Override
	public Stage getResource(String name) {
		Path file = root.resolve(name).normalize();

		if (!file.startsWith(root)) {
			return Stage.ofException(HttpException.notFound404());
		}

		return AsyncFile.readFile(executorService, file)
				.thenComposeEx((result, throwable) -> {
					if (throwable instanceof NoSuchFileException) {
						return Stage.ofException(HttpException.notFound404());
					}
					return Stage.of(result, throwable);
				});
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy