View Javadoc

1   package org.apache.continuum.purge.repository.utils;
2   
3   /*
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
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.maven.archiva.model.ArtifactReference;
24  import org.apache.maven.archiva.repository.content.ArtifactClassifierMapping;
25  import org.apache.maven.archiva.repository.content.ArtifactExtensionMapping;
26  import org.apache.maven.archiva.repository.content.PathParser;
27  import org.apache.maven.archiva.repository.layout.LayoutException;
28  
29  /**
30   * Codes were taken from Archiva's LegacyPathParser and made some few changes.
31   *
32   * @plexus.component role="org.apache.maven.archiva.repository.content.PathParser" role-hint="legacy-parser"
33   */
34  public class LegacyPathParser
35      implements PathParser
36  {
37      private static final String INVALID_ARTIFACT_PATH = "Invalid path to Artifact: ";
38  
39      public ArtifactReference toArtifactReference( String path )
40          throws LayoutException
41      {
42          ArtifactReference artifact = new ArtifactReference();
43  
44          String normalizedPath = StringUtils.replace( path, "\\", "/" );
45  
46          String pathParts[] = StringUtils.split( normalizedPath, '/' );
47  
48          /* Always 3 parts. (Never more or less)
49           * 
50           *   path = "commons-lang/jars/commons-lang-2.1.jar"
51           *   path[0] = "commons-lang";          // The Group ID
52           *   path[1] = "jars";                  // The Directory Type
53           *   path[2] = "commons-lang-2.1.jar";  // The Filename.
54           */
55  
56          if ( pathParts.length != 3 )
57          {
58              // Illegal Path Parts Length.
59              throw new LayoutException( INVALID_ARTIFACT_PATH +
60                                             "legacy paths should only have 3 parts [groupId]/[type]s/[artifactId]-[version].[type], found " +
61                                             pathParts.length + " instead." );
62          }
63  
64          // The Group ID.
65          artifact.setGroupId( pathParts[0] );
66  
67          // The Expected Type.
68          String expectedType = pathParts[1];
69  
70          // Sanity Check: expectedType should end in "s".
71          if ( !expectedType.endsWith( "s" ) )
72          {
73              throw new LayoutException( INVALID_ARTIFACT_PATH +
74                                             "legacy paths should have an expected type ending in [s] in the second part of the path." );
75          }
76  
77          // The Filename.
78          String filename = pathParts[2];
79  
80          FilenameParser parser = new FilenameParser( filename );
81  
82          artifact.setArtifactId( parser.nextNonVersion() );
83  
84          // Sanity Check: does it have an artifact id?
85          if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
86          {
87              // Special Case: The filename might start with a version id (like "test-arch-1.0.jar").
88              int idx = filename.indexOf( '-' );
89              if ( idx > 0 )
90              {
91                  parser.reset();
92                  // Take the first section regardless of content.
93                  String artifactId = parser.next();
94  
95                  // Is there anything more that is considered not a version id?
96                  String moreArtifactId = parser.nextNonVersion();
97                  if ( StringUtils.isNotBlank( moreArtifactId ) )
98                  {
99                      artifact.setArtifactId( artifactId + "-" + moreArtifactId );
100                 }
101                 else
102                 {
103                     artifact.setArtifactId( artifactId );
104                 }
105             }
106 
107             // Sanity Check: still no artifact id?
108             if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
109             {
110                 throw new LayoutException( INVALID_ARTIFACT_PATH + "no artifact id present." );
111             }
112         }
113 
114         artifact.setVersion( parser.remaining() );
115 
116         // Sanity Check: does it have a version?
117         if ( StringUtils.isEmpty( artifact.getVersion() ) )
118         {
119             // Special Case: use last section of artifactId as version.
120             String artifactId = artifact.getArtifactId();
121             int idx = artifactId.lastIndexOf( '-' );
122             if ( idx > 0 )
123             {
124                 artifact.setVersion( artifactId.substring( idx + 1 ) );
125                 artifact.setArtifactId( artifactId.substring( 0, idx ) );
126             }
127             else
128             {
129                 throw new LayoutException( INVALID_ARTIFACT_PATH + "no version found." );
130             }
131         }
132 
133         String classifier = ArtifactClassifierMapping.getClassifier( expectedType );
134         if ( classifier != null )
135         {
136             String version = artifact.getVersion();
137             if ( !version.endsWith( "-" + classifier ) )
138             {
139                 throw new LayoutException(
140                     INVALID_ARTIFACT_PATH + expectedType + " artifacts must use the classifier " + classifier );
141             }
142             version = version.substring( 0, version.length() - classifier.length() - 1 );
143             artifact.setVersion( version );
144             artifact.setClassifier( classifier );
145         }
146 
147         String extension = parser.getExtension();
148 
149         // Set Type
150         String defaultExtension = expectedType.substring( 0, expectedType.length() - 1 );
151         artifact.setType( ArtifactExtensionMapping.mapExtensionAndClassifierToType( classifier, extension,
152                                                                                     defaultExtension ) );
153 
154         // Sanity Check: does it have an extension?
155         if ( StringUtils.isEmpty( artifact.getType() ) )
156         {
157             throw new LayoutException( INVALID_ARTIFACT_PATH + "no extension found." );
158         }
159 
160         // Special Case with Maven Plugins
161         if ( StringUtils.equals( "jar", extension ) && StringUtils.equals( "plugins", expectedType ) )
162         {
163             artifact.setType( ArtifactExtensionMapping.MAVEN_ONE_PLUGIN );
164         }
165         else
166         {
167             // Sanity Check: does extension match pathType on path?
168             String expectedExtension = ArtifactExtensionMapping.getExtension( artifact.getType() );
169 
170             if ( !expectedExtension.equals( extension ) )
171             {
172                 throw new LayoutException( INVALID_ARTIFACT_PATH + "mismatch on extension [" + extension +
173                                                "] and layout specified type [" + artifact.getType() +
174                                                "] (which maps to extension: [" +
175                                                expectedExtension + "]) on path [" + path + "]" );
176             }
177         }
178 
179         return artifact;
180     }
181 }