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.model.ArtifactReference;
24 import org.apache.maven.archiva.repository.layout.LayoutException;
27 * LegacyPathParser is a parser for maven 1 (legacy layout) paths to ArtifactReference.
29 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
32 public class LegacyPathParser
34 private static final String INVALID_ARTIFACT_PATH = "Invalid path to Artifact: ";
36 protected static ArtifactReference toArtifactReference( String path )
37 throws LayoutException
39 ArtifactReference artifact = new ArtifactReference();
41 String normalizedPath = StringUtils.replace( path, "\\", "/" );
43 String pathParts[] = StringUtils.split( normalizedPath, '/' );
45 /* Always 3 parts. (Never more or less)
47 * path = "commons-lang/jars/commons-lang-2.1.jar"
48 * path[0] = "commons-lang"; // The Group ID
49 * path[1] = "jars"; // The Directory Type
50 * path[2] = "commons-lang-2.1.jar"; // The Filename.
53 if ( pathParts.length != 3 )
55 // Illegal Path Parts Length.
56 throw new LayoutException( INVALID_ARTIFACT_PATH
57 + "legacy paths should only have 3 parts [groupId]/[type]s/[artifactId]-[version].[type], found "
58 + pathParts.length + " instead." );
62 artifact.setGroupId( pathParts[0] );
65 String expectedType = pathParts[1];
67 // Sanity Check: expectedType should end in "s".
68 if ( !expectedType.endsWith( "s" ) )
70 throw new LayoutException( INVALID_ARTIFACT_PATH
71 + "legacy paths should have an expected type ending in [s] in the second part of the path." );
75 String filename = pathParts[2];
77 FilenameParser parser = new FilenameParser( filename );
79 artifact.setArtifactId( parser.nextNonVersion() );
81 // Sanity Check: does it have an artifact id?
82 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
84 // Special Case: The filename might start with a version id (like "test-arch-1.0.jar").
85 int idx = filename.indexOf( '-' );
89 // Take the first section regardless of content.
90 String artifactId = parser.next();
92 // Is there anything more that is considered not a version id?
93 String moreArtifactId = parser.nextNonVersion();
94 if ( StringUtils.isNotBlank( moreArtifactId ) )
96 artifact.setArtifactId( artifactId + "-" + moreArtifactId );
100 artifact.setArtifactId( artifactId );
104 // Sanity Check: still no artifact id?
105 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
107 throw new LayoutException( INVALID_ARTIFACT_PATH + "no artifact id present." );
111 artifact.setVersion( parser.remaining() );
113 // Sanity Check: does it have a version?
114 if ( StringUtils.isEmpty( artifact.getVersion() ) )
116 // Special Case: use last section of artifactId as version.
117 String artifactId = artifact.getArtifactId();
118 int idx = artifactId.lastIndexOf( '-' );
121 artifact.setVersion( artifactId.substring( idx + 1 ) );
122 artifact.setArtifactId( artifactId.substring( 0, idx ) );
126 throw new LayoutException( INVALID_ARTIFACT_PATH + "no version found." );
130 artifact.setType( ArtifactExtensionMapping.guessTypeFromFilename( filename ) );
132 // Sanity Check: does it have an extension?
133 if ( StringUtils.isEmpty( artifact.getType() ) )
135 throw new LayoutException( INVALID_ARTIFACT_PATH + "no extension found." );
138 String trimPathType = expectedType.substring( 0, expectedType.length() - 1 );
140 // Sanity Check: does extension match pathType on path?
141 String expectedExtension = ArtifactExtensionMapping.getExtension( trimPathType );
142 String actualExtension = parser.getExtension();
144 if ( !expectedExtension.equals( actualExtension ) )
146 throw new LayoutException( INVALID_ARTIFACT_PATH + "mismatch on extension [" + actualExtension
147 + "] and layout specified type [" + expectedType + "] (which maps to extension: [" + expectedExtension
148 + "]) on path [" + path + "]" );