This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.rest.services.utils;
002/*
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 */
020
021import org.apache.archiva.metadata.model.ArtifactMetadata;
022import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet;
023import org.apache.archiva.model.ArtifactReference;
024import org.apache.archiva.repository.ManagedRepositoryContent;
025import org.apache.archiva.maven2.model.Artifact;
026import org.apache.commons.io.FilenameUtils;
027
028import java.io.File;
029import java.text.DecimalFormat;
030import java.text.DecimalFormatSymbols;
031import java.util.Locale;
032import java.util.regex.Matcher;
033import java.util.regex.Pattern;
034
035/**
036 * @author Olivier Lamy
037 * @since 1.4-M3
038 */
039public class ArtifactBuilder
040{
041
042    private ManagedRepositoryContent managedRepositoryContent;
043
044    private ArtifactMetadata artifactMetadata;
045
046    public ArtifactBuilder()
047    {
048        // no op
049    }
050
051
052    public ArtifactBuilder withManagedRepositoryContent( ManagedRepositoryContent managedRepositoryContent )
053    {
054        this.managedRepositoryContent = managedRepositoryContent;
055        return this;
056    }
057
058    public ArtifactBuilder forArtifactMetadata( ArtifactMetadata artifactMetadata )
059    {
060        this.artifactMetadata = artifactMetadata;
061        return this;
062    }
063
064    public Artifact build()
065    {
066        ArtifactReference ref = new ArtifactReference();
067        ref.setArtifactId( artifactMetadata.getProject() );
068        ref.setGroupId( artifactMetadata.getNamespace() );
069        ref.setVersion( artifactMetadata.getVersion() );
070
071        String type = null, classifier = null;
072
073        MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
074        if ( facet != null )
075        {
076            type = facet.getType();
077            classifier = facet.getClassifier();
078        }
079
080        ref.setClassifier( classifier );
081        ref.setType( type );
082        File file = managedRepositoryContent.toFile( ref );
083
084        String extension = getExtensionFromFile(file);
085        
086        Artifact artifact = new Artifact( ref.getGroupId(), ref.getArtifactId(), ref.getVersion() );
087        artifact.setRepositoryId( artifactMetadata.getRepositoryId() );
088        artifact.setClassifier( classifier );
089        artifact.setPackaging( type );
090        artifact.setType( type );
091        artifact.setFileExtension( extension );
092        artifact.setPath( managedRepositoryContent.toPath( ref ) );
093        // TODO: find a reusable formatter for this
094        double s = this.artifactMetadata.getSize();
095        String symbol = "b";
096        if ( s > 1024 )
097        {
098            symbol = "K";
099            s /= 1024;
100
101            if ( s > 1024 )
102            {
103                symbol = "M";
104                s /= 1024;
105
106                if ( s > 1024 )
107                {
108                    symbol = "G";
109                    s /= 1024;
110                }
111            }
112        }
113        artifact.setContext( managedRepositoryContent.getId() );
114        DecimalFormat df = new DecimalFormat( "#,###.##", new DecimalFormatSymbols( Locale.US ) );
115        artifact.setSize( df.format( s ) + " " + symbol );
116
117        artifact.setId( ref.getArtifactId() + "-" + ref.getVersion() + "." + ref.getType() );
118
119        return artifact;
120
121    }
122
123
124    /**
125     * Extract file extension
126     */
127    String getExtensionFromFile( File file )
128    {
129        // we are just interested in the section after the last -
130        String[] parts = file.getName().split( "-" );
131        if ( parts.length > 0 )
132        {
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] );
136            if ( m.find() )
137            {
138                return m.group( 1 );
139            }
140        }
141        // just in case
142        return FilenameUtils.getExtension( file.getName() );
143    }
144
145}