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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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.filelock.DefaultFileLockManager;
  21. import org.apache.archiva.common.utils.VersionUtil;
  22. import org.apache.archiva.metadata.model.ArtifactMetadata;
  23. import org.apache.archiva.metadata.maven.model.MavenArtifactFacet;
  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.*;
  29. import org.apache.archiva.repository.content.Artifact;
  30. import org.apache.archiva.repository.content.ContentItem;
  31. import org.apache.archiva.repository.content.ItemNotFoundException;
  32. import org.apache.archiva.repository.content.ItemSelector;
  33. import org.apache.archiva.repository.content.Namespace;
  34. import org.apache.archiva.repository.content.Project;
  35. import org.apache.archiva.repository.content.Version;
  36. import org.apache.archiva.repository.storage.fs.FilesystemStorage;
  37. import org.apache.archiva.repository.storage.RepositoryStorage;
  38. import org.apache.archiva.repository.storage.StorageAsset;
  39. import org.apache.commons.lang3.StringUtils;
  40. import org.springframework.stereotype.Service;
  41. import java.io.IOException;
  42. import java.nio.file.Path;
  43. import java.nio.file.Paths;
  44. import java.util.HashMap;
  45. import java.util.List;
  46. import java.util.Map;
  47. import java.util.Set;
  48. import java.util.regex.Matcher;
  49. import java.util.regex.Pattern;
  50. import java.util.stream.Stream;
  51. /**
  52. * @author Martin Stockhammer <martin_s@apache.org>
  53. */
  54. @Service("managedRepositoryContent#mock")
  55. public class ManagedRepositoryContentMock implements ManagedRepositoryContent
  56. {
  57. private static final String PATH_SEPARATOR = "/";
  58. private static final String GROUP_SEPARATOR = ".";
  59. public static final String MAVEN_METADATA = "maven-metadata.xml";
  60. private ManagedRepository repository;
  61. private RepositoryStorage fsStorage;
  62. ManagedRepositoryContentMock(ManagedRepository repo) {
  63. this.repository = repo;
  64. this.fsStorage = repo;
  65. }
  66. @Override
  67. public VersionedReference toVersion( String groupId, String artifactId, String version )
  68. {
  69. return null;
  70. }
  71. @Override
  72. public VersionedReference toGenericVersion( ArtifactReference artifactReference )
  73. {
  74. return null;
  75. }
  76. @Override
  77. public VersionedReference toVersion( ArtifactReference artifactReference )
  78. {
  79. return null;
  80. }
  81. @Override
  82. public ArtifactReference toArtifact( String groupId, String artifactId, String version, String type, String classifier )
  83. {
  84. return null;
  85. }
  86. @Override
  87. public void deleteItem( ContentItem item ) throws ItemNotFoundException, ContentAccessException
  88. {
  89. }
  90. @Override
  91. public ContentItem getItem( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
  92. {
  93. return null;
  94. }
  95. @Override
  96. public Namespace getNamespace( ItemSelector namespaceSelector ) throws ContentAccessException, IllegalArgumentException
  97. {
  98. return null;
  99. }
  100. @Override
  101. public Project getProject( ItemSelector projectSelector ) throws ContentAccessException, IllegalArgumentException
  102. {
  103. return null;
  104. }
  105. @Override
  106. public Version getVersion( ItemSelector versionCoordinates ) throws ContentAccessException, IllegalArgumentException
  107. {
  108. return null;
  109. }
  110. @Override
  111. public Artifact getArtifact( ItemSelector selector ) throws ContentAccessException
  112. {
  113. return null;
  114. }
  115. @Override
  116. public List<? extends Artifact> getArtifacts( ItemSelector selector ) throws ContentAccessException
  117. {
  118. return null;
  119. }
  120. @Override
  121. public Stream<? extends Artifact> newArtifactStream( ItemSelector selector ) throws ContentAccessException
  122. {
  123. return null;
  124. }
  125. @Override
  126. public List<? extends Project> getProjects( Namespace namespace ) throws ContentAccessException
  127. {
  128. return null;
  129. }
  130. @Override
  131. public List<? extends Project> getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
  132. {
  133. return null;
  134. }
  135. @Override
  136. public List<? extends Version> getVersions( Project project ) throws ContentAccessException
  137. {
  138. return null;
  139. }
  140. @Override
  141. public List<? extends Version> getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
  142. {
  143. return null;
  144. }
  145. @Override
  146. public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
  147. {
  148. return null;
  149. }
  150. @Override
  151. public Stream<? extends Artifact> newArtifactStream( ContentItem item ) throws ContentAccessException
  152. {
  153. return null;
  154. }
  155. @Override
  156. public boolean hasContent( ItemSelector selector )
  157. {
  158. return false;
  159. }
  160. @Override
  161. public void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException
  162. {
  163. }
  164. @Override
  165. public ContentItem toItem( String path ) throws LayoutException
  166. {
  167. return null;
  168. }
  169. @Override
  170. public ContentItem toItem( StorageAsset assetPath ) throws LayoutException
  171. {
  172. return null;
  173. }
  174. @Override
  175. public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException
  176. {
  177. }
  178. @Override
  179. public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException, ContentAccessException
  180. {
  181. }
  182. @Override
  183. public void deleteGroupId( String groupId ) throws ContentNotFoundException, ContentAccessException
  184. {
  185. }
  186. @Override
  187. public void deleteProject( String namespace, String projectId ) throws ContentNotFoundException, ContentAccessException
  188. {
  189. }
  190. @Override
  191. public void deleteProject( ProjectReference reference ) throws ContentNotFoundException, ContentAccessException
  192. {
  193. }
  194. @Override
  195. public String getId( )
  196. {
  197. return repository.getId();
  198. }
  199. @Override
  200. public List<ArtifactReference> getRelatedArtifacts( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
  201. {
  202. return null;
  203. }
  204. @Override
  205. public List<ArtifactReference> getRelatedArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
  206. {
  207. return null;
  208. }
  209. @Override
  210. public List<StorageAsset> getRelatedAssets( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
  211. {
  212. return null;
  213. }
  214. @Override
  215. public List<ArtifactReference> getArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
  216. {
  217. return null;
  218. }
  219. @Override
  220. public String getRepoRoot( )
  221. {
  222. return getRepoRootAsset().getFilePath().toString();
  223. }
  224. private StorageAsset getRepoRootAsset() {
  225. if (fsStorage==null) {
  226. try {
  227. fsStorage = new FilesystemStorage(Paths.get("", "target", "test-repository", "managed"), new DefaultFileLockManager());
  228. } catch (IOException e) {
  229. e.printStackTrace();
  230. }
  231. }
  232. return fsStorage.getAsset("");
  233. }
  234. @Override
  235. public ManagedRepository getRepository( )
  236. {
  237. return repository;
  238. }
  239. @Override
  240. public Set<String> getVersions( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException, LayoutException
  241. {
  242. return null;
  243. }
  244. @Override
  245. public boolean hasContent( ArtifactReference reference ) throws ContentAccessException
  246. {
  247. return false;
  248. }
  249. @Override
  250. public boolean hasContent( VersionedReference reference ) throws ContentAccessException
  251. {
  252. return false;
  253. }
  254. @Override
  255. public void setRepository( ManagedRepository repo )
  256. {
  257. this.repository = repo;
  258. }
  259. @Override
  260. public StorageAsset toFile( VersionedReference reference )
  261. {
  262. return null;
  263. }
  264. private Map<ArtifactReference, String> refs = new HashMap<>();
  265. @Override
  266. public ArtifactReference toArtifactReference( String path ) throws LayoutException
  267. {
  268. if ( StringUtils.isBlank( path ) )
  269. {
  270. throw new LayoutException( "Unable to convert blank path." );
  271. }
  272. ArtifactMetadata metadata = getArtifactForPath("test-repository", path);
  273. ArtifactReference artifact = new ArtifactReference();
  274. artifact.setGroupId( metadata.getNamespace() );
  275. artifact.setArtifactId( metadata.getProject() );
  276. artifact.setVersion( metadata.getVersion() );
  277. MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
  278. if ( facet != null )
  279. {
  280. artifact.setClassifier( facet.getClassifier() );
  281. artifact.setType( facet.getType() );
  282. }
  283. refs.put(artifact, path);
  284. return artifact;
  285. }
  286. public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
  287. {
  288. String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
  289. int len = parts.length;
  290. if ( len < 4 )
  291. {
  292. throw new IllegalArgumentException(
  293. "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
  294. }
  295. String id = parts[--len];
  296. String baseVersion = parts[--len];
  297. String artifactId = parts[--len];
  298. StringBuilder groupIdBuilder = new StringBuilder();
  299. for ( int i = 0; i < len - 1; i++ )
  300. {
  301. groupIdBuilder.append( parts[i] );
  302. groupIdBuilder.append( '.' );
  303. }
  304. groupIdBuilder.append( parts[len - 1] );
  305. return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
  306. }
  307. private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
  308. public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
  309. String id )
  310. {
  311. if ( !id.startsWith( projectId + "-" ) )
  312. {
  313. throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
  314. + "' doesn't start with artifact ID '" + projectId + "'" );
  315. }
  316. MavenArtifactFacet facet = new MavenArtifactFacet();
  317. int index = projectId.length() + 1;
  318. String version;
  319. String idSubStrFromVersion = id.substring( index );
  320. if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
  321. {
  322. // non-snapshot versions, or non-timestamped snapshot versions
  323. version = projectVersion;
  324. }
  325. else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
  326. {
  327. // timestamped snapshots
  328. try
  329. {
  330. int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
  331. if ( mainVersionLength == 0 )
  332. {
  333. throw new IllegalArgumentException(
  334. "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
  335. }
  336. Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
  337. m.matches();
  338. String timestamp = m.group( 1 );
  339. String buildNumber = m.group( 2 );
  340. facet.setTimestamp( timestamp );
  341. facet.setBuildNumber( Integer.parseInt( buildNumber ) );
  342. version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
  343. }
  344. catch ( IllegalStateException e )
  345. {
  346. throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
  347. + "' doesn't contain a timestamped version matching snapshot '"
  348. + projectVersion + "'", e);
  349. }
  350. }
  351. else
  352. {
  353. // invalid
  354. throw new IllegalArgumentException(
  355. "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
  356. + projectVersion + "'" );
  357. }
  358. String classifier;
  359. String ext;
  360. index += version.length();
  361. if ( index == id.length() )
  362. {
  363. // no classifier or extension
  364. classifier = null;
  365. ext = null;
  366. }
  367. else
  368. {
  369. char c = id.charAt( index );
  370. if ( c == '-' )
  371. {
  372. // classifier up until '.'
  373. int extIndex = id.indexOf( '.', index );
  374. if ( extIndex >= 0 )
  375. {
  376. classifier = id.substring( index + 1, extIndex );
  377. ext = id.substring( extIndex + 1 );
  378. }
  379. else
  380. {
  381. classifier = id.substring( index + 1 );
  382. ext = null;
  383. }
  384. }
  385. else if ( c == '.' )
  386. {
  387. // rest is the extension
  388. classifier = null;
  389. ext = id.substring( index + 1 );
  390. }
  391. else
  392. {
  393. throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
  394. + "' expected classifier or extension but got '"
  395. + id.substring( index ) + "'" );
  396. }
  397. }
  398. ArtifactMetadata metadata = new ArtifactMetadata();
  399. metadata.setId( id );
  400. metadata.setNamespace( namespace );
  401. metadata.setProject( projectId );
  402. metadata.setRepositoryId( repoId );
  403. metadata.setProjectVersion( projectVersion );
  404. metadata.setVersion( version );
  405. facet.setClassifier( classifier );
  406. // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
  407. // to select the correct order to apply multiple extensions mappings to a preferred type
  408. // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
  409. // perhaps the plugins could register missing entries in configuration, then we just use configuration
  410. // here?
  411. String type = null;
  412. // use extension as default
  413. if ( type == null )
  414. {
  415. type = ext;
  416. }
  417. // TODO: should we allow this instead?
  418. if ( type == null )
  419. {
  420. throw new IllegalArgumentException(
  421. "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
  422. }
  423. facet.setType( type );
  424. metadata.addFacet( facet );
  425. return metadata;
  426. }
  427. @Override
  428. public StorageAsset toFile( ArtifactReference reference )
  429. {
  430. return getRepoRootAsset().resolve( refs.get(reference));
  431. }
  432. @Override
  433. public StorageAsset toFile( ArchivaArtifact reference )
  434. {
  435. return null;
  436. }
  437. private String formatAsDirectory( String directory )
  438. {
  439. return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
  440. }
  441. public String toMetadataPath( ProjectReference reference )
  442. {
  443. StringBuilder path = new StringBuilder();
  444. path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
  445. path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
  446. path.append( MAVEN_METADATA );
  447. return path.toString();
  448. }
  449. public String toMetadataPath( VersionedReference reference )
  450. {
  451. StringBuilder path = new StringBuilder();
  452. path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
  453. path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
  454. if ( reference.getVersion() != null )
  455. {
  456. // add the version only if it is present
  457. path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
  458. }
  459. path.append( MAVEN_METADATA );
  460. return path.toString();
  461. }
  462. @Override
  463. public String toPath( ArtifactReference reference )
  464. {
  465. return null;
  466. }
  467. @Override
  468. public String toPath( ItemSelector selector )
  469. {
  470. return null;
  471. }
  472. @Override
  473. public ItemSelector toItemSelector( String path ) throws LayoutException
  474. {
  475. return null;
  476. }
  477. @Override
  478. public String toPath( ArchivaArtifact reference )
  479. {
  480. return null;
  481. }
  482. }