1 package org.apache.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.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.LegacyArtifactPath;
25 import org.apache.archiva.model.ArtifactReference;
26 import org.apache.archiva.repository.layout.LayoutException;
28 import java.util.Collection;
31 * LegacyPathParser is a parser for maven 1 (legacy layout) paths to
36 public class LegacyPathParser
39 private static final String INVALID_ARTIFACT_PATH = "Invalid path to Artifact: ";
41 protected ArchivaConfiguration configuration;
43 public LegacyPathParser( ArchivaConfiguration configuration )
45 this.configuration = configuration;
52 * @see org.apache.archiva.repository.content.PathParser#toArtifactReference(java.lang.String)
54 public ArtifactReference toArtifactReference( String path )
55 throws LayoutException
57 ArtifactReference artifact = new ArtifactReference();
59 // First, look if a custom resolution rule has been set for this artifact
60 Collection<LegacyArtifactPath> legacy = configuration.getConfiguration().getLegacyArtifactPaths();
61 for ( LegacyArtifactPath legacyPath : legacy )
63 if ( legacyPath.match( path ) )
65 artifact.setGroupId( legacyPath.getGroupId() );
66 artifact.setArtifactId( legacyPath.getArtifactId() );
67 artifact.setClassifier( legacyPath.getClassifier() );
68 artifact.setVersion( legacyPath.getVersion() );
69 artifact.setType( legacyPath.getType() );
74 String normalizedPath = StringUtils.replace( path, "\\", "/" );
76 String pathParts[] = StringUtils.split( normalizedPath, '/' );
78 /* Always 3 parts. (Never more or less)
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.
86 if ( pathParts.length != 3 )
88 // Illegal Path Parts Length.
89 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." );
95 artifact.setGroupId( pathParts[0] );
98 String expectedType = pathParts[1];
100 // Sanity Check: expectedType should end in "s".
101 if ( !expectedType.endsWith( "s" ) )
103 throw new LayoutException( INVALID_ARTIFACT_PATH
104 + "legacy paths should have an expected type ending in [s] in the second part of the path." );
108 String filename = pathParts[2];
110 FilenameParser parser = new FilenameParser( filename );
112 artifact.setArtifactId( parser.nextNonVersion() );
114 // Sanity Check: does it have an artifact id?
115 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
117 // Special Case: The filename might start with a version id (like "test-arch-1.0.jar").
118 int idx = filename.indexOf( '-' );
122 // Take the first section regardless of content.
123 String artifactId = parser.next();
125 // Is there anything more that is considered not a version id?
126 String moreArtifactId = parser.nextNonVersion();
127 if ( StringUtils.isNotBlank( moreArtifactId ) )
129 artifact.setArtifactId( artifactId + "-" + moreArtifactId );
133 artifact.setArtifactId( artifactId );
137 // Sanity Check: still no artifact id?
138 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
140 throw new LayoutException( INVALID_ARTIFACT_PATH + "no artifact id present." );
144 artifact.setVersion( parser.remaining() );
146 // Sanity Check: does it have a version?
147 if ( StringUtils.isEmpty( artifact.getVersion() ) )
149 // Special Case: use last section of artifactId as version.
150 String artifactId = artifact.getArtifactId();
151 int idx = artifactId.lastIndexOf( '-' );
154 artifact.setVersion( artifactId.substring( idx + 1 ) );
155 artifact.setArtifactId( artifactId.substring( 0, idx ) );
159 throw new LayoutException( INVALID_ARTIFACT_PATH + "no version found." );
163 String classifier = ArtifactClassifierMapping.getClassifier( expectedType );
164 if ( classifier != null )
166 String version = artifact.getVersion();
167 if ( !version.endsWith( "-" + classifier ) )
169 throw new LayoutException(
170 INVALID_ARTIFACT_PATH + expectedType + " artifacts must use the classifier " + classifier );
172 version = version.substring( 0, version.length() - classifier.length() - 1 );
173 artifact.setVersion( version );
174 artifact.setClassifier( classifier );
177 String extension = parser.getExtension();
180 String defaultExtension = expectedType.substring( 0, expectedType.length() - 1 );
182 ArtifactExtensionMapping.mapExtensionAndClassifierToType( classifier, extension, defaultExtension ) );
184 // Sanity Check: does it have an extension?
185 if ( StringUtils.isEmpty( artifact.getType() ) )
187 throw new LayoutException( INVALID_ARTIFACT_PATH + "no extension found." );
190 // Special Case with Maven Plugins
191 if ( StringUtils.equals( "jar", extension ) && StringUtils.equals( "plugins", expectedType ) )
193 artifact.setType( ArtifactExtensionMapping.MAVEN_ONE_PLUGIN );
197 // Sanity Check: does extension match pathType on path?
198 String expectedExtension = ArtifactExtensionMapping.getExtension( artifact.getType() );
200 if ( !expectedExtension.equals( extension ) )
202 throw new LayoutException(
203 INVALID_ARTIFACT_PATH + "mismatch on extension [" + extension + "] and layout specified type ["
204 + artifact.getType() + "] (which maps to extension: [" + expectedExtension + "]) on path ["