This project has retired. For details please refer to its Attic page.
ArtifactBuilder xref
View Javadoc
1   package org.apache.archiva.rest.services.utils;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
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   * @author Olivier Lamy
37   * @since 1.4-M3
38   */
39  public class ArtifactBuilder
40  {
41  
42      private ManagedRepositoryContent managedRepositoryContent;
43  
44      private ArtifactMetadata artifactMetadata;
45  
46      public ArtifactBuilder()
47      {
48          // no op
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          // TODO: find a reusable formatter for this
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      * Extract file extension
126      */
127     String getExtensionFromFile( File file )
128     {
129         // we are just interested in the section after the last -
130         String[] parts = file.getName().split( "-" );
131         if ( parts.length > 0 )
132         {
133             // get anything after a dot followed by a letter a-z, including other dots
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         // just in case
142         return FilenameUtils.getExtension( file.getName() );
143     }
144 
145 }