001/*
002 * Copyright 2012-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 *      http://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 */
016package org.springframework.boot.autoconfigure.web.servlet;
017
018import org.springframework.boot.web.servlet.ServletRegistrationBean;
019
020/**
021 * Interface that can be used by auto-configurations that need path details Jersey's
022 * application path that serves as the base URI for the application.
023 *
024 * @author Madhura Bhave
025 * @since 2.0.7
026 */
027@FunctionalInterface
028public interface JerseyApplicationPath {
029
030        /**
031         * Returns the configured path of the application.
032         * @return the configured path
033         */
034        String getPath();
035
036        /**
037         * Return a form of the given path that's relative to the Jersey application path.
038         * @param path the path to make relative
039         * @return the relative path
040         */
041        default String getRelativePath(String path) {
042                String prefix = getPrefix();
043                if (!path.startsWith("/")) {
044                        path = "/" + path;
045                }
046                return prefix + path;
047        }
048
049        /**
050         * Return a cleaned up version of the path that can be used as a prefix for URLs. The
051         * resulting path will have path will not have a trailing slash.
052         * @return the prefix
053         * @see #getRelativePath(String)
054         */
055        default String getPrefix() {
056                String result = getPath();
057                int index = result.indexOf('*');
058                if (index != -1) {
059                        result = result.substring(0, index);
060                }
061                if (result.endsWith("/")) {
062                        result = result.substring(0, result.length() - 1);
063                }
064                return result;
065        }
066
067        /**
068         * Return a URL mapping pattern that can be used with a
069         * {@link ServletRegistrationBean} to map Jersey's servlet.
070         * @return the path as a servlet URL mapping
071         */
072        default String getUrlMapping() {
073                String path = getPath();
074                if (!path.startsWith("/")) {
075                        path = "/" + path;
076                }
077                if (path.equals("/")) {
078                        return "/*";
079                }
080                if (path.contains("*")) {
081                        return path;
082                }
083                if (path.endsWith("/")) {
084                        return path + "*";
085                }
086                return path + "/*";
087        }
088
089}