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 java.util.Collection;
23 import java.util.Iterator;
24 import java.util.Properties;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
28 import org.apache.maven.archiva.configuration.LegacyArtifactPath;
29 import org.apache.maven.archiva.model.ArtifactReference;
30 import org.apache.maven.archiva.repository.layout.LayoutException;
33 * LegacyPathParser is a parser for maven 1 (legacy layout) paths to
36 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
38 * @plexus.component role="org.apache.maven.archiva.repository.content.PathParser"
41 public class LegacyPathParser
44 private static final String INVALID_ARTIFACT_PATH = "Invalid path to Artifact: ";
49 protected ArchivaConfiguration configuration;
54 * @see org.apache.maven.archiva.repository.content.PathParser#toArtifactReference(java.lang.String)
56 public ArtifactReference toArtifactReference( String path )
57 throws LayoutException
59 ArtifactReference artifact = new ArtifactReference();
61 // First, look if a custom resolution rule has been set for this artifact
62 Collection legacy = configuration.getConfiguration().getLegacyArtifactPaths();
63 for ( Iterator iterator = legacy.iterator(); iterator.hasNext(); )
65 LegacyArtifactPath legacyPath = (LegacyArtifactPath) iterator.next();
66 if ( legacyPath.match( path ) )
68 artifact.setGroupId( legacyPath.getGroupId() );
69 artifact.setArtifactId( legacyPath.getArtifactId() );
70 artifact.setClassifier( legacyPath.getClassifier() );
71 artifact.setVersion( legacyPath.getVersion() );
72 artifact.setType( legacyPath.getType() );
77 String normalizedPath = StringUtils.replace( path, "\\", "/" );
79 String pathParts[] = StringUtils.split( normalizedPath, '/' );
81 /* Always 3 parts. (Never more or less)
83 * path = "commons-lang/jars/commons-lang-2.1.jar"
84 * path[0] = "commons-lang"; // The Group ID
85 * path[1] = "jars"; // The Directory Type
86 * path[2] = "commons-lang-2.1.jar"; // The Filename.
89 if ( pathParts.length != 3 )
91 // Illegal Path Parts Length.
92 throw new LayoutException( INVALID_ARTIFACT_PATH
93 + "legacy paths should only have 3 parts [groupId]/[type]s/[artifactId]-[version].[type], found "
94 + pathParts.length + " instead." );
98 artifact.setGroupId( pathParts[0] );
100 // The Expected Type.
101 String expectedType = pathParts[1];
103 // Sanity Check: expectedType should end in "s".
104 if ( !expectedType.endsWith( "s" ) )
106 throw new LayoutException( INVALID_ARTIFACT_PATH
107 + "legacy paths should have an expected type ending in [s] in the second part of the path." );
111 String filename = pathParts[2];
113 FilenameParser parser = new FilenameParser( filename );
115 artifact.setArtifactId( parser.nextNonVersion() );
117 // Sanity Check: does it have an artifact id?
118 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
120 // Special Case: The filename might start with a version id (like "test-arch-1.0.jar").
121 int idx = filename.indexOf( '-' );
125 // Take the first section regardless of content.
126 String artifactId = parser.next();
128 // Is there anything more that is considered not a version id?
129 String moreArtifactId = parser.nextNonVersion();
130 if ( StringUtils.isNotBlank( moreArtifactId ) )
132 artifact.setArtifactId( artifactId + "-" + moreArtifactId );
136 artifact.setArtifactId( artifactId );
140 // Sanity Check: still no artifact id?
141 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
143 throw new LayoutException( INVALID_ARTIFACT_PATH + "no artifact id present." );
147 artifact.setVersion( parser.remaining() );
149 // Sanity Check: does it have a version?
150 if ( StringUtils.isEmpty( artifact.getVersion() ) )
152 // Special Case: use last section of artifactId as version.
153 String artifactId = artifact.getArtifactId();
154 int idx = artifactId.lastIndexOf( '-' );
157 artifact.setVersion( artifactId.substring( idx + 1 ) );
158 artifact.setArtifactId( artifactId.substring( 0, idx ) );
162 throw new LayoutException( INVALID_ARTIFACT_PATH + "no version found." );
167 artifact.setType( ArtifactExtensionMapping.guessTypeFromFilename( filename ) );
169 // Sanity Check: does it have an extension?
170 if ( StringUtils.isEmpty( artifact.getType() ) )
172 throw new LayoutException( INVALID_ARTIFACT_PATH + "no extension found." );
175 // Special Case with Maven Plugins
176 if ( StringUtils.equals( "jar", artifact.getType() ) && StringUtils.equals( "plugins", expectedType ) )
178 artifact.setType( ArtifactExtensionMapping.MAVEN_PLUGIN );
182 // Sanity Check: does extension match pathType on path?
183 String trimPathType = expectedType.substring( 0, expectedType.length() - 1 );
185 String expectedExtension = ArtifactExtensionMapping.getExtension( trimPathType );
186 String actualExtension = parser.getExtension();
188 if ( !expectedExtension.equals( actualExtension ) )
190 throw new LayoutException( INVALID_ARTIFACT_PATH + "mismatch on extension [" + actualExtension
191 + "] and layout specified type [" + expectedType + "] (which maps to extension: ["
192 + expectedExtension + "]) on path [" + path + "]" );
196 String classifier = ArtifactClassifierMapping.getClassifier( artifact.getType() );
197 if ( classifier != null )
199 String version = artifact.getVersion();
200 if ( ! version.endsWith( "-" + classifier ) )
202 throw new LayoutException( INVALID_ARTIFACT_PATH + expectedType + " artifacts must use the classifier " + classifier );
204 version = version.substring( 0, version.length() - classifier.length() - 1 );
205 artifact.setVersion( version );
206 artifact.setClassifier( classifier );