1 | |
package org.apache.maven.archiva.repository.content; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.util.Collection; |
23 | |
|
24 | |
import org.apache.commons.lang.StringUtils; |
25 | |
import org.apache.maven.archiva.configuration.ArchivaConfiguration; |
26 | |
import org.apache.maven.archiva.configuration.LegacyArtifactPath; |
27 | |
import org.apache.maven.archiva.model.ArtifactReference; |
28 | |
import org.apache.maven.archiva.repository.layout.LayoutException; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 0 | public class LegacyPathParser |
39 | |
implements PathParser |
40 | |
{ |
41 | |
private static final String INVALID_ARTIFACT_PATH = "Invalid path to Artifact: "; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
protected ArchivaConfiguration configuration; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public ArtifactReference toArtifactReference( String path ) |
55 | |
throws LayoutException |
56 | |
{ |
57 | 0 | ArtifactReference artifact = new ArtifactReference(); |
58 | |
|
59 | |
|
60 | 0 | Collection<LegacyArtifactPath> legacy = configuration.getConfiguration().getLegacyArtifactPaths(); |
61 | 0 | for ( LegacyArtifactPath legacyPath : legacy ) |
62 | |
{ |
63 | 0 | if ( legacyPath.match( path ) ) |
64 | |
{ |
65 | 0 | artifact.setGroupId( legacyPath.getGroupId() ); |
66 | 0 | artifact.setArtifactId( legacyPath.getArtifactId() ); |
67 | 0 | artifact.setClassifier( legacyPath.getClassifier() ); |
68 | 0 | artifact.setVersion( legacyPath.getVersion() ); |
69 | 0 | artifact.setType( legacyPath.getType() ); |
70 | 0 | return artifact; |
71 | |
} |
72 | |
} |
73 | |
|
74 | 0 | String normalizedPath = StringUtils.replace( path, "\\", "/" ); |
75 | |
|
76 | 0 | String pathParts[] = StringUtils.split( normalizedPath, '/' ); |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | 0 | if ( pathParts.length != 3 ) |
87 | |
{ |
88 | |
|
89 | 0 | throw new LayoutException( INVALID_ARTIFACT_PATH |
90 | |
+ "legacy paths should only have 3 parts [groupId]/[type]s/[artifactId]-[version].[type], found " |
91 | |
+ pathParts.length + " instead." ); |
92 | |
} |
93 | |
|
94 | |
|
95 | 0 | artifact.setGroupId( pathParts[0] ); |
96 | |
|
97 | |
|
98 | 0 | String expectedType = pathParts[1]; |
99 | |
|
100 | |
|
101 | 0 | if ( !expectedType.endsWith( "s" ) ) |
102 | |
{ |
103 | 0 | throw new LayoutException( INVALID_ARTIFACT_PATH |
104 | |
+ "legacy paths should have an expected type ending in [s] in the second part of the path." ); |
105 | |
} |
106 | |
|
107 | |
|
108 | 0 | String filename = pathParts[2]; |
109 | |
|
110 | 0 | FilenameParser parser = new FilenameParser( filename ); |
111 | |
|
112 | 0 | artifact.setArtifactId( parser.nextNonVersion() ); |
113 | |
|
114 | |
|
115 | 0 | if ( StringUtils.isEmpty( artifact.getArtifactId() ) ) |
116 | |
{ |
117 | |
|
118 | 0 | int idx = filename.indexOf( '-' ); |
119 | 0 | if ( idx > 0 ) |
120 | |
{ |
121 | 0 | parser.reset(); |
122 | |
|
123 | 0 | String artifactId = parser.next(); |
124 | |
|
125 | |
|
126 | 0 | String moreArtifactId = parser.nextNonVersion(); |
127 | 0 | if ( StringUtils.isNotBlank( moreArtifactId ) ) |
128 | |
{ |
129 | 0 | artifact.setArtifactId( artifactId + "-" + moreArtifactId ); |
130 | |
} |
131 | |
else |
132 | |
{ |
133 | 0 | artifact.setArtifactId( artifactId ); |
134 | |
} |
135 | |
} |
136 | |
|
137 | |
|
138 | 0 | if ( StringUtils.isEmpty( artifact.getArtifactId() ) ) |
139 | |
{ |
140 | 0 | throw new LayoutException( INVALID_ARTIFACT_PATH + "no artifact id present." ); |
141 | |
} |
142 | |
} |
143 | |
|
144 | 0 | artifact.setVersion( parser.remaining() ); |
145 | |
|
146 | |
|
147 | 0 | if ( StringUtils.isEmpty( artifact.getVersion() ) ) |
148 | |
{ |
149 | |
|
150 | 0 | String artifactId = artifact.getArtifactId(); |
151 | 0 | int idx = artifactId.lastIndexOf( '-' ); |
152 | 0 | if ( idx > 0 ) |
153 | |
{ |
154 | 0 | artifact.setVersion( artifactId.substring( idx + 1 ) ); |
155 | 0 | artifact.setArtifactId( artifactId.substring( 0, idx ) ); |
156 | |
} |
157 | |
else |
158 | |
{ |
159 | 0 | throw new LayoutException( INVALID_ARTIFACT_PATH + "no version found." ); |
160 | |
} |
161 | |
} |
162 | |
|
163 | 0 | String classifier = ArtifactClassifierMapping.getClassifier( expectedType ); |
164 | 0 | if ( classifier != null ) |
165 | |
{ |
166 | 0 | String version = artifact.getVersion(); |
167 | 0 | if ( ! version.endsWith( "-" + classifier ) ) |
168 | |
{ |
169 | 0 | throw new LayoutException( INVALID_ARTIFACT_PATH + expectedType + " artifacts must use the classifier " + classifier ); |
170 | |
} |
171 | 0 | version = version.substring( 0, version.length() - classifier.length() - 1 ); |
172 | 0 | artifact.setVersion( version ); |
173 | 0 | artifact.setClassifier( classifier ); |
174 | |
} |
175 | |
|
176 | 0 | String extension = parser.getExtension(); |
177 | |
|
178 | |
|
179 | 0 | String defaultExtension = expectedType.substring( 0, expectedType.length() - 1 ); |
180 | 0 | artifact.setType( |
181 | |
ArtifactExtensionMapping.mapExtensionAndClassifierToType( classifier, extension, defaultExtension ) ); |
182 | |
|
183 | |
|
184 | 0 | if ( StringUtils.isEmpty( artifact.getType() ) ) |
185 | |
{ |
186 | 0 | throw new LayoutException( INVALID_ARTIFACT_PATH + "no extension found." ); |
187 | |
} |
188 | |
|
189 | |
|
190 | 0 | if ( StringUtils.equals( "jar", extension ) && StringUtils.equals( "plugins", expectedType ) ) |
191 | |
{ |
192 | 0 | artifact.setType( ArtifactExtensionMapping.MAVEN_ONE_PLUGIN ); |
193 | |
} |
194 | |
else |
195 | |
{ |
196 | |
|
197 | 0 | String expectedExtension = ArtifactExtensionMapping.getExtension( artifact.getType() ); |
198 | |
|
199 | 0 | if ( !expectedExtension.equals( extension ) ) |
200 | |
{ |
201 | 0 | throw new LayoutException( INVALID_ARTIFACT_PATH + "mismatch on extension [" + extension |
202 | |
+ "] and layout specified type [" + artifact.getType() + "] (which maps to extension: [" |
203 | |
+ expectedExtension + "]) on path [" + path + "]" ); |
204 | |
} |
205 | |
} |
206 | |
|
207 | 0 | return artifact; |
208 | |
} |
209 | |
} |