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