|
| 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.accept; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.TreeSet; |
| 23 | + |
| 24 | +import jakarta.servlet.http.HttpServletRequest; |
| 25 | +import org.jspecify.annotations.Nullable; |
| 26 | + |
| 27 | +import org.springframework.util.Assert; |
| 28 | + |
| 29 | +/** |
| 30 | + * Default implementation of {@link ApiVersionStrategy} that delegates to the |
| 31 | + * configured version resolvers and version parser. |
| 32 | + * |
| 33 | + * @author Rossen Stoyanchev |
| 34 | + * @since 7.0 |
| 35 | + */ |
| 36 | +public class DefaultApiVersionStrategy implements ApiVersionStrategy { |
| 37 | + |
| 38 | + private final List<ApiVersionResolver> versionResolvers; |
| 39 | + |
| 40 | + private final ApiVersionParser<?> versionParser; |
| 41 | + |
| 42 | + private final boolean versionRequired; |
| 43 | + |
| 44 | + private final @Nullable Comparable<?> defaultVersion; |
| 45 | + |
| 46 | + private final Set<Comparable<?>> supportedVersions = new TreeSet<>(); |
| 47 | + |
| 48 | + |
| 49 | + /** |
| 50 | + * Create an instance. |
| 51 | + * @param versionResolvers one or more resolvers to try; the first non-null |
| 52 | + * value returned by any resolver becomes the resolved used |
| 53 | + * @param versionParser parser for to raw version values |
| 54 | + * @param versionRequired whether a version is required; if a request |
| 55 | + * does not have a version, and a {@code defaultVersion} is not specified, |
| 56 | + * validation fails with {@link MissingApiVersionException} |
| 57 | + * @param defaultVersion a default version to assign to requests that |
| 58 | + * don't specify one |
| 59 | + */ |
| 60 | + public DefaultApiVersionStrategy( |
| 61 | + List<ApiVersionResolver> versionResolvers, ApiVersionParser<?> versionParser, |
| 62 | + boolean versionRequired, @Nullable String defaultVersion) { |
| 63 | + |
| 64 | + Assert.notEmpty(versionResolvers, "At least one ApiVersionResolver is required"); |
| 65 | + Assert.notNull(versionParser, "ApiVersionParser is required"); |
| 66 | + |
| 67 | + this.versionResolvers = new ArrayList<>(versionResolvers); |
| 68 | + this.versionParser = versionParser; |
| 69 | + this.versionRequired = (versionRequired && defaultVersion == null); |
| 70 | + this.defaultVersion = (defaultVersion != null ? versionParser.parseVersion(defaultVersion) : null); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + @Override |
| 75 | + public @Nullable Comparable<?> getDefaultVersion() { |
| 76 | + return this.defaultVersion; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Add to the list of known, supported versions to check against in |
| 81 | + * {@link ApiVersionStrategy#validateVersion}. Request versions that are not |
| 82 | + * in the supported result in {@link InvalidApiVersionException} |
| 83 | + * in {@link ApiVersionStrategy#validateVersion}. |
| 84 | + * @param versions the versions to add |
| 85 | + */ |
| 86 | + public void addSupportedVersion(String... versions) { |
| 87 | + for (String version : versions) { |
| 88 | + this.supportedVersions.add(parseVersion(version)); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public @Nullable String resolveVersion(HttpServletRequest request) { |
| 94 | + for (ApiVersionResolver resolver : this.versionResolvers) { |
| 95 | + String version = resolver.resolveVersion(request); |
| 96 | + if (version != null) { |
| 97 | + return version; |
| 98 | + } |
| 99 | + } |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public Comparable<?> parseVersion(String version) { |
| 105 | + return this.versionParser.parseVersion(version); |
| 106 | + } |
| 107 | + |
| 108 | + public void validateVersion(@Nullable Comparable<?> requestVersion, HttpServletRequest request) |
| 109 | + throws MissingApiVersionException, InvalidApiVersionException { |
| 110 | + |
| 111 | + if (requestVersion == null) { |
| 112 | + if (this.versionRequired) { |
| 113 | + throw new MissingApiVersionException(); |
| 114 | + } |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + if (!this.supportedVersions.contains(requestVersion)) { |
| 119 | + throw new InvalidApiVersionException(requestVersion.toString()); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public String toString() { |
| 125 | + return "DefaultApiVersionStrategy[supportedVersions=" + this.supportedVersions + |
| 126 | + ", versionRequired=" + this.versionRequired + ", defaultVersion=" + this.defaultVersion + "]"; |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments