This project has retired. For details please refer to its
Attic page.
ArtifactBuilder xref
1 package org.apache.archiva.rest.services.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import org.apache.archiva.maven2.model.Artifact;
22 import org.apache.archiva.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet;
24 import org.apache.archiva.model.ArtifactReference;
25 import org.apache.archiva.repository.ManagedRepositoryContent;
26 import org.apache.archiva.repository.storage.StorageAsset;
27 import org.apache.archiva.repository.storage.StorageUtil;
28 import org.apache.commons.io.FilenameUtils;
29
30 import java.nio.file.Path;
31 import java.text.DecimalFormat;
32 import java.text.DecimalFormatSymbols;
33 import java.util.Locale;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36
37
38
39
40
41 public class ArtifactBuilder
42 {
43
44 private ManagedRepositoryContent managedRepositoryContent;
45
46 private ArtifactMetadata artifactMetadata;
47
48 public ArtifactBuilder()
49 {
50
51 }
52
53
54 public ArtifactBuilder withManagedRepositoryContent( ManagedRepositoryContent managedRepositoryContent )
55 {
56 this.managedRepositoryContent = managedRepositoryContent;
57 return this;
58 }
59
60 public ArtifactBuilder forArtifactMetadata( ArtifactMetadata artifactMetadata )
61 {
62 this.artifactMetadata = artifactMetadata;
63 return this;
64 }
65
66 public Artifact build()
67 {
68 ArtifactReferenceactReference.html#ArtifactReference">ArtifactReference ref = new ArtifactReference();
69 ref.setArtifactId( artifactMetadata.getProject() );
70 ref.setGroupId( artifactMetadata.getNamespace() );
71 ref.setVersion( artifactMetadata.getVersion() );
72
73 String type = null, classifier = null;
74
75 MavenArtifactFacet/../../org/apache/archiva/metadata/model/maven2/MavenArtifactFacet.html#MavenArtifactFacet">MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
76 if ( facet != null )
77 {
78 type = facet.getType();
79 classifier = facet.getClassifier();
80 }
81
82 ref.setClassifier( classifier );
83 ref.setType( type );
84 StorageAsset file = managedRepositoryContent.toFile( ref );
85
86 String extension = getExtensionFromFile(file);
87
88 Artifactmodel/Artifact.html#Artifact">Artifact artifact = new Artifact( ref.getGroupId(), ref.getArtifactId(), ref.getVersion() );
89 artifact.setRepositoryId( artifactMetadata.getRepositoryId() );
90 artifact.setClassifier( classifier );
91 artifact.setPackaging( type );
92 artifact.setType( type );
93 artifact.setFileExtension( extension );
94 artifact.setPath( managedRepositoryContent.toPath( ref ) );
95
96 double s = this.artifactMetadata.getSize();
97 String symbol = "b";
98 if ( s > 1024 )
99 {
100 symbol = "K";
101 s /= 1024;
102
103 if ( s > 1024 )
104 {
105 symbol = "M";
106 s /= 1024;
107
108 if ( s > 1024 )
109 {
110 symbol = "G";
111 s /= 1024;
112 }
113 }
114 }
115 artifact.setContext( managedRepositoryContent.getId() );
116 DecimalFormat df = new DecimalFormat( "#,###.##", new DecimalFormatSymbols( Locale.US ) );
117 artifact.setSize( df.format( s ) + " " + symbol );
118
119 artifact.setId( ref.getArtifactId() + "-" + ref.getVersion() + "." + ref.getType() );
120
121 return artifact;
122
123 }
124
125
126
127
128
129 String getExtensionFromFile( StorageAsset file )
130 {
131
132 String[] parts = file.getName().split( "-" );
133 if ( parts.length > 0 )
134 {
135
136 Pattern p = Pattern.compile( "\\.([a-z]+[a-z0-9\\.]*)" );
137 Matcher m = p.matcher( parts[parts.length - 1] );
138 if ( m.find() )
139 {
140 return m.group( 1 );
141 }
142 }
143
144 return StorageUtil.getExtension( file );
145 }
146
147 }