001    package org.apache.archiva.webdav.util;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *  http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import org.apache.commons.lang.ArrayUtils;
023    import org.apache.commons.lang.StringUtils;
024    
025    /**
026     */
027    public class RepositoryPathUtil
028    {
029        public static String getLogicalResource( final String href )
030        {
031            String logicalResource = null;
032            String requestPathInfo = StringUtils.defaultString( href );
033    
034            //remove prefix ie /repository/blah becomes /blah
035            requestPathInfo = removePrefix( requestPathInfo );
036    
037            // Remove prefixing slash as the repository id doesn't contain it;
038            if ( requestPathInfo.startsWith( "/" ) )
039            {
040                requestPathInfo = requestPathInfo.substring( 1 );
041            }
042    
043            int slash = requestPathInfo.indexOf( '/' );
044            if ( slash > 0 )
045            {
046                logicalResource = requestPathInfo.substring( slash );
047    
048                if ( logicalResource.endsWith( "/.." ) )
049                {
050                    logicalResource += "/";
051                }
052    
053                if ( logicalResource != null && logicalResource.startsWith( "//" ) )
054                {
055                    logicalResource = logicalResource.substring( 1 );
056                }
057    
058                if ( logicalResource == null )
059                {
060                    logicalResource = "/";
061                }
062            }
063            else
064            {
065                logicalResource = "/";
066            }
067            return logicalResource;
068        }
069    
070        public static String getRepositoryName( final String href )
071        {
072            String requestPathInfo = StringUtils.defaultString( href );
073    
074            //remove prefix ie /repository/blah becomes /blah
075            requestPathInfo = removePrefix( requestPathInfo );
076    
077            // Remove prefixing slash as the repository id doesn't contain it;
078            if ( requestPathInfo.startsWith( "/" ) )
079            {
080                requestPathInfo = requestPathInfo.substring( 1 );
081            }
082    
083            // Find first element, if slash exists.
084            int slash = requestPathInfo.indexOf( '/' );
085            if ( slash > 0 )
086            {
087                // Filtered: "central/org/apache/maven/" -> "central"
088                return requestPathInfo.substring( 0, slash );
089            }
090            return requestPathInfo;
091        }
092    
093        private static String removePrefix( final String href )
094        {
095            String[] parts = StringUtils.split( href, '/' );
096            parts = (String[]) ArrayUtils.subarray( parts, 1, parts.length );
097            if ( parts == null || parts.length == 0 )
098            {
099                return "/";
100            }
101    
102            String joinedString = StringUtils.join( parts, '/' );
103            if ( href.endsWith( "/" ) )
104            {
105                joinedString = joinedString + "/";
106            }
107    
108            return joinedString;
109        }
110    }