001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.request;
018
019import org.apache.wicket.util.string.Strings;
020
021/**
022 * Various url utilities
023 *
024 * @author igor.vaynberg
025 */
026public class UrlUtils
027{
028        /**
029         * Constructor
030         */
031        // TODO make it private in Wicket 7.0
032        protected UrlUtils()
033        {
034
035        }
036
037        /**
038         * Checks if the url is relative or absolute
039         *
040         * @param url
041         * @return <code>true</code> if url is relative, <code>false</code> otherwise
042         */
043        public static boolean isRelative(final String url)
044        {
045                // the regex means "doesn't start with 'scheme://'"
046                if ((url != null) && (url.startsWith("/") == false) && (!url.matches("^\\w+\\:\\/\\/.*")) &&
047                        !(url.startsWith("#")))
048                {
049                        return true;
050                }
051                else
052                {
053                        return false;
054                }
055        }
056
057        /**
058         * Rewrites a relative url to be context relative, leaves absolute urls same.
059         *
060         * @param url
061         * @param requestCycle
062         * @return rewritten url
063         */
064        public static String rewriteToContextRelative(String url, IRequestCycle requestCycle)
065        {
066                if (isRelative(url))
067                {
068                        return requestCycle.getUrlRenderer().renderContextRelativeUrl(url);
069                }
070                else
071                {
072                        return url;
073                }
074        }
075
076        /**
077         * Makes sure the path starts with a slash and does not end with a slash. Empty or null paths
078         * are normalized into an empty string.
079         *
080         * @param path
081         *            path to normalize
082         * @return normalized path
083         */
084        public static String normalizePath(String path)
085        {
086                if (Strings.isEmpty(path))
087                {
088                        return "";
089                }
090                path = path.trim();
091                if (!path.startsWith("/"))
092                {
093                        path = "/" + path;
094                }
095                if (path.endsWith("/"))
096                {
097                        path = path.substring(0, path.length() - 1);
098                }
099                return path;
100        }
101}