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.maven2.model.Artifact; 022import org.apache.archiva.metadata.model.ArtifactMetadata; 023import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet; 024import org.apache.archiva.model.ArtifactReference; 025import org.apache.archiva.repository.ManagedRepositoryContent; 026import org.apache.archiva.repository.storage.StorageAsset; 027import org.apache.archiva.repository.storage.StorageUtil; 028import org.apache.commons.io.FilenameUtils; 029 030import java.nio.file.Path; 031import java.text.DecimalFormat; 032import java.text.DecimalFormatSymbols; 033import java.util.Locale; 034import java.util.regex.Matcher; 035import java.util.regex.Pattern; 036 037/** 038 * @author Olivier Lamy 039 * @since 1.4-M3 040 */ 041public class ArtifactBuilder 042{ 043 044 private ManagedRepositoryContent managedRepositoryContent; 045 046 private ArtifactMetadata artifactMetadata; 047 048 public ArtifactBuilder() 049 { 050 // no op 051 } 052 053 054 public ArtifactBuilder withManagedRepositoryContent( ManagedRepositoryContent managedRepositoryContent ) 055 { 056 this.managedRepositoryContent = managedRepositoryContent; 057 return this; 058 } 059 060 public ArtifactBuilder forArtifactMetadata( ArtifactMetadata artifactMetadata ) 061 { 062 this.artifactMetadata = artifactMetadata; 063 return this; 064 } 065 066 public Artifact build() 067 { 068 ArtifactReference ref = new ArtifactReference(); 069 ref.setArtifactId( artifactMetadata.getProject() ); 070 ref.setGroupId( artifactMetadata.getNamespace() ); 071 ref.setVersion( artifactMetadata.getVersion() ); 072 073 String type = null, classifier = null; 074 075 MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID ); 076 if ( facet != null ) 077 { 078 type = facet.getType(); 079 classifier = facet.getClassifier(); 080 } 081 082 ref.setClassifier( classifier ); 083 ref.setType( type ); 084 StorageAsset file = managedRepositoryContent.toFile( ref ); 085 086 String extension = getExtensionFromFile(file); 087 088 Artifact artifact = new Artifact( ref.getGroupId(), ref.getArtifactId(), ref.getVersion() ); 089 artifact.setRepositoryId( artifactMetadata.getRepositoryId() ); 090 artifact.setClassifier( classifier ); 091 artifact.setPackaging( type ); 092 artifact.setType( type ); 093 artifact.setFileExtension( extension ); 094 artifact.setPath( managedRepositoryContent.toPath( ref ) ); 095 // TODO: find a reusable formatter for this 096 double s = this.artifactMetadata.getSize(); 097 String symbol = "b"; 098 if ( s > 1024 ) 099 { 100 symbol = "K"; 101 s /= 1024; 102 103 if ( s > 1024 ) 104 { 105 symbol = "M"; 106 s /= 1024; 107 108 if ( s > 1024 ) 109 { 110 symbol = "G"; 111 s /= 1024; 112 } 113 } 114 } 115 artifact.setContext( managedRepositoryContent.getId() ); 116 DecimalFormat df = new DecimalFormat( "#,###.##", new DecimalFormatSymbols( Locale.US ) ); 117 artifact.setSize( df.format( s ) + " " + symbol ); 118 119 artifact.setId( ref.getArtifactId() + "-" + ref.getVersion() + "." + ref.getType() ); 120 121 return artifact; 122 123 } 124 125 126 /** 127 * Extract file extension 128 */ 129 String getExtensionFromFile( StorageAsset file ) 130 { 131 // we are just interested in the section after the last - 132 String[] parts = file.getName().split( "-" ); 133 if ( parts.length > 0 ) 134 { 135 // get anything after a dot followed by a letter a-z, including other dots 136 Pattern p = Pattern.compile( "\\.([a-z]+[a-z0-9\\.]*)" ); 137 Matcher m = p.matcher( parts[parts.length - 1] ); 138 if ( m.find() ) 139 { 140 return m.group( 1 ); 141 } 142 } 143 // just in case 144 return StorageUtil.getExtension( file ); 145 } 146 147}