This project has retired. For details please refer to its Attic page.
DefaultPathParser xref
View Javadoc
1   package org.apache.archiva.repository.content.maven2;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.archiva.metadata.model.ArtifactMetadata;
23  import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet;
24  import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
25  import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider;
26  import org.apache.archiva.metadata.repository.storage.maven2.DefaultArtifactMappingProvider;
27  import org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryPathTranslator;
28  import org.apache.archiva.model.ArtifactReference;
29  import org.apache.archiva.repository.LayoutException;
30  import org.apache.archiva.repository.content.PathParser;
31  import org.apache.commons.lang3.StringUtils;
32  import org.springframework.stereotype.Service;
33  
34  import java.util.Collections;
35  
36  /**
37   * DefaultPathParser is a parser for maven 2 (default layout) paths to ArtifactReference.
38   *
39   * TODO: remove in favour of path translator, this is just delegating for the most part, but won't accommodate other
40   * extensions like NPanday
41   *
42   *
43   */
44  @Service( "pathParser#default" )
45  public class DefaultPathParser
46      implements PathParser
47  {
48      private static final String INVALID_ARTIFACT_PATH = "Invalid path to Artifact: ";
49  
50      private RepositoryPathTranslator pathTranslator = new Maven2RepositoryPathTranslator(
51          Collections.<ArtifactMappingProvider>singletonList( new DefaultArtifactMappingProvider() ) );
52  
53      /**
54       * {@inheritDoc}
55       *
56       * @see org.apache.archiva.repository.content.PathParser#toArtifactReference(String)
57       */
58      @Override
59      public ArtifactReference toArtifactReference( String path )
60          throws LayoutException
61      {
62          if ( StringUtils.isBlank( path ) )
63          {
64              throw new LayoutException( "Unable to convert blank path." );
65          }
66  
67          ArtifactMetadata metadata;
68          try
69          {
70              metadata = pathTranslator.getArtifactForPath( null, path );
71          }
72          catch ( IllegalArgumentException e )
73          {
74              throw new LayoutException( e.getMessage(), e );
75          }
76  
77          ArtifactReferenceference.html#ArtifactReference">ArtifactReference artifact = new ArtifactReference();
78          artifact.setGroupId( metadata.getNamespace() );
79          artifact.setArtifactId( metadata.getProject() );
80          artifact.setVersion( metadata.getVersion() );
81          MavenArtifactFacet/../../org/apache/archiva/metadata/model/maven2/MavenArtifactFacet.html#MavenArtifactFacet">MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
82          if ( facet != null )
83          {
84              artifact.setClassifier( facet.getClassifier() );
85              artifact.setType( facet.getType() );
86          }
87  
88          return artifact;
89      }
90  
91  }