Coverage Report - org.apache.maven.archiva.repository.content.RepositoryRequest
 
Classes in this File Line Coverage Branch Coverage Complexity
RepositoryRequest
0%
0/59
0%
0/50
6
 
 1  
 package org.apache.maven.archiva.repository.content;
 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.maven.archiva.model.ArtifactReference;
 24  
 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
 25  
 import org.apache.maven.archiva.repository.layout.LayoutException;
 26  
 import org.apache.maven.archiva.repository.metadata.MetadataTools;
 27  
 
 28  
 /**
 29  
  * RepositoryRequest is used to determine the type of request that is incoming, and convert it to an appropriate
 30  
  * ArtifactReference.
 31  
  *
 32  
  * @version $Id: RepositoryRequest.java 825449 2009-10-15 09:40:40Z oching $
 33  
  *
 34  
  * @todo no need to be a component once legacy path parser is not
 35  
  *
 36  
  * @plexus.component
 37  
  *      role="org.apache.maven.archiva.repository.content.RepositoryRequest"
 38  
  */
 39  0
 public class RepositoryRequest
 40  
 {
 41  0
     private PathParser defaultPathParser = new DefaultPathParser();
 42  
 
 43  
     /**
 44  
      * @plexus.requirement role-hint="legacy"
 45  
      */
 46  
     private PathParser legacyPathParser;
 47  
 
 48  
     /**
 49  
      * Takes an incoming requested path (in "/" format) and gleans the layout
 50  
      * and ArtifactReference appropriate for that content.
 51  
      *
 52  
      * @param requestedPath the relative path to the content.
 53  
      * @return the ArtifactReference for the requestedPath.
 54  
      * @throws LayoutException if the request path is not layout valid.
 55  
      */
 56  
     public ArtifactReference toArtifactReference( String requestedPath )
 57  
         throws LayoutException
 58  
     {
 59  0
         if ( StringUtils.isBlank( requestedPath ) )
 60  
         {
 61  0
             throw new LayoutException( "Blank request path is not a valid." );
 62  
         }
 63  
 
 64  0
         String path = requestedPath;
 65  0
         while ( path.startsWith( "/" ) )
 66  
         {
 67  0
             path = path.substring( 1 );
 68  
 
 69  
             // Only slash? that's bad, mmm-kay?
 70  0
             if ( "/".equals( path ) )
 71  
             {
 72  0
                 throw new LayoutException( "Invalid request path: Slash only." );
 73  
             }
 74  
         }
 75  
 
 76  0
         if ( isDefault( path ) )
 77  
         {
 78  0
             return defaultPathParser.toArtifactReference( path );
 79  
         }
 80  0
         else if ( isLegacy( path ) )
 81  
         {
 82  0
             return legacyPathParser.toArtifactReference( path );
 83  
         }
 84  
         else
 85  
         {
 86  0
             throw new LayoutException( "Not a valid request path layout, too short." );
 87  
         }
 88  
     }
 89  
 
 90  
     /**
 91  
      * <p>
 92  
      * Tests the path to see if it conforms to the expectations of a metadata request.
 93  
      * </p>
 94  
      * <p>
 95  
      * NOTE: This does a cursory check on the path's last element.  A result of true
 96  
      * from this method is not a guarantee that the metadata is in a valid format, or
 97  
      * that it even contains data.
 98  
      * </p>
 99  
      *
 100  
      * @param requestedPath the path to test.
 101  
      * @return true if the requestedPath is likely a metadata request.
 102  
      */
 103  
     public boolean isMetadata( String requestedPath )
 104  
     {
 105  0
         return requestedPath.endsWith( "/" + MetadataTools.MAVEN_METADATA );
 106  
     }
 107  
 
 108  
     /**
 109  
      * <p>
 110  
      * Tests the path to see if it conforms to the expectations of a support file request.
 111  
      * </p>
 112  
      * <p>
 113  
      * Tests for <code>.sha1</code>, <code>.md5</code>, <code>.asc</code>, and <code>.php</code>.
 114  
      * </p>
 115  
      * <p>
 116  
      * NOTE: This does a cursory check on the path's extension only.  A result of true
 117  
      * from this method is not a guarantee that the support resource is in a valid format, or
 118  
      * that it even contains data.
 119  
      * </p>
 120  
      *
 121  
      * @param requestedPath the path to test.
 122  
      * @return true if the requestedPath is likely that of a support file request.
 123  
      */
 124  
     public boolean isSupportFile( String requestedPath )
 125  
     {
 126  0
         int idx = requestedPath.lastIndexOf( '.' );
 127  0
         if ( idx <= 0 )
 128  
         {
 129  0
             return false;
 130  
         }
 131  
 
 132  0
         String ext = requestedPath.substring( idx );
 133  0
         return ( ".sha1".equals( ext ) || ".md5".equals( ext ) || ".asc".equals( ext ) || ".pgp".equals( ext ) );
 134  
     }
 135  
 
 136  
     public boolean isMetadataSupportFile( String requestedPath )
 137  
     {
 138  0
         if( isSupportFile( requestedPath ) )
 139  
         {
 140  0
             String basefilePath = StringUtils.substring( requestedPath, 0, requestedPath.lastIndexOf( '.' ) );
 141  0
             if( isMetadata( basefilePath ) )
 142  
             {
 143  0
                 return true;
 144  
             }
 145  
         }
 146  
         
 147  0
         return false;
 148  
     }
 149  
     
 150  
     /**
 151  
      * <p>
 152  
      * Tests the path to see if it conforms to the expectations of a default layout request.
 153  
      * </p>
 154  
      * <p>
 155  
      * NOTE: This does a cursory check on the count of path elements only.  A result of
 156  
      * true from this method is not a guarantee that the path sections are valid and
 157  
      * can be resolved to an artifact reference.  use {@link #toArtifactReference(String)}
 158  
      * if you want a more complete analysis of the validity of the path.
 159  
      * </p>
 160  
      *
 161  
      * @param requestedPath the path to test.
 162  
      * @return true if the requestedPath is likely that of a default layout request.
 163  
      */
 164  
     public boolean isDefault( String requestedPath )
 165  
     {
 166  0
         if ( StringUtils.isBlank( requestedPath ) )
 167  
         {
 168  0
             return false;
 169  
         }
 170  
 
 171  0
         String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );        
 172  0
         if( pathParts.length > 3 )
 173  
         {
 174  0
             return true;
 175  
         }
 176  0
         else if ( pathParts.length == 3 )
 177  
         {            
 178  
             // check if artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml)
 179  0
             if( isMetadata( requestedPath ) )
 180  
             {
 181  0
                 return true;
 182  
             }
 183  
             else 
 184  
             {
 185  
                 // check if checksum of artifact-level metadata (ex. eclipse/jdtcore/maven-metadata.xml.sha1)
 186  0
                 int idx = requestedPath.lastIndexOf( '.' );               
 187  0
                 if ( idx > 0 )
 188  
                 {
 189  0
                     String base = requestedPath.substring( 0, idx );
 190  0
                     if( isMetadata( base ) && isSupportFile( requestedPath ) )
 191  
                     {
 192  0
                         return true;
 193  
                     }
 194  
                 }
 195  
                 
 196  0
                 return false;
 197  
             }
 198  
         }
 199  
         else
 200  
         {
 201  0
             return false;
 202  
         }
 203  
     }
 204  
 
 205  
     /**
 206  
      * <p>
 207  
      * Tests the path to see if it conforms to the expectations of a legacy layout request.
 208  
      * </p>
 209  
      * <p>
 210  
      * NOTE: This does a cursory check on the count of path elements only.  A result of
 211  
      * true from this method is not a guarantee that the path sections are valid and
 212  
      * can be resolved to an artifact reference.  use {@link #toArtifactReference(String)}
 213  
      * if you want a more complete analysis of the validity of the path.
 214  
      * </p>
 215  
      *
 216  
      * @param requestedPath the path to test.
 217  
      * @return true if the requestedPath is likely that of a legacy layout request.
 218  
      */
 219  
     public boolean isLegacy( String requestedPath )
 220  
     {
 221  0
         if ( StringUtils.isBlank( requestedPath ) )
 222  
         {
 223  0
             return false;
 224  
         }
 225  
 
 226  0
         String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
 227  0
         return pathParts.length == 3;
 228  
     }
 229  
 
 230  
     /**
 231  
      * Adjust the requestedPath to conform to the native layout of the provided {@link ManagedRepositoryContent}.
 232  
      *
 233  
      * @param requestedPath the incoming requested path.
 234  
      * @param repository the repository to adjust to.
 235  
      * @return the adjusted (to native) path.
 236  
      * @throws LayoutException if the path cannot be parsed.
 237  
      */
 238  
     public String toNativePath( String requestedPath, ManagedRepositoryContent repository ) throws LayoutException
 239  
     {
 240  0
         if ( StringUtils.isBlank( requestedPath ) )
 241  
         {
 242  0
             throw new LayoutException( "Request Path is blank." );
 243  
         }
 244  
 
 245  0
         String referencedResource = requestedPath;
 246  
         // No checksum by default.
 247  0
         String supportfile = "";
 248  
 
 249  
         // Figure out support file, and actual referencedResource.
 250  0
         if( isSupportFile( requestedPath ) )
 251  
         {
 252  0
             int idx = requestedPath.lastIndexOf( '.' );
 253  0
             referencedResource = requestedPath.substring( 0, idx );
 254  0
             supportfile = requestedPath.substring( idx );
 255  
         }
 256  
 
 257  0
         if ( isMetadata( referencedResource ) )
 258  
         {
 259  0
             if ( repository instanceof ManagedLegacyRepositoryContent )
 260  
             {
 261  0
                 throw new LayoutException( "Cannot translate metadata request to legacy layout." );
 262  
             }
 263  
 
 264  
             /* Nothing to translate.
 265  
              * Default layout is the only layout that can contain maven-metadata.xml files, and
 266  
              * if the managedRepository is layout legacy, this request would never occur.
 267  
              */
 268  0
             return requestedPath;
 269  
         }
 270  
 
 271  
         // Treat as an artifact reference.
 272  0
         ArtifactReference ref = toArtifactReference( referencedResource );
 273  0
         String adjustedPath = repository.toPath( ref );
 274  0
         return adjustedPath + supportfile;
 275  
     }
 276  
 }