This project has retired. For details please refer to its
Attic page.
LegacyToDefaultConverter xref
1 package org.apache.archiva.converter.artifact;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.archiva.checksum.ChecksumAlgorithm;
23 import org.apache.archiva.checksum.ChecksumValidationException;
24 import org.apache.archiva.checksum.ChecksummedFile;
25 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
26 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
27 import org.apache.archiva.transaction.FileTransaction;
28 import org.apache.archiva.transaction.TransactionException;
29 import org.apache.commons.io.FileUtils;
30 import org.apache.maven.artifact.Artifact;
31 import org.apache.maven.artifact.factory.ArtifactFactory;
32 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
33 import org.apache.maven.artifact.repository.ArtifactRepository;
34 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
35 import org.apache.maven.artifact.repository.metadata.Metadata;
36 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
37 import org.apache.maven.artifact.repository.metadata.Snapshot;
38 import org.apache.maven.artifact.repository.metadata.Versioning;
39 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
40 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
41 import org.apache.maven.model.DistributionManagement;
42 import org.apache.maven.model.Model;
43 import org.apache.maven.model.Relocation;
44 import org.apache.maven.model.converter.ModelConverter;
45 import org.apache.maven.model.converter.PomTranslationException;
46 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
47 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
48 import org.springframework.stereotype.Service;
49
50 import javax.annotation.PostConstruct;
51 import javax.inject.Inject;
52 import java.io.IOException;
53 import java.io.Reader;
54 import java.io.StringReader;
55 import java.io.StringWriter;
56 import java.nio.charset.Charset;
57 import java.nio.file.Files;
58 import java.nio.file.Path;
59 import java.nio.file.Paths;
60 import java.util.ArrayList;
61 import java.util.Arrays;
62 import java.util.HashMap;
63 import java.util.List;
64 import java.util.Map;
65 import java.util.Properties;
66 import java.util.regex.Matcher;
67
68
69
70
71 @Service("artifactConverter#legacy-to-default")
72 public class LegacyToDefaultConverter
73 implements ArtifactConverter
74 {
75
76
77
78 private List<ChecksumAlgorithm> digesters;
79
80 @Inject
81 private PlexusSisuBridge plexusSisuBridge;
82
83 private ModelConverter translator;
84
85 private ArtifactFactory artifactFactory;
86
87 private ArtifactHandlerManager artifactHandlerManager;
88
89 private boolean force;
90
91 private boolean dryrun;
92
93 private Map<Artifact, List<String>> warnings = new HashMap<>();
94
95 @PostConstruct
96 public void initialize()
97 throws PlexusSisuBridgeException
98 {
99
100 this.digesters = Arrays.asList(ChecksumAlgorithm.SHA256, ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5);
101 translator = plexusSisuBridge.lookup( ModelConverter.class );
102 artifactFactory = plexusSisuBridge.lookup( ArtifactFactory.class );
103 artifactHandlerManager = plexusSisuBridge.lookup( ArtifactHandlerManager.class );
104 }
105
106 @Override
107 public void convert( Artifact artifact, ArtifactRepository targetRepository )
108 throws ArtifactConversionException
109 {
110 if ( artifact.getRepository().getUrl().equals( targetRepository.getUrl() ) )
111 {
112 throw new ArtifactConversionException( Messages.getString( "exception.repositories.match" ) );
113 }
114
115 if ( !validateMetadata( artifact ) )
116 {
117 addWarning( artifact, Messages.getString( "unable.to.validate.metadata" ) );
118 return;
119 }
120
121 FileTransactionsaction.html#FileTransaction">FileTransaction transaction = new FileTransaction();
122
123 if ( !copyPom( artifact, targetRepository, transaction ) )
124 {
125 addWarning( artifact, Messages.getString( "unable.to.copy.pom" ) );
126 return;
127 }
128
129 if ( !copyArtifact( artifact, targetRepository, transaction ) )
130 {
131 addWarning( artifact, Messages.getString( "unable.to.copy.artifact" ) );
132 return;
133 }
134
135 Metadata metadata = createBaseMetadata( artifact );
136 Versioning versioning = new Versioning();
137 versioning.addVersion( artifact.getBaseVersion() );
138 metadata.setVersioning( versioning );
139 updateMetadata( new ArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
140
141 metadata = createBaseMetadata( artifact );
142 metadata.setVersion( artifact.getBaseVersion() );
143 versioning = new Versioning();
144
145 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
146 if ( matcher.matches() )
147 {
148 Snapshot snapshot = new Snapshot();
149 snapshot.setBuildNumber( Integer.parseInt( matcher.group( 3 ) ) );
150 snapshot.setTimestamp( matcher.group( 2 ) );
151 versioning.setSnapshot( snapshot );
152 }
153
154
155 metadata.setVersioning( versioning );
156 updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), targetRepository, metadata, transaction );
157
158 if ( !dryrun )
159 {
160 try
161 {
162 transaction.commit();
163 }
164 catch ( TransactionException e )
165 {
166 throw new ArtifactConversionException( Messages.getString( "transaction.failure", e.getMessage() ),
167 e );
168 }
169 }
170 }
171
172 @SuppressWarnings("unchecked")
173 private boolean copyPom( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
174 throws ArtifactConversionException
175 {
176 Artifact pom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
177 artifact.getVersion() );
178 pom.setBaseVersion( artifact.getBaseVersion() );
179 ArtifactRepository repository = artifact.getRepository();
180 Path file = Paths.get( repository.getBasedir(), repository.pathOf( pom ) );
181
182 boolean result = true;
183 if ( Files.exists(file) )
184 {
185 Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( pom ) );
186
187 String contents = null;
188 boolean checksumsValid = false;
189 try
190 {
191 if ( testChecksums( artifact, file ) )
192 {
193 checksumsValid = true;
194 }
195
196
197 contents = org.apache.archiva.common.utils.FileUtils.readFileToString( file, Charset.defaultCharset() );
198 }
199 catch ( IOException e )
200 {
201 throw new ArtifactConversionException(
202 Messages.getString( "unable.to.read.source.pom", e.getMessage() ), e );
203 }
204
205 if ( checksumsValid && contents.indexOf( "modelVersion" ) >= 0 )
206 {
207
208 boolean matching = false;
209 if ( !force && Files.exists( targetFile ) )
210 {
211 String targetContents = org.apache.archiva.common.utils.FileUtils.readFileToString( targetFile, Charset.defaultCharset( ) );
212 matching = targetContents.equals( contents );
213 }
214 if ( force || !matching )
215 {
216 transaction.createFile( contents, targetFile, digesters );
217 }
218 }
219 else
220 {
221
222 try (StringReader stringReader = new StringReader( contents ))
223 {
224
225 try (StringWriter writer = new StringWriter())
226 {
227 org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader v3Reader =
228 new org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader();
229 org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read( stringReader );
230
231 if ( doRelocation( artifact, v3Model, targetRepository, transaction ) )
232 {
233 Artifact relocatedPom =
234 artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
235 artifact.getVersion() );
236 targetFile =
237 Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( relocatedPom ) );
238 }
239
240 Model v4Model = translator.translate( v3Model );
241
242 translator.validateV4Basics( v4Model, v3Model.getGroupId(), v3Model.getArtifactId(),
243 v3Model.getVersion(), v3Model.getPackage() );
244
245 MavenXpp3Writer xpp3Writer = new MavenXpp3Writer();
246 xpp3Writer.write( writer, v4Model );
247
248 transaction.createFile( writer.toString(), targetFile, digesters );
249
250 List<String> warnings = translator.getWarnings();
251
252 for ( String message : warnings )
253 {
254 addWarning( artifact, message );
255 }
256 }
257 catch ( XmlPullParserException e )
258 {
259 addWarning( artifact,
260 Messages.getString( "invalid.source.pom", e.getMessage() ) );
261 result = false;
262 }
263 catch ( IOException e )
264 {
265 throw new ArtifactConversionException( Messages.getString( "unable.to.write.converted.pom" ),
266 e );
267 }
268 catch ( PomTranslationException e )
269 {
270 addWarning( artifact,
271 Messages.getString( "invalid.source.pom", e.getMessage() ) );
272 result = false;
273 }
274 }
275 }
276 }
277 else
278 {
279 addWarning( artifact, Messages.getString( "warning.missing.pom" ) );
280 }
281 return result;
282 }
283
284 private boolean testChecksums( Artifact artifact, Path file )
285 throws IOException
286 {
287 boolean result = true;
288 for ( ChecksumAlgorithm digester : digesters )
289 {
290 result &= verifyChecksum( file, file.getFileName() + "." + getDigesterFileExtension( digester ), digester,
291
292 artifact,
293 "failure.incorrect." + getDigesterFileExtension( digester ) );
294 }
295 return result;
296 }
297
298 private boolean verifyChecksum( Path file, String fileName, ChecksumAlgorithm digester, Artifact artifact, String key )
299 throws IOException
300 {
301 boolean result;
302 Path checksumFile = file.resolveSibling( fileName );
303
304 if (!Files.exists(checksumFile)) {
305 return true;
306 }
307 ChecksummedFileummedFile.html#ChecksummedFile">ChecksummedFile csFile = new ChecksummedFile( file );
308 try
309 {
310 result = csFile.isValidChecksum( digester, true );
311 } catch (ChecksumValidationException e ) {
312 addWarning( artifact, Messages.getString( key ) );
313 result = false;
314 }
315 return result;
316 }
317
318
319
320
321
322 private String getDigesterFileExtension( ChecksumAlgorithm checksumAlgorithm )
323 {
324 return checksumAlgorithm.getExt().get(0);
325 }
326
327 private boolean copyArtifact( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction )
328 throws ArtifactConversionException
329 {
330 Path sourceFile = artifact.getFile().toPath();
331
332 if ( sourceFile.toAbsolutePath().toString().indexOf( "/plugins/" ) > -1 )
333 {
334 artifact.setArtifactHandler( artifactHandlerManager.getArtifactHandler( "maven-plugin" ) );
335 }
336
337 Path targetFile = Paths.get( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
338
339 boolean result = true;
340 try
341 {
342 boolean matching = false;
343 if ( !force && Files.exists(targetFile) )
344 {
345 matching = FileUtils.contentEquals( sourceFile.toFile(), targetFile.toFile() );
346 if ( !matching )
347 {
348 addWarning( artifact, Messages.getString( "failure.target.already.exists" ) );
349 result = false;
350 }
351 }
352 if ( result )
353 {
354 if ( force || !matching )
355 {
356 if ( testChecksums( artifact, sourceFile ) )
357 {
358 transaction.copyFile( sourceFile, targetFile, digesters );
359 }
360 else
361 {
362 result = false;
363 }
364 }
365 }
366 }
367 catch ( IOException e )
368 {
369 throw new ArtifactConversionException( Messages.getString( "error.copying.artifact" ), e );
370 }
371 return result;
372 }
373
374 private Metadata createBaseMetadata( Artifact artifact )
375 {
376 Metadata metadata = new Metadata();
377 metadata.setArtifactId( artifact.getArtifactId() );
378 metadata.setGroupId( artifact.getGroupId() );
379 return metadata;
380 }
381
382 private Metadata readMetadata( Path file )
383 throws ArtifactConversionException
384 {
385 MetadataXpp3Reader reader = new MetadataXpp3Reader();
386
387 try (Reader fileReader = Files.newBufferedReader( file, Charset.defaultCharset() ))
388 {
389 return reader.read( fileReader );
390 }
391 catch ( IOException | XmlPullParserException e )
392 {
393 throw new ArtifactConversionException( Messages.getString( "error.reading.target.metadata" ),
394 e );
395 }
396 }
397
398 private boolean validateMetadata( Artifact artifact )
399 throws ArtifactConversionException
400 {
401 ArtifactRepository repository = artifact.getRepository();
402
403 boolean result = true;
404
405 RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact );
406 Path file = Paths.get( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
407 if ( Files.exists(file) )
408 {
409 Metadata metadata = readMetadata( file );
410 result = validateMetadata( metadata, repositoryMetadata, artifact );
411 }
412
413 repositoryMetadata = new SnapshotArtifactRepositoryMetadata( artifact );
414 file = Paths.get( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) );
415 if ( Files.exists(file) )
416 {
417 Metadata metadata = readMetadata( file );
418 result = result && validateMetadata( metadata, repositoryMetadata, artifact );
419 }
420
421 return result;
422 }
423
424 @SuppressWarnings("unchecked")
425 private boolean validateMetadata( Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact )
426 {
427 String groupIdKey;
428 String artifactIdKey = null;
429 String snapshotKey = null;
430 String versionKey = null;
431 String versionsKey = null;
432
433 if ( repositoryMetadata.storedInGroupDirectory() )
434 {
435 groupIdKey = "failure.incorrect.groupMetadata.groupId";
436 }
437 else if ( repositoryMetadata.storedInArtifactVersionDirectory() )
438 {
439 groupIdKey = "failure.incorrect.snapshotMetadata.groupId";
440 artifactIdKey = "failure.incorrect.snapshotMetadata.artifactId";
441 versionKey = "failure.incorrect.snapshotMetadata.version";
442 snapshotKey = "failure.incorrect.snapshotMetadata.snapshot";
443 }
444 else
445 {
446 groupIdKey = "failure.incorrect.artifactMetadata.groupId";
447 artifactIdKey = "failure.incorrect.artifactMetadata.artifactId";
448 versionsKey = "failure.incorrect.artifactMetadata.versions";
449 }
450
451 boolean result = true;
452
453 if ( metadata.getGroupId() == null || !metadata.getGroupId().equals( artifact.getGroupId() ) )
454 {
455 addWarning( artifact, Messages.getString( groupIdKey ) );
456 result = false;
457 }
458 if ( !repositoryMetadata.storedInGroupDirectory() )
459 {
460 if ( metadata.getGroupId() == null || !metadata.getArtifactId().equals( artifact.getArtifactId() ) )
461 {
462 addWarning( artifact, Messages.getString( artifactIdKey ) );
463 result = false;
464 }
465 if ( !repositoryMetadata.storedInArtifactVersionDirectory() )
466 {
467
468
469 boolean foundVersion = false;
470 if ( metadata.getVersioning() != null )
471 {
472 for ( String version : (List<String>) metadata.getVersioning().getVersions() )
473 {
474 if ( version.equals( artifact.getBaseVersion() ) )
475 {
476 foundVersion = true;
477 break;
478 }
479 }
480 }
481
482 if ( !foundVersion )
483 {
484 addWarning( artifact, Messages.getString( versionsKey ) );
485 result = false;
486 }
487 }
488 else
489 {
490
491 if ( !artifact.getBaseVersion().equals( metadata.getVersion() ) )
492 {
493 addWarning( artifact, Messages.getString( versionKey ) );
494 result = false;
495 }
496
497 if ( artifact.isSnapshot() )
498 {
499 Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
500 if ( matcher.matches() )
501 {
502 boolean correct = false;
503 if ( metadata.getVersioning() != null && metadata.getVersioning().getSnapshot() != null )
504 {
505 Snapshot snapshot = metadata.getVersioning().getSnapshot();
506 int build = Integer.parseInt( matcher.group( 3 ) );
507 String ts = matcher.group( 2 );
508 if ( build == snapshot.getBuildNumber() && ts.equals( snapshot.getTimestamp() ) )
509 {
510 correct = true;
511 }
512 }
513
514 if ( !correct )
515 {
516 addWarning( artifact, Messages.getString( snapshotKey ) );
517 result = false;
518 }
519 }
520 }
521 }
522 }
523 return result;
524 }
525
526 private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactRepository targetRepository,
527 Metadata newMetadata, FileTransaction transaction )
528 throws ArtifactConversionException
529 {
530 Path file = Paths.get( targetRepository.getBasedir(),
531 targetRepository.pathOfRemoteRepositoryMetadata( artifactMetadata ) );
532
533 Metadata metadata;
534 boolean changed;
535
536 if ( Files.exists(file) )
537 {
538 metadata = readMetadata( file );
539 changed = metadata.merge( newMetadata );
540 }
541 else
542 {
543 changed = true;
544 metadata = newMetadata;
545 }
546
547 if ( changed )
548 {
549
550 try (StringWriter writer = new StringWriter())
551 {
552 MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
553
554 mappingWriter.write( writer, metadata );
555
556 transaction.createFile( writer.toString(), file, digesters );
557 }
558 catch ( IOException e )
559 {
560 throw new ArtifactConversionException( Messages.getString( "error.writing.target.metadata" ),
561 e );
562 }
563 }
564 }
565
566 private boolean doRelocation( Artifact artifact, org.apache.maven.model.v3_0_0.Model v3Model,
567 ArtifactRepository repository, FileTransaction transaction )
568 throws IOException
569 {
570 Properties properties = v3Model.getProperties();
571 if ( properties.containsKey( "relocated.groupId" ) || properties.containsKey( "relocated.artifactId" )
572
573 || properties.containsKey( "relocated.version" ) )
574 {
575 String newGroupId = properties.getProperty( "relocated.groupId", v3Model.getGroupId() );
576 properties.remove( "relocated.groupId" );
577
578 String newArtifactId =
579 properties.getProperty( "relocated.artifactId", v3Model.getArtifactId() );
580 properties.remove( "relocated.artifactId" );
581
582 String newVersion = properties.getProperty( "relocated.version", v3Model.getVersion() );
583 properties.remove( "relocated.version" );
584
585 String message = properties.getProperty( "relocated.message", "" );
586 properties.remove( "relocated.message" );
587
588 if ( properties.isEmpty() )
589 {
590 v3Model.setProperties( null );
591 }
592
593 writeRelocationPom( v3Model.getGroupId(), v3Model.getArtifactId(), v3Model.getVersion(), newGroupId,
594 newArtifactId, newVersion, message, repository, transaction );
595
596 v3Model.setGroupId( newGroupId );
597 v3Model.setArtifactId( newArtifactId );
598 v3Model.setVersion( newVersion );
599
600 artifact.setGroupId( newGroupId );
601 artifact.setArtifactId( newArtifactId );
602 artifact.setVersion( newVersion );
603
604 return true;
605 }
606 else
607 {
608 return false;
609 }
610 }
611
612 private void writeRelocationPom( String groupId, String artifactId, String version, String newGroupId,
613 String newArtifactId, String newVersion, String message,
614 ArtifactRepository repository, FileTransaction transaction )
615 throws IOException
616 {
617 Model pom = new Model();
618 pom.setGroupId( groupId );
619 pom.setArtifactId( artifactId );
620 pom.setVersion( version );
621
622 DistributionManagement dMngt = new DistributionManagement();
623
624 Relocation relocation = new Relocation();
625 relocation.setGroupId( newGroupId );
626 relocation.setArtifactId( newArtifactId );
627 relocation.setVersion( newVersion );
628 if ( message != null && message.length() > 0 )
629 {
630 relocation.setMessage( message );
631 }
632
633 dMngt.setRelocation( relocation );
634
635 pom.setDistributionManagement( dMngt );
636
637 Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" );
638 Path pomFile = Paths.get( repository.getBasedir(), repository.pathOf( artifact ) );
639
640 StringWriter strWriter = new StringWriter();
641 MavenXpp3Writer pomWriter = new MavenXpp3Writer();
642 pomWriter.write( strWriter, pom );
643
644 transaction.createFile( strWriter.toString(), pomFile, digesters );
645 }
646
647 private void addWarning( Artifact artifact, String message )
648 {
649 List<String> messages = warnings.get( artifact );
650 if ( messages == null )
651 {
652 messages = new ArrayList<>( 1 );
653 }
654 messages.add( message );
655 warnings.put( artifact, messages );
656 }
657
658 @Override
659 public void clearWarnings()
660 {
661 warnings.clear();
662 }
663
664 @Override
665 public Map<Artifact, List<String>> getWarnings()
666 {
667 return warnings;
668 }
669
670
671 public List<ChecksumAlgorithm> getDigesters()
672 {
673 return digesters;
674 }
675
676 public void setDigesters( List<ChecksumAlgorithm> digesters )
677 {
678 this.digesters = digesters;
679 }
680
681 public ModelConverter getTranslator()
682 {
683 return translator;
684 }
685
686 public void setTranslator( ModelConverter translator )
687 {
688 this.translator = translator;
689 }
690
691 public ArtifactFactory getArtifactFactory()
692 {
693 return artifactFactory;
694 }
695
696 public void setArtifactFactory( ArtifactFactory artifactFactory )
697 {
698 this.artifactFactory = artifactFactory;
699 }
700
701 public ArtifactHandlerManager getArtifactHandlerManager()
702 {
703 return artifactHandlerManager;
704 }
705
706 public void setArtifactHandlerManager( ArtifactHandlerManager artifactHandlerManager )
707 {
708 this.artifactHandlerManager = artifactHandlerManager;
709 }
710
711 public boolean isForce()
712 {
713 return force;
714 }
715
716 public void setForce( boolean force )
717 {
718 this.force = force;
719 }
720
721 public boolean isDryrun()
722 {
723 return dryrun;
724 }
725
726 public void setDryrun( boolean dryrun )
727 {
728 this.dryrun = dryrun;
729 }
730 }