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.content.PathParser;
31 import org.apache.archiva.repository.layout.LayoutException;
32 import org.apache.commons.lang.StringUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import javax.annotation.PostConstruct;
37 import javax.inject.Inject;
38 import java.util.List;
41 * AbstractDefaultRepositoryContent - common methods for working with default (maven 2) layout.
43 public abstract class AbstractDefaultRepositoryContent
45 protected Logger log = LoggerFactory.getLogger( getClass() );
47 public static final String MAVEN_METADATA = "maven-metadata.xml";
49 protected static final char PATH_SEPARATOR = '/';
51 protected static final char GROUP_SEPARATOR = '.';
53 protected static final char ARTIFACT_SEPARATOR = '-';
55 private RepositoryPathTranslator pathTranslator = new Maven2RepositoryPathTranslator();
57 private PathParser defaultPathParser = new DefaultPathParser();
63 protected List<? extends ArtifactMappingProvider> artifactMappingProviders;
66 protected void initialize()
71 public ArtifactReference toArtifactReference( String path )
72 throws LayoutException
74 return defaultPathParser.toArtifactReference( path );
77 public String toMetadataPath( ProjectReference reference )
79 StringBuilder path = new StringBuilder();
81 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
82 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
83 path.append( MAVEN_METADATA );
85 return path.toString();
88 public String toMetadataPath( VersionedReference reference )
90 StringBuilder path = new StringBuilder();
92 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
93 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
94 if ( reference.getVersion() != null )
96 // add the version only if it is present
97 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
99 path.append( MAVEN_METADATA );
101 return path.toString();
104 public String toPath( ArchivaArtifact reference )
106 if ( reference == null )
108 throw new IllegalArgumentException( "ArchivaArtifact cannot be null" );
111 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
112 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
113 reference.getClassifier(), reference.getType() );
116 public String toPath( ArtifactReference reference )
118 if ( reference == null )
120 throw new IllegalArgumentException( "Artifact reference cannot be null" );
122 if ( reference.getVersion() != null )
124 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
125 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
126 reference.getClassifier(), reference.getType() );
128 return toPath( reference.getGroupId(), reference.getArtifactId(), null, null,
129 reference.getClassifier(), reference.getType() );
132 private String formatAsDirectory( String directory )
134 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
137 private String toPath( String groupId, String artifactId, String baseVersion, String version, String classifier,
140 if ( baseVersion != null )
142 return pathTranslator.toPath( groupId, artifactId, baseVersion,
143 constructId( artifactId, version, classifier, type ) );
147 return pathTranslator.toPath( groupId, artifactId );
151 // TODO: move into the Maven Artifact facet when refactoring away the caller - the caller will need to have access
152 // to the facet or filename (for the original ID)
153 private String constructId( String artifactId, String version, String classifier, String type )
156 for ( ArtifactMappingProvider provider : artifactMappingProviders )
158 ext = provider.mapTypeToExtension( type );
169 StringBuilder id = new StringBuilder();
170 if ( ( version != null ) && ( type != null ) )
172 id.append( artifactId ).append( ARTIFACT_SEPARATOR ).append( version );
174 if ( StringUtils.isNotBlank( classifier ) )
176 id.append( ARTIFACT_SEPARATOR ).append( classifier );
179 id.append( "." ).append( ext );
181 return id.toString();