MavenArtifactController extends BaseArtifactController { @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:.+}") public ResponseEntity copy( @RepositoryMapping(storageVariableName = "srcStorageId", repositoryVariableName = "srcRepositoryId") Repository srcRepository, @RepositoryMapping(storageVariableName = "destStorageId", repositoryVariableName = "destRepositoryId") Repository destRepository, @PathVariable String path) { final String srcStorageId = srcRepository.getStorage().getId(); final String srcRepositoryId = srcRepository.getId(); final String destStorageId = destRepository.getStorage().getId(); final String destRepositoryId = destRepository.getId(); logger.debug("Copying {} from {}:{} to {}:{}...", path, srcStorageId, srcRepositoryId, destStorageId, destRepositoryId); try { final RepositoryPath srcRepositoryPath = repositoryPathResolver.resolve(srcRepository, path); if (!Files.exists(srcRepositoryPath)) { return ResponseEntity.status(NOT_FOUND) .body("The source path does not exist!"); } RepositoryPath srcPath = repositoryPathResolver.resolve(srcRepository, path); RepositoryPath destPath = repositoryPathResolver.resolve(destRepository, path); artifactManagementService.copy(srcPath, destPath); } catch (ArtifactStorageException e) { logger.error("Unable to copy artifact due to ArtifactStorageException", e); return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body(e.getMessage()); } catch (Exception e) { logger.error("Unable to copy artifact", e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(e.getMessage()); } return ResponseEntity.ok("The path was copied successfully."); }  @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 testCopyArtifactFile(@MavenRepository(repositoryId = REPOSITORY_RELEASES_1, setup = MavenIndexedRepositorySetup.class) Repository repository1, @MavenRepository(repositoryId = REPOSITORY_RELEASES_2, setup = MavenIndexedRepositorySetup.class) Repository repository2, @MavenTestArtifact(repositoryId = REPOSITORY_RELEASES_1, id = "org.carlspring.strongbox.copy:copy-foo", versions = "1.1") Path artifactPath) throws IOException { RepositoryPath artifactRepositoryPath = (RepositoryPath) artifactPath.normalize(); String artifactRepositoryPathStr = RepositoryFiles.relativizePath(artifactRepositoryPath); client.copy(artifactRepositoryPathStr, repository1.getStorage().getId(), repository1.getId(), repository2.getStorage().getId(), repository2.getId()); RootRepositoryPath repositoryPath2 = repositoryPathResolver.resolve(repository2); Path destArtifactPath = repositoryPath2.resolve(artifactRepositoryPathStr); assertThat(Files.exists(destArtifactPath)) .as("Failed to copy artifact to destination repository '" + destArtifactPath + "'!") .isTrue(); }
@ExtendWith({ RepositoryManagementTestExecutionListener.class, ArtifactManagementTestExecutionListener.class }) @Test public void testCopyArtifactDirectory(@MavenRepository(repositoryId = REPOSITORY_RELEASES_1, setup = MavenIndexedRepositorySetup.class) Repository repository1, @MavenRepository(repositoryId = REPOSITORY_RELEASES_2, setup = MavenIndexedRepositorySetup.class) Repository repository2, @MavenTestArtifact(repositoryId = REPOSITORY_RELEASES_1, id = "org.carlspring.strongbox.copy:copy-foo", versions = "1.2") Path artifactPath) throws IOException { RepositoryPath artifactDirectoryPath = (RepositoryPath) artifactPath.getParent().normalize(); String artifactDirectoryPathStr = RepositoryFiles.relativizePath(artifactDirectoryPath); client.copy(artifactDirectoryPathStr, repository1.getStorage().getId(), repository1.getId(), repository2.getStorage().getId(), repository2.getId()); RootRepositoryPath repositoryPath2 = repositoryPathResolver.resolve(repository2); Path destArtifactPath = repositoryPath2.resolve(artifactDirectoryPathStr); assertThat(Files.exists(destArtifactPath)) .as("Failed to copy artifact to destination repository '" + destArtifactPath + "'!") .isTrue(); }