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

io.datakernel.http.AsyncServlet Maven / Gradle / Ivy

Go to download

High-performance asynchronous HTTP clients and servers collection. Package contains a bunch of different built-in servlets for request dispatching, loading of a static content, etc.

There is a newer version: 3.1.0
Show newest version
/*
 * 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.AsyncFunction;
import io.datakernel.async.Callback;
import io.datakernel.async.SettableStage;
import io.datakernel.async.Stage;

import java.util.function.BiConsumer;
import java.util.function.Function;

import static io.datakernel.async.Callback.stageToCallback;

/**
 * Servlet receives and responds to {@link HttpRequest} from clients across
 * HTTP. Receives {@link HttpRequest}, creates {@link HttpResponse} and sends
 * it.
 */
public interface AsyncServlet {
	default Stage serve(HttpRequest request) {
		SettableStage result = SettableStage.create();
		serve(request, result);
		return result;
	}

	default void serve(HttpRequest request, Callback callback) {
		stageToCallback(serve(request), callback);
	}

	static AsyncServlet of(AsyncFunction function) {
		return new AsyncServlet() {
			@Override
			public Stage serve(HttpRequest request) {
				return function.apply(request);
			}
		};
	}

	static AsyncServlet of(BiConsumer> consumer) {
		return new AsyncServlet() {
			@Override
			public void serve(HttpRequest request, Callback callback) {
				consumer.accept(request, callback);
			}
		};
	}

	static AsyncServlet ofBlocking(Function function) {
		return new AsyncServlet() {
			@Override
			public Stage serve(HttpRequest request) {
				return Stage.of(function.apply(request));
			}
		};
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy