|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 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 | + * https://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.result.view; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.function.Consumer; |
| 24 | + |
| 25 | +import org.reactivestreams.Publisher; |
| 26 | +import reactor.core.publisher.Flux; |
| 27 | + |
| 28 | +import org.springframework.http.HttpHeaders; |
| 29 | +import org.springframework.http.HttpStatusCode; |
| 30 | +import org.springframework.lang.Nullable; |
| 31 | + |
| 32 | +/** |
| 33 | + * Default implementation of {@link FragmentsRendering.Builder}. |
| 34 | + * |
| 35 | + * @author Rossen Stoyanchev |
| 36 | + * @since 6.2 |
| 37 | + */ |
| 38 | +class DefaultFragmentsRenderingBuilder implements FragmentsRendering.Builder { |
| 39 | + |
| 40 | + @Nullable |
| 41 | + private Collection<Fragment> fragmentsCollection; |
| 42 | + |
| 43 | + @Nullable |
| 44 | + private final Flux<Fragment> fragmentsFlux; |
| 45 | + |
| 46 | + @Nullable |
| 47 | + private HttpStatusCode status; |
| 48 | + |
| 49 | + @Nullable |
| 50 | + private HttpHeaders headers; |
| 51 | + |
| 52 | + DefaultFragmentsRenderingBuilder(Collection<Fragment> fragments) { |
| 53 | + this.fragmentsCollection = new ArrayList<>(fragments); |
| 54 | + this.fragmentsFlux = null; |
| 55 | + } |
| 56 | + |
| 57 | + DefaultFragmentsRenderingBuilder(Publisher<Fragment> fragments) { |
| 58 | + this.fragmentsFlux = Flux.from(fragments); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + @Override |
| 63 | + public FragmentsRendering.Builder status(HttpStatusCode status) { |
| 64 | + this.status = status; |
| 65 | + return this; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public FragmentsRendering.Builder header(String headerName, String... headerValues) { |
| 70 | + initHeaders().put(headerName, Arrays.asList(headerValues)); |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public FragmentsRendering.Builder headers(Consumer<HttpHeaders> headersConsumer) { |
| 76 | + headersConsumer.accept(initHeaders()); |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + private HttpHeaders initHeaders() { |
| 81 | + if (this.headers == null) { |
| 82 | + this.headers = new HttpHeaders(); |
| 83 | + } |
| 84 | + return this.headers; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public FragmentsRendering.Builder fragment(String viewName, Map<String, Object> model) { |
| 89 | + return fragment(Fragment.create(viewName, model)); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public FragmentsRendering.Builder fragment(String viewName) { |
| 94 | + return fragment(Fragment.create(viewName)); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public FragmentsRendering.Builder fragment(Fragment fragment) { |
| 99 | + initFragmentsCollection().add(fragment); |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
| 103 | + private Collection<Fragment> initFragmentsCollection() { |
| 104 | + if (this.fragmentsCollection == null) { |
| 105 | + this.fragmentsCollection = new ArrayList<>(); |
| 106 | + } |
| 107 | + return this.fragmentsCollection; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public FragmentsRendering build() { |
| 112 | + return new DefaultFragmentsRendering( |
| 113 | + this.status, (this.headers != null ? this.headers : HttpHeaders.EMPTY), getFragmentsFlux()); |
| 114 | + } |
| 115 | + |
| 116 | + private Flux<Fragment> getFragmentsFlux() { |
| 117 | + if (this.fragmentsFlux != null && this.fragmentsCollection != null) { |
| 118 | + return this.fragmentsFlux.concatWith(Flux.fromIterable(this.fragmentsCollection)); |
| 119 | + } |
| 120 | + else if (this.fragmentsFlux != null) { |
| 121 | + return this.fragmentsFlux; |
| 122 | + } |
| 123 | + else if (this.fragmentsCollection != null) { |
| 124 | + return Flux.fromIterable(this.fragmentsCollection); |
| 125 | + } |
| 126 | + else { |
| 127 | + return Flux.empty(); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | + /** |
| 133 | + * Default implementation of {@link FragmentsRendering}. |
| 134 | + */ |
| 135 | + private record DefaultFragmentsRendering(@Nullable HttpStatusCode status, HttpHeaders headers, Flux<Fragment> fragments) |
| 136 | + implements FragmentsRendering { |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments