Coverage Report - org.apache.maven.archiva.repository.content.LegacyPathParser
 
Classes in this File Line Coverage Branch Coverage Complexity
LegacyPathParser
0%
0/60
0%
0/32
0
 
 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 java.util.Collection;
 23  
 
 24  
 import org.apache.commons.lang.StringUtils;
 25  
 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
 26  
 import org.apache.maven.archiva.configuration.LegacyArtifactPath;
 27  
 import org.apache.maven.archiva.model.ArtifactReference;
 28  
 import org.apache.maven.archiva.repository.layout.LayoutException;
 29  
 
 30  
 /**
 31  
  * LegacyPathParser is a parser for maven 1 (legacy layout) paths to
 32  
  * ArtifactReference.
 33  
  *
 34  
  * @version $Id: LegacyPathParser.java 755296 2009-03-17 16:13:38Z brett $
 35  
  * @plexus.component role="org.apache.maven.archiva.repository.content.PathParser"
 36  
  * role-hint="legacy"
 37  
  */
 38  0
 public class LegacyPathParser
 39  
     implements PathParser
 40  
 {
 41  
     private static final String INVALID_ARTIFACT_PATH = "Invalid path to Artifact: ";
 42  
 
 43  
     /**
 44  
      * @todo pass these in on construction instead, since this can't be long lived (no config listener), then no need to be a component
 45  
      * @plexus.requirement
 46  
      */
 47  
     protected ArchivaConfiguration configuration;
 48  
 
 49  
     /**
 50  
      * {@inheritDoc}
 51  
      *
 52  
      * @see org.apache.maven.archiva.repository.content.PathParser#toArtifactReference(java.lang.String)
 53  
      */
 54  
     public ArtifactReference toArtifactReference( String path )
 55  
         throws LayoutException
 56  
     {
 57  0
         ArtifactReference artifact = new ArtifactReference();
 58  
 
 59  
         // First, look if a custom resolution rule has been set for this artifact
 60  0
         Collection<LegacyArtifactPath> legacy = configuration.getConfiguration().getLegacyArtifactPaths();
 61  0
         for ( LegacyArtifactPath legacyPath : legacy )
 62  
         {
 63  0
             if ( legacyPath.match( path ) )
 64  
             {                        
 65  0
                             artifact.setGroupId( legacyPath.getGroupId() );
 66  0
                             artifact.setArtifactId( legacyPath.getArtifactId() );
 67  0
                             artifact.setClassifier( legacyPath.getClassifier() );
 68  0
                             artifact.setVersion( legacyPath.getVersion() );
 69  0
                             artifact.setType( legacyPath.getType() );
 70  0
                 return artifact;
 71  
             }
 72  
         }
 73  
 
 74  0
         String normalizedPath = StringUtils.replace( path, "\\", "/" );
 75  
 
 76  0
         String pathParts[] = StringUtils.split( normalizedPath, '/' );
 77  
 
 78  
         /* Always 3 parts. (Never more or less)
 79  
          * 
 80  
          *   path = "commons-lang/jars/commons-lang-2.1.jar"
 81  
          *   path[0] = "commons-lang";          // The Group ID
 82  
          *   path[1] = "jars";                  // The Directory Type
 83  
          *   path[2] = "commons-lang-2.1.jar";  // The Filename.
 84  
          */
 85  
 
 86  0
         if ( pathParts.length != 3 )
 87  
         {
 88  
             // Illegal Path Parts Length.
 89  0
             throw new LayoutException( INVALID_ARTIFACT_PATH
 90  
                     + "legacy paths should only have 3 parts [groupId]/[type]s/[artifactId]-[version].[type], found "
 91  
                     + pathParts.length + " instead." );
 92  
         }
 93  
 
 94  
         // The Group ID.
 95  0
         artifact.setGroupId( pathParts[0] );
 96  
 
 97  
         // The Expected Type.
 98  0
         String expectedType = pathParts[1];
 99  
 
 100  
         // Sanity Check: expectedType should end in "s".
 101  0
         if ( !expectedType.endsWith( "s" ) )
 102  
         {
 103  0
             throw new LayoutException( INVALID_ARTIFACT_PATH
 104  
                     + "legacy paths should have an expected type ending in [s] in the second part of the path." );
 105  
         }
 106  
 
 107  
         // The Filename.
 108  0
         String filename = pathParts[2];
 109  
 
 110  0
         FilenameParser parser = new FilenameParser( filename );
 111  
 
 112  0
         artifact.setArtifactId( parser.nextNonVersion() );
 113  
 
 114  
         // Sanity Check: does it have an artifact id?
 115  0
         if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
 116  
         {
 117  
             // Special Case: The filename might start with a version id (like "test-arch-1.0.jar").
 118  0
             int idx = filename.indexOf( '-' );
 119  0
             if ( idx > 0 )
 120  
             {
 121  0
                 parser.reset();
 122  
                 // Take the first section regardless of content.
 123  0
                 String artifactId = parser.next();
 124  
 
 125  
                 // Is there anything more that is considered not a version id?
 126  0
                 String moreArtifactId = parser.nextNonVersion();
 127  0
                 if ( StringUtils.isNotBlank( moreArtifactId ) )
 128  
                 {
 129  0
                     artifact.setArtifactId( artifactId + "-" + moreArtifactId );
 130  
                 }
 131  
                 else
 132  
                 {
 133  0
                     artifact.setArtifactId( artifactId );
 134  
                 }
 135  
             }
 136  
 
 137  
             // Sanity Check: still no artifact id?
 138  0
             if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
 139  
             {
 140  0
                 throw new LayoutException( INVALID_ARTIFACT_PATH + "no artifact id present." );
 141  
             }
 142  
         }
 143  
 
 144  0
         artifact.setVersion( parser.remaining() );
 145  
 
 146  
         // Sanity Check: does it have a version?
 147  0
         if ( StringUtils.isEmpty( artifact.getVersion() ) )
 148  
         {
 149  
             // Special Case: use last section of artifactId as version.
 150  0
             String artifactId = artifact.getArtifactId();
 151  0
             int idx = artifactId.lastIndexOf( '-' );
 152  0
             if ( idx > 0 )
 153  
             {
 154  0
                 artifact.setVersion( artifactId.substring( idx + 1 ) );
 155  0
                 artifact.setArtifactId( artifactId.substring( 0, idx ) );
 156  
             }
 157  
             else
 158  
             {
 159  0
                 throw new LayoutException( INVALID_ARTIFACT_PATH + "no version found." );
 160  
             }
 161  
         }
 162  
 
 163  0
         String classifier = ArtifactClassifierMapping.getClassifier( expectedType );
 164  0
         if ( classifier != null )
 165  
         {
 166  0
             String version = artifact.getVersion();
 167  0
             if ( ! version.endsWith( "-" + classifier ) )
 168  
             {
 169  0
                 throw new LayoutException( INVALID_ARTIFACT_PATH + expectedType + " artifacts must use the classifier " + classifier );
 170  
             }
 171  0
             version = version.substring( 0, version.length() - classifier.length() - 1 );
 172  0
             artifact.setVersion( version );
 173  0
             artifact.setClassifier( classifier );
 174  
         }
 175  
 
 176  0
         String extension = parser.getExtension();
 177  
 
 178  
         // Set Type
 179  0
         String defaultExtension = expectedType.substring( 0, expectedType.length() - 1 );
 180  0
         artifact.setType(
 181  
             ArtifactExtensionMapping.mapExtensionAndClassifierToType( classifier, extension, defaultExtension ) );
 182  
 
 183  
         // Sanity Check: does it have an extension?
 184  0
         if ( StringUtils.isEmpty( artifact.getType() ) )
 185  
         {
 186  0
             throw new LayoutException( INVALID_ARTIFACT_PATH + "no extension found." );
 187  
         }
 188  
 
 189  
         // Special Case with Maven Plugins
 190  0
         if ( StringUtils.equals( "jar", extension ) && StringUtils.equals( "plugins", expectedType ) )
 191  
         {
 192  0
             artifact.setType( ArtifactExtensionMapping.MAVEN_ONE_PLUGIN );
 193  
         }
 194  
         else
 195  
         {
 196  
             // Sanity Check: does extension match pathType on path?
 197  0
             String expectedExtension = ArtifactExtensionMapping.getExtension( artifact.getType() );
 198  
 
 199  0
             if ( !expectedExtension.equals( extension ) )
 200  
             {
 201  0
                 throw new LayoutException( INVALID_ARTIFACT_PATH + "mismatch on extension [" + extension
 202  
                     + "] and layout specified type [" + artifact.getType() + "] (which maps to extension: ["
 203  
                     + expectedExtension + "]) on path [" + path + "]" );
 204  
             }
 205  
         }
 206  
 
 207  0
         return artifact;
 208  
     }
 209  
 }