001 package 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 021 import org.apache.archiva.metadata.model.ArtifactMetadata; 022 import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet; 023 import org.apache.archiva.model.ArtifactReference; 024 import org.apache.archiva.repository.ManagedRepositoryContent; 025 import org.apache.archiva.maven2.model.Artifact; 026 import org.apache.commons.io.FilenameUtils; 027 028 import java.io.File; 029 import java.text.DecimalFormat; 030 import java.text.DecimalFormatSymbols; 031 import java.util.Locale; 032 033 /** 034 * @author Olivier Lamy 035 * @since 1.4-M3 036 */ 037 public class ArtifactBuilder 038 { 039 040 private ManagedRepositoryContent managedRepositoryContent; 041 042 private ArtifactMetadata artifactMetadata; 043 044 public ArtifactBuilder() 045 { 046 // no op 047 } 048 049 050 public ArtifactBuilder withManagedRepositoryContent( ManagedRepositoryContent managedRepositoryContent ) 051 { 052 this.managedRepositoryContent = managedRepositoryContent; 053 return this; 054 } 055 056 public ArtifactBuilder forArtifactMetadata( ArtifactMetadata artifactMetadata ) 057 { 058 this.artifactMetadata = artifactMetadata; 059 return this; 060 } 061 062 public Artifact build() 063 { 064 ArtifactReference ref = new ArtifactReference(); 065 ref.setArtifactId( artifactMetadata.getProject() ); 066 ref.setGroupId( artifactMetadata.getNamespace() ); 067 ref.setVersion( artifactMetadata.getVersion() ); 068 069 String type = null, classifier = null; 070 071 MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID ); 072 if ( facet != null ) 073 { 074 type = facet.getType(); 075 classifier = facet.getClassifier(); 076 } 077 078 ref.setClassifier( classifier ); 079 ref.setType( type ); 080 File file = managedRepositoryContent.toFile( ref ); 081 082 String extension = FilenameUtils.getExtension( file.getName() ); 083 084 Artifact artifact = new Artifact( ref.getGroupId(), ref.getArtifactId(), ref.getVersion() ); 085 artifact.setRepositoryId( artifactMetadata.getRepositoryId() ); 086 artifact.setClassifier( classifier ); 087 artifact.setPackaging( type ); 088 artifact.setType( type ); 089 artifact.setFileExtension( extension ); 090 artifact.setPath( managedRepositoryContent.toPath( ref ) ); 091 // TODO: find a reusable formatter for this 092 double s = this.artifactMetadata.getSize(); 093 String symbol = "b"; 094 if ( s > 1024 ) 095 { 096 symbol = "K"; 097 s /= 1024; 098 099 if ( s > 1024 ) 100 { 101 symbol = "M"; 102 s /= 1024; 103 104 if ( s > 1024 ) 105 { 106 symbol = "G"; 107 s /= 1024; 108 } 109 } 110 } 111 artifact.setContext( managedRepositoryContent.getId() ); 112 DecimalFormat df = new DecimalFormat( "#,###.##", new DecimalFormatSymbols( Locale.US ) ); 113 artifact.setSize( df.format( s ) + " " + symbol ); 114 115 artifact.setId( ref.getArtifactId() + "-" + ref.getVersion() + "." + ref.getType() ); 116 117 return artifact; 118 119 } 120 121 122 }