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.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;
29 import java.text.DecimalFormat;
30 import java.text.DecimalFormatSymbols;
31 import java.util.Locale;
34 * @author Olivier Lamy
37 public class ArtifactBuilder
40 private ManagedRepositoryContent managedRepositoryContent;
42 private ArtifactMetadata artifactMetadata;
44 public ArtifactBuilder()
50 public ArtifactBuilder withManagedRepositoryContent( ManagedRepositoryContent managedRepositoryContent )
52 this.managedRepositoryContent = managedRepositoryContent;
56 public ArtifactBuilder forArtifactMetadata( ArtifactMetadata artifactMetadata )
58 this.artifactMetadata = artifactMetadata;
62 public Artifact build()
64 ArtifactReference ref = new ArtifactReference();
65 ref.setArtifactId( artifactMetadata.getProject() );
66 ref.setGroupId( artifactMetadata.getNamespace() );
67 ref.setVersion( artifactMetadata.getVersion() );
69 String type = null, classifier = null;
71 MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
74 type = facet.getType();
75 classifier = facet.getClassifier();
78 ref.setClassifier( classifier );
80 File file = managedRepositoryContent.toFile( ref );
82 String extension = FilenameUtils.getExtension( file.getName() );
84 Artifact artifact = new Artifact( ref.getGroupId(), ref.getArtifactId(), ref.getVersion() );
85 artifact.setRepositoryId( artifactMetadata.getRepositoryId() );
86 artifact.setClassifier( classifier );
87 artifact.setPackaging( type );
88 artifact.setType( type );
89 artifact.setFileExtension( extension );
90 artifact.setPath( managedRepositoryContent.toPath( ref ) );
91 // TODO: find a reusable formatter for this
92 double s = this.artifactMetadata.getSize();
111 artifact.setContext( managedRepositoryContent.getId() );
112 DecimalFormat df = new DecimalFormat( "#,###.##", new DecimalFormatSymbols( Locale.US ) );
113 artifact.setSize( df.format( s ) + " " + symbol );
115 artifact.setId( ref.getArtifactId() + "-" + ref.getVersion() + "." + ref.getType() );