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