|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 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.client; |
| 18 | + |
| 19 | +import java.net.URI; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.jspecify.annotations.Nullable; |
| 24 | + |
| 25 | +import org.springframework.http.HttpHeaders; |
| 26 | +import org.springframework.util.Assert; |
| 27 | +import org.springframework.web.util.UriComponentsBuilder; |
| 28 | + |
| 29 | +/** |
| 30 | + * Default implementation of {@link ApiVersionInserter} to insert the version |
| 31 | + * into a request header, query parameter, or the URL path. |
| 32 | + * |
| 33 | + * <p>Use {@link #builder()} to create an instance. |
| 34 | + * |
| 35 | + * @author Rossen Stoyanchev |
| 36 | + * @since 7.0 |
| 37 | + */ |
| 38 | +public final class DefaultApiVersionInserter implements ApiVersionInserter { |
| 39 | + |
| 40 | + private final @Nullable String header; |
| 41 | + |
| 42 | + private final @Nullable String queryParam; |
| 43 | + |
| 44 | + private final @Nullable Integer pathSegmentIndex; |
| 45 | + |
| 46 | + private final ApiVersionFormatter versionFormatter; |
| 47 | + |
| 48 | + |
| 49 | + private DefaultApiVersionInserter( |
| 50 | + @Nullable String header, @Nullable String queryParam, @Nullable Integer pathSegmentIndex, |
| 51 | + @Nullable ApiVersionFormatter formatter) { |
| 52 | + |
| 53 | + Assert.isTrue(header != null || queryParam != null || pathSegmentIndex != null, |
| 54 | + "Expected 'header', 'queryParam', or 'pathSegmentIndex' to be configured"); |
| 55 | + |
| 56 | + this.header = header; |
| 57 | + this.queryParam = queryParam; |
| 58 | + this.pathSegmentIndex = pathSegmentIndex; |
| 59 | + this.versionFormatter = (formatter != null ? formatter : Object::toString); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + @Override |
| 64 | + public URI insertVersion(Object version, URI uri) { |
| 65 | + if (this.queryParam == null && this.pathSegmentIndex == null) { |
| 66 | + return uri; |
| 67 | + } |
| 68 | + String formattedVersion = this.versionFormatter.formatVersion(version); |
| 69 | + UriComponentsBuilder builder = UriComponentsBuilder.fromUri(uri); |
| 70 | + if (this.queryParam != null) { |
| 71 | + builder.queryParam(this.queryParam, formattedVersion); |
| 72 | + } |
| 73 | + if (this.pathSegmentIndex != null) { |
| 74 | + List<String> pathSegments = new ArrayList<>(builder.build().getPathSegments()); |
| 75 | + assertPathSegmentIndex(this.pathSegmentIndex, pathSegments.size(), uri); |
| 76 | + pathSegments.add(this.pathSegmentIndex, formattedVersion); |
| 77 | + builder.replacePath(null); |
| 78 | + pathSegments.forEach(builder::pathSegment); |
| 79 | + } |
| 80 | + return builder.build().toUri(); |
| 81 | + } |
| 82 | + |
| 83 | + private void assertPathSegmentIndex(Integer index, int pathSegmentsSize, URI uri) { |
| 84 | + Assert.state(index <= pathSegmentsSize, |
| 85 | + "Cannot insert version into '" + uri.getPath() + "' at path segment index " + index); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void insertVersion(Object version, HttpHeaders headers) { |
| 90 | + if (this.header != null) { |
| 91 | + headers.set(this.header, this.versionFormatter.formatVersion(version)); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + /** |
| 97 | + * Create a builder for an inserter that sets a header. |
| 98 | + * @param header the name of a header to hold the version |
| 99 | + */ |
| 100 | + public static Builder fromHeader(@Nullable String header) { |
| 101 | + return new Builder(header, null, null); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Create a builder for an inserter that sets a query parameter. |
| 106 | + * @param queryParam the name of a query parameter to hold the version |
| 107 | + */ |
| 108 | + public static Builder fromQueryParam(@Nullable String queryParam) { |
| 109 | + return new Builder(null, queryParam, null); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Create a builder for an inserter that inserts a path segment. |
| 114 | + * @param pathSegmentIndex the index of the path segment to hold the version |
| 115 | + */ |
| 116 | + public static Builder fromPathSegment(@Nullable Integer pathSegmentIndex) { |
| 117 | + return new Builder(null, null, pathSegmentIndex); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Create a builder. |
| 122 | + */ |
| 123 | + public static Builder builder() { |
| 124 | + return new Builder(null, null, null); |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + /** |
| 129 | + * A builder for {@link DefaultApiVersionInserter}. |
| 130 | + */ |
| 131 | + public static final class Builder { |
| 132 | + |
| 133 | + private @Nullable String header; |
| 134 | + |
| 135 | + private @Nullable String queryParam; |
| 136 | + |
| 137 | + private @Nullable Integer pathSegmentIndex; |
| 138 | + |
| 139 | + private @Nullable ApiVersionFormatter versionFormatter; |
| 140 | + |
| 141 | + private Builder(@Nullable String header, @Nullable String queryParam, @Nullable Integer pathSegmentIndex) { |
| 142 | + this.header = header; |
| 143 | + this.queryParam = queryParam; |
| 144 | + this.pathSegmentIndex = pathSegmentIndex; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Configure the inserter to set a header. |
| 149 | + * @param header the name of the header to hold the version |
| 150 | + */ |
| 151 | + public Builder fromHeader(@Nullable String header) { |
| 152 | + this.header = header; |
| 153 | + return this; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Configure the inserter to set a query parameter. |
| 158 | + * @param queryParam the name of the query parameter to hold the version |
| 159 | + */ |
| 160 | + public Builder fromQueryParam(@Nullable String queryParam) { |
| 161 | + this.queryParam = queryParam; |
| 162 | + return this; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Configure the inserter to insert a path segment. |
| 167 | + * @param pathSegmentIndex the index of the path segment to hold the version |
| 168 | + */ |
| 169 | + public Builder fromPathSegment(@Nullable Integer pathSegmentIndex) { |
| 170 | + this.pathSegmentIndex = pathSegmentIndex; |
| 171 | + return this; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Format the version Object into a String using the given {@link ApiVersionFormatter}. |
| 176 | + * <p>By default, the version is formatted with {@link Object#toString()}. |
| 177 | + * @param versionFormatter the formatter to use |
| 178 | + */ |
| 179 | + public Builder withVersionFormatter(ApiVersionFormatter versionFormatter) { |
| 180 | + this.versionFormatter = versionFormatter; |
| 181 | + return this; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Build the inserter. |
| 186 | + */ |
| 187 | + public ApiVersionInserter build() { |
| 188 | + return new DefaultApiVersionInserter( |
| 189 | + this.header, this.queryParam, this.pathSegmentIndex, this.versionFormatter); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | +} |
0 commit comments