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.commons.lang.StringUtils;
23 import org.apache.maven.archiva.common.utils.VersionUtil;
24 import org.apache.maven.archiva.model.ArchivaArtifact;
25 import org.apache.maven.archiva.model.ArtifactReference;
26 import org.apache.maven.archiva.model.ProjectReference;
27 import org.apache.maven.archiva.model.VersionedReference;
28 import org.apache.maven.archiva.repository.layout.LayoutException;
31 * AbstractDefaultRepositoryContent - common methods for working with default (maven 2) layout.
33 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
36 public abstract class AbstractDefaultRepositoryContent
38 public static final String MAVEN_METADATA = "maven-metadata.xml";
40 protected static final char PATH_SEPARATOR = '/';
42 protected static final char GROUP_SEPARATOR = '.';
44 protected static final char ARTIFACT_SEPARATOR = '-';
46 public ArtifactReference toArtifactReference( String path )
47 throws LayoutException
49 return DefaultPathParser.toArtifactReference( path );
52 public String toMetadataPath( ProjectReference reference )
54 StringBuffer path = new StringBuffer();
56 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
57 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
58 path.append( MAVEN_METADATA );
60 return path.toString();
63 public String toMetadataPath( VersionedReference reference )
65 StringBuffer path = new StringBuffer();
67 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
68 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
69 if ( reference.getVersion() != null )
71 // add the version only if it is present
72 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
74 path.append( MAVEN_METADATA );
76 return path.toString();
79 public String toPath( ArchivaArtifact reference )
81 if ( reference == null )
83 throw new IllegalArgumentException( "ArchivaArtifact cannot be null" );
86 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
87 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
88 reference.getClassifier(), reference.getType() );
91 public String toPath( ArtifactReference reference )
93 if ( reference == null )
95 throw new IllegalArgumentException( "Artifact reference cannot be null" );
98 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
99 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
100 reference.getClassifier(), reference.getType() );
103 private String formatAsDirectory( String directory )
105 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
108 private String toPath( String groupId, String artifactId, String baseVersion, String version, String classifier,
111 StringBuffer path = new StringBuffer();
113 path.append( formatAsDirectory( groupId ) ).append( PATH_SEPARATOR );
114 path.append( artifactId ).append( PATH_SEPARATOR );
116 if ( baseVersion != null )
118 path.append( baseVersion ).append( PATH_SEPARATOR );
119 if ( ( version != null ) && ( type != null ) )
121 path.append( artifactId ).append( ARTIFACT_SEPARATOR ).append( version );
123 if ( StringUtils.isNotBlank( classifier ) )
125 path.append( ARTIFACT_SEPARATOR ).append( classifier );
128 path.append( GROUP_SEPARATOR ).append( ArtifactExtensionMapping.getExtension( type ) );
132 return path.toString();