This project has retired. For details please refer to its Attic page.
AbstractDefaultRepositoryContent 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.common.utils.VersionUtil;
23  import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
24  import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider;
25  import org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryPathTranslator;
26  import org.apache.archiva.model.ArchivaArtifact;
27  import org.apache.archiva.model.ArtifactReference;
28  import org.apache.archiva.model.ProjectReference;
29  import org.apache.archiva.model.VersionedReference;
30  import org.apache.archiva.repository.LayoutException;
31  import org.apache.archiva.repository.RepositoryContent;
32  import org.apache.archiva.repository.content.PathParser;
33  import org.apache.commons.lang3.StringUtils;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  import java.util.List;
38  
39  /**
40   * AbstractDefaultRepositoryContent - common methods for working with default (maven 2) layout.
41   */
42  public abstract class AbstractDefaultRepositoryContent implements RepositoryContent
43  {
44  
45  
46      protected Logger log = LoggerFactory.getLogger( getClass() );
47  
48      public static final String MAVEN_METADATA = "maven-metadata.xml";
49  
50      protected static final char PATH_SEPARATOR = '/';
51  
52      protected static final char GROUP_SEPARATOR = '.';
53  
54      protected static final char ARTIFACT_SEPARATOR = '-';
55  
56      private RepositoryPathTranslator pathTranslator = new Maven2RepositoryPathTranslator();
57  
58      private PathParser defaultPathParser = new DefaultPathParser();
59  
60  
61  
62      /**
63       *
64       */
65      protected List<? extends ArtifactMappingProvider> artifactMappingProviders;
66  
67      AbstractDefaultRepositoryContent(List<? extends ArtifactMappingProvider> artifactMappingProviders) {
68          this.artifactMappingProviders = artifactMappingProviders;
69      }
70  
71      public void setArtifactMappingProviders(List<? extends ArtifactMappingProvider> artifactMappingProviders) {
72          this.artifactMappingProviders = artifactMappingProviders;
73      }
74  
75      @Override
76      public ArtifactReference toArtifactReference( String path )
77          throws LayoutException
78      {
79          return defaultPathParser.toArtifactReference( path );
80      }
81  
82      public String toMetadataPath( ProjectReference reference )
83      {
84          StringBuilder path = new StringBuilder();
85  
86          path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
87          path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
88          path.append( MAVEN_METADATA );
89  
90          return path.toString();
91      }
92  
93      public String toMetadataPath( VersionedReference reference )
94      {
95          StringBuilder path = new StringBuilder();
96  
97          path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
98          path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
99          if ( reference.getVersion() != null )
100         {
101             // add the version only if it is present
102             path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
103         }
104         path.append( MAVEN_METADATA );
105 
106         return path.toString();
107     }
108 
109     public String toPath( ArchivaArtifact reference )
110     {
111         if ( reference == null )
112         {
113             throw new IllegalArgumentException( "ArchivaArtifact cannot be null" );
114         }
115 
116         String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
117         return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
118                        reference.getClassifier(), reference.getType() );
119     }
120 
121     public String toPath( ArtifactReference reference )
122     {
123         if ( reference == null )
124         {
125             throw new IllegalArgumentException( "Artifact reference cannot be null" );
126         }
127         if ( reference.getVersion() != null )
128         {
129             String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
130             return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
131                            reference.getClassifier(), reference.getType() );
132         }
133         return toPath( reference.getGroupId(), reference.getArtifactId(), null, null,
134                        reference.getClassifier(), reference.getType() );
135     }
136 
137     private String formatAsDirectory( String directory )
138     {
139         return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
140     }
141 
142     private String toPath( String groupId, String artifactId, String baseVersion, String version, String classifier,
143                            String type )
144     {
145         if ( baseVersion != null )
146         {
147             return pathTranslator.toPath( groupId, artifactId, baseVersion,
148                                           constructId( artifactId, version, classifier, type ) );
149         }
150         else
151         {
152             return pathTranslator.toPath( groupId, artifactId );
153         }
154     }
155 
156     // TODO: move into the Maven Artifact facet when refactoring away the caller - the caller will need to have access
157     //       to the facet or filename (for the original ID)
158     private String constructId( String artifactId, String version, String classifier, String type )
159     {
160         String ext = null;
161         for ( ArtifactMappingProvider provider : artifactMappingProviders )
162         {
163             ext = provider.mapTypeToExtension( type );
164             if ( ext != null )
165             {
166                 break;
167             }
168         }
169         if ( ext == null )
170         {
171             ext = type;
172         }
173 
174         StringBuilder id = new StringBuilder();
175         if ( ( version != null ) && ( type != null ) )
176         {
177             id.append( artifactId ).append( ARTIFACT_SEPARATOR ).append( version );
178 
179             if ( StringUtils.isNotBlank( classifier ) )
180             {
181                 id.append( ARTIFACT_SEPARATOR ).append( classifier );
182             }
183 
184             id.append( "." ).append( ext );
185         }
186         return id.toString();
187     }
188 }