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.nio.file.Path;
30 import java.text.DecimalFormat;
31 import java.text.DecimalFormatSymbols;
32 import java.util.Locale;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
37 * @author Olivier Lamy
40 public class ArtifactBuilder
43 private ManagedRepositoryContent managedRepositoryContent;
45 private ArtifactMetadata artifactMetadata;
47 public ArtifactBuilder()
53 public ArtifactBuilder withManagedRepositoryContent( ManagedRepositoryContent managedRepositoryContent )
55 this.managedRepositoryContent = managedRepositoryContent;
59 public ArtifactBuilder forArtifactMetadata( ArtifactMetadata artifactMetadata )
61 this.artifactMetadata = artifactMetadata;
65 public Artifact build()
67 ArtifactReference ref = new ArtifactReference();
68 ref.setArtifactId( artifactMetadata.getProject() );
69 ref.setGroupId( artifactMetadata.getNamespace() );
70 ref.setVersion( artifactMetadata.getVersion() );
72 String type = null, classifier = null;
74 MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
77 type = facet.getType();
78 classifier = facet.getClassifier();
81 ref.setClassifier( classifier );
83 Path file = managedRepositoryContent.toFile( ref );
85 String extension = getExtensionFromFile(file);
87 Artifact artifact = new Artifact( ref.getGroupId(), ref.getArtifactId(), ref.getVersion() );
88 artifact.setRepositoryId( artifactMetadata.getRepositoryId() );
89 artifact.setClassifier( classifier );
90 artifact.setPackaging( type );
91 artifact.setType( type );
92 artifact.setFileExtension( extension );
93 artifact.setPath( managedRepositoryContent.toPath( ref ) );
94 // TODO: find a reusable formatter for this
95 double s = this.artifactMetadata.getSize();
114 artifact.setContext( managedRepositoryContent.getId() );
115 DecimalFormat df = new DecimalFormat( "#,###.##", new DecimalFormatSymbols( Locale.US ) );
116 artifact.setSize( df.format( s ) + " " + symbol );
118 artifact.setId( ref.getArtifactId() + "-" + ref.getVersion() + "." + ref.getType() );
126 * Extract file extension
128 String getExtensionFromFile( Path file )
130 // we are just interested in the section after the last -
131 String[] parts = file.getFileName().toString().split( "-" );
132 if ( parts.length > 0 )
134 // get anything after a dot followed by a letter a-z, including other dots
135 Pattern p = Pattern.compile( "\\.([a-z]+[a-z0-9\\.]*)" );
136 Matcher m = p.matcher( parts[parts.length - 1] );
143 return FilenameUtils.getExtension( file.toFile().getName() );