1 package org.apache.maven.archiva.repository.content;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.maven.archiva.model.ArtifactReference;
24 import org.apache.maven.archiva.repository.layout.LayoutException;
27 * LegacyPathParser is a parser for maven 1 (legacy layout) paths to ArtifactReference.
29 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
32 public class LegacyPathParser
34 private static final String INVALID_ARTIFACT_PATH = "Invalid path to Artifact: ";
36 protected static ArtifactReference toArtifactReference( String path )
37 throws LayoutException
39 ArtifactReference artifact = new ArtifactReference();
41 String normalizedPath = StringUtils.replace( path, "\\", "/" );
43 String pathParts[] = StringUtils.split( normalizedPath, '/' );
45 /* Always 3 parts. (Never more or less)
47 * path = "commons-lang/jars/commons-lang-2.1.jar"
48 * path[0] = "commons-lang"; // The Group ID
49 * path[1] = "jars"; // The Directory Type
50 * path[2] = "commons-lang-2.1.jar"; // The Filename.
53 if ( pathParts.length != 3 )
55 // Illegal Path Parts Length.
56 throw new LayoutException( INVALID_ARTIFACT_PATH
57 + "legacy paths should only have 3 parts [groupId]/[type]s/[artifactId]-[version].[type], found "
58 + pathParts.length + " instead." );
62 artifact.setGroupId( pathParts[0] );
65 String expectedType = pathParts[1];
67 // Sanity Check: expectedType should end in "s".
68 if ( !expectedType.endsWith( "s" ) )
70 throw new LayoutException( INVALID_ARTIFACT_PATH
71 + "legacy paths should have an expected type ending in [s] in the second part of the path." );
75 String filename = pathParts[2];
77 FilenameParser parser = new FilenameParser( filename );
79 artifact.setArtifactId( parser.nextNonVersion() );
81 // Sanity Check: does it have an artifact id?
82 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
84 // Special Case: The filename might start with a version id (like "test-arch-1.0.jar").
85 int idx = filename.indexOf( '-' );
89 // Take the first section regardless of content.
90 String artifactId = parser.next();
92 // Is there anything more that is considered not a version id?
93 String moreArtifactId = parser.nextNonVersion();
94 if ( StringUtils.isNotBlank( moreArtifactId ) )
96 artifact.setArtifactId( artifactId + "-" + moreArtifactId );
100 artifact.setArtifactId( artifactId );
104 // Sanity Check: still no artifact id?
105 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
107 throw new LayoutException( INVALID_ARTIFACT_PATH + "no artifact id present." );
111 artifact.setVersion( parser.remaining() );
113 // Sanity Check: does it have a version?
114 if ( StringUtils.isEmpty( artifact.getVersion() ) )
116 // Special Case: use last section of artifactId as version.
117 String artifactId = artifact.getArtifactId();
118 int idx = artifactId.lastIndexOf( '-' );
121 artifact.setVersion( artifactId.substring( idx + 1 ) );
122 artifact.setArtifactId( artifactId.substring( 0, idx ) );
126 throw new LayoutException( INVALID_ARTIFACT_PATH + "no version found." );
131 artifact.setType( ArtifactExtensionMapping.guessTypeFromFilename( filename ) );
133 // Sanity Check: does it have an extension?
134 if ( StringUtils.isEmpty( artifact.getType() ) )
136 throw new LayoutException( INVALID_ARTIFACT_PATH + "no extension found." );
139 // Special Case with Maven Plugins
140 if ( StringUtils.equals( "jar", artifact.getType() ) && StringUtils.equals( "plugins", expectedType ) )
142 artifact.setType( ArtifactExtensionMapping.MAVEN_PLUGIN );
146 // Sanity Check: does extension match pathType on path?
147 String trimPathType = expectedType.substring( 0, expectedType.length() - 1 );
149 String expectedExtension = ArtifactExtensionMapping.getExtension( trimPathType );
150 String actualExtension = parser.getExtension();
152 if ( !expectedExtension.equals( actualExtension ) )
154 throw new LayoutException( INVALID_ARTIFACT_PATH + "mismatch on extension [" + actualExtension
155 + "] and layout specified type [" + expectedType + "] (which maps to extension: ["
156 + expectedExtension + "]) on path [" + path + "]" );
160 String classifier = ArtifactClassifierMapping.getClassifier( artifact.getType() );
161 if ( classifier != null )
163 String version = artifact.getVersion();
164 if ( ! version.endsWith( "-" + classifier ) )
166 throw new LayoutException( INVALID_ARTIFACT_PATH + expectedType + " artifacts must use the classifier " + classifier );
168 version = version.substring( 0, version.length() - classifier.length() - 1 );
169 artifact.setVersion( version );
170 artifact.setClassifier( classifier );