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

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