1 package org.apache.maven.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.Maven2RepositoryPathTranslator;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.maven.archiva.common.utils.VersionUtil;
26 import org.apache.maven.archiva.model.ArchivaArtifact;
27 import org.apache.maven.archiva.model.ArtifactReference;
28 import org.apache.maven.archiva.model.ProjectReference;
29 import org.apache.maven.archiva.model.VersionedReference;
30 import org.apache.maven.archiva.repository.layout.LayoutException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * AbstractDefaultRepositoryContent - common methods for working with default (maven 2) layout.
39 public abstract class AbstractDefaultRepositoryContent
41 protected Logger log = LoggerFactory.getLogger( AbstractDefaultRepositoryContent.class );
43 public static final String MAVEN_METADATA = "maven-metadata.xml";
45 protected static final char PATH_SEPARATOR = '/';
47 protected static final char GROUP_SEPARATOR = '.';
49 protected static final char ARTIFACT_SEPARATOR = '-';
51 private RepositoryPathTranslator pathTranslator = new Maven2RepositoryPathTranslator();
53 private PathParser defaultPathParser = new DefaultPathParser();
55 public ArtifactReference toArtifactReference( String path )
56 throws LayoutException
58 return defaultPathParser.toArtifactReference( path );
61 public String toMetadataPath( ProjectReference reference )
63 StringBuffer path = new StringBuffer();
65 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
66 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
67 path.append( MAVEN_METADATA );
69 return path.toString();
72 public String toMetadataPath( VersionedReference reference )
74 StringBuffer path = new StringBuffer();
76 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
77 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
78 if ( reference.getVersion() != null )
80 // add the version only if it is present
81 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
83 path.append( MAVEN_METADATA );
85 return path.toString();
88 public String toPath( ArchivaArtifact reference )
90 if ( reference == null )
92 throw new IllegalArgumentException( "ArchivaArtifact cannot be null" );
95 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
96 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
97 reference.getClassifier(), reference.getType() );
100 public String toPath( ArtifactReference reference )
102 if ( reference == null )
104 throw new IllegalArgumentException( "Artifact reference cannot be null" );
107 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
108 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
109 reference.getClassifier(), reference.getType() );
112 private String formatAsDirectory( String directory )
114 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
117 private String toPath( String groupId, String artifactId, String baseVersion, String version, String classifier,
120 if ( baseVersion != null )
122 return pathTranslator.toPath( groupId, artifactId, baseVersion, constructId( artifactId, version,
123 classifier, type ) );
127 return pathTranslator.toPath( groupId, artifactId );
131 // TODO: move into the Maven Artifact facet when refactoring away the caller - the caller will need to have access
132 // to the facet or filename (for the original ID)
133 private String constructId( String artifactId, String version, String classifier, String type )
135 StringBuilder id = new StringBuilder();
136 if ( ( version != null ) && ( type != null ) )
138 id.append( artifactId ).append( ARTIFACT_SEPARATOR ).append( version );
140 if ( StringUtils.isNotBlank( classifier ) )
142 id.append( ARTIFACT_SEPARATOR ).append( classifier );
145 id.append( "." ).append( ArtifactExtensionMapping.getExtension( type ) );
147 return id.toString();