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.repository.content.BaseRepositoryContentLayout;
25 import org.apache.archiva.repository.ManagedRepositoryContent;
26 import org.apache.archiva.repository.content.LayoutException;
27 import org.apache.archiva.repository.content.base.ArchivaItemSelector;
28 import org.apache.archiva.repository.storage.StorageAsset;
29 import org.apache.archiva.repository.storage.util.StorageUtil;
30 import org.apache.commons.lang3.StringUtils;
32 import java.text.DecimalFormat;
33 import java.text.DecimalFormatSymbols;
34 import java.util.Locale;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
39 * @author Olivier Lamy
42 public class ArtifactBuilder
45 private ManagedRepositoryContent managedRepositoryContent;
47 private ArtifactMetadata artifactMetadata;
49 public ArtifactBuilder()
55 public ArtifactBuilder withManagedRepositoryContent( ManagedRepositoryContent managedRepositoryContent )
57 this.managedRepositoryContent = managedRepositoryContent;
61 public ArtifactBuilder forArtifactMetadata( ArtifactMetadata artifactMetadata )
63 this.artifactMetadata = artifactMetadata;
67 public Artifact build()
69 String type = null, classifier = null;
71 MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
74 type = facet.getType();
75 classifier = facet.getClassifier();
78 ArchivaItemSelector.Builder selectorBuilder = ArchivaItemSelector.builder( )
79 .withNamespace( artifactMetadata.getNamespace( ) )
80 .withProjectId( artifactMetadata.getProject( ) )
81 .withVersion( artifactMetadata.getProjectVersion( ) )
82 .withArtifactId( artifactMetadata.getProject( ) )
83 .withArtifactVersion( artifactMetadata.getVersion( ) );
84 if (StringUtils.isNotEmpty( type ) ) {
85 selectorBuilder.withType( type );
87 if (StringUtils.isNotEmpty( classifier )) {
88 selectorBuilder.withClassifier( classifier );
92 BaseRepositoryContentLayout layout;
95 layout = managedRepositoryContent.getLayout( BaseRepositoryContentLayout.class );
97 catch ( LayoutException e )
99 throw new RuntimeException( "Could not convert to layout " + e.getMessage( ) );
101 org.apache.archiva.repository.content.Artifact repoArtifact = layout.getArtifact( selectorBuilder.build( ) );
103 String extension = repoArtifact.getExtension();
105 Artifact artifact = new Artifact( repoArtifact.getVersion( ).getProject( ).getNamespace( ).getId( ), repoArtifact.getId( ), repoArtifact.getArtifactVersion( ) );
106 artifact.setRepositoryId( artifactMetadata.getRepositoryId() );
107 artifact.setClassifier( classifier );
108 artifact.setPackaging( type );
109 artifact.setType( type );
110 artifact.setFileExtension( extension );
111 artifact.setPath( managedRepositoryContent.toPath( repoArtifact ) );
112 // TODO: find a reusable formatter for this
113 double s = this.artifactMetadata.getSize();
132 artifact.setContext( managedRepositoryContent.getId() );
133 DecimalFormat df = new DecimalFormat( "#,###.##", new DecimalFormatSymbols( Locale.US ) );
134 artifact.setSize( df.format( s ) + " " + symbol );
136 artifact.setId( repoArtifact.getId() + "-" + repoArtifact.getArtifactVersion() + "." + repoArtifact.getType() );
144 * Extract file extension
146 String getExtensionFromFile( StorageAsset file )
148 // we are just interested in the section after the last -
149 String[] parts = file.getName().split( "-" );
150 if ( parts.length > 0 )
152 // get anything after a dot followed by a letter a-z, including other dots
153 Pattern p = Pattern.compile( "\\.([a-z]+[a-z0-9\\.]*)" );
154 Matcher m = p.matcher( parts[parts.length - 1] );
161 return StorageUtil.getExtension( file );