You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ManagedRepositoryContentMock.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package org.apache.archiva.repository.mock;
  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.model.ArtifactMetadata;
  22. import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet;
  23. import org.apache.archiva.model.ArchivaArtifact;
  24. import org.apache.archiva.model.ArtifactReference;
  25. import org.apache.archiva.model.ProjectReference;
  26. import org.apache.archiva.model.VersionedReference;
  27. import org.apache.archiva.repository.*;
  28. import org.apache.archiva.repository.content.PathParser;
  29. import org.apache.commons.lang.StringUtils;
  30. import org.springframework.stereotype.Service;
  31. import java.nio.file.Path;
  32. import java.nio.file.Paths;
  33. import java.util.HashMap;
  34. import java.util.Map;
  35. import java.util.Set;
  36. import java.util.regex.Matcher;
  37. import java.util.regex.Pattern;
  38. /**
  39. * @author Martin Stockhammer <martin_s@apache.org>
  40. */
  41. @Service("managedRepositoryContent#mock")
  42. public class ManagedRepositoryContentMock implements ManagedRepositoryContent
  43. {
  44. private static final String PATH_SEPARATOR = "/";
  45. private static final String GROUP_SEPARATOR = ".";
  46. public static final String MAVEN_METADATA = "maven-metadata.xml";
  47. private ManagedRepository repository;
  48. ManagedRepositoryContentMock(ManagedRepository repo) {
  49. this.repository = repo;
  50. }
  51. @Override
  52. public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException
  53. {
  54. }
  55. @Override
  56. public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException
  57. {
  58. }
  59. @Override
  60. public void deleteGroupId( String groupId ) throws ContentNotFoundException
  61. {
  62. }
  63. @Override
  64. public void deleteProject( String namespace, String projectId ) throws RepositoryException
  65. {
  66. }
  67. @Override
  68. public String getId( )
  69. {
  70. return repository.getId();
  71. }
  72. @Override
  73. public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference ) throws ContentNotFoundException
  74. {
  75. return null;
  76. }
  77. @Override
  78. public String getRepoRoot( )
  79. {
  80. return Paths.get("", "target", "test-repository", "managed").toString();
  81. }
  82. @Override
  83. public ManagedRepository getRepository( )
  84. {
  85. return repository;
  86. }
  87. @Override
  88. public Set<String> getVersions( ProjectReference reference ) throws ContentNotFoundException, LayoutException
  89. {
  90. return null;
  91. }
  92. @Override
  93. public Set<String> getVersions( VersionedReference reference ) throws ContentNotFoundException
  94. {
  95. return null;
  96. }
  97. @Override
  98. public boolean hasContent( ArtifactReference reference )
  99. {
  100. return false;
  101. }
  102. @Override
  103. public boolean hasContent( ProjectReference reference )
  104. {
  105. return false;
  106. }
  107. @Override
  108. public boolean hasContent( VersionedReference reference )
  109. {
  110. return false;
  111. }
  112. @Override
  113. public void setRepository( ManagedRepository repo )
  114. {
  115. this.repository = repo;
  116. }
  117. private Map<ArtifactReference, String> refs = new HashMap<>();
  118. @Override
  119. public ArtifactReference toArtifactReference( String path ) throws LayoutException
  120. {
  121. if ( StringUtils.isBlank( path ) )
  122. {
  123. throw new LayoutException( "Unable to convert blank path." );
  124. }
  125. ArtifactMetadata metadata = getArtifactForPath("test-repository", path);
  126. ArtifactReference artifact = new ArtifactReference();
  127. artifact.setGroupId( metadata.getNamespace() );
  128. artifact.setArtifactId( metadata.getProject() );
  129. artifact.setVersion( metadata.getVersion() );
  130. MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
  131. if ( facet != null )
  132. {
  133. artifact.setClassifier( facet.getClassifier() );
  134. artifact.setType( facet.getType() );
  135. }
  136. refs.put(artifact, path);
  137. return artifact;
  138. }
  139. public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
  140. {
  141. String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
  142. int len = parts.length;
  143. if ( len < 4 )
  144. {
  145. throw new IllegalArgumentException(
  146. "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
  147. }
  148. String id = parts[--len];
  149. String baseVersion = parts[--len];
  150. String artifactId = parts[--len];
  151. StringBuilder groupIdBuilder = new StringBuilder();
  152. for ( int i = 0; i < len - 1; i++ )
  153. {
  154. groupIdBuilder.append( parts[i] );
  155. groupIdBuilder.append( '.' );
  156. }
  157. groupIdBuilder.append( parts[len - 1] );
  158. return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
  159. }
  160. private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
  161. public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
  162. String id )
  163. {
  164. if ( !id.startsWith( projectId + "-" ) )
  165. {
  166. throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
  167. + "' doesn't start with artifact ID '" + projectId + "'" );
  168. }
  169. MavenArtifactFacet facet = new MavenArtifactFacet();
  170. int index = projectId.length() + 1;
  171. String version;
  172. String idSubStrFromVersion = id.substring( index );
  173. if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
  174. {
  175. // non-snapshot versions, or non-timestamped snapshot versions
  176. version = projectVersion;
  177. }
  178. else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
  179. {
  180. // timestamped snapshots
  181. try
  182. {
  183. int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
  184. if ( mainVersionLength == 0 )
  185. {
  186. throw new IllegalArgumentException(
  187. "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
  188. }
  189. Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
  190. m.matches();
  191. String timestamp = m.group( 1 );
  192. String buildNumber = m.group( 2 );
  193. facet.setTimestamp( timestamp );
  194. facet.setBuildNumber( Integer.parseInt( buildNumber ) );
  195. version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
  196. }
  197. catch ( IllegalStateException e )
  198. {
  199. throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
  200. + "' doesn't contain a timestamped version matching snapshot '"
  201. + projectVersion + "'", e);
  202. }
  203. }
  204. else
  205. {
  206. // invalid
  207. throw new IllegalArgumentException(
  208. "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
  209. + projectVersion + "'" );
  210. }
  211. String classifier;
  212. String ext;
  213. index += version.length();
  214. if ( index == id.length() )
  215. {
  216. // no classifier or extension
  217. classifier = null;
  218. ext = null;
  219. }
  220. else
  221. {
  222. char c = id.charAt( index );
  223. if ( c == '-' )
  224. {
  225. // classifier up until '.'
  226. int extIndex = id.indexOf( '.', index );
  227. if ( extIndex >= 0 )
  228. {
  229. classifier = id.substring( index + 1, extIndex );
  230. ext = id.substring( extIndex + 1 );
  231. }
  232. else
  233. {
  234. classifier = id.substring( index + 1 );
  235. ext = null;
  236. }
  237. }
  238. else if ( c == '.' )
  239. {
  240. // rest is the extension
  241. classifier = null;
  242. ext = id.substring( index + 1 );
  243. }
  244. else
  245. {
  246. throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
  247. + "' expected classifier or extension but got '"
  248. + id.substring( index ) + "'" );
  249. }
  250. }
  251. ArtifactMetadata metadata = new ArtifactMetadata();
  252. metadata.setId( id );
  253. metadata.setNamespace( namespace );
  254. metadata.setProject( projectId );
  255. metadata.setRepositoryId( repoId );
  256. metadata.setProjectVersion( projectVersion );
  257. metadata.setVersion( version );
  258. facet.setClassifier( classifier );
  259. // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
  260. // to select the correct order to apply multiple extensions mappings to a preferred type
  261. // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
  262. // perhaps the plugins could register missing entries in configuration, then we just use configuration
  263. // here?
  264. String type = null;
  265. // use extension as default
  266. if ( type == null )
  267. {
  268. type = ext;
  269. }
  270. // TODO: should we allow this instead?
  271. if ( type == null )
  272. {
  273. throw new IllegalArgumentException(
  274. "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
  275. }
  276. facet.setType( type );
  277. metadata.addFacet( facet );
  278. return metadata;
  279. }
  280. @Override
  281. public Path toFile( ArtifactReference reference )
  282. {
  283. return Paths.get(getRepoRoot(), refs.get(reference));
  284. }
  285. @Override
  286. public Path toFile( ArchivaArtifact reference )
  287. {
  288. return null;
  289. }
  290. private String formatAsDirectory( String directory )
  291. {
  292. return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
  293. }
  294. public String toMetadataPath( ProjectReference reference )
  295. {
  296. StringBuilder path = new StringBuilder();
  297. path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
  298. path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
  299. path.append( MAVEN_METADATA );
  300. return path.toString();
  301. }
  302. public String toMetadataPath( VersionedReference reference )
  303. {
  304. StringBuilder path = new StringBuilder();
  305. path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
  306. path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
  307. if ( reference.getVersion() != null )
  308. {
  309. // add the version only if it is present
  310. path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
  311. }
  312. path.append( MAVEN_METADATA );
  313. return path.toString();
  314. }
  315. @Override
  316. public String toPath( ArtifactReference reference )
  317. {
  318. return null;
  319. }
  320. @Override
  321. public String toPath( ArchivaArtifact reference )
  322. {
  323. return null;
  324. }
  325. }