1 package org.apache.maven.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.StringUtils;
23 import org.apache.commons.lang.ArrayUtils;
24 import org.apache.commons.io.FilenameUtils;
27 * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
29 public class RepositoryPathUtil
31 public static String getLogicalResource(final String href)
33 String logicalResource = null;
34 String requestPathInfo = StringUtils.defaultString( href );
36 //remove prefix ie /repository/blah becomes /blah
37 requestPathInfo = removePrefix(requestPathInfo);
39 // Remove prefixing slash as the repository id doesn't contain it;
40 if ( requestPathInfo.startsWith( "/" ) )
42 requestPathInfo = requestPathInfo.substring( 1 );
45 int slash = requestPathInfo.indexOf( '/' );
48 logicalResource = requestPathInfo.substring( slash );
50 if (logicalResource.endsWith( "/.." ) )
52 logicalResource += "/";
55 if ( logicalResource != null && logicalResource.startsWith( "//" ) )
57 logicalResource = logicalResource.substring( 1 );
60 if ( logicalResource == null )
62 logicalResource = "/";
67 logicalResource = "/";
69 return logicalResource;
72 public static String getRepositoryName(final String href)
74 String requestPathInfo = StringUtils.defaultString( href );
76 //remove prefix ie /repository/blah becomes /blah
77 requestPathInfo = removePrefix(requestPathInfo);
79 // Remove prefixing slash as the repository id doesn't contain it;
80 if ( requestPathInfo.startsWith( "/" ) )
82 requestPathInfo = requestPathInfo.substring( 1 );
85 // Find first element, if slash exists.
86 int slash = requestPathInfo.indexOf( '/' );
89 // Filtered: "central/org/apache/maven/" -> "central"
90 return requestPathInfo.substring( 0, slash );
92 return requestPathInfo;
95 private static String removePrefix(final String href)
97 String[] parts = StringUtils.split(href, '/');
98 parts = (String[]) ArrayUtils.subarray(parts, 1, parts.length);
99 if (parts == null || parts.length == 0)
103 return StringUtils.join(parts, '/');