MavenArtifactController extends BaseArtifactController { @ApiOperation(value = "Deletes a path from a repository.") @ApiResponses(value = { @ApiResponse(code = 200, message = "The artifact was deleted."), @ApiResponse(code = 400, message = "Bad request."), @ApiResponse(code = 404, message = "The specified storageId/repositoryId/path does not exist!") }) @PreAuthorize("hasAuthority('ARTIFACTS_DELETE')") @DeleteMapping(value = "/{storageId}/{repositoryId}/{artifactPath:.+}") public ResponseEntity delete(@RepositoryMapping Repository repository, @ApiParam(value = "Whether to use force delete") @RequestParam(defaultValue = "false", name = "force", required = false) boolean force, @PathVariable String artifactPath) throws IOException { final String storageId = repository.getStorage().getId(); final String repositoryId = repository.getId(); logger.info("Deleting {}:{}/{}...", storageId, repositoryId, artifactPath); try { final RepositoryPath repositoryPath = repositoryPathResolver.resolve(storageId, repositoryId, artifactPath); if (!Files.exists(repositoryPath)) { return ResponseEntity.status(NOT_FOUND) .body("The specified path does not exist!"); } artifactManagementService.delete(repositoryPath, force); } catch (ArtifactStorageException e) { logger.error(e.getMessage(), e); return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body(e.getMessage()); } return ResponseEntity.ok("The artifact was deleted."); }  @PreAuthorize("authenticated") @GetMapping(value = "/{storageId}/{repositoryId}") ResponseEntity<String> checkRepositoryAccess(); @ApiOperation(value = "Used to retrieve an artifact") @ApiResponses(value = { @ApiResponse(code = 200, message = ""), @ApiResponse(code = 404, message = "Requested path not found."), @ApiResponse(code = 500, message = "Server error."), @ApiResponse(code = 503, message = "Repository currently not in service.")}) @PreAuthorize("hasAuthority('ARTIFACTS_RESOLVE')") @RequestMapping(value = { "/{storageId}/{repositoryId}/{artifactPath:.+}" }, method = {RequestMethod.GET, RequestMethod.HEAD}) void download(@RepositoryMapping Repository repository,
                         @RequestHeader HttpHeaders httpHeaders,
                         @PathVariable String artifactPath,
                         HttpServletRequest request,
                         HttpServletResponse response); @ApiOperation(value = "Used to deploy an artifact") @ApiResponses(value = { @ApiResponse(code = 200, message = "The artifact was deployed successfully."), @ApiResponse(code = 400, message = "An error occurred.") }) @PreAuthorize("hasAuthority('ARTIFACTS_DEPLOY')") @PutMapping(value = "{storageId}/{repositoryId}/{artifactPath:.+}") ResponseEntity upload(@RepositoryMapping Repository repository,
                                 @PathVariable String artifactPath,
                                 HttpServletRequest request); @ApiOperation(value = "Copies a path from one repository to another.") @ApiResponses(value = { @ApiResponse(code = 200, message = "The path was copied successfully."), @ApiResponse(code = 400, message = "Bad request."), @ApiResponse(code = 404, message = "The source/destination storageId/repositoryId/path does not exist!") }) @PreAuthorize("hasAuthority('ARTIFACTS_COPY')") @PostMapping(value = "/copy/{path:.+}") ResponseEntity copy(
            @RepositoryMapping(storageVariableName = "srcStorageId", repositoryVariableName = "srcRepositoryId")
                    Repository srcRepository,
            @RepositoryMapping(storageVariableName = "destStorageId", repositoryVariableName = "destRepositoryId")
                    Repository destRepository,
            @PathVariable String path); @ApiOperation(value = "Deletes a path from a repository.") @ApiResponses(value = { @ApiResponse(code = 200, message = "The artifact was deleted."), @ApiResponse(code = 400, message = "Bad request."), @ApiResponse(code = 404, message = "The specified storageId/repositoryId/path does not exist!") }) @PreAuthorize("hasAuthority('ARTIFACTS_DELETE')") @DeleteMapping(value = "/{storageId}/{repositoryId}/{artifactPath:.+}") ResponseEntity delete(@RepositoryMapping Repository repository,
                                 @ApiParam(value = "Whether to use force delete")
                                 @RequestParam(defaultValue = "false",
                                               name = "force",
                                               required = false) boolean force,
                                 @PathVariable String artifactPath);  }
@ExtendWith({ RepositoryManagementTestExecutionListener.class, ArtifactManagementTestExecutionListener.class }) @Test public void testDeleteArtifactFile(@MavenRepository(repositoryId = REPOSITORY_RELEASES_1, setup = MavenIndexedRepositorySetup.class) Repository repository, @MavenTestArtifact(repositoryId = REPOSITORY_RELEASES_1, id = "com.artifacts.to.delete.releases:delete-foo", versions = "1.2.1") Path artifactPath) throws Exception { final RepositoryPath artifactRepositoryPath = (RepositoryPath) artifactPath.normalize(); final String artifactRepositoryPathStr = RepositoryFiles.relativizePath(artifactRepositoryPath); assertThat(Files.exists(artifactRepositoryPath)) .as("Failed to locate artifact file '" + artifactRepositoryPath + "'!") .isTrue(); client.delete(repository.getStorage().getId(), repository.getId(), artifactRepositoryPathStr); assertThat(Files.notExists(artifactRepositoryPath)) .as("Failed to delete artifact file '" + artifactRepositoryPath + "'!") .isTrue(); }
@ExtendWith({ RepositoryManagementTestExecutionListener.class, ArtifactManagementTestExecutionListener.class }) @Test public void testDeleteArtifactDirectory(@MavenRepository(repositoryId = REPOSITORY_RELEASES_1, setup = MavenIndexedRepositorySetup.class) Repository repository, @MavenTestArtifact(repositoryId = REPOSITORY_RELEASES_1, id = "com.artifacts.to.delete.releases:delete-foo", versions = "1.2.2") Path artifactPath) throws Exception { RepositoryPath artifactDirectoryPath = (RepositoryPath) artifactPath.getParent().normalize(); String artifactDirectoryPathStr = RepositoryFiles.relativizePath(artifactDirectoryPath); assertThat(Files.exists(artifactDirectoryPath)) .as("Failed to locate artifact directory '" + artifactDirectoryPath + "'!") .isTrue(); client.delete(repository.getStorage().getId(), repository.getId(), artifactDirectoryPathStr); assertThat(Files.notExists(artifactDirectoryPath)) .as("Failed to delete artifact file '" + artifactDirectoryPath + "'!") .isTrue(); }
@ExtendWith({ RepositoryManagementTestExecutionListener.class, ArtifactManagementTestExecutionListener.class }) @Test public void testUpdateMetadataOnDeleteReleaseVersionDirectory(@MavenRepository(repositoryId = REPOSITORY_RELEASES_2, setup = MavenIndexedRepositorySetup.class) Repository repository, @MavenTestArtifact(repositoryId = REPOSITORY_RELEASES_2, id = "org.carlspring.strongbox.delete-metadata:metadata-foo", versions = { "1.2.1", "1.2.2" }, classifiers = "javadoc") List<Path> artifactsPaths) throws Exception { final String storageId = repository.getStorage().getId(); final String repositoryId = repository.getId(); RepositoryPath artifact1RepositoryPath = (RepositoryPath) artifactsPaths.get(0).normalize(); String artifact1RepositoryPathStr = RepositoryFiles.relativizePath(artifact1RepositoryPath); String unixBasedRelativePath = FilenameUtils.separatorsToUnix(artifact1RepositoryPathStr); Artifact artifact1 = MavenArtifactUtils.convertPathToArtifact(unixBasedRelativePath); RepositoryPath artifact2RepositoryPath = (RepositoryPath) artifactsPaths.get(1).normalize(); String artifact2RepositoryPathStr = RepositoryFiles.relativizePath(artifact2RepositoryPath); mavenMetadataServiceHelper.generateMavenMetadata(repository); client.delete(storageId, repositoryId, artifact2RepositoryPathStr); assertThat(artifact1).isNotNull(); String metadataPath = String.format("/storages/%s/%s/%s", storageId, repositoryId, getArtifactLevelMetadataPath(artifact1)); Metadata metadata = defaultMavenArtifactDeployer.retrieveMetadata(metadataPath); assertThat(metadata).isNotNull(); assertThat(metadata.getVersioning()).isNotNull(); assertThat(metadata.getVersioning().getVersions()).isNotNull(); assertThat(metadata.getVersioning().getVersions().contains("1.2.2")).isFalse(); }
@ExtendWith({ RepositoryManagementTestExecutionListener.class, ArtifactManagementTestExecutionListener.class }) @Test public void testUpdateMetadataOnDeleteSnapshotVersionDirectory(@MavenRepository(repositoryId = REPOSITORY_SNAPSHOTS, policy = RepositoryPolicyEnum.SNAPSHOT) Repository repository, @MavenTestArtifact(repositoryId = REPOSITORY_SNAPSHOTS, id = "org.carlspring.strongbox.metadata:metadata-foo", versions = { "3.1-20190812.124500-1", "3.1-20190812.124600-2", "3.1-20190812.124700-3", "3.1-20190812.124800-4" }) List<Path> artifactsPaths) throws XmlPullParserException, IOException, ArtifactOperationException, ArtifactTransportException { final String storageId = repository.getStorage().getId(); final String repositoryId = repository.getId(); mavenMetadataServiceHelper.generateMavenMetadata(repository); RepositoryPath artifact1RepositoryPath = (RepositoryPath) artifactsPaths.get(0).normalize(); String artifact1RepositoryPathStr = RepositoryFiles.relativizePath(artifact1RepositoryPath); String unixBasedRelativePath = FilenameUtils.separatorsToUnix(artifact1RepositoryPathStr); Artifact artifact1 = MavenArtifactUtils.convertPathToArtifact(unixBasedRelativePath); RepositoryPath artifact1ParentPath = artifact1RepositoryPath.getParent(); String artifact1ParentPathStr = RepositoryFiles.relativizePath(artifact1ParentPath); client.delete(storageId, repositoryId, artifact1ParentPathStr); assertThat(artifact1).isNotNull(); String metadataPath = String.format("/storages/%s/%s/%s", storageId, repositoryId, getArtifactLevelMetadataPath(artifact1)); Metadata metadata = defaultMavenArtifactDeployer.retrieveMetadata(metadataPath); assertThat(metadata.getVersioning().getVersions().contains("3.1-SNAPSHOT")).isFalse(); }
@Test public void shouldNotAllowGettingExistingDirectories() throws IOException { Repository repository = configurationManagementService.getConfiguration() .getRepository(STORAGE0, REPOSITORY_RELEASES); RootRepositoryPath repositoryPath = repositoryPathResolver.resolve(repository); String artifactParentPathStr = "org/carlspring/logging/logback-configuration-core"; Path artifactParentPath = repositoryPath.resolve(artifactParentPathStr); Files.createDirectories(artifactParentPath); String url = getContextBaseUrl() + "/storages/{storageId}/{repositoryId}/{artifactPath}"; mockMvc.when() .get(url, repository.getStorage().getId(), repository.getId(), artifactParentPathStr) .peek() .then() .statusCode(HttpStatus.BAD_REQUEST.value()) .statusLine(equalTo("400 The specified path is a directory!")); Files.delete(artifactParentPath); Files.delete(artifactParentPath.getParent()); }