001    package 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    
022    import org.apache.archiva.metadata.model.ArtifactMetadata;
023    import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet;
024    import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
025    import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider;
026    import org.apache.archiva.metadata.repository.storage.maven2.DefaultArtifactMappingProvider;
027    import org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryPathTranslator;
028    import org.apache.archiva.model.ArtifactReference;
029    import org.apache.archiva.repository.content.PathParser;
030    import org.apache.archiva.repository.layout.LayoutException;
031    import org.apache.commons.lang.StringUtils;
032    import org.springframework.stereotype.Service;
033    
034    import 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" )
045    public 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        public ArtifactReference toArtifactReference( String path )
059            throws LayoutException
060        {
061            if ( StringUtils.isBlank( path ) )
062            {
063                throw new LayoutException( "Unable to convert blank path." );
064            }
065    
066            ArtifactMetadata metadata;
067            try
068            {
069                metadata = pathTranslator.getArtifactForPath( null, path );
070            }
071            catch ( IllegalArgumentException e )
072            {
073                throw new LayoutException( e.getMessage(), e );
074            }
075    
076            ArtifactReference artifact = new ArtifactReference();
077            artifact.setGroupId( metadata.getNamespace() );
078            artifact.setArtifactId( metadata.getProject() );
079            artifact.setVersion( metadata.getVersion() );
080            MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
081            if ( facet != null )
082            {
083                artifact.setClassifier( facet.getClassifier() );
084                artifact.setType( facet.getType() );
085            }
086    
087            return artifact;
088        }
089    
090    }