1 package org.apache.archiva.repository.content.legacy;
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.archiva.configuration.ArchivaConfiguration;
23 import org.apache.archiva.configuration.LegacyArtifactPath;
24 import org.apache.archiva.model.ArtifactReference;
25 import org.apache.archiva.repository.content.ArtifactClassifierMapping;
26 import org.apache.archiva.repository.content.PathParser;
27 import org.apache.archiva.repository.content.maven2.ArtifactExtensionMapping;
28 import org.apache.archiva.repository.content.maven2.FilenameParser;
29 import org.apache.archiva.repository.layout.LayoutException;
30 import org.apache.commons.lang.StringUtils;
32 import java.util.Collection;
35 * LegacyPathParser is a parser for maven 1 (legacy layout) paths to
40 public class LegacyPathParser
43 private static final String INVALID_ARTIFACT_PATH = "Invalid path to Artifact: ";
45 protected ArchivaConfiguration configuration;
47 public LegacyPathParser( ArchivaConfiguration configuration )
49 this.configuration = configuration;
56 * @see org.apache.archiva.repository.content.PathParser#toArtifactReference(String)
59 public ArtifactReference toArtifactReference( String path )
60 throws LayoutException
62 ArtifactReference artifact = new ArtifactReference();
64 // First, look if a custom resolution rule has been set for this artifact
65 Collection<LegacyArtifactPath> legacy = configuration.getConfiguration().getLegacyArtifactPaths();
66 for ( LegacyArtifactPath legacyPath : legacy )
68 if ( legacyPath.match( path ) )
70 artifact.setGroupId( legacyPath.getGroupId() );
71 artifact.setArtifactId( legacyPath.getArtifactId() );
72 artifact.setClassifier( legacyPath.getClassifier() );
73 artifact.setVersion( legacyPath.getVersion() );
74 artifact.setType( legacyPath.getType() );
79 String normalizedPath = StringUtils.replace( path, "\\", "/" );
81 String pathParts[] = StringUtils.split( normalizedPath, '/' );
83 /* Always 3 parts. (Never more or less)
85 * path = "commons-lang/jars/commons-lang-2.1.jar"
86 * path[0] = "commons-lang"; // The Group ID
87 * path[1] = "jars"; // The Directory Type
88 * path[2] = "commons-lang-2.1.jar"; // The Filename.
91 if ( pathParts.length != 3 )
93 // Illegal Path Parts Length.
94 throw new LayoutException( INVALID_ARTIFACT_PATH
95 + "legacy paths should only have 3 parts [groupId]/[type]s/[artifactId]-[version].[type], found "
96 + pathParts.length + " instead." );
100 artifact.setGroupId( pathParts[0] );
102 // The Expected Type.
103 String expectedType = pathParts[1];
105 // Sanity Check: expectedType should end in "s".
106 if ( !expectedType.endsWith( "s" ) )
108 throw new LayoutException( INVALID_ARTIFACT_PATH
109 + "legacy paths should have an expected type ending in [s] in the second part of the path." );
113 String filename = pathParts[2];
115 FilenameParser parser = new FilenameParser( filename );
117 artifact.setArtifactId( parser.nextNonVersion() );
119 // Sanity Check: does it have an artifact id?
120 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
122 // Special Case: The filename might start with a version id (like "test-arch-1.0.jar").
123 int idx = filename.indexOf( '-' );
127 // Take the first section regardless of content.
128 String artifactId = parser.next();
130 // Is there anything more that is considered not a version id?
131 String moreArtifactId = parser.nextNonVersion();
132 if ( StringUtils.isNotBlank( moreArtifactId ) )
134 artifact.setArtifactId( artifactId + "-" + moreArtifactId );
138 artifact.setArtifactId( artifactId );
142 // Sanity Check: still no artifact id?
143 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
145 throw new LayoutException( INVALID_ARTIFACT_PATH + "no artifact id present." );
149 artifact.setVersion( parser.remaining() );
151 // Sanity Check: does it have a version?
152 if ( StringUtils.isEmpty( artifact.getVersion() ) )
154 // Special Case: use last section of artifactId as version.
155 String artifactId = artifact.getArtifactId();
156 int idx = artifactId.lastIndexOf( '-' );
159 artifact.setVersion( artifactId.substring( idx + 1 ) );
160 artifact.setArtifactId( artifactId.substring( 0, idx ) );
164 throw new LayoutException( INVALID_ARTIFACT_PATH + "no version found." );
168 String classifier = ArtifactClassifierMapping.getClassifier( expectedType );
169 if ( classifier != null )
171 String version = artifact.getVersion();
172 if ( !version.endsWith( "-" + classifier ) )
174 throw new LayoutException(
175 INVALID_ARTIFACT_PATH + expectedType + " artifacts must use the classifier " + classifier );
177 version = version.substring( 0, version.length() - classifier.length() - 1 );
178 artifact.setVersion( version );
179 artifact.setClassifier( classifier );
182 String extension = parser.getExtension();
185 String defaultExtension = expectedType.substring( 0, expectedType.length() - 1 );
187 ArtifactExtensionMapping.mapExtensionAndClassifierToType( classifier, extension, defaultExtension ) );
189 // Sanity Check: does it have an extension?
190 if ( StringUtils.isEmpty( artifact.getType() ) )
192 throw new LayoutException( INVALID_ARTIFACT_PATH + "no extension found." );
195 // Special Case with Maven Plugins
196 if ( StringUtils.equals( "jar", extension ) && StringUtils.equals( "plugins", expectedType ) )
198 artifact.setType( ArtifactExtensionMapping.MAVEN_ONE_PLUGIN );
202 // Sanity Check: does extension match pathType on path?
203 String expectedExtension = ArtifactExtensionMapping.getExtension( artifact.getType() );
205 if ( !expectedExtension.equals( extension ) )
207 throw new LayoutException(
208 INVALID_ARTIFACT_PATH + "mismatch on extension [" + extension + "] and layout specified type ["
209 + artifact.getType() + "] (which maps to extension: [" + expectedExtension + "]) on path ["