|
| 1 | +/* |
| 2 | + * Copyright 2002-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.web.server; |
| 17 | + |
| 18 | +import java.security.Principal; |
| 19 | +import java.util.Optional; |
| 20 | + |
| 21 | +import reactor.core.publisher.Mono; |
| 22 | + |
| 23 | +import org.springframework.http.server.reactive.ServerHttpRequest; |
| 24 | +import org.springframework.http.server.reactive.ServerHttpResponse; |
| 25 | +import org.springframework.util.Assert; |
| 26 | + |
| 27 | +/** |
| 28 | + * Default implementation of |
| 29 | + * {@link org.springframework.web.server.ServerWebExchange.MutativeBuilder}. |
| 30 | + * |
| 31 | + * @author Rossen Stoyanchev |
| 32 | + * @since 5.0 |
| 33 | + */ |
| 34 | +class DefaultServerWebExchangeMutativeBuilder implements ServerWebExchange.MutativeBuilder { |
| 35 | + |
| 36 | + private final ServerWebExchange delegate; |
| 37 | + |
| 38 | + private ServerHttpRequest request; |
| 39 | + |
| 40 | + private ServerHttpResponse response; |
| 41 | + |
| 42 | + private Principal user; |
| 43 | + |
| 44 | + private Mono<WebSession> session; |
| 45 | + |
| 46 | + |
| 47 | + public DefaultServerWebExchangeMutativeBuilder(ServerWebExchange delegate) { |
| 48 | + Assert.notNull(delegate, "'delegate' is required."); |
| 49 | + this.delegate = delegate; |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + @Override |
| 54 | + public ServerWebExchange.MutativeBuilder setRequest(ServerHttpRequest request) { |
| 55 | + this.request = request; |
| 56 | + return this; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public ServerWebExchange.MutativeBuilder setResponse(ServerHttpResponse response) { |
| 61 | + this.response = response; |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public ServerWebExchange.MutativeBuilder setPrincipal(Principal user) { |
| 67 | + this.user = user; |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public ServerWebExchange.MutativeBuilder setSession(Mono<WebSession> session) { |
| 73 | + this.session = session; |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public ServerWebExchange build() { |
| 79 | + return new MutativeDecorator(this.delegate, |
| 80 | + this.request, this.response, this.user, this.session); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + /** |
| 85 | + * An immutable wrapper of an exchange returning property overrides -- given |
| 86 | + * to the constructor -- or original values otherwise. |
| 87 | + */ |
| 88 | + private static class MutativeDecorator extends ServerWebExchangeDecorator { |
| 89 | + |
| 90 | + private final ServerHttpRequest request; |
| 91 | + |
| 92 | + private final ServerHttpResponse response; |
| 93 | + |
| 94 | + private final Principal user; |
| 95 | + |
| 96 | + private final Mono<WebSession> session; |
| 97 | + |
| 98 | + |
| 99 | + public MutativeDecorator(ServerWebExchange delegate, |
| 100 | + ServerHttpRequest request, ServerHttpResponse response, Principal user, |
| 101 | + Mono<WebSession> session) { |
| 102 | + |
| 103 | + super(delegate); |
| 104 | + this.request = request; |
| 105 | + this.response = response; |
| 106 | + this.user = user; |
| 107 | + this.session = session; |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + @Override |
| 112 | + public ServerHttpRequest getRequest() { |
| 113 | + return (this.request != null ? this.request : getDelegate().getRequest()); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public ServerHttpResponse getResponse() { |
| 118 | + return (this.response != null ? this.response : getDelegate().getResponse()); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public Mono<WebSession> getSession() { |
| 123 | + return (this.session != null ? this.session : getDelegate().getSession()); |
| 124 | + } |
| 125 | + |
| 126 | + @SuppressWarnings("unchecked") |
| 127 | + @Override |
| 128 | + public <T extends Principal> Optional<T> getPrincipal() { |
| 129 | + return (this.user != null ? Optional.of((T) this.user) : getDelegate().getPrincipal()); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | +} |
| 134 | + |
0 commit comments