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

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