1 package org.apache.archiva.repository.content.maven2;
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
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
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;
37 import java.util.List;
40 * AbstractDefaultRepositoryContent - common methods for working with default (maven 2) layout.
42 public abstract class AbstractDefaultRepositoryContent implements RepositoryContent
46 protected Logger log = LoggerFactory.getLogger( getClass() );
48 public static final String MAVEN_METADATA = "maven-metadata.xml";
50 protected static final char PATH_SEPARATOR = '/';
52 protected static final char GROUP_SEPARATOR = '.';
54 protected static final char ARTIFACT_SEPARATOR = '-';
56 private RepositoryPathTranslator pathTranslator = new Maven2RepositoryPathTranslator();
58 private PathParser defaultPathParser = new DefaultPathParser();
65 protected List<? extends ArtifactMappingProvider> artifactMappingProviders;
67 AbstractDefaultRepositoryContent(List<? extends ArtifactMappingProvider> artifactMappingProviders) {
68 this.artifactMappingProviders = artifactMappingProviders;
71 public void setArtifactMappingProviders(List<? extends ArtifactMappingProvider> artifactMappingProviders) {
72 this.artifactMappingProviders = artifactMappingProviders;
76 public ArtifactReference toArtifactReference( String path )
77 throws LayoutException
79 return defaultPathParser.toArtifactReference( path );
82 public String toPath (ProjectReference reference) {
83 final StringBuilder path = new StringBuilder();
84 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
85 path.append( reference.getArtifactId( ) );
86 return path.toString( );
89 public String toMetadataPath( ProjectReference reference )
91 final StringBuilder path = new StringBuilder();
92 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
93 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
94 path.append( MAVEN_METADATA );
95 return path.toString();
98 public String toPath( String namespace )
100 return formatAsDirectory( namespace );
103 public String toPath( VersionedReference reference )
105 final StringBuilder path = new StringBuilder();
106 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
107 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
108 if ( reference.getVersion() != null )
110 // add the version only if it is present
111 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) );
113 return path.toString();
116 public String toMetadataPath( VersionedReference reference )
118 StringBuilder path = new StringBuilder();
120 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
121 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
122 if ( reference.getVersion() != null )
124 // add the version only if it is present
125 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
127 path.append( MAVEN_METADATA );
129 return path.toString();
132 public String toPath( ArchivaArtifact reference )
134 if ( reference == null )
136 throw new IllegalArgumentException( "ArchivaArtifact cannot be null" );
139 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
140 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
141 reference.getClassifier(), reference.getType() );
144 public String toPath( ArtifactReference reference )
146 if ( reference == null )
148 throw new IllegalArgumentException( "Artifact reference cannot be null" );
150 if ( reference.getVersion() != null )
152 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
153 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
154 reference.getClassifier(), reference.getType() );
156 return toPath( reference.getGroupId(), reference.getArtifactId(), null, null,
157 reference.getClassifier(), reference.getType() );
160 private String formatAsDirectory( String directory )
162 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
165 private String toPath( String groupId, String artifactId, String baseVersion, String version, String classifier,
168 if ( baseVersion != null )
170 return pathTranslator.toPath( groupId, artifactId, baseVersion,
171 constructId( artifactId, version, classifier, type ) );
175 return pathTranslator.toPath( groupId, artifactId );
179 // TODO: move into the Maven Artifact facet when refactoring away the caller - the caller will need to have access
180 // to the facet or filename (for the original ID)
181 private String constructId( String artifactId, String version, String classifier, String type )
184 for ( ArtifactMappingProvider provider : artifactMappingProviders )
186 ext = provider.mapTypeToExtension( type );
197 StringBuilder id = new StringBuilder();
198 if ( ( version != null ) && ( type != null ) )
200 id.append( artifactId ).append( ARTIFACT_SEPARATOR ).append( version );
202 if ( StringUtils.isNotBlank( classifier ) )
204 id.append( ARTIFACT_SEPARATOR ).append( classifier );
207 id.append( "." ).append( ext );
209 return id.toString();