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.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.util.HashSet; |
25 | |
import java.util.Set; |
26 | |
|
27 | |
import org.apache.commons.io.FileUtils; |
28 | |
import org.apache.maven.archiva.common.utils.PathUtil; |
29 | |
import org.apache.maven.archiva.configuration.FileTypes; |
30 | |
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; |
31 | |
import org.apache.maven.archiva.model.ArchivaArtifact; |
32 | |
import org.apache.maven.archiva.model.ArtifactReference; |
33 | |
import org.apache.maven.archiva.model.ProjectReference; |
34 | |
import org.apache.maven.archiva.model.VersionedReference; |
35 | |
import org.apache.maven.archiva.repository.ContentNotFoundException; |
36 | |
import org.apache.maven.archiva.repository.ManagedRepositoryContent; |
37 | |
import org.apache.maven.archiva.repository.layout.LayoutException; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 0 | public class ManagedDefaultRepositoryContent |
52 | |
extends AbstractDefaultRepositoryContent |
53 | |
implements ManagedRepositoryContent |
54 | |
{ |
55 | |
|
56 | |
|
57 | |
|
58 | |
private FileTypes filetypes; |
59 | |
|
60 | |
private ManagedRepositoryConfiguration repository; |
61 | |
|
62 | |
public void deleteVersion( VersionedReference reference ) |
63 | |
throws ContentNotFoundException |
64 | |
{ |
65 | 0 | String path = toMetadataPath( reference ); |
66 | 0 | File projectPath = new File( getRepoRoot(), path ); |
67 | |
|
68 | 0 | File projectDir = projectPath.getParentFile(); |
69 | 0 | if( projectDir.exists() && projectDir.isDirectory() ) |
70 | |
{ |
71 | |
try |
72 | |
{ |
73 | 0 | FileUtils.deleteDirectory( projectDir ); |
74 | |
} |
75 | 0 | catch ( IOException e ) |
76 | |
{ |
77 | |
|
78 | 0 | } |
79 | |
} |
80 | |
else |
81 | |
{ |
82 | 0 | throw new ContentNotFoundException( "Unable to delete non-existing project directory." ); |
83 | |
} |
84 | 0 | } |
85 | |
|
86 | |
public String getId() |
87 | |
{ |
88 | 0 | return repository.getId(); |
89 | |
} |
90 | |
|
91 | |
public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference ) |
92 | |
throws ContentNotFoundException |
93 | |
{ |
94 | 0 | File artifactFile = toFile( reference ); |
95 | 0 | File repoDir = artifactFile.getParentFile(); |
96 | |
|
97 | 0 | if ( !repoDir.exists() ) |
98 | |
{ |
99 | 0 | throw new ContentNotFoundException( "Unable to get related artifacts using a non-existant directory: " |
100 | |
+ repoDir.getAbsolutePath() ); |
101 | |
} |
102 | |
|
103 | 0 | if ( !repoDir.isDirectory() ) |
104 | |
{ |
105 | 0 | throw new ContentNotFoundException( "Unable to get related artifacts using a non-directory: " |
106 | |
+ repoDir.getAbsolutePath() ); |
107 | |
} |
108 | |
|
109 | 0 | Set<ArtifactReference> foundArtifacts = new HashSet<ArtifactReference>(); |
110 | |
|
111 | |
|
112 | 0 | File repoFiles[] = repoDir.listFiles(); |
113 | 0 | for ( int i = 0; i < repoFiles.length; i++ ) |
114 | |
{ |
115 | 0 | if ( repoFiles[i].isDirectory() ) |
116 | |
{ |
117 | |
|
118 | 0 | continue; |
119 | |
} |
120 | |
|
121 | 0 | String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] ); |
122 | |
|
123 | 0 | if ( filetypes.matchesArtifactPattern( relativePath ) ) |
124 | |
{ |
125 | |
try |
126 | |
{ |
127 | 0 | ArtifactReference artifact = toArtifactReference( relativePath ); |
128 | |
|
129 | |
|
130 | 0 | if ( artifact.getGroupId().equals( reference.getGroupId() ) |
131 | |
&& artifact.getArtifactId().equals( reference.getArtifactId() ) |
132 | |
&& artifact.getVersion().equals( reference.getVersion() ) ) |
133 | |
{ |
134 | 0 | foundArtifacts.add( artifact ); |
135 | |
} |
136 | |
} |
137 | 0 | catch ( LayoutException e ) |
138 | |
{ |
139 | 0 | log.debug( "Not processing file that is not an artifact: " + e.getMessage() ); |
140 | 0 | } |
141 | |
} |
142 | |
} |
143 | |
|
144 | 0 | return foundArtifacts; |
145 | |
} |
146 | |
|
147 | |
public String getRepoRoot() |
148 | |
{ |
149 | 0 | return repository.getLocation(); |
150 | |
} |
151 | |
|
152 | |
public ManagedRepositoryConfiguration getRepository() |
153 | |
{ |
154 | 0 | return repository; |
155 | |
} |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public Set<String> getVersions( ProjectReference reference ) |
166 | |
throws ContentNotFoundException, LayoutException |
167 | |
{ |
168 | 0 | String path = toMetadataPath( reference ); |
169 | |
|
170 | 0 | int idx = path.lastIndexOf( '/' ); |
171 | 0 | if ( idx > 0 ) |
172 | |
{ |
173 | 0 | path = path.substring( 0, idx ); |
174 | |
} |
175 | |
|
176 | 0 | File repoDir = new File( repository.getLocation(), path ); |
177 | |
|
178 | 0 | if ( !repoDir.exists() ) |
179 | |
{ |
180 | 0 | throw new ContentNotFoundException( "Unable to get Versions on a non-existant directory: " |
181 | |
+ repoDir.getAbsolutePath() ); |
182 | |
} |
183 | |
|
184 | 0 | if ( !repoDir.isDirectory() ) |
185 | |
{ |
186 | 0 | throw new ContentNotFoundException( "Unable to get Versions on a non-directory: " |
187 | |
+ repoDir.getAbsolutePath() ); |
188 | |
} |
189 | |
|
190 | 0 | Set<String> foundVersions = new HashSet<String>(); |
191 | 0 | VersionedReference versionRef = new VersionedReference(); |
192 | 0 | versionRef.setGroupId( reference.getGroupId() ); |
193 | 0 | versionRef.setArtifactId( reference.getArtifactId() ); |
194 | |
|
195 | 0 | File repoFiles[] = repoDir.listFiles(); |
196 | 0 | for ( int i = 0; i < repoFiles.length; i++ ) |
197 | |
{ |
198 | 0 | if ( !repoFiles[i].isDirectory() ) |
199 | |
{ |
200 | |
|
201 | 0 | continue; |
202 | |
} |
203 | |
|
204 | |
|
205 | 0 | String version = repoFiles[i].getName(); |
206 | 0 | versionRef.setVersion( version ); |
207 | |
|
208 | 0 | if ( hasArtifact( versionRef ) ) |
209 | |
{ |
210 | |
|
211 | 0 | foundVersions.add( version ); |
212 | |
} |
213 | |
} |
214 | |
|
215 | 0 | return foundVersions; |
216 | |
} |
217 | |
|
218 | |
public Set<String> getVersions( VersionedReference reference ) |
219 | |
throws ContentNotFoundException |
220 | |
{ |
221 | 0 | String path = toMetadataPath( reference ); |
222 | |
|
223 | 0 | int idx = path.lastIndexOf( '/' ); |
224 | 0 | if ( idx > 0 ) |
225 | |
{ |
226 | 0 | path = path.substring( 0, idx ); |
227 | |
} |
228 | |
|
229 | 0 | File repoDir = new File( repository.getLocation(), path ); |
230 | |
|
231 | 0 | if ( !repoDir.exists() ) |
232 | |
{ |
233 | 0 | throw new ContentNotFoundException( "Unable to get versions on a non-existant directory: " |
234 | |
+ repoDir.getAbsolutePath() ); |
235 | |
} |
236 | |
|
237 | 0 | if ( !repoDir.isDirectory() ) |
238 | |
{ |
239 | 0 | throw new ContentNotFoundException( "Unable to get versions on a non-directory: " |
240 | |
+ repoDir.getAbsolutePath() ); |
241 | |
} |
242 | |
|
243 | 0 | Set<String> foundVersions = new HashSet<String>(); |
244 | |
|
245 | |
|
246 | 0 | File repoFiles[] = repoDir.listFiles(); |
247 | 0 | for ( int i = 0; i < repoFiles.length; i++ ) |
248 | |
{ |
249 | 0 | if ( repoFiles[i].isDirectory() ) |
250 | |
{ |
251 | |
|
252 | 0 | continue; |
253 | |
} |
254 | |
|
255 | 0 | String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] ); |
256 | |
|
257 | 0 | if ( filetypes.matchesDefaultExclusions( relativePath ) ) |
258 | |
{ |
259 | |
|
260 | 0 | continue; |
261 | |
} |
262 | |
|
263 | 0 | if ( filetypes.matchesArtifactPattern( relativePath ) ) |
264 | |
{ |
265 | |
try |
266 | |
{ |
267 | 0 | ArtifactReference artifact = toArtifactReference( relativePath ); |
268 | |
|
269 | 0 | foundVersions.add( artifact.getVersion() ); |
270 | |
} |
271 | 0 | catch ( LayoutException e ) |
272 | |
{ |
273 | 0 | log.debug( "Not processing file that is not an artifact: " + e.getMessage() ); |
274 | 0 | } |
275 | |
} |
276 | |
} |
277 | |
|
278 | 0 | return foundVersions; |
279 | |
} |
280 | |
|
281 | |
public boolean hasContent( ArtifactReference reference ) |
282 | |
{ |
283 | 0 | File artifactFile = toFile( reference ); |
284 | 0 | return artifactFile.exists() && artifactFile.isFile(); |
285 | |
} |
286 | |
|
287 | |
public boolean hasContent( ProjectReference reference ) |
288 | |
{ |
289 | |
try |
290 | |
{ |
291 | 0 | Set<String> versions = getVersions( reference ); |
292 | 0 | return !versions.isEmpty(); |
293 | |
} |
294 | 0 | catch ( ContentNotFoundException e ) |
295 | |
{ |
296 | 0 | return false; |
297 | |
} |
298 | 0 | catch ( LayoutException e ) |
299 | |
{ |
300 | 0 | return false; |
301 | |
} |
302 | |
} |
303 | |
|
304 | |
public boolean hasContent( VersionedReference reference ) |
305 | |
{ |
306 | |
try |
307 | |
{ |
308 | 0 | return ( getFirstArtifact( reference ) != null ); |
309 | |
} |
310 | 0 | catch ( IOException e ) |
311 | |
{ |
312 | 0 | return false; |
313 | |
} |
314 | 0 | catch ( LayoutException e ) |
315 | |
{ |
316 | 0 | return false; |
317 | |
} |
318 | |
} |
319 | |
|
320 | |
public void setRepository( ManagedRepositoryConfiguration repository ) |
321 | |
{ |
322 | 0 | this.repository = repository; |
323 | 0 | } |
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
@Override |
332 | |
public ArtifactReference toArtifactReference( String path ) |
333 | |
throws LayoutException |
334 | |
{ |
335 | 0 | if ( ( path != null ) && path.startsWith( repository.getLocation() ) ) |
336 | |
{ |
337 | 0 | return super.toArtifactReference( path.substring( repository.getLocation().length() ) ); |
338 | |
} |
339 | |
|
340 | 0 | return super.toArtifactReference( path ); |
341 | |
} |
342 | |
|
343 | |
public File toFile( ArtifactReference reference ) |
344 | |
{ |
345 | 0 | return new File( repository.getLocation(), toPath( reference ) ); |
346 | |
} |
347 | |
|
348 | |
public File toFile( ArchivaArtifact reference ) |
349 | |
{ |
350 | 0 | return new File( repository.getLocation(), toPath( reference ) ); |
351 | |
} |
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
private ArtifactReference getFirstArtifact( VersionedReference reference ) |
364 | |
throws LayoutException, IOException |
365 | |
{ |
366 | 0 | String path = toMetadataPath( reference ); |
367 | |
|
368 | 0 | int idx = path.lastIndexOf( '/' ); |
369 | 0 | if ( idx > 0 ) |
370 | |
{ |
371 | 0 | path = path.substring( 0, idx ); |
372 | |
} |
373 | |
|
374 | 0 | File repoDir = new File( repository.getLocation(), path ); |
375 | |
|
376 | 0 | if ( !repoDir.exists() ) |
377 | |
{ |
378 | 0 | throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: " |
379 | |
+ repoDir.getAbsolutePath() ); |
380 | |
} |
381 | |
|
382 | 0 | if ( !repoDir.isDirectory() ) |
383 | |
{ |
384 | 0 | throw new IOException( "Unable to gather the list of snapshot versions on a non-directory: " |
385 | |
+ repoDir.getAbsolutePath() ); |
386 | |
} |
387 | |
|
388 | 0 | File repoFiles[] = repoDir.listFiles(); |
389 | 0 | for ( int i = 0; i < repoFiles.length; i++ ) |
390 | |
{ |
391 | 0 | if ( repoFiles[i].isDirectory() ) |
392 | |
{ |
393 | |
|
394 | 0 | continue; |
395 | |
} |
396 | |
|
397 | 0 | String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] ); |
398 | |
|
399 | 0 | if ( filetypes.matchesArtifactPattern( relativePath ) ) |
400 | |
{ |
401 | 0 | ArtifactReference artifact = toArtifactReference( relativePath ); |
402 | |
|
403 | 0 | return artifact; |
404 | |
} |
405 | |
} |
406 | |
|
407 | |
|
408 | 0 | return null; |
409 | |
} |
410 | |
|
411 | |
private boolean hasArtifact( VersionedReference reference ) |
412 | |
throws LayoutException |
413 | |
{ |
414 | |
try |
415 | |
{ |
416 | 0 | return ( getFirstArtifact( reference ) != null ); |
417 | |
} |
418 | 0 | catch ( IOException e ) |
419 | |
{ |
420 | 0 | return false; |
421 | |
} |
422 | |
} |
423 | |
|
424 | |
public void setFiletypes( FileTypes filetypes ) |
425 | |
{ |
426 | 0 | this.filetypes = filetypes; |
427 | 0 | } |
428 | |
} |