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.

ManagedDefaultRepositoryContent.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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.filelock.FileLockManager;
  21. import org.apache.archiva.common.utils.PathUtil;
  22. import org.apache.archiva.configuration.FileTypes;
  23. import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider;
  24. import org.apache.archiva.metadata.repository.storage.maven2.DefaultArtifactMappingProvider;
  25. import org.apache.archiva.model.ArchivaArtifact;
  26. import org.apache.archiva.model.ArtifactReference;
  27. import org.apache.archiva.model.ProjectReference;
  28. import org.apache.archiva.model.VersionedReference;
  29. import org.apache.archiva.repository.ContentNotFoundException;
  30. import org.apache.archiva.repository.EditableManagedRepository;
  31. import org.apache.archiva.repository.LayoutException;
  32. import org.apache.archiva.repository.ManagedRepository;
  33. import org.apache.archiva.repository.ManagedRepositoryContent;
  34. import org.apache.archiva.repository.RepositoryException;
  35. import org.apache.archiva.repository.storage.StorageAsset;
  36. import org.apache.commons.lang.StringUtils;
  37. import java.io.IOException;
  38. import java.net.URI;
  39. import java.nio.file.Files;
  40. import java.nio.file.Path;
  41. import java.nio.file.Paths;
  42. import java.util.Collections;
  43. import java.util.HashSet;
  44. import java.util.List;
  45. import java.util.Objects;
  46. import java.util.Set;
  47. import java.util.stream.Collectors;
  48. import java.util.stream.Stream;
  49. /**
  50. * ManagedDefaultRepositoryContent
  51. */
  52. public class ManagedDefaultRepositoryContent
  53. extends AbstractDefaultRepositoryContent
  54. implements ManagedRepositoryContent
  55. {
  56. private FileTypes filetypes;
  57. public void setFileTypes(FileTypes fileTypes) {
  58. this.filetypes = fileTypes;
  59. }
  60. private ManagedRepository repository;
  61. private Path repoDir;
  62. FileLockManager lockManager;
  63. public ManagedDefaultRepositoryContent(ManagedRepository repository, FileTypes fileTypes, FileLockManager lockManager) {
  64. super(Collections.singletonList( new DefaultArtifactMappingProvider() ));
  65. setFileTypes( fileTypes );
  66. this.lockManager = lockManager;
  67. setRepository( repository );
  68. }
  69. public ManagedDefaultRepositoryContent( ManagedRepository repository, List<? extends ArtifactMappingProvider> artifactMappingProviders, FileTypes fileTypes, FileLockManager lockManager )
  70. {
  71. super(artifactMappingProviders==null ? Collections.singletonList( new DefaultArtifactMappingProvider() ) : artifactMappingProviders);
  72. setFileTypes( fileTypes );
  73. this.lockManager = lockManager;
  74. setRepository( repository );
  75. }
  76. private Path getRepoDir() {
  77. return repoDir;
  78. }
  79. @Override
  80. public void deleteVersion( VersionedReference reference )
  81. {
  82. String path = toMetadataPath( reference );
  83. Path projectPath = Paths.get( getRepoRoot(), path );
  84. Path projectDir = projectPath.getParent();
  85. if ( Files.exists(projectDir) && Files.isDirectory(projectDir) )
  86. {
  87. org.apache.archiva.common.utils.FileUtils.deleteQuietly( projectDir );
  88. }
  89. }
  90. @Override
  91. public void deleteProject( String namespace, String projectId )
  92. throws RepositoryException
  93. {
  94. ArtifactReference artifactReference = new ArtifactReference();
  95. artifactReference.setGroupId( namespace );
  96. artifactReference.setArtifactId( projectId );
  97. String path = toPath( artifactReference );
  98. Path directory = Paths.get( getRepoRoot(), path );
  99. if ( !Files.exists(directory) )
  100. {
  101. throw new ContentNotFoundException( "cannot found project " + namespace + ":" + projectId );
  102. }
  103. if ( Files.isDirectory(directory) )
  104. {
  105. try
  106. {
  107. org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
  108. }
  109. catch ( IOException e )
  110. {
  111. throw new RepositoryException( e.getMessage(), e );
  112. }
  113. }
  114. else
  115. {
  116. log.warn( "project {}:{} is not a directory", namespace, projectId );
  117. }
  118. }
  119. @Override
  120. public void deleteArtifact( ArtifactReference artifactReference )
  121. {
  122. String path = toPath( artifactReference );
  123. Path filePath = Paths.get( getRepoRoot(), path );
  124. if ( Files.exists(filePath) )
  125. {
  126. org.apache.archiva.common.utils.FileUtils.deleteQuietly( filePath );
  127. }
  128. Path filePathmd5 = Paths.get( getRepoRoot(), path + ".md5" );
  129. if ( Files.exists(filePathmd5) )
  130. {
  131. org.apache.archiva.common.utils.FileUtils.deleteQuietly( filePathmd5 );
  132. }
  133. Path filePathsha1 = Paths.get( getRepoRoot(), path + ".sha1" );
  134. if ( Files.exists(filePathsha1) )
  135. {
  136. org.apache.archiva.common.utils.FileUtils.deleteQuietly( filePathsha1 );
  137. }
  138. }
  139. @Override
  140. public void deleteGroupId( String groupId )
  141. throws ContentNotFoundException
  142. {
  143. String path = StringUtils.replaceChars( groupId, '.', '/' );
  144. Path directory = Paths.get( getRepoRoot(), path );
  145. if ( Files.exists(directory) )
  146. {
  147. try
  148. {
  149. org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
  150. }
  151. catch ( IOException e )
  152. {
  153. log.warn( "skip error deleting directory {}:", directory, e );
  154. }
  155. }
  156. }
  157. @Override
  158. public String getId()
  159. {
  160. return repository.getId();
  161. }
  162. @Override
  163. public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
  164. throws ContentNotFoundException
  165. {
  166. StorageAsset artifactFile = toFile( reference );
  167. StorageAsset repoBase = repository.getAsset( "" );
  168. StorageAsset repoDir = artifactFile.getParent();
  169. if ( !repoDir.exists())
  170. {
  171. throw new ContentNotFoundException(
  172. "Unable to get related artifacts using a non-existant directory: " + repoDir.getPath() );
  173. }
  174. if ( !repoDir.isContainer() )
  175. {
  176. throw new ContentNotFoundException(
  177. "Unable to get related artifacts using a non-directory: " + repoDir.getPath() );
  178. }
  179. Set<ArtifactReference> foundArtifacts;
  180. // First gather up the versions found as artifacts in the managed repository.
  181. try (Stream<StorageAsset> stream = repoDir.list().stream() ) {
  182. foundArtifacts = stream.filter(asset -> !asset.isContainer()).map(path -> {
  183. try {
  184. ArtifactReference artifact = toArtifactReference(path.getPath());
  185. if( artifact.getGroupId().equals( reference.getGroupId() ) && artifact.getArtifactId().equals(
  186. reference.getArtifactId() ) && artifact.getVersion().equals( reference.getVersion() )) {
  187. return artifact;
  188. } else {
  189. return null;
  190. }
  191. } catch (LayoutException e) {
  192. log.debug( "Not processing file that is not an artifact: {}", e.getMessage() );
  193. return null;
  194. }
  195. }).filter(Objects::nonNull).collect(Collectors.toSet());
  196. }
  197. return foundArtifacts;
  198. }
  199. @Override
  200. public String getRepoRoot()
  201. {
  202. return convertUriToPath( repository.getLocation() );
  203. }
  204. private String convertUriToPath( URI uri ) {
  205. if (uri.getScheme()==null) {
  206. return Paths.get(uri.getPath()).toString();
  207. } else if ("file".equals(uri.getScheme())) {
  208. return Paths.get(uri).toString();
  209. } else {
  210. return uri.toString();
  211. }
  212. }
  213. @Override
  214. public org.apache.archiva.repository.ManagedRepository getRepository()
  215. {
  216. return repository;
  217. }
  218. /**
  219. * Gather the Available Versions (on disk) for a specific Project Reference, based on filesystem
  220. * information.
  221. *
  222. * @return the Set of available versions, based on the project reference.
  223. * @throws LayoutException
  224. */
  225. @Override
  226. public Set<String> getVersions( ProjectReference reference )
  227. throws ContentNotFoundException, LayoutException
  228. {
  229. String path = toMetadataPath( reference );
  230. int idx = path.lastIndexOf( '/' );
  231. if ( idx > 0 )
  232. {
  233. path = path.substring( 0, idx );
  234. }
  235. Path repoDir = PathUtil.getPathFromUri( repository.getLocation() ).resolve( path );
  236. if ( !Files.exists(repoDir) )
  237. {
  238. throw new ContentNotFoundException(
  239. "Unable to get Versions on a non-existant directory: " + repoDir.toAbsolutePath() );
  240. }
  241. if ( !Files.isDirectory(repoDir) )
  242. {
  243. throw new ContentNotFoundException(
  244. "Unable to get Versions on a non-directory: " + repoDir.toAbsolutePath() );
  245. }
  246. final String groupId = reference.getGroupId();
  247. final String artifactId = reference.getArtifactId();
  248. try(Stream<Path> stream = Files.list(repoDir)) {
  249. return stream.filter(Files::isDirectory).map(
  250. p -> newVersionedRef(groupId, artifactId, p.getFileName().toString())
  251. ).filter(this::hasArtifact).map(ref -> ref.getVersion())
  252. .collect(Collectors.toSet());
  253. } catch (IOException e) {
  254. log.error("Could not read directory {}: {}", repoDir, e.getMessage(), e);
  255. } catch (RuntimeException e) {
  256. if (e.getCause()!=null && e.getCause() instanceof LayoutException) {
  257. throw (LayoutException)e.getCause();
  258. } else {
  259. throw e;
  260. }
  261. }
  262. return Collections.emptySet();
  263. }
  264. static final VersionedReference newVersionedRef(final String groupId, final String artifactId, final String version) {
  265. VersionedReference ref = new VersionedReference();
  266. ref.setGroupId(groupId);
  267. ref.setArtifactId(artifactId);
  268. ref.setVersion(version);
  269. return ref;
  270. }
  271. @Override
  272. public Set<String> getVersions( VersionedReference reference )
  273. throws ContentNotFoundException
  274. {
  275. String path = toMetadataPath( reference );
  276. int idx = path.lastIndexOf( '/' );
  277. if ( idx > 0 )
  278. {
  279. path = path.substring( 0, idx );
  280. }
  281. Path repoBase = PathUtil.getPathFromUri(repository.getLocation());
  282. Path repoDir = repoBase.resolve( path );
  283. if ( !Files.exists(repoDir) )
  284. {
  285. throw new ContentNotFoundException(
  286. "Unable to get versions on a non-existant directory: " + repoDir.toAbsolutePath() );
  287. }
  288. if ( !Files.isDirectory(repoDir) )
  289. {
  290. throw new ContentNotFoundException(
  291. "Unable to get versions on a non-directory: " + repoDir.toAbsolutePath() );
  292. }
  293. Set<String> foundVersions = new HashSet<>();
  294. try(Stream<Path> stream = Files.list(repoDir)) {
  295. return stream.filter(Files::isRegularFile)
  296. .map(p -> repoBase.relativize(p).toString())
  297. .filter(p -> !filetypes.matchesDefaultExclusions(p))
  298. .filter(filetypes::matchesArtifactPattern)
  299. .map(path1 -> {
  300. try {
  301. return toArtifactReference(path1);
  302. } catch (LayoutException e) {
  303. log.debug( "Not processing file that is not an artifact: {}", e.getMessage() );
  304. return null;
  305. }
  306. }).filter(Objects::nonNull)
  307. .map(ar -> ar.getVersion())
  308. .collect(Collectors.toSet());
  309. } catch (IOException e) {
  310. log.error("Could not read directory {}: {}", repoDir, e.getMessage(), e);
  311. }
  312. return Collections.emptySet();
  313. }
  314. @Override
  315. public boolean hasContent( ArtifactReference reference )
  316. {
  317. StorageAsset artifactFile = toFile( reference );
  318. return artifactFile.exists() && !artifactFile.isContainer();
  319. }
  320. @Override
  321. public boolean hasContent( ProjectReference reference )
  322. {
  323. try
  324. {
  325. Set<String> versions = getVersions( reference );
  326. return !versions.isEmpty();
  327. }
  328. catch ( ContentNotFoundException | LayoutException e )
  329. {
  330. return false;
  331. }
  332. }
  333. @Override
  334. public boolean hasContent( VersionedReference reference )
  335. {
  336. try
  337. {
  338. return ( getFirstArtifact( reference ) != null );
  339. }
  340. catch ( IOException | LayoutException e )
  341. {
  342. return false;
  343. }
  344. }
  345. @Override
  346. public void setRepository( ManagedRepository repo )
  347. {
  348. this.repository = repo;
  349. if (repo!=null) {
  350. this.repoDir = PathUtil.getPathFromUri(repository.getLocation());
  351. if (repository instanceof EditableManagedRepository) {
  352. ((EditableManagedRepository) repository).setContent(this);
  353. }
  354. }
  355. }
  356. /**
  357. * Convert a path to an artifact reference.
  358. *
  359. * @param path the path to convert. (relative or full location path)
  360. * @throws LayoutException if the path cannot be converted to an artifact reference.
  361. */
  362. @Override
  363. public ArtifactReference toArtifactReference( String path )
  364. throws LayoutException
  365. {
  366. String repoPath = convertUriToPath( repository.getLocation() );
  367. if ( ( path != null ) && path.startsWith( repoPath ) && repoPath.length() > 0 )
  368. {
  369. return super.toArtifactReference( path.substring( repoPath.length() + 1 ) );
  370. } else {
  371. repoPath = path;
  372. if (repoPath!=null) {
  373. while (repoPath.startsWith("/")) {
  374. repoPath = repoPath.substring(1);
  375. }
  376. }
  377. return super.toArtifactReference( repoPath );
  378. }
  379. }
  380. // The variant with runtime exception for stream usage
  381. private ArtifactReference toArtifactRef(String path) {
  382. try {
  383. return toArtifactReference(path);
  384. } catch (LayoutException e) {
  385. throw new RuntimeException(e);
  386. }
  387. }
  388. @Override
  389. public StorageAsset toFile( ArtifactReference reference )
  390. {
  391. return repository.getAsset(toPath(reference));
  392. }
  393. @Override
  394. public StorageAsset toFile( ArchivaArtifact reference )
  395. {
  396. return repository.getAsset( toPath( reference ) );
  397. }
  398. /**
  399. * Get the first Artifact found in the provided VersionedReference location.
  400. *
  401. * @param reference the reference to the versioned reference to search within
  402. * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
  403. * no artifact was found within the versioned reference.
  404. * @throws java.io.IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
  405. * @throws LayoutException
  406. */
  407. private ArtifactReference getFirstArtifact( VersionedReference reference )
  408. throws LayoutException, IOException
  409. {
  410. String path = toMetadataPath( reference );
  411. int idx = path.lastIndexOf( '/' );
  412. if ( idx > 0 )
  413. {
  414. path = path.substring( 0, idx );
  415. }
  416. Path repoBase = PathUtil.getPathFromUri(repository.getLocation()).toAbsolutePath();
  417. Path repoDir = repoBase.resolve( path );
  418. if ( !Files.exists(repoDir) )
  419. {
  420. throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
  421. + repoDir.toAbsolutePath() );
  422. }
  423. if ( !Files.isDirectory(repoDir) )
  424. {
  425. throw new IOException(
  426. "Unable to gather the list of snapshot versions on a non-directory: " + repoDir.toAbsolutePath() );
  427. }
  428. try(Stream<Path> stream = Files.list(repoDir)) {
  429. return stream.filter(Files::isRegularFile)
  430. .map(p -> repoBase.relativize(p).toString())
  431. .filter(filetypes::matchesArtifactPattern)
  432. .map(this::toArtifactRef).findFirst().orElse(null);
  433. } catch (RuntimeException e) {
  434. if (e.getCause()!=null && e.getCause() instanceof LayoutException) {
  435. throw (LayoutException)e.getCause();
  436. } else {
  437. throw e;
  438. }
  439. }
  440. }
  441. private boolean hasArtifact( VersionedReference reference )
  442. {
  443. try
  444. {
  445. return ( getFirstArtifact( reference ) != null );
  446. }
  447. catch ( IOException e )
  448. {
  449. return false;
  450. } catch (LayoutException e) {
  451. // We throw the runtime exception for better stream handling
  452. throw new RuntimeException(e);
  453. }
  454. }
  455. public void setFiletypes( FileTypes filetypes )
  456. {
  457. this.filetypes = filetypes;
  458. }
  459. }