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 // First, look if a custom resolution rule has been set for this artifact
60 Collection legacy = configuration.getConfiguration().getLegacyArtifactPaths();
61 for ( Iterator iterator = legacy.iterator(); iterator.hasNext(); )
63 LegacyArtifactPath legacyPath = (LegacyArtifactPath) iterator.next();
64 if ( legacyPath.match( path ) )
66 return legacyPath.getArtifactReference();
70 ArtifactReference artifact = new ArtifactReference();
72 String normalizedPath = StringUtils.replace( path, "\\", "/" );
74 String pathParts[] = StringUtils.split( normalizedPath, '/' );
76 /* Always 3 parts. (Never more or less)
78 * path = "commons-lang/jars/commons-lang-2.1.jar"
79 * path[0] = "commons-lang"; // The Group ID
80 * path[1] = "jars"; // The Directory Type
81 * path[2] = "commons-lang-2.1.jar"; // The Filename.
84 if ( pathParts.length != 3 )
86 // Illegal Path Parts Length.
87 throw new LayoutException( INVALID_ARTIFACT_PATH
88 + "legacy paths should only have 3 parts [groupId]/[type]s/[artifactId]-[version].[type], found "
89 + pathParts.length + " instead." );
93 artifact.setGroupId( pathParts[0] );
96 String expectedType = pathParts[1];
98 // Sanity Check: expectedType should end in "s".
99 if ( !expectedType.endsWith( "s" ) )
101 throw new LayoutException( INVALID_ARTIFACT_PATH
102 + "legacy paths should have an expected type ending in [s] in the second part of the path." );
106 String filename = pathParts[2];
108 FilenameParser parser = new FilenameParser( filename );
110 artifact.setArtifactId( parser.nextNonVersion() );
112 // Sanity Check: does it have an artifact id?
113 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
115 // Special Case: The filename might start with a version id (like "test-arch-1.0.jar").
116 int idx = filename.indexOf( '-' );
120 // Take the first section regardless of content.
121 String artifactId = parser.next();
123 // Is there anything more that is considered not a version id?
124 String moreArtifactId = parser.nextNonVersion();
125 if ( StringUtils.isNotBlank( moreArtifactId ) )
127 artifact.setArtifactId( artifactId + "-" + moreArtifactId );
131 artifact.setArtifactId( artifactId );
135 // Sanity Check: still no artifact id?
136 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
138 throw new LayoutException( INVALID_ARTIFACT_PATH + "no artifact id present." );
142 artifact.setVersion( parser.remaining() );
144 // Sanity Check: does it have a version?
145 if ( StringUtils.isEmpty( artifact.getVersion() ) )
147 // Special Case: use last section of artifactId as version.
148 String artifactId = artifact.getArtifactId();
149 int idx = artifactId.lastIndexOf( '-' );
152 artifact.setVersion( artifactId.substring( idx + 1 ) );
153 artifact.setArtifactId( artifactId.substring( 0, idx ) );
157 throw new LayoutException( INVALID_ARTIFACT_PATH + "no version found." );
162 artifact.setType( ArtifactExtensionMapping.guessTypeFromFilename( filename ) );
164 // Sanity Check: does it have an extension?
165 if ( StringUtils.isEmpty( artifact.getType() ) )
167 throw new LayoutException( INVALID_ARTIFACT_PATH + "no extension found." );
170 // Special Case with Maven Plugins
171 if ( StringUtils.equals( "jar", artifact.getType() ) && StringUtils.equals( "plugins", expectedType ) )
173 artifact.setType( ArtifactExtensionMapping.MAVEN_PLUGIN );
177 // Sanity Check: does extension match pathType on path?
178 String trimPathType = expectedType.substring( 0, expectedType.length() - 1 );
180 String expectedExtension = ArtifactExtensionMapping.getExtension( trimPathType );
181 String actualExtension = parser.getExtension();
183 if ( !expectedExtension.equals( actualExtension ) )
185 throw new LayoutException( INVALID_ARTIFACT_PATH + "mismatch on extension [" + actualExtension
186 + "] and layout specified type [" + expectedType + "] (which maps to extension: ["
187 + expectedExtension + "]) on path [" + path + "]" );
191 String classifier = ArtifactClassifierMapping.getClassifier( artifact.getType() );
192 if ( classifier != null )
194 String version = artifact.getVersion();
195 if ( ! version.endsWith( "-" + classifier ) )
197 throw new LayoutException( INVALID_ARTIFACT_PATH + expectedType + " artifacts must use the classifier " + classifier );
199 version = version.substring( 0, version.length() - classifier.length() - 1 );
200 artifact.setVersion( version );
201 artifact.setClassifier( classifier );