001package org.apache.archiva.repository.content.base; 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.model.ArtifactReference; 022import org.apache.archiva.repository.ManagedRepository; 023import org.apache.archiva.repository.ManagedRepositoryContent; 024import org.apache.archiva.repository.RepositoryContentFactory; 025import org.apache.archiva.repository.RepositoryException; 026import org.apache.archiva.repository.storage.StorageAsset; 027import org.springframework.stereotype.Service; 028 029import javax.inject.Inject; 030import java.nio.file.Path; 031import java.nio.file.Paths; 032 033/** 034 * Utility class that gives information about the physical location of artifacts. 035 */ 036@Service( "ArtifactUtil#default" ) 037public class ArtifactUtil { 038 039 @Inject 040 RepositoryContentFactory repositoryContentFactory; 041 042 /** 043 * Returns the physical location of a given artifact in the repository. There is no check for the 044 * existence of the returned file. 045 * 046 * @param repository The repository, where the artifact is stored. 047 * @param artifactReference The artifact reference. 048 * @return The absolute path to the artifact. 049 * @throws RepositoryException 050 */ 051 public Path getArtifactPath(ManagedRepository repository, ArtifactReference artifactReference) throws RepositoryException { 052 final ManagedRepositoryContent content = repositoryContentFactory.getManagedRepositoryContent(repository); 053 final String artifactPath = content.toPath( artifactReference ); 054 return Paths.get(repository.getLocation()).resolve(artifactPath); 055 } 056 057 /** 058 * Returns the physical location of a given artifact in the repository. There is no check for the 059 * existence of the returned file. 060 * 061 * @param repository The repository, where the artifact is stored. 062 * @param artifactReference The artifact reference. 063 * @return The asset representation of the artifact. 064 * @throws RepositoryException 065 */ 066 public StorageAsset getArtifactAsset(ManagedRepository repository, ArtifactReference artifactReference) throws RepositoryException { 067 final ManagedRepositoryContent content = repositoryContentFactory.getManagedRepositoryContent(repository); 068 final String artifactPath = content.toPath( artifactReference ); 069 return repository.getAsset(artifactPath); 070 } 071 072}