View Javadoc

1   package org.apache.archiva.webdav.util;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import org.apache.commons.lang.ArrayUtils;
23  import org.apache.commons.lang.StringUtils;
24  
25  /**
26   */
27  public class RepositoryPathUtil
28  {
29      public static String getLogicalResource( final String href )
30      {
31          String logicalResource = null;
32          String requestPathInfo = StringUtils.defaultString( href );
33  
34          //remove prefix ie /repository/blah becomes /blah
35          requestPathInfo = removePrefix( requestPathInfo );
36  
37          // Remove prefixing slash as the repository id doesn't contain it;
38          if ( requestPathInfo.startsWith( "/" ) )
39          {
40              requestPathInfo = requestPathInfo.substring( 1 );
41          }
42  
43          int slash = requestPathInfo.indexOf( '/' );
44          if ( slash > 0 )
45          {
46              logicalResource = requestPathInfo.substring( slash );
47  
48              if ( logicalResource.endsWith( "/.." ) )
49              {
50                  logicalResource += "/";
51              }
52  
53              if ( logicalResource != null && logicalResource.startsWith( "//" ) )
54              {
55                  logicalResource = logicalResource.substring( 1 );
56              }
57  
58              if ( logicalResource == null )
59              {
60                  logicalResource = "/";
61              }
62          }
63          else
64          {
65              logicalResource = "/";
66          }
67          return logicalResource;
68      }
69  
70      public static String getRepositoryName( final String href )
71      {
72          String requestPathInfo = StringUtils.defaultString( href );
73  
74          //remove prefix ie /repository/blah becomes /blah
75          requestPathInfo = removePrefix( requestPathInfo );
76  
77          // Remove prefixing slash as the repository id doesn't contain it;
78          if ( requestPathInfo.startsWith( "/" ) )
79          {
80              requestPathInfo = requestPathInfo.substring( 1 );
81          }
82  
83          // Find first element, if slash exists.
84          int slash = requestPathInfo.indexOf( '/' );
85          if ( slash > 0 )
86          {
87              // Filtered: "central/org/apache/maven/" -> "central"
88              return requestPathInfo.substring( 0, slash );
89          }
90          return requestPathInfo;
91      }
92  
93      private static String removePrefix( final String href )
94      {
95          String[] parts = StringUtils.split( href, '/' );
96          parts = (String[]) ArrayUtils.subarray( parts, 1, parts.length );
97          if ( parts == null || parts.length == 0 )
98          {
99              return "/";
100         }
101 
102         String joinedString = StringUtils.join( parts, '/' );
103         if ( href.endsWith( "/" ) )
104         {
105             joinedString = joinedString + "/";
106         }
107 
108         return joinedString;
109     }
110 }