
io.datakernel.http.AsyncServlet Maven / Gradle / Ivy
/*
* Copyright (C) 2015 SoftIndex LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.datakernel.http;
import io.datakernel.async.Promise;
import io.datakernel.async.Promises;
import io.datakernel.exception.UncheckedException;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.Executor;
import java.util.function.Function;
import java.util.stream.Stream;
/**
* An interface for a servlet function that asyncronously receives a {@link HttpRequest},
* processes it and then returns a {@link HttpResponse}.
*/
@FunctionalInterface
public interface AsyncServlet {
@NotNull
Promise serve(@NotNull HttpRequest request) throws UncheckedException;
default AsyncServlet then(AsyncServletDecorator decorator) {
return decorator.serve(this);
}
@NotNull
static AsyncServlet of(@NotNull Function fn) {
return request -> Promise.of(fn.apply(request));
}
/**
* Wraps given {@link BlockingServlet} into async one using given {@link Executor}.
*/
@NotNull
static AsyncServlet ofBlocking(@NotNull Executor executor, @NotNull BlockingServlet blockingServlet) {
return request -> request.loadBody()
.then($ -> Promise.ofBlockingCallable(executor,
() -> blockingServlet.serve(request)));
}
Promise NEXT = Promise.of(null);
static AsyncServlet firstSuccessful(AsyncServlet... servlets) {
return httpRequest -> Promises.first(
(httpResponse, e) -> e == null && httpResponse != null,
Stream.of(servlets).map(servlet -> () -> servlet.serve(httpRequest)));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy