DefaultServerWebExchange.java 2.4 KB
Newer Older
1
/*
2
 * Copyright 2002-2016 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * 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.
 */
16

17
package org.springframework.web.server.adapter;
18

R
Rossen Stoyanchev 已提交
19
import java.util.Map;
20
import java.util.Optional;
R
Rossen Stoyanchev 已提交
21
import java.util.concurrent.ConcurrentHashMap;
22

R
Rossen Stoyanchev 已提交
23 24
import reactor.core.publisher.Mono;

25 26
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
27
import org.springframework.util.Assert;
28
import org.springframework.web.server.ServerWebExchange;
29
import org.springframework.web.server.WebSession;
R
Rossen Stoyanchev 已提交
30
import org.springframework.web.server.session.WebSessionManager;
31

32
/**
33
 * Default implementation of {@link ServerWebExchange}.
34 35
 *
 * @author Rossen Stoyanchev
36
 * @since 5.0
37
 */
38
public class DefaultServerWebExchange implements ServerWebExchange {
39

R
Rossen Stoyanchev 已提交
40
	private final ServerHttpRequest request;
41

R
Rossen Stoyanchev 已提交
42
	private final ServerHttpResponse response;
43

R
Rossen Stoyanchev 已提交
44
	private final Map<String, Object> attributes = new ConcurrentHashMap<>();
45

46
	private final Mono<WebSession> sessionMono;
R
Rossen Stoyanchev 已提交
47 48


49
	public DefaultServerWebExchange(ServerHttpRequest request, ServerHttpResponse response,
R
Rossen Stoyanchev 已提交
50
			WebSessionManager sessionManager) {
51

52 53 54
		Assert.notNull(request, "'request' is required");
		Assert.notNull(response, "'response' is required");
		Assert.notNull(response, "'sessionManager' is required");
R
Rossen Stoyanchev 已提交
55 56
		this.request = request;
		this.response = response;
57
		this.sessionMono = sessionManager.getSession(this).cache();
58 59
	}

60 61

	@Override
R
Rossen Stoyanchev 已提交
62 63 64 65 66 67 68
	public ServerHttpRequest getRequest() {
		return this.request;
	}

	@Override
	public ServerHttpResponse getResponse() {
		return this.response;
69
	}
70 71

	@Override
R
Rossen Stoyanchev 已提交
72 73
	public Map<String, Object> getAttributes() {
		return this.attributes;
74 75
	}

76 77 78 79 80
	@Override @SuppressWarnings("unchecked")
	public <T> Optional<T> getAttribute(String name) {
		return Optional.ofNullable((T) this.attributes.get(name));
	}

R
Rossen Stoyanchev 已提交
81 82 83 84 85
	@Override
	public Mono<WebSession> getSession() {
		return this.sessionMono;
	}

86
}