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.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.LegacyArtifactPath;
25 import org.apache.maven.archiva.model.ArtifactReference;
26 import org.apache.maven.archiva.repository.layout.LayoutException;
28 import java.util.Collection;
29 import java.util.Iterator;
32 * LegacyPathParser is a parser for maven 1 (legacy layout) paths to
35 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
37 * @plexus.component role="org.apache.maven.archiva.repository.content.PathParser"
40 public class LegacyPathParser
43 private static final String INVALID_ARTIFACT_PATH = "Invalid path to Artifact: ";
48 protected ArchivaConfiguration configuration;
53 * @see org.apache.maven.archiva.repository.content.PathParser#toArtifactReference(java.lang.String)
55 public ArtifactReference toArtifactReference( String path )
56 throws LayoutException
58 ArtifactReference artifact = new ArtifactReference();
60 // First, look if a custom resolution rule has been set for this artifact
61 Collection legacy = configuration.getConfiguration().getLegacyArtifactPaths();
62 for ( Iterator iterator = legacy.iterator(); iterator.hasNext(); )
64 LegacyArtifactPath legacyPath = (LegacyArtifactPath) iterator.next();
65 if ( legacyPath.match( path ) )
67 artifact.setGroupId( legacyPath.getGroupId() );
68 artifact.setArtifactId( legacyPath.getArtifactId() );
69 artifact.setClassifier( legacyPath.getClassifier() );
70 artifact.setVersion( legacyPath.getVersion() );
71 artifact.setType( legacyPath.getType() );
76 String normalizedPath = StringUtils.replace( path, "\\", "/" );
78 String pathParts[] = StringUtils.split( normalizedPath, '/' );
80 /* Always 3 parts. (Never more or less)
82 * path = "commons-lang/jars/commons-lang-2.1.jar"
83 * path[0] = "commons-lang"; // The Group ID
84 * path[1] = "jars"; // The Directory Type
85 * path[2] = "commons-lang-2.1.jar"; // The Filename.
88 if ( pathParts.length != 3 )
90 // Illegal Path Parts Length.
91 throw new LayoutException( INVALID_ARTIFACT_PATH
92 + "legacy paths should only have 3 parts [groupId]/[type]s/[artifactId]-[version].[type], found "
93 + pathParts.length + " instead." );
97 artifact.setGroupId( pathParts[0] );
100 String expectedType = pathParts[1];
102 // Sanity Check: expectedType should end in "s".
103 if ( !expectedType.endsWith( "s" ) )
105 throw new LayoutException( INVALID_ARTIFACT_PATH
106 + "legacy paths should have an expected type ending in [s] in the second part of the path." );
110 String filename = pathParts[2];
112 FilenameParser parser = new FilenameParser( filename );
114 artifact.setArtifactId( parser.nextNonVersion() );
116 // Sanity Check: does it have an artifact id?
117 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
119 // Special Case: The filename might start with a version id (like "test-arch-1.0.jar").
120 int idx = filename.indexOf( '-' );
124 // Take the first section regardless of content.
125 String artifactId = parser.next();
127 // Is there anything more that is considered not a version id?
128 String moreArtifactId = parser.nextNonVersion();
129 if ( StringUtils.isNotBlank( moreArtifactId ) )
131 artifact.setArtifactId( artifactId + "-" + moreArtifactId );
135 artifact.setArtifactId( artifactId );
139 // Sanity Check: still no artifact id?
140 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
142 throw new LayoutException( INVALID_ARTIFACT_PATH + "no artifact id present." );
146 artifact.setVersion( parser.remaining() );
148 // Sanity Check: does it have a version?
149 if ( StringUtils.isEmpty( artifact.getVersion() ) )
151 // Special Case: use last section of artifactId as version.
152 String artifactId = artifact.getArtifactId();
153 int idx = artifactId.lastIndexOf( '-' );
156 artifact.setVersion( artifactId.substring( idx + 1 ) );
157 artifact.setArtifactId( artifactId.substring( 0, idx ) );
161 throw new LayoutException( INVALID_ARTIFACT_PATH + "no version found." );
165 String classifier = ArtifactClassifierMapping.getClassifier( expectedType );
166 if ( classifier != null )
168 String version = artifact.getVersion();
169 if ( ! version.endsWith( "-" + classifier ) )
171 throw new LayoutException( INVALID_ARTIFACT_PATH + expectedType + " artifacts must use the classifier " + classifier );
173 version = version.substring( 0, version.length() - classifier.length() - 1 );
174 artifact.setVersion( version );
175 artifact.setClassifier( classifier );
178 String extension = parser.getExtension();
181 String defaultExtension = expectedType.substring( 0, expectedType.length() - 1 );
183 ArtifactExtensionMapping.mapExtensionAndClassifierToType( classifier, extension, defaultExtension ) );
185 // Sanity Check: does it have an extension?
186 if ( StringUtils.isEmpty( artifact.getType() ) )
188 throw new LayoutException( INVALID_ARTIFACT_PATH + "no extension found." );
191 // Special Case with Maven Plugins
192 if ( StringUtils.equals( "jar", extension ) && StringUtils.equals( "plugins", expectedType ) )
194 artifact.setType( ArtifactExtensionMapping.MAVEN_ONE_PLUGIN );
198 // Sanity Check: does extension match pathType on path?
199 String expectedExtension = ArtifactExtensionMapping.getExtension( artifact.getType() );
201 if ( !expectedExtension.equals( extension ) )
203 throw new LayoutException( INVALID_ARTIFACT_PATH + "mismatch on extension [" + extension
204 + "] and layout specified type [" + artifact.getType() + "] (which maps to extension: ["
205 + expectedExtension + "]) on path [" + path + "]" );