001package org.apache.archiva.repository.content.maven2; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.archiva.metadata.model.ArtifactMetadata; 023import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet; 024import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator; 025import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider; 026import org.apache.archiva.metadata.repository.storage.maven2.DefaultArtifactMappingProvider; 027import org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryPathTranslator; 028import org.apache.archiva.model.ArtifactReference; 029import org.apache.archiva.repository.content.PathParser; 030import org.apache.archiva.repository.layout.LayoutException; 031import org.apache.commons.lang.StringUtils; 032import org.springframework.stereotype.Service; 033 034import java.util.Collections; 035 036/** 037 * DefaultPathParser is a parser for maven 2 (default layout) paths to ArtifactReference. 038 * 039 * TODO: remove in favour of path translator, this is just delegating for the most part, but won't accommodate other 040 * extensions like NPanday 041 * 042 * 043 */ 044@Service( "pathParser#default" ) 045public class DefaultPathParser 046 implements PathParser 047{ 048 private static final String INVALID_ARTIFACT_PATH = "Invalid path to Artifact: "; 049 050 private RepositoryPathTranslator pathTranslator = new Maven2RepositoryPathTranslator( 051 Collections.<ArtifactMappingProvider>singletonList( new DefaultArtifactMappingProvider() ) ); 052 053 /** 054 * {@inheritDoc} 055 * 056 * @see org.apache.archiva.repository.content.PathParser#toArtifactReference(String) 057 */ 058 @Override 059 public ArtifactReference toArtifactReference( String path ) 060 throws LayoutException 061 { 062 if ( StringUtils.isBlank( path ) ) 063 { 064 throw new LayoutException( "Unable to convert blank path." ); 065 } 066 067 ArtifactMetadata metadata; 068 try 069 { 070 metadata = pathTranslator.getArtifactForPath( null, path ); 071 } 072 catch ( IllegalArgumentException e ) 073 { 074 throw new LayoutException( e.getMessage(), e ); 075 } 076 077 ArtifactReference artifact = new ArtifactReference(); 078 artifact.setGroupId( metadata.getNamespace() ); 079 artifact.setArtifactId( metadata.getProject() ); 080 artifact.setVersion( metadata.getVersion() ); 081 MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID ); 082 if ( facet != null ) 083 { 084 artifact.setClassifier( facet.getClassifier() ); 085 artifact.setType( facet.getType() ); 086 } 087 088 return artifact; 089 } 090 091}