Coverage Report - org.apache.maven.archiva.repository.content.DefaultPathParser
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultPathParser
0%
0/56
0%
0/34
29
 
 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.common.utils.VersionUtil;
 24  
 import org.apache.maven.archiva.model.ArtifactReference;
 25  
 import org.apache.maven.archiva.repository.layout.LayoutException;
 26  
 
 27  
 /**
 28  
  * DefaultPathParser is a parser for maven 2 (default layout) paths to ArtifactReference.
 29  
  *
 30  
  * @version $Id: DefaultPathParser.java 718864 2008-11-19 06:33:35Z brett $
 31  
  */
 32  0
 public class DefaultPathParser implements PathParser
 33  
 {
 34  
     private static final String INVALID_ARTIFACT_PATH = "Invalid path to Artifact: ";
 35  
 
 36  
     /**
 37  
      * {@inheritDoc}
 38  
      * @see org.apache.maven.archiva.repository.content.PathParser#toArtifactReference(java.lang.String)
 39  
      */
 40  
     public ArtifactReference toArtifactReference( String path )
 41  
         throws LayoutException
 42  
     {
 43  0
         if ( StringUtils.isBlank( path ) )
 44  
         {
 45  0
             throw new LayoutException( "Unable to convert blank path." );
 46  
         }
 47  
 
 48  0
         ArtifactReference artifact = new ArtifactReference();
 49  
 
 50  0
         String normalizedPath = StringUtils.replace( path, "\\", "/" );
 51  0
         String pathParts[] = StringUtils.split( normalizedPath, '/' );
 52  
 
 53  
         /* Minimum parts.
 54  
          *
 55  
          *   path = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar"
 56  
          *   path[0] = "commons-lang";        // The Group ID
 57  
          *   path[1] = "commons-lang";        // The Artifact ID
 58  
          *   path[2] = "2.1";                 // The Version
 59  
          *   path[3] = "commons-lang-2.1.jar" // The filename.
 60  
          */
 61  
 
 62  0
         if ( pathParts.length < 4 )
 63  
         {
 64  
             // Illegal Path Parts Length.
 65  0
             throw new LayoutException( "Not enough parts to the path [" + path
 66  
                 + "] to construct an ArchivaArtifact from. (Requires at least 4 parts)" );
 67  
         }
 68  
 
 69  
         // Maven 2.x path.
 70  0
         int partCount = pathParts.length;
 71  0
         int filenamePos = partCount - 1;
 72  0
         int baseVersionPos = partCount - 2;
 73  0
         int artifactIdPos = partCount - 3;
 74  0
         int groupIdPos = partCount - 4;
 75  
 
 76  
         // Second to last is the baseVersion (the directory version)
 77  0
         String baseVersion = pathParts[baseVersionPos];
 78  
 
 79  
         // Third to last is the artifact Id.
 80  0
         artifact.setArtifactId( pathParts[artifactIdPos] );
 81  
 
 82  
         // Remaining pieces are the groupId.
 83  0
         for ( int i = 0; i <= groupIdPos; i++ )
 84  
         {
 85  0
             if ( i == 0 )
 86  
             {
 87  0
                 artifact.setGroupId( pathParts[i] );
 88  
             }
 89  
             else
 90  
             {
 91  0
                 artifact.setGroupId( artifact.getGroupId() + "." + pathParts[i] );
 92  
             }
 93  
         }
 94  
 
 95  
         try
 96  
         {
 97  
             // Last part is the filename
 98  0
             String filename = pathParts[filenamePos];
 99  
 
 100  
             // Now we need to parse the filename to get the artifact version Id.
 101  0
             if ( StringUtils.isBlank( filename ) )
 102  
             {
 103  0
                 throw new IllegalArgumentException( INVALID_ARTIFACT_PATH + "Unable to split blank filename." );
 104  
             }
 105  
 
 106  0
             FilenameParser parser = new FilenameParser( filename );
 107  
 
 108  
             // Expect the filename to start with the artifactId.
 109  0
             artifact.setArtifactId( parser.expect( artifact.getArtifactId() ) );
 110  
 
 111  0
             if ( artifact.getArtifactId() == null )
 112  
             {
 113  0
                 throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid, "
 114  
                     + "should start with artifactId as stated in path." );
 115  
             }
 116  
 
 117  
             // Process the version.
 118  0
             artifact.setVersion( parser.expect( baseVersion ) );
 119  
 
 120  0
             if ( artifact.getVersion() == null )
 121  
             {
 122  
                 // We working with a snapshot?
 123  0
                 if ( VersionUtil.isSnapshot( baseVersion ) )
 124  
                 {
 125  0
                     artifact.setVersion( parser.nextVersion() );
 126  0
                     if ( !VersionUtil.isUniqueSnapshot( artifact.getVersion() ) )
 127  
                     {
 128  0
                         throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid,"
 129  
                             + "expected timestamp format in filename." );
 130  
                     }
 131  
                 }
 132  
                 else
 133  
                 {
 134  0
                     throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid, "
 135  
                         + "expected version as stated in path." );
 136  
                 }
 137  
             }
 138  
 
 139  
             // Do we have a classifier?
 140  0
             switch(parser.seperator())
 141  
             {
 142  
                 case '-':
 143  
                     // Definately a classifier.
 144  0
                     artifact.setClassifier( parser.remaining() );
 145  
 
 146  
                     // Set the type.
 147  0
                     artifact.setType( ArtifactExtensionMapping.mapExtensionAndClassifierToType( artifact.getClassifier(), parser.getExtension() ) );
 148  0
                     break;
 149  
                 case '.':
 150  
                     // We have an dual extension possibility.
 151  0
                     String extension = parser.remaining() + '.' + parser.getExtension();
 152  0
                     artifact.setType( extension );
 153  0
                     break;
 154  
                 case 0:
 155  
                     // End of the filename, only a simple extension left. - Set the type.
 156  0
                     String type = ArtifactExtensionMapping.mapExtensionToType( parser.getExtension() );
 157  0
                     if ( type == null )
 158  
                     {
 159  0
                         throw new LayoutException( "Invalid artifact: no type was specified" );
 160  
                     }
 161  0
                     artifact.setType( type );
 162  
                     break;
 163  
             }
 164  
 
 165  
             // Special case for maven plugins
 166  0
             if ( StringUtils.equals( "jar", artifact.getType() ) &&
 167  
                  ArtifactExtensionMapping.isMavenPlugin( artifact.getArtifactId() ) )
 168  
             {
 169  0
                 artifact.setType( ArtifactExtensionMapping.MAVEN_PLUGIN );
 170  
             }
 171  
         }
 172  0
         catch ( LayoutException e )
 173  
         {
 174  0
             throw e;
 175  0
         }
 176  
 
 177  
         // Sanity Checks.
 178  
 
 179  
         // Do we have a snapshot version?
 180  0
         if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
 181  
         {
 182  
             // Rules are different for SNAPSHOTS
 183  0
             if ( !VersionUtil.isGenericSnapshot( baseVersion ) )
 184  
             {
 185  0
                 String filenameBaseVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
 186  0
                 throw new LayoutException( "Invalid snapshot artifact location, version directory should be "
 187  
                     + filenameBaseVersion );
 188  
             }
 189  
         }
 190  
         else
 191  
         {
 192  
             // Non SNAPSHOT rules.
 193  
             // Do we pass the simple test?
 194  0
             if ( !StringUtils.equals( baseVersion, artifact.getVersion() ) )
 195  
             {
 196  0
                 throw new LayoutException( "Invalid artifact: version declared in directory path does"
 197  
                     + " not match what was found in the artifact filename." );
 198  
             }
 199  
         }
 200  
 
 201  0
         return artifact;
 202  
     }
 203  
 
 204  
 }