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.common.utils.VersionUtil;
24 import org.apache.maven.archiva.model.ArtifactReference;
25 import org.apache.maven.archiva.repository.layout.LayoutException;
28 * DefaultPathParser is a parser for maven 2 (default layout) paths to ArtifactReference.
32 public class DefaultPathParser implements PathParser
34 private static final String INVALID_ARTIFACT_PATH = "Invalid path to Artifact: ";
38 * @see org.apache.maven.archiva.repository.content.PathParser#toArtifactReference(java.lang.String)
40 public ArtifactReference toArtifactReference( String path )
41 throws LayoutException
43 if ( StringUtils.isBlank( path ) )
45 throw new LayoutException( "Unable to convert blank path." );
48 ArtifactReference artifact = new ArtifactReference();
50 String normalizedPath = StringUtils.replace( path, "\\", "/" );
51 String pathParts[] = StringUtils.split( normalizedPath, '/' );
55 * path = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar"
56 * path[0] = "commons-lang"; // The Group ID
57 * path[1] = "commons-lang"; // The Artifact ID
58 * path[2] = "2.1"; // The Version
59 * path[3] = "commons-lang-2.1.jar" // The filename.
62 if ( pathParts.length < 4 )
64 // Illegal Path Parts Length.
65 throw new LayoutException( "Not enough parts to the path [" + path
66 + "] to construct an ArchivaArtifact from. (Requires at least 4 parts)" );
70 int partCount = pathParts.length;
71 int filenamePos = partCount - 1;
72 int baseVersionPos = partCount - 2;
73 int artifactIdPos = partCount - 3;
74 int groupIdPos = partCount - 4;
76 // Second to last is the baseVersion (the directory version)
77 String baseVersion = pathParts[baseVersionPos];
79 // Third to last is the artifact Id.
80 artifact.setArtifactId( pathParts[artifactIdPos] );
82 // Remaining pieces are the groupId.
83 for ( int i = 0; i <= groupIdPos; i++ )
87 artifact.setGroupId( pathParts[i] );
91 artifact.setGroupId( artifact.getGroupId() + "." + pathParts[i] );
97 // Last part is the filename
98 String filename = pathParts[filenamePos];
100 // Now we need to parse the filename to get the artifact version Id.
101 if ( StringUtils.isBlank( filename ) )
103 throw new IllegalArgumentException( INVALID_ARTIFACT_PATH + "Unable to split blank filename." );
106 FilenameParser parser = new FilenameParser( filename );
108 // Expect the filename to start with the artifactId.
109 artifact.setArtifactId( parser.expect( artifact.getArtifactId() ) );
111 if ( artifact.getArtifactId() == null )
113 throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid, "
114 + "should start with artifactId as stated in path." );
117 // Process the version.
118 artifact.setVersion( parser.expect( baseVersion ) );
120 if ( artifact.getVersion() == null )
122 // We working with a snapshot?
123 if ( VersionUtil.isSnapshot( baseVersion ) )
125 artifact.setVersion( parser.nextVersion() );
126 if ( !VersionUtil.isUniqueSnapshot( artifact.getVersion() ) )
128 throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid,"
129 + "expected timestamp format in filename." );
134 throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid, "
135 + "expected version as stated in path." );
139 // Do we have a classifier?
140 switch(parser.seperator())
143 // Definately a classifier.
144 artifact.setClassifier( parser.remaining() );
147 artifact.setType( ArtifactExtensionMapping.mapExtensionAndClassifierToType( artifact.getClassifier(), parser.getExtension() ) );
150 // We have an dual extension possibility.
151 String extension = parser.remaining() + '.' + parser.getExtension();
152 artifact.setType( extension );
155 // End of the filename, only a simple extension left. - Set the type.
156 String type = ArtifactExtensionMapping.mapExtensionToType( parser.getExtension() );
159 throw new LayoutException( "Invalid artifact: no type was specified" );
161 artifact.setType( type );
165 // Special case for maven plugins
166 if ( StringUtils.equals( "jar", artifact.getType() ) &&
167 ArtifactExtensionMapping.isMavenPlugin( artifact.getArtifactId() ) )
169 artifact.setType( ArtifactExtensionMapping.MAVEN_PLUGIN );
172 catch ( LayoutException e )
179 // Do we have a snapshot version?
180 if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
182 // Rules are different for SNAPSHOTS
183 if ( !VersionUtil.isGenericSnapshot( baseVersion ) )
185 String filenameBaseVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
186 throw new LayoutException( "Invalid snapshot artifact location, version directory should be "
187 + filenameBaseVersion );
192 // Non SNAPSHOT rules.
193 // Do we pass the simple test?
194 if ( !StringUtils.equals( baseVersion, artifact.getVersion() ) )
196 throw new LayoutException( "Invalid artifact: version declared in directory path does"
197 + " not match what was found in the artifact filename." );