1 package org.apache.continuum.purge.repository.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
31
32
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
49
50
51
52
53
54
55
56 if ( pathParts.length != 3 )
57 {
58
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
65 artifact.setGroupId( pathParts[0] );
66
67
68 String expectedType = pathParts[1];
69
70
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
78 String filename = pathParts[2];
79
80 FilenameParser parser = new FilenameParser( filename );
81
82 artifact.setArtifactId( parser.nextNonVersion() );
83
84
85 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
86 {
87
88 int idx = filename.indexOf( '-' );
89 if ( idx > 0 )
90 {
91 parser.reset();
92
93 String artifactId = parser.next();
94
95
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
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
117 if ( StringUtils.isEmpty( artifact.getVersion() ) )
118 {
119
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
150 String defaultExtension = expectedType.substring( 0, expectedType.length() - 1 );
151 artifact.setType( ArtifactExtensionMapping.mapExtensionAndClassifierToType( classifier, extension,
152 defaultExtension ) );
153
154
155 if ( StringUtils.isEmpty( artifact.getType() ) )
156 {
157 throw new LayoutException( INVALID_ARTIFACT_PATH + "no extension found." );
158 }
159
160
161 if ( StringUtils.equals( "jar", extension ) && StringUtils.equals( "plugins", expectedType ) )
162 {
163 artifact.setType( ArtifactExtensionMapping.MAVEN_ONE_PLUGIN );
164 }
165 else
166 {
167
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 }