001/*
002 * Copyright 2002-2018 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 */
016
017package org.springframework.web.reactive.resource;
018
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021
022import org.springframework.util.Assert;
023
024/**
025 * Abstract base class for {@link VersionStrategy} implementations that insert
026 * a prefix into the URL path, e.g. "version/static/myresource.js".
027 *
028 * @author Rossen Stoyanchev
029 * @author Brian Clozel
030 * @since 5.0
031 */
032public abstract class AbstractPrefixVersionStrategy implements VersionStrategy {
033
034        protected final Log logger = LogFactory.getLog(getClass());
035
036
037        private final String prefix;
038
039
040        protected AbstractPrefixVersionStrategy(String version) {
041                Assert.hasText(version, "Version must not be empty");
042                this.prefix = version;
043        }
044
045
046        @Override
047        public String extractVersion(String requestPath) {
048                return (requestPath.startsWith(this.prefix) ? this.prefix : null);
049        }
050
051        @Override
052        public String removeVersion(String requestPath, String version) {
053                return requestPath.substring(this.prefix.length());
054        }
055
056        @Override
057        public String addVersion(String path, String version) {
058                if (path.startsWith(".")) {
059                        return path;
060                }
061                else if (this.prefix.endsWith("/") || path.startsWith("/")) {
062                        return this.prefix + path;
063                }
064                else {
065                        return this.prefix + '/' + path;
066                }
067        }
068
069}