|
| 1 | +/* |
| 2 | + * Copyright 2002-2017 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 | + |
| 17 | +package org.springframework.web.reactive.function.server; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.LinkedHashMap; |
| 23 | +import java.util.Locale; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.stream.Stream; |
| 26 | + |
| 27 | +import reactor.core.publisher.Flux; |
| 28 | +import reactor.core.publisher.Mono; |
| 29 | + |
| 30 | +import org.springframework.core.Conventions; |
| 31 | +import org.springframework.http.HttpHeaders; |
| 32 | +import org.springframework.http.HttpStatus; |
| 33 | +import org.springframework.http.MediaType; |
| 34 | +import org.springframework.http.server.reactive.ServerHttpResponse; |
| 35 | +import org.springframework.util.Assert; |
| 36 | +import org.springframework.web.reactive.result.view.ViewResolver; |
| 37 | +import org.springframework.web.server.ServerWebExchange; |
| 38 | + |
| 39 | +/** |
| 40 | + * Default {@link RenderingResponse.Builder} implementation. |
| 41 | + * |
| 42 | + * @author Arjen Poutsma |
| 43 | + * @since 5.0 |
| 44 | + */ |
| 45 | +class DefaultRenderingResponseBuilder implements RenderingResponse.Builder { |
| 46 | + |
| 47 | + private final String name; |
| 48 | + |
| 49 | + private final HttpHeaders headers = new HttpHeaders(); |
| 50 | + |
| 51 | + private final Map<String, Object> model = new LinkedHashMap<String, Object>(); |
| 52 | + |
| 53 | + private HttpStatus status = HttpStatus.OK; |
| 54 | + |
| 55 | + public DefaultRenderingResponseBuilder(String name) { |
| 56 | + this.name = name; |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + @Override |
| 61 | + public RenderingResponse.Builder modelAttribute(Object attribute) { |
| 62 | + Assert.notNull(attribute, "'value' must not be null"); |
| 63 | + if (attribute instanceof Collection && ((Collection<?>) attribute).isEmpty()) { |
| 64 | + return this; |
| 65 | + } |
| 66 | + return modelAttribute(Conventions.getVariableName(attribute), attribute); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public RenderingResponse.Builder modelAttribute(String name, Object value) { |
| 71 | + Assert.notNull(name, "'name' must not be null"); |
| 72 | + this.model.put(name, value); |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public RenderingResponse.Builder modelAttributes(Object... attributes) { |
| 78 | + if (attributes != null) { |
| 79 | + modelAttributes(Arrays.asList(attributes)); |
| 80 | + } |
| 81 | + return this; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public RenderingResponse.Builder modelAttributes(Collection<?> attributes) { |
| 86 | + if (attributes != null) { |
| 87 | + attributes.forEach(this::modelAttribute); |
| 88 | + } |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public RenderingResponse.Builder modelAttributes(Map<String, ?> attributes) { |
| 94 | + if (attributes != null) { |
| 95 | + this.model.putAll(attributes); |
| 96 | + } |
| 97 | + return this; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public RenderingResponse.Builder header(String headerName, String... headerValues) { |
| 102 | + for (String headerValue : headerValues) { |
| 103 | + this.headers.add(headerName, headerValue); |
| 104 | + } |
| 105 | + return this; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public RenderingResponse.Builder headers(HttpHeaders headers) { |
| 110 | + if (headers != null) { |
| 111 | + this.headers.putAll(headers); |
| 112 | + } |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public RenderingResponse.Builder status(HttpStatus status) { |
| 118 | + Assert.notNull(status, "'status' must not be null"); |
| 119 | + this.status = status; |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + @Override |
| 125 | + public Mono<RenderingResponse> build() { |
| 126 | + return Mono.just(new DefaultRenderingResponse(this.status, this.headers, this.name, |
| 127 | + this.model)); |
| 128 | + } |
| 129 | + |
| 130 | + static class DefaultRenderingResponse extends DefaultServerResponseBuilder.AbstractServerResponse |
| 131 | + implements RenderingResponse { |
| 132 | + |
| 133 | + private final String name; |
| 134 | + |
| 135 | + private final Map<String, Object> model; |
| 136 | + |
| 137 | + public DefaultRenderingResponse(HttpStatus statusCode, HttpHeaders headers, String name, |
| 138 | + Map<String, Object> model) { |
| 139 | + super(statusCode, headers); |
| 140 | + this.name = name; |
| 141 | + this.model = unmodifiableCopy(model); |
| 142 | + } |
| 143 | + |
| 144 | + private static <K, V> Map<K, V> unmodifiableCopy(Map<? extends K, ? extends V> m) { |
| 145 | + return Collections.unmodifiableMap(new LinkedHashMap<>(m)); |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + @Override |
| 150 | + public String name() { |
| 151 | + return this.name; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public Map<String, Object> model() { |
| 156 | + return this.model; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public Mono<Void> writeTo(ServerWebExchange exchange, HandlerStrategies strategies) { |
| 161 | + ServerHttpResponse response = exchange.getResponse(); |
| 162 | + writeStatusAndHeaders(response); |
| 163 | + MediaType contentType = exchange.getResponse().getHeaders().getContentType(); |
| 164 | + Locale locale = resolveLocale(exchange, strategies); |
| 165 | + Stream<ViewResolver> viewResolverStream = strategies.viewResolvers().get(); |
| 166 | + return Flux.fromStream(viewResolverStream) |
| 167 | + .concatMap(viewResolver -> viewResolver.resolveViewName(this.name, locale)) |
| 168 | + .next() |
| 169 | + .otherwiseIfEmpty(Mono.error(new IllegalArgumentException("Could not resolve view with name '" + |
| 170 | + this.name +"'"))) |
| 171 | + .then(view -> view.render(this.model, contentType, exchange)); |
| 172 | + } |
| 173 | + |
| 174 | + private Locale resolveLocale(ServerWebExchange exchange, HandlerStrategies strategies) { |
| 175 | + ServerRequest request = |
| 176 | + exchange.<ServerRequest>getAttribute(RouterFunctions.REQUEST_ATTRIBUTE) |
| 177 | + .orElseThrow(() -> new IllegalStateException( |
| 178 | + "Could not find ServerRequest in exchange attributes")); |
| 179 | + |
| 180 | + return strategies.localeResolver() |
| 181 | + .apply(request) |
| 182 | + .orElse(Locale.getDefault()); |
| 183 | + |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + |
| 188 | +} |
0 commit comments