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.

DefaultRepositoriesService.java 46KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. package org.apache.archiva.rest.services;
  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.admin.model.RepositoryAdminException;
  21. import org.apache.archiva.admin.model.admin.ArchivaAdministration;
  22. import org.apache.archiva.checksum.ChecksumAlgorithm;
  23. import org.apache.archiva.checksum.ChecksummedFile;
  24. import org.apache.archiva.common.utils.VersionComparator;
  25. import org.apache.archiva.common.utils.VersionUtil;
  26. import org.apache.archiva.maven2.metadata.MavenMetadataReader;
  27. import org.apache.archiva.maven2.model.Artifact;
  28. import org.apache.archiva.metadata.model.ArtifactMetadata;
  29. import org.apache.archiva.metadata.model.facets.AuditEvent;
  30. import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet;
  31. import org.apache.archiva.metadata.repository.MetadataRepository;
  32. import org.apache.archiva.metadata.repository.MetadataRepositoryException;
  33. import org.apache.archiva.metadata.repository.MetadataResolutionException;
  34. import org.apache.archiva.metadata.repository.RepositorySession;
  35. import org.apache.archiva.metadata.repository.RepositorySessionFactory;
  36. import org.apache.archiva.model.ArchivaRepositoryMetadata;
  37. import org.apache.archiva.model.ArtifactReference;
  38. import org.apache.archiva.model.VersionedReference;
  39. import org.apache.archiva.redback.authentication.AuthenticationResult;
  40. import org.apache.archiva.redback.authorization.AuthorizationException;
  41. import org.apache.archiva.redback.components.cache.Cache;
  42. import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
  43. import org.apache.archiva.redback.system.DefaultSecuritySession;
  44. import org.apache.archiva.redback.system.SecuritySession;
  45. import org.apache.archiva.redback.system.SecuritySystem;
  46. import org.apache.archiva.redback.users.User;
  47. import org.apache.archiva.redback.users.UserManagerException;
  48. import org.apache.archiva.redback.users.UserNotFoundException;
  49. import org.apache.archiva.repository.ContentNotFoundException;
  50. import org.apache.archiva.repository.ManagedRepository;
  51. import org.apache.archiva.repository.ManagedRepositoryContent;
  52. import org.apache.archiva.repository.RepositoryException;
  53. import org.apache.archiva.repository.RepositoryNotFoundException;
  54. import org.apache.archiva.repository.RepositoryRegistry;
  55. import org.apache.archiva.repository.storage.RepositoryStorage;
  56. import org.apache.archiva.repository.storage.StorageAsset;
  57. import org.apache.archiva.repository.storage.StorageUtil;
  58. import org.apache.archiva.repository.events.RepositoryListener;
  59. import org.apache.archiva.repository.metadata.MetadataTools;
  60. import org.apache.archiva.repository.metadata.RepositoryMetadataException;
  61. import org.apache.archiva.repository.metadata.RepositoryMetadataWriter;
  62. import org.apache.archiva.repository.scanner.RepositoryScanStatistics;
  63. import org.apache.archiva.repository.scanner.RepositoryScanner;
  64. import org.apache.archiva.repository.scanner.RepositoryScannerException;
  65. import org.apache.archiva.repository.scanner.RepositoryScannerInstance;
  66. import org.apache.archiva.rest.api.model.ArtifactTransferRequest;
  67. import org.apache.archiva.rest.api.model.StringList;
  68. import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
  69. import org.apache.archiva.rest.api.services.RepositoriesService;
  70. import org.apache.archiva.scheduler.ArchivaTaskScheduler;
  71. import org.apache.archiva.scheduler.indexing.maven.ArchivaIndexingTaskExecutor;
  72. import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
  73. import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexException;
  74. import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexScheduler;
  75. import org.apache.archiva.scheduler.repository.model.RepositoryTask;
  76. import org.apache.archiva.security.ArchivaSecurityException;
  77. import org.apache.archiva.security.common.ArchivaRoleConstants;
  78. import org.apache.archiva.xml.XMLException;
  79. import org.apache.commons.io.FilenameUtils;
  80. import org.apache.commons.lang.StringUtils;
  81. import org.slf4j.Logger;
  82. import org.slf4j.LoggerFactory;
  83. import org.springframework.beans.factory.annotation.Autowired;
  84. import org.springframework.stereotype.Service;
  85. import javax.inject.Inject;
  86. import javax.inject.Named;
  87. import javax.ws.rs.core.Response;
  88. import java.io.IOException;
  89. import java.io.OutputStreamWriter;
  90. import java.nio.file.Path;
  91. import java.text.DateFormat;
  92. import java.text.SimpleDateFormat;
  93. import java.util.ArrayList;
  94. import java.util.Arrays;
  95. import java.util.Calendar;
  96. import java.util.Collection;
  97. import java.util.Collections;
  98. import java.util.Date;
  99. import java.util.List;
  100. import java.util.Set;
  101. import java.util.TimeZone;
  102. /**
  103. * @author Olivier Lamy
  104. * @since 1.4-M1
  105. */
  106. @Service("repositoriesService#rest")
  107. public class DefaultRepositoriesService
  108. extends AbstractRestService
  109. implements RepositoriesService
  110. {
  111. private Logger log = LoggerFactory.getLogger( getClass() );
  112. @Inject
  113. @Named(value = "taskExecutor#indexing")
  114. private ArchivaIndexingTaskExecutor archivaIndexingTaskExecutor;
  115. @Inject
  116. private RepositoryRegistry repositoryRegistry;
  117. @Inject
  118. private SecuritySystem securitySystem;
  119. @Inject
  120. @Named(value = "archivaTaskScheduler#repository")
  121. private ArchivaTaskScheduler<RepositoryTask> scheduler;
  122. @Inject
  123. private DownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
  124. @Inject
  125. @Named(value = "repositorySessionFactory")
  126. protected RepositorySessionFactory repositorySessionFactory;
  127. @Inject
  128. @Autowired(required = false)
  129. protected List<RepositoryListener> listeners = new ArrayList<RepositoryListener>();
  130. @Inject
  131. private RepositoryScanner repoScanner;
  132. /**
  133. * Cache used for namespaces
  134. */
  135. @Inject
  136. @Named(value = "cache#namespaces")
  137. private Cache<String, Collection<String>> namespacesCache;
  138. private List<ChecksumAlgorithm> algorithms = Arrays.asList(ChecksumAlgorithm.SHA256, ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 );
  139. @Override
  140. public Boolean scanRepository( String repositoryId, boolean fullScan )
  141. {
  142. return doScanRepository( repositoryId, fullScan );
  143. }
  144. @Override
  145. public Boolean alreadyScanning( String repositoryId )
  146. {
  147. // check queue first to make sure it doesn't get dequeued between calls
  148. if ( repositoryTaskScheduler.isProcessingRepositoryTask( repositoryId ) )
  149. {
  150. return true;
  151. }
  152. for ( RepositoryScannerInstance scan : repoScanner.getInProgressScans() )
  153. {
  154. if ( scan.getRepository().getId().equals( repositoryId ) )
  155. {
  156. return true;
  157. }
  158. }
  159. return false;
  160. }
  161. @Override
  162. public Boolean removeScanningTaskFromQueue( String repositoryId )
  163. {
  164. RepositoryTask task = new RepositoryTask();
  165. task.setRepositoryId( repositoryId );
  166. try
  167. {
  168. return repositoryTaskScheduler.unQueueTask( task );
  169. }
  170. catch ( TaskQueueException e )
  171. {
  172. log.error( "failed to unschedule scanning of repo with id {}", repositoryId, e );
  173. return false;
  174. }
  175. }
  176. private ManagedRepositoryContent getManagedRepositoryContent(String id) throws RepositoryException
  177. {
  178. org.apache.archiva.repository.ManagedRepository repo = repositoryRegistry.getManagedRepository( id );
  179. if (repo==null) {
  180. throw new RepositoryException( "Repository not found "+id );
  181. }
  182. return repo.getContent();
  183. }
  184. @Override
  185. public Boolean scanRepositoryNow( String repositoryId, boolean fullScan )
  186. throws ArchivaRestServiceException
  187. {
  188. try
  189. {
  190. org.apache.archiva.repository.ManagedRepository repository = repositoryRegistry.getManagedRepository( repositoryId );
  191. ArtifactIndexingTask task =
  192. new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, repository.getIndexingContext() );
  193. task.setExecuteOnEntireRepo( true );
  194. task.setOnlyUpdate( !fullScan );
  195. archivaIndexingTaskExecutor.executeTask( task );
  196. scheduler.queueTask( new RepositoryTask( repositoryId, fullScan ) );
  197. return Boolean.TRUE;
  198. }
  199. catch ( Exception e )
  200. {
  201. log.error( e.getMessage(), e );
  202. throw new ArchivaRestServiceException( e.getMessage(), e );
  203. }
  204. }
  205. @Override
  206. public Boolean scheduleDownloadRemoteIndex( String repositoryId, boolean now, boolean fullDownload )
  207. throws ArchivaRestServiceException
  208. {
  209. try
  210. {
  211. downloadRemoteIndexScheduler.scheduleDownloadRemote( repositoryId, now, fullDownload );
  212. }
  213. catch ( DownloadRemoteIndexException e )
  214. {
  215. log.error( e.getMessage(), e );
  216. throw new ArchivaRestServiceException( e.getMessage(), e );
  217. }
  218. return Boolean.TRUE;
  219. }
  220. @Override
  221. public Boolean copyArtifact( ArtifactTransferRequest artifactTransferRequest )
  222. throws ArchivaRestServiceException
  223. {
  224. // check parameters
  225. String userName = getAuditInformation().getUser().getUsername();
  226. if ( StringUtils.isBlank( userName ) )
  227. {
  228. throw new ArchivaRestServiceException( "copyArtifact call: userName not found", null );
  229. }
  230. if ( StringUtils.isBlank( artifactTransferRequest.getRepositoryId() ) )
  231. {
  232. throw new ArchivaRestServiceException( "copyArtifact call: sourceRepositoryId cannot be null", null );
  233. }
  234. if ( StringUtils.isBlank( artifactTransferRequest.getTargetRepositoryId() ) )
  235. {
  236. throw new ArchivaRestServiceException( "copyArtifact call: targetRepositoryId cannot be null", null );
  237. }
  238. ManagedRepository source = null;
  239. source = repositoryRegistry.getManagedRepository( artifactTransferRequest.getRepositoryId() );
  240. if ( source == null )
  241. {
  242. throw new ArchivaRestServiceException(
  243. "cannot find repository with id " + artifactTransferRequest.getRepositoryId(), null );
  244. }
  245. ManagedRepository target = null;
  246. target = repositoryRegistry.getManagedRepository( artifactTransferRequest.getTargetRepositoryId() );
  247. if ( target == null )
  248. {
  249. throw new ArchivaRestServiceException(
  250. "cannot find repository with id " + artifactTransferRequest.getTargetRepositoryId(), null );
  251. }
  252. if ( StringUtils.isBlank( artifactTransferRequest.getGroupId() ) )
  253. {
  254. throw new ArchivaRestServiceException( "groupId is mandatory", null );
  255. }
  256. if ( StringUtils.isBlank( artifactTransferRequest.getArtifactId() ) )
  257. {
  258. throw new ArchivaRestServiceException( "artifactId is mandatory", null );
  259. }
  260. if ( StringUtils.isBlank( artifactTransferRequest.getVersion() ) )
  261. {
  262. throw new ArchivaRestServiceException( "version is mandatory", null );
  263. }
  264. if ( VersionUtil.isSnapshot( artifactTransferRequest.getVersion() ) )
  265. {
  266. throw new ArchivaRestServiceException( "copy of SNAPSHOT not supported", null );
  267. }
  268. // end check parameters
  269. User user = null;
  270. try
  271. {
  272. user = securitySystem.getUserManager().findUser( userName );
  273. }
  274. catch ( UserNotFoundException e )
  275. {
  276. throw new ArchivaRestServiceException( "user " + userName + " not found", e );
  277. }
  278. catch ( UserManagerException e )
  279. {
  280. throw new ArchivaRestServiceException( "ArchivaRestServiceException:" + e.getMessage(), e );
  281. }
  282. // check karma on source : read
  283. AuthenticationResult authn = new AuthenticationResult( true, userName, null );
  284. SecuritySession securitySession = new DefaultSecuritySession( authn, user );
  285. try
  286. {
  287. boolean authz =
  288. securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS,
  289. artifactTransferRequest.getRepositoryId() );
  290. if ( !authz )
  291. {
  292. throw new ArchivaRestServiceException(
  293. "not authorized to access repo:" + artifactTransferRequest.getRepositoryId(), null );
  294. }
  295. }
  296. catch ( AuthorizationException e )
  297. {
  298. log.error( "error reading permission: {}", e.getMessage(), e );
  299. throw new ArchivaRestServiceException( e.getMessage(), e );
  300. }
  301. // check karma on target: write
  302. try
  303. {
  304. boolean authz =
  305. securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD,
  306. artifactTransferRequest.getTargetRepositoryId() );
  307. if ( !authz )
  308. {
  309. throw new ArchivaRestServiceException(
  310. "not authorized to write to repo:" + artifactTransferRequest.getTargetRepositoryId(), null );
  311. }
  312. }
  313. catch ( AuthorizationException e )
  314. {
  315. log.error( "error reading permission: {}", e.getMessage(), e );
  316. throw new ArchivaRestServiceException( e.getMessage(), e );
  317. }
  318. // sounds good we can continue !
  319. ArtifactReference artifactReference = new ArtifactReference();
  320. artifactReference.setArtifactId( artifactTransferRequest.getArtifactId() );
  321. artifactReference.setGroupId( artifactTransferRequest.getGroupId() );
  322. artifactReference.setVersion( artifactTransferRequest.getVersion() );
  323. artifactReference.setClassifier( artifactTransferRequest.getClassifier() );
  324. String packaging = StringUtils.trim( artifactTransferRequest.getPackaging() );
  325. artifactReference.setType( StringUtils.isEmpty( packaging ) ? "jar" : packaging );
  326. try
  327. {
  328. ManagedRepositoryContent sourceRepository =
  329. getManagedRepositoryContent( artifactTransferRequest.getRepositoryId() );
  330. String artifactSourcePath = sourceRepository.toPath( artifactReference );
  331. if ( StringUtils.isEmpty( artifactSourcePath ) )
  332. {
  333. log.error( "cannot find artifact {}", artifactTransferRequest );
  334. throw new ArchivaRestServiceException( "cannot find artifact " + artifactTransferRequest.toString(),
  335. null );
  336. }
  337. StorageAsset artifactFile = source.getAsset( artifactSourcePath );
  338. if ( !artifactFile.exists() )
  339. {
  340. log.error( "cannot find artifact {}", artifactTransferRequest );
  341. throw new ArchivaRestServiceException( "cannot find artifact " + artifactTransferRequest.toString(),
  342. null );
  343. }
  344. ManagedRepositoryContent targetRepository =
  345. getManagedRepositoryContent( artifactTransferRequest.getTargetRepositoryId() );
  346. String artifactPath = targetRepository.toPath( artifactReference );
  347. int lastIndex = artifactPath.lastIndexOf( '/' );
  348. String path = artifactPath.substring( 0, lastIndex );
  349. StorageAsset targetDir = target.getAsset( path );
  350. Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
  351. int newBuildNumber = 1;
  352. String timestamp = null;
  353. StorageAsset versionMetadataFile = target.getAsset(path + "/" + MetadataTools.MAVEN_METADATA );
  354. /* unused */ getMetadata( versionMetadataFile );
  355. if ( !targetDir.exists() )
  356. {
  357. targetDir = target.addAsset(targetDir.getPath(), true);
  358. targetDir.create();
  359. }
  360. String filename = artifactPath.substring( lastIndex + 1 );
  361. boolean fixChecksums =
  362. !( archivaAdministration.getKnownContentConsumers().contains( "create-missing-checksums" ) );
  363. StorageAsset targetFile = target.getAsset(targetDir.getPath() + "/" + filename );
  364. if ( targetFile.exists() && target.blocksRedeployments())
  365. {
  366. throw new ArchivaRestServiceException(
  367. "artifact already exists in target repo: " + artifactTransferRequest.getTargetRepositoryId()
  368. + " and redeployment blocked", null
  369. );
  370. }
  371. else
  372. {
  373. copyFile(artifactFile, targetFile, fixChecksums );
  374. queueRepositoryTask( target.getId(), targetFile );
  375. }
  376. // copy source pom to target repo
  377. String pomFilename = filename;
  378. if ( StringUtils.isNotBlank( artifactTransferRequest.getClassifier() ) )
  379. {
  380. pomFilename = StringUtils.remove( pomFilename, "-" + artifactTransferRequest.getClassifier() );
  381. }
  382. pomFilename = FilenameUtils.removeExtension( pomFilename ) + ".pom";
  383. StorageAsset pomFile = source.getAsset(
  384. artifactSourcePath.substring( 0, artifactPath.lastIndexOf( '/' ) )+"/"+ pomFilename );
  385. if ( pomFile != null && pomFile.exists() )
  386. {
  387. StorageAsset targetPomFile = target.getAsset( targetDir.getPath() + "/" + pomFilename );
  388. copyFile(pomFile, targetPomFile, fixChecksums );
  389. queueRepositoryTask( target.getId(), targetPomFile );
  390. }
  391. // explicitly update only if metadata-updater consumer is not enabled!
  392. if ( !archivaAdministration.getKnownContentConsumers().contains( "metadata-updater" ) )
  393. {
  394. updateProjectMetadata( target, targetDir, lastUpdatedTimestamp, timestamp, newBuildNumber,
  395. fixChecksums, artifactTransferRequest );
  396. }
  397. String msg =
  398. "Artifact \'" + artifactTransferRequest.getGroupId() + ":" + artifactTransferRequest.getArtifactId()
  399. + ":" + artifactTransferRequest.getVersion() + "\' was successfully deployed to repository \'"
  400. + artifactTransferRequest.getTargetRepositoryId() + "\'";
  401. log.debug("copyArtifact {}", msg);
  402. }
  403. catch ( RepositoryException e )
  404. {
  405. log.error( "RepositoryException: {}", e.getMessage(), e );
  406. throw new ArchivaRestServiceException( e.getMessage(), e );
  407. }
  408. catch ( RepositoryAdminException e )
  409. {
  410. log.error( "RepositoryAdminException: {}", e.getMessage(), e );
  411. throw new ArchivaRestServiceException( e.getMessage(), e );
  412. }
  413. catch ( IOException e )
  414. {
  415. log.error( "IOException: {}", e.getMessage(), e );
  416. throw new ArchivaRestServiceException( e.getMessage(), e );
  417. }
  418. return true;
  419. }
  420. private void queueRepositoryTask( String repositoryId, StorageAsset localFile )
  421. {
  422. RepositoryTask task = new RepositoryTask();
  423. task.setRepositoryId( repositoryId );
  424. task.setResourceFile( localFile );
  425. task.setUpdateRelatedArtifacts( true );
  426. //task.setScanAll( true );
  427. try
  428. {
  429. scheduler.queueTask( task );
  430. }
  431. catch ( TaskQueueException e )
  432. {
  433. log.error( "Unable to queue repository task to execute consumers on resource file ['{}"
  434. + "'].", localFile.getName());
  435. }
  436. }
  437. private ArchivaRepositoryMetadata getMetadata( StorageAsset metadataFile )
  438. throws RepositoryMetadataException
  439. {
  440. ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
  441. if ( metadataFile.exists() )
  442. {
  443. try
  444. {
  445. metadata = MavenMetadataReader.read( metadataFile.getFilePath() );
  446. }
  447. catch (XMLException e )
  448. {
  449. throw new RepositoryMetadataException( e.getMessage(), e );
  450. }
  451. }
  452. return metadata;
  453. }
  454. private StorageAsset getMetadata( RepositoryStorage storage, String targetPath )
  455. {
  456. return storage.getAsset( targetPath + "/" + MetadataTools.MAVEN_METADATA );
  457. }
  458. /*
  459. * Copies the asset to the new target.
  460. */
  461. private void copyFile(StorageAsset sourceFile, StorageAsset targetPath, boolean fixChecksums)
  462. throws IOException
  463. {
  464. StorageUtil.copyAsset( sourceFile, targetPath, true );
  465. if ( fixChecksums )
  466. {
  467. fixChecksums( targetPath );
  468. }
  469. }
  470. private void fixChecksums( StorageAsset file )
  471. {
  472. Path destinationFile = file.getFilePath();
  473. if (destinationFile!=null)
  474. {
  475. ChecksummedFile checksum = new ChecksummedFile( destinationFile );
  476. checksum.fixChecksums( algorithms );
  477. }
  478. }
  479. private void updateProjectMetadata( RepositoryStorage storage, StorageAsset targetPath, Date lastUpdatedTimestamp, String timestamp, int buildNumber,
  480. boolean fixChecksums, ArtifactTransferRequest artifactTransferRequest )
  481. throws RepositoryMetadataException
  482. {
  483. List<String> availableVersions = new ArrayList<>();
  484. String latestVersion = artifactTransferRequest.getVersion();
  485. StorageAsset projectDir = targetPath.getParent();
  486. StorageAsset projectMetadataFile = storage.getAsset( projectDir.getPath()+"/"+MetadataTools.MAVEN_METADATA );
  487. ArchivaRepositoryMetadata projectMetadata = getMetadata( projectMetadataFile );
  488. if ( projectMetadataFile.exists() )
  489. {
  490. availableVersions = projectMetadata.getAvailableVersions();
  491. Collections.sort( availableVersions, VersionComparator.getInstance() );
  492. if ( !availableVersions.contains( artifactTransferRequest.getVersion() ) )
  493. {
  494. availableVersions.add( artifactTransferRequest.getVersion() );
  495. }
  496. latestVersion = availableVersions.get( availableVersions.size() - 1 );
  497. }
  498. else
  499. {
  500. availableVersions.add( artifactTransferRequest.getVersion() );
  501. projectMetadata.setGroupId( artifactTransferRequest.getGroupId() );
  502. projectMetadata.setArtifactId( artifactTransferRequest.getArtifactId() );
  503. }
  504. if ( projectMetadata.getGroupId() == null )
  505. {
  506. projectMetadata.setGroupId( artifactTransferRequest.getGroupId() );
  507. }
  508. if ( projectMetadata.getArtifactId() == null )
  509. {
  510. projectMetadata.setArtifactId( artifactTransferRequest.getArtifactId() );
  511. }
  512. projectMetadata.setLatestVersion( latestVersion );
  513. projectMetadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
  514. projectMetadata.setAvailableVersions( availableVersions );
  515. if ( !VersionUtil.isSnapshot( artifactTransferRequest.getVersion() ) )
  516. {
  517. projectMetadata.setReleasedVersion( latestVersion );
  518. }
  519. try(OutputStreamWriter writer = new OutputStreamWriter(projectMetadataFile.getWriteStream(true))) {
  520. RepositoryMetadataWriter.write(projectMetadata, writer);
  521. } catch (IOException e) {
  522. throw new RepositoryMetadataException(e);
  523. }
  524. if ( fixChecksums )
  525. {
  526. fixChecksums( projectMetadataFile );
  527. }
  528. }
  529. @Override
  530. public Boolean removeProjectVersion( String repositoryId, String namespace, String projectId, String version )
  531. throws ArchivaRestServiceException
  532. {
  533. // if not a generic we can use the standard way to delete artifact
  534. if ( !VersionUtil.isGenericSnapshot( version ) )
  535. {
  536. Artifact artifact = new Artifact( namespace, projectId, version );
  537. artifact.setRepositoryId( repositoryId );
  538. artifact.setContext( repositoryId );
  539. return deleteArtifact( artifact );
  540. }
  541. if ( StringUtils.isEmpty( repositoryId ) )
  542. {
  543. throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null );
  544. }
  545. if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
  546. {
  547. throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null );
  548. }
  549. if ( StringUtils.isEmpty( namespace ) )
  550. {
  551. throw new ArchivaRestServiceException( "groupId cannot be null", 400, null );
  552. }
  553. if ( StringUtils.isEmpty( projectId ) )
  554. {
  555. throw new ArchivaRestServiceException( "artifactId cannot be null", 400, null );
  556. }
  557. if ( StringUtils.isEmpty( version ) )
  558. {
  559. throw new ArchivaRestServiceException( "version cannot be null", 400, null );
  560. }
  561. RepositorySession repositorySession = repositorySessionFactory.createSession();
  562. try
  563. {
  564. ManagedRepositoryContent repository = getManagedRepositoryContent( repositoryId );
  565. VersionedReference ref = new VersionedReference();
  566. ref.setArtifactId( projectId );
  567. ref.setGroupId( namespace );
  568. ref.setVersion( version );
  569. repository.deleteVersion( ref );
  570. /*
  571. ProjectReference projectReference = new ProjectReference();
  572. projectReference.setGroupId( namespace );
  573. projectReference.setArtifactId( projectId );
  574. repository.getVersions( )
  575. */
  576. ArtifactReference artifactReference = new ArtifactReference();
  577. artifactReference.setGroupId( namespace );
  578. artifactReference.setArtifactId( projectId );
  579. artifactReference.setVersion( version );
  580. MetadataRepository metadataRepository = repositorySession.getRepository();
  581. Set<ArtifactReference> related = repository.getRelatedArtifacts( artifactReference );
  582. log.debug( "related: {}", related );
  583. for ( ArtifactReference artifactRef : related )
  584. {
  585. repository.deleteArtifact( artifactRef );
  586. }
  587. Collection<ArtifactMetadata> artifacts =
  588. metadataRepository.getArtifacts( repositoryId, namespace, projectId, version );
  589. for ( ArtifactMetadata artifactMetadata : artifacts )
  590. {
  591. metadataRepository.removeArtifact( artifactMetadata, version );
  592. }
  593. metadataRepository.removeProjectVersion( repositoryId, namespace, projectId, version );
  594. }
  595. catch ( MetadataRepositoryException e )
  596. {
  597. throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
  598. }
  599. catch ( MetadataResolutionException e )
  600. {
  601. throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
  602. }
  603. catch ( RepositoryException e )
  604. {
  605. throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
  606. }
  607. finally
  608. {
  609. repositorySession.save();
  610. repositorySession.close();
  611. }
  612. return Boolean.TRUE;
  613. }
  614. @Override
  615. public Boolean deleteArtifact( Artifact artifact )
  616. throws ArchivaRestServiceException
  617. {
  618. String repositoryId = artifact.getContext();
  619. // some rest call can use context or repositoryId
  620. // so try both!!
  621. if ( StringUtils.isEmpty( repositoryId ) )
  622. {
  623. repositoryId = artifact.getRepositoryId();
  624. }
  625. if ( StringUtils.isEmpty( repositoryId ) )
  626. {
  627. throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null );
  628. }
  629. if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
  630. {
  631. throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null );
  632. }
  633. if ( artifact == null )
  634. {
  635. throw new ArchivaRestServiceException( "artifact cannot be null", 400, null );
  636. }
  637. if ( StringUtils.isEmpty( artifact.getGroupId() ) )
  638. {
  639. throw new ArchivaRestServiceException( "artifact.groupId cannot be null", 400, null );
  640. }
  641. if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
  642. {
  643. throw new ArchivaRestServiceException( "artifact.artifactId cannot be null", 400, null );
  644. }
  645. // TODO more control on artifact fields
  646. boolean snapshotVersion =
  647. VersionUtil.isSnapshot( artifact.getVersion() ) | VersionUtil.isGenericSnapshot( artifact.getVersion() );
  648. RepositorySession repositorySession = repositorySessionFactory.createSession();
  649. try
  650. {
  651. Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
  652. TimeZone timezone = TimeZone.getTimeZone( "UTC" );
  653. DateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
  654. fmt.setTimeZone( timezone );
  655. ManagedRepository repo = repositoryRegistry.getManagedRepository( repositoryId );
  656. VersionedReference ref = new VersionedReference();
  657. ref.setArtifactId( artifact.getArtifactId() );
  658. ref.setGroupId( artifact.getGroupId() );
  659. ref.setVersion( artifact.getVersion() );
  660. ManagedRepositoryContent repository = getManagedRepositoryContent( repositoryId );
  661. ArtifactReference artifactReference = new ArtifactReference();
  662. artifactReference.setArtifactId( artifact.getArtifactId() );
  663. artifactReference.setGroupId( artifact.getGroupId() );
  664. artifactReference.setVersion( artifact.getVersion() );
  665. artifactReference.setClassifier( artifact.getClassifier() );
  666. artifactReference.setType( artifact.getPackaging() );
  667. MetadataRepository metadataRepository = repositorySession.getRepository();
  668. String path = repository.toMetadataPath( ref );
  669. if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
  670. {
  671. if ( StringUtils.isBlank( artifact.getPackaging() ) )
  672. {
  673. throw new ArchivaRestServiceException( "You must configure a type/packaging when using classifier",
  674. 400, null );
  675. }
  676. repository.deleteArtifact( artifactReference );
  677. }
  678. else
  679. {
  680. int index = path.lastIndexOf( '/' );
  681. path = path.substring( 0, index );
  682. StorageAsset targetPath = repo.getAsset( path );
  683. if ( !targetPath.exists() )
  684. {
  685. //throw new ContentNotFoundException(
  686. // artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
  687. log.warn( "targetPath {} not found skip file deletion", targetPath );
  688. return false;
  689. }
  690. // TODO: this should be in the storage mechanism so that it is all tied together
  691. // delete from file system
  692. if ( !snapshotVersion )
  693. {
  694. repository.deleteVersion( ref );
  695. }
  696. else
  697. {
  698. Set<ArtifactReference> related = repository.getRelatedArtifacts( artifactReference );
  699. log.debug( "related: {}", related );
  700. for ( ArtifactReference artifactRef : related )
  701. {
  702. repository.deleteArtifact( artifactRef );
  703. }
  704. StorageAsset metadataFile = getMetadata( repo, targetPath.getPath() );
  705. ArchivaRepositoryMetadata metadata = getMetadata( metadataFile );
  706. updateMetadata( metadata, metadataFile, lastUpdatedTimestamp, artifact );
  707. }
  708. }
  709. Collection<ArtifactMetadata> artifacts = Collections.emptyList();
  710. if ( snapshotVersion )
  711. {
  712. String baseVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
  713. artifacts =
  714. metadataRepository.getArtifacts( repositoryId, artifact.getGroupId(), artifact.getArtifactId(),
  715. baseVersion );
  716. }
  717. else
  718. {
  719. artifacts =
  720. metadataRepository.getArtifacts( repositoryId, artifact.getGroupId(), artifact.getArtifactId(),
  721. artifact.getVersion() );
  722. }
  723. log.debug( "artifacts: {}", artifacts );
  724. if ( artifacts.isEmpty() )
  725. {
  726. if ( !snapshotVersion )
  727. {
  728. // verify metata repository doesn't contains anymore the version
  729. Collection<String> projectVersions =
  730. metadataRepository.getProjectVersions( repositoryId, artifact.getGroupId(),
  731. artifact.getArtifactId() );
  732. if ( projectVersions.contains( artifact.getVersion() ) )
  733. {
  734. log.warn( "artifact not found when deleted but version still here ! so force cleanup" );
  735. metadataRepository.removeProjectVersion( repositoryId, artifact.getGroupId(),
  736. artifact.getArtifactId(), artifact.getVersion() );
  737. }
  738. }
  739. }
  740. for ( ArtifactMetadata artifactMetadata : artifacts )
  741. {
  742. // TODO: mismatch between artifact (snapshot) version and project (base) version here
  743. if ( artifactMetadata.getVersion().equals( artifact.getVersion() ) )
  744. {
  745. if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
  746. {
  747. if ( StringUtils.isBlank( artifact.getPackaging() ) )
  748. {
  749. throw new ArchivaRestServiceException(
  750. "You must configure a type/packaging when using classifier", 400, null );
  751. }
  752. // cleanup facet which contains classifier information
  753. MavenArtifactFacet mavenArtifactFacet =
  754. (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
  755. if ( StringUtils.equals( artifact.getClassifier(), mavenArtifactFacet.getClassifier() ) )
  756. {
  757. artifactMetadata.removeFacet( MavenArtifactFacet.FACET_ID );
  758. String groupId = artifact.getGroupId(), artifactId = artifact.getArtifactId(), version =
  759. artifact.getVersion();
  760. MavenArtifactFacet mavenArtifactFacetToCompare = new MavenArtifactFacet();
  761. mavenArtifactFacetToCompare.setClassifier( artifact.getClassifier() );
  762. metadataRepository.removeArtifact( repositoryId, groupId, artifactId, version,
  763. mavenArtifactFacetToCompare );
  764. metadataRepository.save();
  765. }
  766. }
  767. else
  768. {
  769. if ( snapshotVersion )
  770. {
  771. metadataRepository.removeArtifact( artifactMetadata,
  772. VersionUtil.getBaseVersion( artifact.getVersion() ) );
  773. }
  774. else
  775. {
  776. metadataRepository.removeArtifact( artifactMetadata.getRepositoryId(),
  777. artifactMetadata.getNamespace(),
  778. artifactMetadata.getProject(), artifact.getVersion(),
  779. artifactMetadata.getId() );
  780. }
  781. }
  782. // TODO: move into the metadata repository proper - need to differentiate attachment of
  783. // repository metadata to an artifact
  784. for ( RepositoryListener listener : listeners )
  785. {
  786. listener.deleteArtifact( metadataRepository, repository.getId(),
  787. artifactMetadata.getNamespace(), artifactMetadata.getProject(),
  788. artifactMetadata.getVersion(), artifactMetadata.getId() );
  789. }
  790. triggerAuditEvent( repositoryId, path, AuditEvent.REMOVE_FILE );
  791. }
  792. }
  793. }
  794. catch ( ContentNotFoundException e )
  795. {
  796. throw new ArchivaRestServiceException( "Artifact does not exist: " + e.getMessage(), 400, e );
  797. }
  798. catch ( RepositoryNotFoundException e )
  799. {
  800. throw new ArchivaRestServiceException( "Target repository cannot be found: " + e.getMessage(), 400, e );
  801. }
  802. catch ( RepositoryException e )
  803. {
  804. throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
  805. }
  806. catch ( MetadataResolutionException e )
  807. {
  808. throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
  809. }
  810. catch ( MetadataRepositoryException e )
  811. {
  812. throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
  813. }
  814. finally
  815. {
  816. repositorySession.save();
  817. repositorySession.close();
  818. }
  819. return Boolean.TRUE;
  820. }
  821. @Override
  822. public Boolean deleteGroupId( String groupId, String repositoryId )
  823. throws ArchivaRestServiceException
  824. {
  825. if ( StringUtils.isEmpty( repositoryId ) )
  826. {
  827. throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null );
  828. }
  829. if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
  830. {
  831. throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null );
  832. }
  833. if ( StringUtils.isEmpty( groupId ) )
  834. {
  835. throw new ArchivaRestServiceException( "groupId cannot be null", 400, null );
  836. }
  837. RepositorySession repositorySession = repositorySessionFactory.createSession();
  838. try
  839. {
  840. ManagedRepositoryContent repository = getManagedRepositoryContent( repositoryId );
  841. repository.deleteGroupId( groupId );
  842. MetadataRepository metadataRepository = repositorySession.getRepository();
  843. metadataRepository.removeNamespace( repositoryId, groupId );
  844. // just invalidate cache entry
  845. String cacheKey = repositoryId + "-" + groupId;
  846. namespacesCache.remove( cacheKey );
  847. namespacesCache.remove( repositoryId );
  848. metadataRepository.save();
  849. }
  850. catch ( MetadataRepositoryException e )
  851. {
  852. log.error( e.getMessage(), e );
  853. throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
  854. }
  855. catch ( RepositoryException e )
  856. {
  857. log.error( e.getMessage(), e );
  858. throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
  859. }
  860. finally
  861. {
  862. repositorySession.close();
  863. }
  864. return true;
  865. }
  866. @Override
  867. public Boolean deleteProject( String groupId, String projectId, String repositoryId )
  868. throws ArchivaRestServiceException
  869. {
  870. if ( StringUtils.isEmpty( repositoryId ) )
  871. {
  872. throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null );
  873. }
  874. if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
  875. {
  876. throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null );
  877. }
  878. if ( StringUtils.isEmpty( groupId ) )
  879. {
  880. throw new ArchivaRestServiceException( "groupId cannot be null", 400, null );
  881. }
  882. if ( StringUtils.isEmpty( projectId ) )
  883. {
  884. throw new ArchivaRestServiceException( "artifactId cannot be null", 400, null );
  885. }
  886. RepositorySession repositorySession = repositorySessionFactory.createSession();
  887. try
  888. {
  889. ManagedRepositoryContent repository = getManagedRepositoryContent( repositoryId );
  890. repository.deleteProject( groupId, projectId );
  891. }
  892. catch ( ContentNotFoundException e )
  893. {
  894. log.warn( "skip ContentNotFoundException: {}", e.getMessage() );
  895. }
  896. catch ( RepositoryException e )
  897. {
  898. log.error( e.getMessage(), e );
  899. throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
  900. }
  901. try
  902. {
  903. MetadataRepository metadataRepository = repositorySession.getRepository();
  904. metadataRepository.removeProject( repositoryId, groupId, projectId );
  905. metadataRepository.save();
  906. }
  907. catch ( MetadataRepositoryException e )
  908. {
  909. log.error( e.getMessage(), e );
  910. throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
  911. }
  912. finally
  913. {
  914. repositorySession.close();
  915. }
  916. return true;
  917. }
  918. @Override
  919. public Boolean isAuthorizedToDeleteArtifacts( String repoId )
  920. throws ArchivaRestServiceException
  921. {
  922. String userName =
  923. getAuditInformation().getUser() == null ? "guest" : getAuditInformation().getUser().getUsername();
  924. try
  925. {
  926. return userRepositories.isAuthorizedToDeleteArtifacts( userName, repoId );
  927. }
  928. catch ( ArchivaSecurityException e )
  929. {
  930. throw new ArchivaRestServiceException( e.getMessage(),
  931. Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
  932. }
  933. }
  934. @Override
  935. public RepositoryScanStatistics scanRepositoryDirectoriesNow( String repositoryId )
  936. throws ArchivaRestServiceException
  937. {
  938. long sinceWhen = RepositoryScanner.FRESH_SCAN;
  939. try
  940. {
  941. return repoScanner.scan( repositoryRegistry.getManagedRepository( repositoryId ), sinceWhen );
  942. }
  943. catch ( RepositoryScannerException e )
  944. {
  945. log.error( e.getMessage(), e );
  946. throw new ArchivaRestServiceException( "RepositoryScannerException exception: " + e.getMessage(), 500, e );
  947. }
  948. }
  949. /**
  950. * Update artifact level metadata. Creates one if metadata does not exist after artifact deletion.
  951. *
  952. * @param metadata
  953. */
  954. private void updateMetadata( ArchivaRepositoryMetadata metadata, StorageAsset metadataFile, Date lastUpdatedTimestamp,
  955. Artifact artifact )
  956. throws RepositoryMetadataException
  957. {
  958. List<String> availableVersions = new ArrayList<>();
  959. String latestVersion = "";
  960. if ( metadataFile.exists() )
  961. {
  962. if ( metadata.getAvailableVersions() != null )
  963. {
  964. availableVersions = metadata.getAvailableVersions();
  965. if ( availableVersions.size() > 0 )
  966. {
  967. Collections.sort( availableVersions, VersionComparator.getInstance() );
  968. if ( availableVersions.contains( artifact.getVersion() ) )
  969. {
  970. availableVersions.remove( availableVersions.indexOf( artifact.getVersion() ) );
  971. }
  972. if ( availableVersions.size() > 0 )
  973. {
  974. latestVersion = availableVersions.get( availableVersions.size() - 1 );
  975. }
  976. }
  977. }
  978. }
  979. if ( metadata.getGroupId() == null )
  980. {
  981. metadata.setGroupId( artifact.getGroupId() );
  982. }
  983. if ( metadata.getArtifactId() == null )
  984. {
  985. metadata.setArtifactId( artifact.getArtifactId() );
  986. }
  987. if ( !VersionUtil.isSnapshot( artifact.getVersion() ) )
  988. {
  989. if ( metadata.getReleasedVersion() != null && metadata.getReleasedVersion().equals(
  990. artifact.getVersion() ) )
  991. {
  992. metadata.setReleasedVersion( latestVersion );
  993. }
  994. }
  995. metadata.setLatestVersion( latestVersion );
  996. metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
  997. metadata.setAvailableVersions( availableVersions );
  998. try (OutputStreamWriter writer = new OutputStreamWriter(metadataFile.getWriteStream(true))) {
  999. RepositoryMetadataWriter.write(metadata, writer);
  1000. } catch (IOException e) {
  1001. throw new RepositoryMetadataException(e);
  1002. }
  1003. ChecksummedFile checksum = new ChecksummedFile( metadataFile.getFilePath() );
  1004. checksum.fixChecksums( algorithms );
  1005. }
  1006. @Override
  1007. public StringList getRunningRemoteDownloadIds()
  1008. {
  1009. return new StringList( downloadRemoteIndexScheduler.getRunningRemoteDownloadIds() );
  1010. }
  1011. public RepositorySessionFactory getRepositorySessionFactory()
  1012. {
  1013. return repositorySessionFactory;
  1014. }
  1015. public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
  1016. {
  1017. this.repositorySessionFactory = repositorySessionFactory;
  1018. }
  1019. public List<RepositoryListener> getListeners()
  1020. {
  1021. return listeners;
  1022. }
  1023. public void setListeners( List<RepositoryListener> listeners )
  1024. {
  1025. this.listeners = listeners;
  1026. }
  1027. public ArchivaAdministration getArchivaAdministration()
  1028. {
  1029. return archivaAdministration;
  1030. }
  1031. public void setArchivaAdministration( ArchivaAdministration archivaAdministration )
  1032. {
  1033. this.archivaAdministration = archivaAdministration;
  1034. }
  1035. }