1 package org.apache.archiva.repository.maven.content;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.metadata.model.ArtifactMetadata;
22 import org.apache.archiva.metadata.maven.model.MavenArtifactFacet;
23 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
24 import org.apache.archiva.repository.maven.metadata.storage.ArtifactMappingProvider;
25 import org.apache.archiva.repository.maven.metadata.storage.DefaultArtifactMappingProvider;
26 import org.apache.archiva.repository.maven.metadata.storage.Maven2RepositoryPathTranslator;
27 import org.apache.archiva.model.ArtifactReference;
28 import org.apache.archiva.repository.LayoutException;
29 import org.apache.archiva.repository.content.ItemSelector;
30 import org.apache.archiva.repository.content.PathParser;
31 import org.apache.archiva.repository.content.base.ArchivaItemSelector;
32 import org.apache.commons.lang3.StringUtils;
33 import org.springframework.stereotype.Service;
35 import java.util.Collections;
38 * DefaultPathParser is a parser for maven 2 (default layout) paths to ArtifactReference.
40 * TODO: remove in favour of path translator, this is just delegating for the most part, but won't accommodate other
41 * extensions like NPanday
45 @Service( "pathParser#default" )
46 public class DefaultPathParser
49 private static final String INVALID_ARTIFACT_PATH = "Invalid path to Artifact: ";
51 private RepositoryPathTranslator pathTranslator = new Maven2RepositoryPathTranslator(
52 Collections.<ArtifactMappingProvider>singletonList( new DefaultArtifactMappingProvider() ) );
57 * @see org.apache.archiva.repository.content.PathParser#toArtifactReference(String)
60 public ArtifactReference toArtifactReference( String path )
61 throws LayoutException
63 if ( StringUtils.isBlank( path ) )
65 throw new LayoutException( "Unable to convert blank path." );
68 ArtifactMetadata metadata;
71 metadata = pathTranslator.getArtifactForPath( null, path );
73 catch ( IllegalArgumentException e )
75 throw new LayoutException( e.getMessage(), e );
78 ArtifactReference artifact = new ArtifactReference();
79 artifact.setGroupId( metadata.getNamespace() );
80 artifact.setArtifactId( metadata.getProject() );
81 artifact.setVersion( metadata.getVersion() );
82 artifact.setProjectVersion( metadata.getProjectVersion( ) );
83 MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
86 artifact.setClassifier( facet.getClassifier() );
87 artifact.setType( facet.getType() );
94 public ItemSelector toItemSelector( String path ) throws LayoutException
96 if ( StringUtils.isBlank( path ) )
98 throw new LayoutException( "Unable to convert blank path." );
103 ArtifactMetadata metadata = pathTranslator.getArtifactForPath( null, path );
104 ArchivaItemSelector.Builder builder = ArchivaItemSelector.builder( ).withNamespace( metadata.getNamespace( ) )
105 .withProjectId( metadata.getProject( ) )
106 .withVersion( metadata.getProjectVersion( ) )
107 .withArtifactId( metadata.getProject( ) )
108 .withArtifactVersion( metadata.getVersion( ) );
109 MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
112 builder.withClassifier( facet.getClassifier() );
113 builder.withType( facet.getType() );
115 return builder.build( );
117 catch ( IllegalArgumentException e )
119 throw new LayoutException( e.getMessage(), e );