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.common.utils.VersionUtil;
22 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
23 import org.apache.archiva.repository.maven.metadata.storage.ArtifactMappingProvider;
24 import org.apache.archiva.repository.maven.metadata.storage.Maven2RepositoryPathTranslator;
25 import org.apache.archiva.model.ArchivaArtifact;
26 import org.apache.archiva.model.ArtifactReference;
27 import org.apache.archiva.model.ProjectReference;
28 import org.apache.archiva.model.VersionedReference;
29 import org.apache.archiva.repository.LayoutException;
30 import org.apache.archiva.repository.RepositoryContent;
31 import org.apache.archiva.repository.content.ItemSelector;
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();
61 PathParser getPathParser() {
62 return defaultPathParser;
70 protected List<? extends ArtifactMappingProvider> artifactMappingProviders;
72 AbstractDefaultRepositoryContent(List<? extends ArtifactMappingProvider> artifactMappingProviders) {
73 this.artifactMappingProviders = artifactMappingProviders;
76 public void setArtifactMappingProviders(List<? extends ArtifactMappingProvider> artifactMappingProviders) {
77 this.artifactMappingProviders = artifactMappingProviders;
81 public ItemSelector toItemSelector( String path ) throws LayoutException
83 return defaultPathParser.toItemSelector( path );
86 public String toPath ( ProjectReference reference) {
87 final StringBuilder path = new StringBuilder();
88 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
89 path.append( reference.getArtifactId( ) );
90 return path.toString( );
94 public String toPath ( ItemSelector selector ) {
96 throw new IllegalArgumentException( "ItemSelector must not be null." );
99 // Initialize the project id if not set
100 if (selector.hasProjectId()) {
101 projectId = selector.getProjectId( );
102 } else if (selector.hasArtifactId()) {
103 // projectId same as artifact id, if set
104 projectId = selector.getArtifactId( );
106 // we arrive here, if projectId && artifactId not set
107 return pathTranslator.toPath( selector.getNamespace(), "");
109 if ( !selector.hasArtifactId( )) {
110 return pathTranslator.toPath( selector.getNamespace( ), projectId );
112 // this part only, if projectId && artifactId is set
113 String artifactVersion = "";
115 if (selector.hasVersion() && selector.hasArtifactVersion() ) {
116 artifactVersion = selector.getArtifactVersion();
117 version = VersionUtil.getBaseVersion( selector.getVersion( ) );
118 } else if (!selector.hasVersion() && selector.hasArtifactVersion()) {
119 // we try to retrieve the base version, if artifact version is only set
120 version = VersionUtil.getBaseVersion( selector.getArtifactVersion( ) );
121 artifactVersion = selector.getArtifactVersion( );
122 } else if (selector.hasVersion() && !selector.hasArtifactVersion()) {
123 artifactVersion = selector.getVersion();
124 version = VersionUtil.getBaseVersion( selector.getVersion( ) );
127 return pathTranslator.toPath( selector.getNamespace(), projectId, version,
128 constructId( selector.getArtifactId(), artifactVersion, selector.getClassifier(), selector.getType() ) );
133 public String toMetadataPath( ProjectReference reference )
135 final StringBuilder path = new StringBuilder();
136 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
137 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
138 path.append( MAVEN_METADATA );
139 return path.toString();
142 public String toPath( String namespace )
144 return formatAsDirectory( namespace );
147 public String toPath( VersionedReference reference )
149 final StringBuilder path = new StringBuilder();
150 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
151 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
152 if ( reference.getVersion() != null )
154 // add the version only if it is present
155 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) );
157 return path.toString();
160 public String toMetadataPath( VersionedReference reference )
162 StringBuilder path = new StringBuilder();
164 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
165 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
166 if ( reference.getVersion() != null )
168 // add the version only if it is present
169 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
171 path.append( MAVEN_METADATA );
173 return path.toString();
176 public String toPath( ArchivaArtifact reference )
178 if ( reference == null )
180 throw new IllegalArgumentException( "ArchivaArtifact cannot be null" );
183 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
184 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
185 reference.getClassifier(), reference.getType() );
189 public String toPath( ArtifactReference reference )
191 if ( reference == null )
193 throw new IllegalArgumentException( "Artifact reference cannot be null" );
195 if ( reference.getVersion() != null )
197 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
198 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
199 reference.getClassifier(), reference.getType() );
201 return toPath( reference.getGroupId(), reference.getArtifactId(), null, null,
202 reference.getClassifier(), reference.getType() );
207 protected String formatAsDirectory( String directory )
209 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
212 private String toPath( String groupId, String artifactId, String baseVersion, String version, String classifier,
215 if ( baseVersion != null )
217 return pathTranslator.toPath( groupId, artifactId, baseVersion,
218 constructId( artifactId, version, classifier, type ) );
222 return pathTranslator.toPath( groupId, artifactId );
226 // TODO: move into the Maven Artifact facet when refactoring away the caller - the caller will need to have access
227 // to the facet or filename (for the original ID)
228 private String constructId( String artifactId, String version, String classifier, String type )
231 for ( ArtifactMappingProvider provider : artifactMappingProviders )
233 ext = provider.mapTypeToExtension( type );
244 StringBuilder id = new StringBuilder();
245 if ( ( version != null ) && ( type != null ) )
247 id.append( artifactId ).append( ARTIFACT_SEPARATOR ).append( version );
249 if ( StringUtils.isNotBlank( classifier ) )
251 id.append( ARTIFACT_SEPARATOR ).append( classifier );
254 id.append( "." ).append( ext );
256 return id.toString();