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

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