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

net.guerlab.cloud.web.webflux.request.WebfluxRequestContextHolder Maven / Gradle / Ivy

There is a newer version: 2024.11.1
Show newest version
/*
 * Copyright 2018-2025 guerlab.net and other contributors.
 *
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3 (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.gnu.org/licenses/lgpl-3.0.html
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.guerlab.cloud.web.webflux.request;

import jakarta.annotation.Nullable;
import lombok.extern.slf4j.Slf4j;

import org.springframework.http.server.reactive.ServerHttpRequest;

import net.guerlab.cloud.context.core.ContextAttributes;
import net.guerlab.cloud.context.core.ContextAttributesHolder;
import net.guerlab.cloud.web.core.request.RequestContextHolder;
import net.guerlab.cloud.web.webflux.utils.RequestUtils;

/**
 * webflux请求上下文保持.
 *
 * @author guer
 */
@Slf4j
public class WebfluxRequestContextHolder implements RequestContextHolder {

	/**
	 * 获取ServerHttpRequest.
	 *
	 * @return ServerHttpRequest
	 */
	public static ServerHttpRequest getRequest() {
		ContextAttributes contextAttributes = ContextAttributesHolder.get();
		ServerHttpRequest request = (ServerHttpRequest) contextAttributes.get(ContextAttributes.REQUEST_KEY);

		if (request == null) {
			throw new NullPointerException("ServerHttpRequest is null");
		}

		return request;
	}

	@Nullable
	@Override
	public String getRequestMethod() {
		return getRequest().getMethod().name();
	}

	@Nullable
	@Override
	public String getRequestPath() {
		return RequestUtils.parseRequestUri(getRequest());
	}

	@Nullable
	@Override
	public Integer getResponseStatusCode() {
		return null;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy