001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.springframework.web.server;
017
018import java.security.Principal;
019import java.time.Instant;
020import java.util.Map;
021import java.util.function.Function;
022
023import reactor.core.publisher.Mono;
024
025import org.springframework.context.ApplicationContext;
026import org.springframework.context.i18n.LocaleContext;
027import org.springframework.http.codec.multipart.Part;
028import org.springframework.http.server.reactive.ServerHttpRequest;
029import org.springframework.http.server.reactive.ServerHttpResponse;
030import org.springframework.lang.Nullable;
031import org.springframework.util.Assert;
032import org.springframework.util.MultiValueMap;
033
034/**
035 * A convenient base class for classes that need to wrap another
036 * {@link ServerWebExchange}. Pre-implements all methods by delegating to the
037 * wrapped instance.
038 *
039 * <p><strong>Note:</strong> if the purpose for using a decorator is to override
040 * properties like {@link #getPrincipal()}, consider using
041 * {@link ServerWebExchange#mutate()} instead.
042 *
043 * @author Rossen Stoyanchev
044 * @since 5.0
045 *
046 * @see ServerWebExchange#mutate()
047 */
048public class ServerWebExchangeDecorator implements ServerWebExchange {
049
050        private final ServerWebExchange delegate;
051
052
053        protected ServerWebExchangeDecorator(ServerWebExchange delegate) {
054                Assert.notNull(delegate, "ServerWebExchange 'delegate' is required.");
055                this.delegate = delegate;
056        }
057
058
059        public ServerWebExchange getDelegate() {
060                return this.delegate;
061        }
062
063        // ServerWebExchange delegation methods...
064
065        @Override
066        public ServerHttpRequest getRequest() {
067                return getDelegate().getRequest();
068        }
069
070        @Override
071        public ServerHttpResponse getResponse() {
072                return getDelegate().getResponse();
073        }
074
075        @Override
076        public Map<String, Object> getAttributes() {
077                return getDelegate().getAttributes();
078        }
079
080        @Override
081        public Mono<WebSession> getSession() {
082                return getDelegate().getSession();
083        }
084
085        @Override
086        public <T extends Principal> Mono<T> getPrincipal() {
087                return getDelegate().getPrincipal();
088        }
089
090        @Override
091        public LocaleContext getLocaleContext() {
092                return getDelegate().getLocaleContext();
093        }
094
095        @Override
096        public ApplicationContext getApplicationContext() {
097                return getDelegate().getApplicationContext();
098        }
099
100        @Override
101        public Mono<MultiValueMap<String, String>> getFormData() {
102                return getDelegate().getFormData();
103        }
104
105        @Override
106        public Mono<MultiValueMap<String, Part>> getMultipartData() {
107                return getDelegate().getMultipartData();
108        }
109
110        @Override
111        public boolean isNotModified() {
112                return getDelegate().isNotModified();
113        }
114
115        @Override
116        public boolean checkNotModified(Instant lastModified) {
117                return getDelegate().checkNotModified(lastModified);
118        }
119
120        @Override
121        public boolean checkNotModified(String etag) {
122                return getDelegate().checkNotModified(etag);
123        }
124
125        @Override
126        public boolean checkNotModified(@Nullable String etag, Instant lastModified) {
127                return getDelegate().checkNotModified(etag, lastModified);
128        }
129
130        @Override
131        public String transformUrl(String url) {
132                return getDelegate().transformUrl(url);
133        }
134
135        @Override
136        public void addUrlTransformer(Function<String, String> transformer) {
137                getDelegate().addUrlTransformer(transformer);
138        }
139
140        @Override
141        public String getLogPrefix() {
142                return getDelegate().getLogPrefix();
143        }
144
145        @Override
146        public String toString() {
147                return getClass().getSimpleName() + " [delegate=" + getDelegate() + "]";
148        }
149
150}