Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AbstractDefaultRepositoryContent.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package org.apache.archiva.repository.content.maven2;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.common.utils.VersionUtil;
  21. import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
  22. import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider;
  23. import org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryPathTranslator;
  24. import org.apache.archiva.model.ArchivaArtifact;
  25. import org.apache.archiva.model.ArtifactReference;
  26. import org.apache.archiva.model.ProjectReference;
  27. import org.apache.archiva.model.VersionedReference;
  28. import org.apache.archiva.repository.LayoutException;
  29. import org.apache.archiva.repository.RepositoryContent;
  30. import org.apache.archiva.repository.content.PathParser;
  31. import org.apache.commons.lang.StringUtils;
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;
  34. import java.util.List;
  35. /**
  36. * AbstractDefaultRepositoryContent - common methods for working with default (maven 2) layout.
  37. */
  38. public abstract class AbstractDefaultRepositoryContent implements RepositoryContent
  39. {
  40. protected Logger log = LoggerFactory.getLogger( getClass() );
  41. public static final String MAVEN_METADATA = "maven-metadata.xml";
  42. protected static final char PATH_SEPARATOR = '/';
  43. protected static final char GROUP_SEPARATOR = '.';
  44. protected static final char ARTIFACT_SEPARATOR = '-';
  45. private RepositoryPathTranslator pathTranslator = new Maven2RepositoryPathTranslator();
  46. private PathParser defaultPathParser = new DefaultPathParser();
  47. /**
  48. *
  49. */
  50. protected List<? extends ArtifactMappingProvider> artifactMappingProviders;
  51. AbstractDefaultRepositoryContent(List<? extends ArtifactMappingProvider> artifactMappingProviders) {
  52. this.artifactMappingProviders = artifactMappingProviders;
  53. }
  54. public void setArtifactMappingProviders(List<? extends ArtifactMappingProvider> artifactMappingProviders) {
  55. this.artifactMappingProviders = artifactMappingProviders;
  56. }
  57. public ArtifactReference toArtifactReference( String path )
  58. throws LayoutException
  59. {
  60. return defaultPathParser.toArtifactReference( path );
  61. }
  62. public String toMetadataPath( ProjectReference reference )
  63. {
  64. StringBuilder path = new StringBuilder();
  65. path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
  66. path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
  67. path.append( MAVEN_METADATA );
  68. return path.toString();
  69. }
  70. public String toMetadataPath( VersionedReference reference )
  71. {
  72. StringBuilder path = new StringBuilder();
  73. path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
  74. path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
  75. if ( reference.getVersion() != null )
  76. {
  77. // add the version only if it is present
  78. path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
  79. }
  80. path.append( MAVEN_METADATA );
  81. return path.toString();
  82. }
  83. public String toPath( ArchivaArtifact reference )
  84. {
  85. if ( reference == null )
  86. {
  87. throw new IllegalArgumentException( "ArchivaArtifact cannot be null" );
  88. }
  89. String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
  90. return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
  91. reference.getClassifier(), reference.getType() );
  92. }
  93. public String toPath( ArtifactReference reference )
  94. {
  95. if ( reference == null )
  96. {
  97. throw new IllegalArgumentException( "Artifact reference cannot be null" );
  98. }
  99. if ( reference.getVersion() != null )
  100. {
  101. String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
  102. return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
  103. reference.getClassifier(), reference.getType() );
  104. }
  105. return toPath( reference.getGroupId(), reference.getArtifactId(), null, null,
  106. reference.getClassifier(), reference.getType() );
  107. }
  108. private String formatAsDirectory( String directory )
  109. {
  110. return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
  111. }
  112. private String toPath( String groupId, String artifactId, String baseVersion, String version, String classifier,
  113. String type )
  114. {
  115. if ( baseVersion != null )
  116. {
  117. return pathTranslator.toPath( groupId, artifactId, baseVersion,
  118. constructId( artifactId, version, classifier, type ) );
  119. }
  120. else
  121. {
  122. return pathTranslator.toPath( groupId, artifactId );
  123. }
  124. }
  125. // TODO: move into the Maven Artifact facet when refactoring away the caller - the caller will need to have access
  126. // to the facet or filename (for the original ID)
  127. private String constructId( String artifactId, String version, String classifier, String type )
  128. {
  129. String ext = null;
  130. for ( ArtifactMappingProvider provider : artifactMappingProviders )
  131. {
  132. ext = provider.mapTypeToExtension( type );
  133. if ( ext != null )
  134. {
  135. break;
  136. }
  137. }
  138. if ( ext == null )
  139. {
  140. ext = type;
  141. }
  142. StringBuilder id = new StringBuilder();
  143. if ( ( version != null ) && ( type != null ) )
  144. {
  145. id.append( artifactId ).append( ARTIFACT_SEPARATOR ).append( version );
  146. if ( StringUtils.isNotBlank( classifier ) )
  147. {
  148. id.append( ARTIFACT_SEPARATOR ).append( classifier );
  149. }
  150. id.append( "." ).append( ext );
  151. }
  152. return id.toString();
  153. }
  154. }