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.configuration.FileTypes;
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.Maven2RepositoryPathTranslator;
27 import org.apache.archiva.model.ArchivaArtifact;
28 import org.apache.archiva.model.ArtifactReference;
29 import org.apache.archiva.model.ProjectReference;
30 import org.apache.archiva.model.VersionedReference;
31 import org.apache.archiva.repository.RepositoryContent;
32 import org.apache.archiva.repository.content.PathParser;
33 import org.apache.archiva.repository.layout.LayoutException;
34 import org.apache.commons.lang.StringUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import javax.annotation.PostConstruct;
39 import javax.inject.Inject;
40 import java.util.List;
43 * AbstractDefaultRepositoryContent - common methods for working with default (maven 2) layout.
45 public abstract class AbstractDefaultRepositoryContent implements RepositoryContent
49 protected Logger log = LoggerFactory.getLogger( getClass() );
51 public static final String MAVEN_METADATA = "maven-metadata.xml";
53 protected static final char PATH_SEPARATOR = '/';
55 protected static final char GROUP_SEPARATOR = '.';
57 protected static final char ARTIFACT_SEPARATOR = '-';
59 private RepositoryPathTranslator pathTranslator = new Maven2RepositoryPathTranslator();
61 private PathParser defaultPathParser = new DefaultPathParser();
68 protected List<? extends ArtifactMappingProvider> artifactMappingProviders;
70 AbstractDefaultRepositoryContent(List<? extends ArtifactMappingProvider> artifactMappingProviders) {
71 this.artifactMappingProviders = artifactMappingProviders;
74 public void setArtifactMappingProviders(List<? extends ArtifactMappingProvider> artifactMappingProviders) {
75 this.artifactMappingProviders = artifactMappingProviders;
78 public ArtifactReference toArtifactReference( String path )
79 throws LayoutException
81 return defaultPathParser.toArtifactReference( path );
84 public String toMetadataPath( ProjectReference reference )
86 StringBuilder path = new StringBuilder();
88 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
89 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
90 path.append( MAVEN_METADATA );
92 return path.toString();
95 public String toMetadataPath( VersionedReference reference )
97 StringBuilder path = new StringBuilder();
99 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
100 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
101 if ( reference.getVersion() != null )
103 // add the version only if it is present
104 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
106 path.append( MAVEN_METADATA );
108 return path.toString();
111 public String toPath( ArchivaArtifact reference )
113 if ( reference == null )
115 throw new IllegalArgumentException( "ArchivaArtifact cannot be null" );
118 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
119 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
120 reference.getClassifier(), reference.getType() );
123 public String toPath( ArtifactReference reference )
125 if ( reference == null )
127 throw new IllegalArgumentException( "Artifact reference cannot be null" );
129 if ( reference.getVersion() != null )
131 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
132 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
133 reference.getClassifier(), reference.getType() );
135 return toPath( reference.getGroupId(), reference.getArtifactId(), null, null,
136 reference.getClassifier(), reference.getType() );
139 private String formatAsDirectory( String directory )
141 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
144 private String toPath( String groupId, String artifactId, String baseVersion, String version, String classifier,
147 if ( baseVersion != null )
149 return pathTranslator.toPath( groupId, artifactId, baseVersion,
150 constructId( artifactId, version, classifier, type ) );
154 return pathTranslator.toPath( groupId, artifactId );
158 // TODO: move into the Maven Artifact facet when refactoring away the caller - the caller will need to have access
159 // to the facet or filename (for the original ID)
160 private String constructId( String artifactId, String version, String classifier, String type )
163 for ( ArtifactMappingProvider provider : artifactMappingProviders )
165 ext = provider.mapTypeToExtension( type );
176 StringBuilder id = new StringBuilder();
177 if ( ( version != null ) && ( type != null ) )
179 id.append( artifactId ).append( ARTIFACT_SEPARATOR ).append( version );
181 if ( StringUtils.isNotBlank( classifier ) )
183 id.append( ARTIFACT_SEPARATOR ).append( classifier );
186 id.append( "." ).append( ext );
188 return id.toString();