1 package org.apache.archiva.webdav.util;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.lang.ArrayUtils;
23 import org.apache.commons.lang.StringUtils;
27 public class RepositoryPathUtil
29 public static String getLogicalResource( final String href )
31 String logicalResource = null;
32 String requestPathInfo = StringUtils.defaultString( href );
34 //remove prefix ie /repository/blah becomes /blah
35 requestPathInfo = removePrefix( requestPathInfo );
37 // Remove prefixing slash as the repository id doesn't contain it;
38 if ( requestPathInfo.startsWith( "/" ) )
40 requestPathInfo = requestPathInfo.substring( 1 );
43 int slash = requestPathInfo.indexOf( '/' );
46 logicalResource = requestPathInfo.substring( slash );
48 if ( logicalResource.endsWith( "/.." ) )
50 logicalResource += "/";
53 if ( logicalResource != null && logicalResource.startsWith( "//" ) )
55 logicalResource = logicalResource.substring( 1 );
58 if ( logicalResource == null )
60 logicalResource = "/";
65 logicalResource = "/";
67 return logicalResource;
70 public static String getRepositoryName( final String href )
72 String requestPathInfo = StringUtils.defaultString( href );
74 //remove prefix ie /repository/blah becomes /blah
75 requestPathInfo = removePrefix( requestPathInfo );
77 // Remove prefixing slash as the repository id doesn't contain it;
78 if ( requestPathInfo.startsWith( "/" ) )
80 requestPathInfo = requestPathInfo.substring( 1 );
83 // Find first element, if slash exists.
84 int slash = requestPathInfo.indexOf( '/' );
87 // Filtered: "central/org/apache/maven/" -> "central"
88 return requestPathInfo.substring( 0, slash );
90 return requestPathInfo;
93 private static String removePrefix( final String href )
95 String[] parts = StringUtils.split( href, '/' );
96 parts = (String[]) ArrayUtils.subarray( parts, 1, parts.length );
97 if ( parts == null || parts.length == 0 )
102 String joinedString = StringUtils.join( parts, '/' );
103 if ( href.endsWith( "/" ) )
105 joinedString = joinedString + "/";