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.model.maven2.MavenArtifactFacet;
24 import org.apache.archiva.model.ArtifactReference;
25 import org.apache.archiva.repository.ManagedRepositoryContent;
26 import org.apache.commons.io.FilenameUtils;
28 import java.nio.file.Path;
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;
36 * @author Olivier Lamy
39 public class ArtifactBuilder
42 private ManagedRepositoryContent managedRepositoryContent;
44 private ArtifactMetadata artifactMetadata;
46 public ArtifactBuilder()
52 public ArtifactBuilder withManagedRepositoryContent( ManagedRepositoryContent managedRepositoryContent )
54 this.managedRepositoryContent = managedRepositoryContent;
58 public ArtifactBuilder forArtifactMetadata( ArtifactMetadata artifactMetadata )
60 this.artifactMetadata = artifactMetadata;
64 public Artifact build()
66 ArtifactReference ref = new ArtifactReference();
67 ref.setArtifactId( artifactMetadata.getProject() );
68 ref.setGroupId( artifactMetadata.getNamespace() );
69 ref.setVersion( artifactMetadata.getVersion() );
71 String type = null, classifier = null;
73 MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
76 type = facet.getType();
77 classifier = facet.getClassifier();
80 ref.setClassifier( classifier );
82 Path file = managedRepositoryContent.toFile( ref );
84 String extension = getExtensionFromFile(file);
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();
113 artifact.setContext( managedRepositoryContent.getId() );
114 DecimalFormat df = new DecimalFormat( "#,###.##", new DecimalFormatSymbols( Locale.US ) );
115 artifact.setSize( df.format( s ) + " " + symbol );
117 artifact.setId( ref.getArtifactId() + "-" + ref.getVersion() + "." + ref.getType() );
125 * Extract file extension
127 String getExtensionFromFile( Path file )
129 // we are just interested in the section after the last -
130 String[] parts = file.getFileName().toString().split( "-" );
131 if ( parts.length > 0 )
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] );
142 return FilenameUtils.getExtension( file.toFile().getName() );