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.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   * @author Olivier Lamy
39   * @since 1.4-M3
40   */
41  public class ArtifactBuilder
42  {
43  
44      private ManagedRepositoryContent managedRepositoryContent;
45  
46      private ArtifactMetadata artifactMetadata;
47  
48      public ArtifactBuilder()
49      {
50          // no op
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          // TODO: find a reusable formatter for this
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      * Extract file extension
128      */
129     String getExtensionFromFile( StorageAsset file )
130     {
131         // we are just interested in the section after the last -
132         String[] parts = file.getName().split( "-" );
133         if ( parts.length > 0 )
134         {
135             // get anything after a dot followed by a letter a-z, including other dots
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         // just in case
144         return StorageUtil.getExtension( file );
145     }
146 
147 }