1 package org.apache.archiva.repository.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
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.metadata.repository.storage.RepositoryPathTranslator;
23 import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider;
24 import org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryPathTranslator;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.archiva.common.utils.VersionUtil;
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.layout.LayoutException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import javax.annotation.PostConstruct;
36 import javax.inject.Inject;
37 import java.util.List;
40 * AbstractDefaultRepositoryContent - common methods for working with default (maven 2) layout.
44 public abstract class AbstractDefaultRepositoryContent
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();
64 protected List<? extends ArtifactMappingProvider> artifactMappingProviders;
67 protected void initialize()
72 public ArtifactReference toArtifactReference( String path )
73 throws LayoutException
75 return defaultPathParser.toArtifactReference( path );
78 public String toMetadataPath( ProjectReference reference )
80 StringBuilder path = new StringBuilder();
82 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
83 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
84 path.append( MAVEN_METADATA );
86 return path.toString();
89 public String toMetadataPath( VersionedReference reference )
91 StringBuilder path = new StringBuilder();
93 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
94 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
95 if ( reference.getVersion() != null )
97 // add the version only if it is present
98 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
100 path.append( MAVEN_METADATA );
102 return path.toString();
105 public String toPath( ArchivaArtifact reference )
107 if ( reference == null )
109 throw new IllegalArgumentException( "ArchivaArtifact cannot be null" );
112 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
113 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
114 reference.getClassifier(), reference.getType() );
117 public String toPath( ArtifactReference reference )
119 if ( reference == null )
121 throw new IllegalArgumentException( "Artifact reference cannot be null" );
124 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
125 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
126 reference.getClassifier(), reference.getType() );
129 private String formatAsDirectory( String directory )
131 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
134 private String toPath( String groupId, String artifactId, String baseVersion, String version, String classifier,
137 if ( baseVersion != null )
139 return pathTranslator.toPath( groupId, artifactId, baseVersion,
140 constructId( artifactId, version, classifier, type ) );
144 return pathTranslator.toPath( groupId, artifactId );
148 // TODO: move into the Maven Artifact facet when refactoring away the caller - the caller will need to have access
149 // to the facet or filename (for the original ID)
150 private String constructId( String artifactId, String version, String classifier, String type )
153 for ( ArtifactMappingProvider provider : artifactMappingProviders )
155 ext = provider.mapTypeToExtension( type );
166 StringBuilder id = new StringBuilder();
167 if ( ( version != null ) && ( type != null ) )
169 id.append( artifactId ).append( ARTIFACT_SEPARATOR ).append( version );
171 if ( StringUtils.isNotBlank( classifier ) )
173 id.append( ARTIFACT_SEPARATOR ).append( classifier );
176 id.append( "." ).append( ext );
178 return id.toString();