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.ItemSelector;
33 import org.apache.archiva.repository.content.PathParser;
34 import org.apache.archiva.repository.content.Version;
35 import org.apache.commons.lang3.StringUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import java.util.List;
42 * AbstractDefaultRepositoryContent - common methods for working with default (maven 2) layout.
44 public abstract class AbstractDefaultRepositoryContent implements RepositoryContent
48 protected Logger log = LoggerFactory.getLogger( getClass() );
50 public static final String MAVEN_METADATA = "maven-metadata.xml";
52 protected static final char PATH_SEPARATOR = '/';
54 protected static final char GROUP_SEPARATOR = '.';
56 protected static final char ARTIFACT_SEPARATOR = '-';
58 private RepositoryPathTranslator pathTranslator = new Maven2RepositoryPathTranslator();
60 private PathParser defaultPathParser = new DefaultPathParser();
67 protected List<? extends ArtifactMappingProvider> artifactMappingProviders;
69 AbstractDefaultRepositoryContent(List<? extends ArtifactMappingProvider> artifactMappingProviders) {
70 this.artifactMappingProviders = artifactMappingProviders;
73 public void setArtifactMappingProviders(List<? extends ArtifactMappingProvider> artifactMappingProviders) {
74 this.artifactMappingProviders = artifactMappingProviders;
78 public ArtifactReference toArtifactReference( String path )
79 throws LayoutException
81 return defaultPathParser.toArtifactReference( path );
84 public String toPath (ProjectReference reference) {
85 final StringBuilder path = new StringBuilder();
86 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
87 path.append( reference.getArtifactId( ) );
88 return path.toString( );
92 public String toPath ( ItemSelector selector ) {
94 throw new IllegalArgumentException( "ItemSelector must not be null." );
97 // Initialize the project id if not set
98 if (selector.hasProjectId()) {
99 projectId = selector.getProjectId( );
100 } else if (selector.hasArtifactId()) {
101 // projectId same as artifact id, if set
102 projectId = selector.getArtifactId( );
104 // we arrive here, if projectId && artifactId not set
105 return pathTranslator.toPath( selector.getNamespace(), "");
107 if ( !selector.hasArtifactId( )) {
108 return pathTranslator.toPath( selector.getNamespace( ), projectId );
110 // this part only, if projectId && artifactId is set
111 String artifactVersion = "";
113 if (selector.hasVersion() && selector.hasArtifactVersion() ) {
114 artifactVersion = selector.getArtifactVersion();
115 version = VersionUtil.getBaseVersion( selector.getVersion( ) );
116 } else if (!selector.hasVersion() && selector.hasArtifactVersion()) {
117 // we try to retrieve the base version, if artifact version is only set
118 version = VersionUtil.getBaseVersion( selector.getArtifactVersion( ) );
119 artifactVersion = selector.getArtifactVersion( );
120 } else if (selector.hasVersion() && !selector.hasArtifactVersion()) {
121 artifactVersion = selector.getVersion();
122 version = VersionUtil.getBaseVersion( selector.getVersion( ) );
125 return pathTranslator.toPath( selector.getNamespace(), projectId, version,
126 constructId( selector.getArtifactId(), artifactVersion, selector.getClassifier(), selector.getType() ) );
130 public String toMetadataPath( ProjectReference reference )
132 final StringBuilder path = new StringBuilder();
133 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
134 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
135 path.append( MAVEN_METADATA );
136 return path.toString();
139 public String toPath( String namespace )
141 return formatAsDirectory( namespace );
144 public String toPath( VersionedReference reference )
146 final StringBuilder path = new StringBuilder();
147 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
148 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
149 if ( reference.getVersion() != null )
151 // add the version only if it is present
152 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) );
154 return path.toString();
157 public String toMetadataPath( VersionedReference reference )
159 StringBuilder path = new StringBuilder();
161 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
162 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
163 if ( reference.getVersion() != null )
165 // add the version only if it is present
166 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
168 path.append( MAVEN_METADATA );
170 return path.toString();
173 public String toPath( ArchivaArtifact reference )
175 if ( reference == null )
177 throw new IllegalArgumentException( "ArchivaArtifact cannot be null" );
180 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
181 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
182 reference.getClassifier(), reference.getType() );
186 public String toPath( ArtifactReference reference )
188 if ( reference == null )
190 throw new IllegalArgumentException( "Artifact reference cannot be null" );
192 if ( reference.getVersion() != null )
194 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
195 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
196 reference.getClassifier(), reference.getType() );
198 return toPath( reference.getGroupId(), reference.getArtifactId(), null, null,
199 reference.getClassifier(), reference.getType() );
202 private String formatAsDirectory( String directory )
204 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
207 private String toPath( String groupId, String artifactId, String baseVersion, String version, String classifier,
210 if ( baseVersion != null )
212 return pathTranslator.toPath( groupId, artifactId, baseVersion,
213 constructId( artifactId, version, classifier, type ) );
217 return pathTranslator.toPath( groupId, artifactId );
221 // TODO: move into the Maven Artifact facet when refactoring away the caller - the caller will need to have access
222 // to the facet or filename (for the original ID)
223 private String constructId( String artifactId, String version, String classifier, String type )
226 for ( ArtifactMappingProvider provider : artifactMappingProviders )
228 ext = provider.mapTypeToExtension( type );
239 StringBuilder id = new StringBuilder();
240 if ( ( version != null ) && ( type != null ) )
242 id.append( artifactId ).append( ARTIFACT_SEPARATOR ).append( version );
244 if ( StringUtils.isNotBlank( classifier ) )
246 id.append( ARTIFACT_SEPARATOR ).append( classifier );
249 id.append( "." ).append( ext );
251 return id.toString();