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 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; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | 0 | public class DefaultPathParser implements PathParser |
33 | |
{ |
34 | |
private static final String INVALID_ARTIFACT_PATH = "Invalid path to Artifact: "; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public ArtifactReference toArtifactReference( String path ) |
41 | |
throws LayoutException |
42 | |
{ |
43 | 0 | if ( StringUtils.isBlank( path ) ) |
44 | |
{ |
45 | 0 | throw new LayoutException( "Unable to convert blank path." ); |
46 | |
} |
47 | |
|
48 | 0 | ArtifactReference artifact = new ArtifactReference(); |
49 | |
|
50 | 0 | String normalizedPath = StringUtils.replace( path, "\\", "/" ); |
51 | 0 | String pathParts[] = StringUtils.split( normalizedPath, '/' ); |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | 0 | if ( pathParts.length < 4 ) |
63 | |
{ |
64 | |
|
65 | 0 | throw new LayoutException( "Not enough parts to the path [" + path |
66 | |
+ "] to construct an ArchivaArtifact from. (Requires at least 4 parts)" ); |
67 | |
} |
68 | |
|
69 | |
|
70 | 0 | int partCount = pathParts.length; |
71 | 0 | int filenamePos = partCount - 1; |
72 | 0 | int baseVersionPos = partCount - 2; |
73 | 0 | int artifactIdPos = partCount - 3; |
74 | 0 | int groupIdPos = partCount - 4; |
75 | |
|
76 | |
|
77 | 0 | String baseVersion = pathParts[baseVersionPos]; |
78 | |
|
79 | |
|
80 | 0 | artifact.setArtifactId( pathParts[artifactIdPos] ); |
81 | |
|
82 | |
|
83 | 0 | for ( int i = 0; i <= groupIdPos; i++ ) |
84 | |
{ |
85 | 0 | if ( i == 0 ) |
86 | |
{ |
87 | 0 | artifact.setGroupId( pathParts[i] ); |
88 | |
} |
89 | |
else |
90 | |
{ |
91 | 0 | artifact.setGroupId( artifact.getGroupId() + "." + pathParts[i] ); |
92 | |
} |
93 | |
} |
94 | |
|
95 | |
try |
96 | |
{ |
97 | |
|
98 | 0 | String filename = pathParts[filenamePos]; |
99 | |
|
100 | |
|
101 | 0 | if ( StringUtils.isBlank( filename ) ) |
102 | |
{ |
103 | 0 | throw new IllegalArgumentException( INVALID_ARTIFACT_PATH + "Unable to split blank filename." ); |
104 | |
} |
105 | |
|
106 | 0 | FilenameParser parser = new FilenameParser( filename ); |
107 | |
|
108 | |
|
109 | 0 | artifact.setArtifactId( parser.expect( artifact.getArtifactId() ) ); |
110 | |
|
111 | 0 | if ( artifact.getArtifactId() == null ) |
112 | |
{ |
113 | 0 | throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid, " |
114 | |
+ "should start with artifactId as stated in path." ); |
115 | |
} |
116 | |
|
117 | |
|
118 | 0 | artifact.setVersion( parser.expect( baseVersion ) ); |
119 | |
|
120 | 0 | if ( artifact.getVersion() == null ) |
121 | |
{ |
122 | |
|
123 | 0 | if ( VersionUtil.isSnapshot( baseVersion ) ) |
124 | |
{ |
125 | 0 | artifact.setVersion( parser.nextVersion() ); |
126 | 0 | if ( !VersionUtil.isUniqueSnapshot( artifact.getVersion() ) ) |
127 | |
{ |
128 | 0 | throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid," |
129 | |
+ "expected timestamp format in filename." ); |
130 | |
} |
131 | |
} |
132 | |
else |
133 | |
{ |
134 | 0 | throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid, " |
135 | |
+ "expected version as stated in path." ); |
136 | |
} |
137 | |
} |
138 | |
|
139 | |
|
140 | 0 | switch(parser.seperator()) |
141 | |
{ |
142 | |
case '-': |
143 | |
|
144 | 0 | artifact.setClassifier( parser.remaining() ); |
145 | |
|
146 | |
|
147 | 0 | artifact.setType( ArtifactExtensionMapping.mapExtensionAndClassifierToType( artifact.getClassifier(), parser.getExtension() ) ); |
148 | 0 | break; |
149 | |
case '.': |
150 | |
|
151 | 0 | String extension = parser.remaining() + '.' + parser.getExtension(); |
152 | 0 | artifact.setType( extension ); |
153 | 0 | break; |
154 | |
case 0: |
155 | |
|
156 | 0 | String type = ArtifactExtensionMapping.mapExtensionToType( parser.getExtension() ); |
157 | 0 | if ( type == null ) |
158 | |
{ |
159 | 0 | throw new LayoutException( "Invalid artifact: no type was specified" ); |
160 | |
} |
161 | 0 | artifact.setType( type ); |
162 | |
break; |
163 | |
} |
164 | |
|
165 | |
|
166 | 0 | if ( StringUtils.equals( "jar", artifact.getType() ) && |
167 | |
ArtifactExtensionMapping.isMavenPlugin( artifact.getArtifactId() ) ) |
168 | |
{ |
169 | 0 | artifact.setType( ArtifactExtensionMapping.MAVEN_PLUGIN ); |
170 | |
} |
171 | |
} |
172 | 0 | catch ( LayoutException e ) |
173 | |
{ |
174 | 0 | throw e; |
175 | 0 | } |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | 0 | if ( VersionUtil.isSnapshot( artifact.getVersion() ) ) |
181 | |
{ |
182 | |
|
183 | 0 | if ( !VersionUtil.isGenericSnapshot( baseVersion ) ) |
184 | |
{ |
185 | 0 | String filenameBaseVersion = VersionUtil.getBaseVersion( artifact.getVersion() ); |
186 | 0 | throw new LayoutException( "Invalid snapshot artifact location, version directory should be " |
187 | |
+ filenameBaseVersion ); |
188 | |
} |
189 | |
} |
190 | |
else |
191 | |
{ |
192 | |
|
193 | |
|
194 | 0 | if ( !StringUtils.equals( baseVersion, artifact.getVersion() ) ) |
195 | |
{ |
196 | 0 | throw new LayoutException( "Invalid artifact: version declared in directory path does" |
197 | |
+ " not match what was found in the artifact filename." ); |
198 | |
} |
199 | |
} |
200 | |
|
201 | 0 | return artifact; |
202 | |
} |
203 | |
|
204 | |
} |