io.micronaut.http.context.ServerRequestContext Maven / Gradle / Ivy
/*
* Copyright 2017-2020 original authors
*
* 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
*
* https://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.micronaut.http.context;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.propagation.PropagatedContext;
import io.micronaut.http.HttpRequest;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
/**
* The holder for the current {@link HttpRequest} that is bound to instrumented threads.
* Allowing lookup of the current request if it is present.
*
* @author Graeme Rocher
* @since 1.0
*/
public final class ServerRequestContext {
public static final String KEY = "micronaut.http.server.request";
private ServerRequestContext() {
}
/**
* Wrap the execution of the given runnable in request context processing.
*
* @param request The request
* @param runnable The runnable
*/
public static void with(@Nullable HttpRequest> request, @NonNull Runnable runnable) {
try (PropagatedContext.Scope ignore = PropagatedContext.getOrEmpty().plus(new ServerHttpRequestContext(request)).propagate()) {
runnable.run();
}
}
/**
* Return a new runnable by instrumenting the given runnable with request context handling.
*
* @param request The request
* @param runnable The runnable
* @return The newly instrumented runnable
*/
public static Runnable instrument(@Nullable HttpRequest> request, @NonNull Runnable runnable) {
return () -> with(request, runnable);
}
/**
* Wrap the execution of the given callable in request context processing.
*
* @param request The request
* @param supplier The callable
* @param The return type of the callable
* @return The return value of the callable
*/
public static T with(@Nullable HttpRequest> request, @NonNull Supplier supplier) {
try (PropagatedContext.Scope ignore = PropagatedContext.getOrEmpty().plus(new ServerHttpRequestContext(request)).propagate()) {
return supplier.get();
}
}
/**
* Wrap the execution of the given callable in request context processing.
*
* @param request The request
* @param callable The callable
* @param The return type of the callable
* @return The return value of the callable
* @throws Exception If the callable throws an exception
*/
public static T with(@Nullable HttpRequest> request, @NonNull Callable callable) throws Exception {
try (PropagatedContext.Scope ignore = PropagatedContext.getOrEmpty().plus(new ServerHttpRequestContext(request)).propagate()) {
return callable.call();
}
}
/**
* Retrieve the current server request context.
*
* @param The body type
* @return The request context if it is present
*/
public static Optional> currentRequest() {
return ServerHttpRequestContext.find();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy