1 package org.apache.archiva.rest.services.utils;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
21 import org.apache.archiva.maven2.model.Artifact;
22 import org.apache.archiva.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.maven.model.MavenArtifactFacet;
24 import org.apache.archiva.model.ArtifactReference;
25 import org.apache.archiva.repository.BaseRepositoryContentLayout;
26 import org.apache.archiva.repository.ManagedRepositoryContent;
27 import org.apache.archiva.repository.LayoutException;
28 import org.apache.archiva.repository.storage.StorageAsset;
29 import org.apache.archiva.repository.storage.util.StorageUtil;
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;
38 * @author Olivier Lamy
41 public class ArtifactBuilder
44 private ManagedRepositoryContent managedRepositoryContent;
46 private ArtifactMetadata artifactMetadata;
48 public ArtifactBuilder()
54 public ArtifactBuilder withManagedRepositoryContent( ManagedRepositoryContent managedRepositoryContent )
56 this.managedRepositoryContent = managedRepositoryContent;
60 public ArtifactBuilder forArtifactMetadata( ArtifactMetadata artifactMetadata )
62 this.artifactMetadata = artifactMetadata;
66 public Artifact build()
68 ArtifactReference ref = new ArtifactReference();
69 ref.setArtifactId( artifactMetadata.getProject() );
70 ref.setGroupId( artifactMetadata.getNamespace() );
71 ref.setVersion( artifactMetadata.getVersion() );
73 String type = null, classifier = null;
75 MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
78 type = facet.getType();
79 classifier = facet.getClassifier();
82 ref.setClassifier( classifier );
84 BaseRepositoryContentLayout layout;
87 layout = managedRepositoryContent.getLayout( BaseRepositoryContentLayout.class );
89 catch ( LayoutException e )
91 throw new RuntimeException( "Could not convert to layout " + e.getMessage( ) );
93 StorageAsset file = layout.toFile( ref );
95 String extension = getExtensionFromFile(file);
97 Artifact artifact = new Artifact( ref.getGroupId(), ref.getArtifactId(), ref.getVersion() );
98 artifact.setRepositoryId( artifactMetadata.getRepositoryId() );
99 artifact.setClassifier( classifier );
100 artifact.setPackaging( type );
101 artifact.setType( type );
102 artifact.setFileExtension( extension );
103 artifact.setPath( layout.toPath( ref ) );
104 // TODO: find a reusable formatter for this
105 double s = this.artifactMetadata.getSize();
124 artifact.setContext( managedRepositoryContent.getId() );
125 DecimalFormat df = new DecimalFormat( "#,###.##", new DecimalFormatSymbols( Locale.US ) );
126 artifact.setSize( df.format( s ) + " " + symbol );
128 artifact.setId( ref.getArtifactId() + "-" + ref.getVersion() + "." + ref.getType() );
136 * Extract file extension
138 String getExtensionFromFile( StorageAsset file )
140 // we are just interested in the section after the last -
141 String[] parts = file.getName().split( "-" );
142 if ( parts.length > 0 )
144 // get anything after a dot followed by a letter a-z, including other dots
145 Pattern p = Pattern.compile( "\\.([a-z]+[a-z0-9\\.]*)" );
146 Matcher m = p.matcher( parts[parts.length - 1] );
153 return StorageUtil.getExtension( file );