From 0ed7c8863131f39e471db95671372ef16c75bedd Mon Sep 17 00:00:00 2001 From: Philipp Zehnder Date: Sun, 7 Apr 2024 12:31:57 +0200 Subject: [PATCH] refactor(#2708): Make all methods of FileManager none static --- .../generator/ExportPackageGenerator.java | 2 +- .../streampipes/manager/file/FileManager.java | 32 ++++++++--------- .../manager/file/TestFileManager.java | 34 ++++++++++++------- .../streampipes/rest/ResetManagement.java | 5 +-- .../rest/impl/AssetDashboardResource.java | 6 ++-- .../rest/impl/PipelineElementFile.java | 18 ++++++---- 6 files changed, 56 insertions(+), 41 deletions(-) diff --git a/streampipes-data-export/src/main/java/org/apache/streampipes/export/generator/ExportPackageGenerator.java b/streampipes-data-export/src/main/java/org/apache/streampipes/export/generator/ExportPackageGenerator.java index fc3663929f..13abd92077 100644 --- a/streampipes-data-export/src/main/java/org/apache/streampipes/export/generator/ExportPackageGenerator.java +++ b/streampipes-data-export/src/main/java/org/apache/streampipes/export/generator/ExportPackageGenerator.java @@ -124,7 +124,7 @@ public byte[] generateExportPackage() throws IOException { String filename = fileResolver.findDocument(item.getResourceId()).getFilename(); addDoc(builder, item, new FileResolver(), manifest::addFile); try { - builder.addBinary(filename, Files.readAllBytes(FileManager.getFile(filename).toPath())); + builder.addBinary(filename, Files.readAllBytes(new FileManager().getFile(filename).toPath())); } catch (IOException e) { e.printStackTrace(); } diff --git a/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/file/FileManager.java b/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/file/FileManager.java index 5038ec3af0..bfd7518b86 100644 --- a/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/file/FileManager.java +++ b/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/file/FileManager.java @@ -34,16 +34,16 @@ public class FileManager { - public static List getAllFiles() { + public List getAllFiles() { return getAllFiles(null); } - public static List getAllFiles(String filetypes) { + public List getAllFiles(String filetypes) { List allFiles = getFileMetadataStorage().getAllFileMetadataDescriptions(); return filetypes != null ? filterFiletypes(allFiles, filetypes) : allFiles; } - public static File getFile(String filename) { + public File getFile(String filename) { return new FileHandler().getFile(filename); } @@ -56,7 +56,7 @@ public static File getFile(String filename) { * @param fileInputStream content of file * @return metadata of file */ - public static FileMetadata storeFile(String user, + public FileMetadata storeFile(String user, String filename, InputStream fileInputStream) throws IOException { @@ -72,13 +72,13 @@ public static FileMetadata storeFile(String user, } - public static void deleteFile(String id) { + public void deleteFile(String id) { FileMetadata fileMetadata = getFileMetadataStorage().getMetadataById(id); new FileHandler().deleteFile(fileMetadata.getFilename()); getFileMetadataStorage().deleteFileMetadata(id); } - private static InputStream validateFileNameAndCleanFile(String filename, + private InputStream validateFileNameAndCleanFile(String filename, String filetype, InputStream fileInputStream) { if (!validateFileType(filename)) { @@ -95,7 +95,7 @@ private static InputStream validateFileNameAndCleanFile(String filename, * @param filetype file of type * @return input stream without BOM */ - protected static InputStream cleanFile(InputStream fileInputStream, String filetype) { + protected InputStream cleanFile(InputStream fileInputStream, String filetype) { if (Filetypes.CSV.getFileExtensions().contains(filetype.toLowerCase())) { fileInputStream = new BOMInputStream(fileInputStream); } @@ -103,34 +103,34 @@ protected static InputStream cleanFile(InputStream fileInputStream, String filet return fileInputStream; } - public static boolean checkFileContentChanged(String filename, String hash) throws IOException { + public boolean checkFileContentChanged(String filename, String hash) throws IOException { var fileHash = FileHasher.hash(getFile(filename)); return !fileHash.equals(hash); } - public static String sanitizeFilename(String filename) { + public String sanitizeFilename(String filename) { return filename.replaceAll("[^a-zA-Z0-9.\\-]", "_"); } - public static boolean validateFileType(String filename) { + public boolean validateFileType(String filename) { return Filetypes.getAllFileExtensions() .stream() .anyMatch(filename::endsWith); } - protected static void writeToFile(String sanitizedFilename, InputStream fileInputStream) throws IOException { + protected void writeToFile(String sanitizedFilename, InputStream fileInputStream) throws IOException { new FileHandler().storeFile(sanitizedFilename, fileInputStream); } - private static IFileMetadataStorage getFileMetadataStorage() { + private IFileMetadataStorage getFileMetadataStorage() { return StorageDispatcher .INSTANCE .getNoSqlStore() .getFileMetadataStorage(); } - protected static FileMetadata makeAndStoreFileMetadata(String user, + protected FileMetadata makeAndStoreFileMetadata(String user, String sanitizedFilename, String filetype) { var fileMetadata = makeFileMetadata(user, sanitizedFilename, filetype); @@ -139,7 +139,7 @@ protected static FileMetadata makeAndStoreFileMetadata(String user, return fileMetadata; } - private static FileMetadata makeFileMetadata(String user, + private FileMetadata makeFileMetadata(String user, String filename, String filetype) { @@ -152,12 +152,12 @@ private static FileMetadata makeFileMetadata(String user, return fileMetadata; } - private static void storeFileMetadata(FileMetadata fileMetadata) { + private void storeFileMetadata(FileMetadata fileMetadata) { getFileMetadataStorage().addFileMetadata(fileMetadata); } - private static List filterFiletypes(List allFiles, String filetypes) { + private List filterFiletypes(List allFiles, String filetypes) { return allFiles .stream() .filter(fileMetadata -> Arrays diff --git a/streampipes-pipeline-management/src/test/java/org/apache/streampipes/manager/file/TestFileManager.java b/streampipes-pipeline-management/src/test/java/org/apache/streampipes/manager/file/TestFileManager.java index a02a309fa3..21f8d9dac3 100644 --- a/streampipes-pipeline-management/src/test/java/org/apache/streampipes/manager/file/TestFileManager.java +++ b/streampipes-pipeline-management/src/test/java/org/apache/streampipes/manager/file/TestFileManager.java @@ -18,6 +18,7 @@ package org.apache.streampipes.manager.file; import org.apache.commons.io.IOUtils; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.IOException; @@ -32,19 +33,26 @@ public class TestFileManager { + private FileManager fileManager; + + @BeforeEach + public void setup() { + fileManager = new FileManager(); + } + @Test public void storeFile_throwsExceptionForInvalidFileType() { var filename = "testFile.invalid"; assertThrows(IllegalArgumentException.class, () -> - FileManager.storeFile("", filename, mock(InputStream.class))); + fileManager.storeFile("", filename, mock(InputStream.class))); } @Test public void testCleanFileWithoutBom() throws IOException { var expected = "test"; var inputStream = IOUtils.toInputStream(expected, StandardCharsets.UTF_8); - var resultStream = FileManager.cleanFile(inputStream, "CSV"); + var resultStream = fileManager.cleanFile(inputStream, "CSV"); var resultString = IOUtils.toString(resultStream, StandardCharsets.UTF_8); assertEquals(expected, resultString); @@ -56,7 +64,7 @@ public void testCleanFileWithBom() throws IOException { var utf8Bom = "\uFEFF"; var inputString = utf8Bom + expected; var inputStream = IOUtils.toInputStream(inputString, StandardCharsets.UTF_8); - var resultStream = FileManager.cleanFile(inputStream, "CSV"); + var resultStream = fileManager.cleanFile(inputStream, "CSV"); var resultString = IOUtils.toString(resultStream, StandardCharsets.UTF_8); assertEquals(expected, resultString); @@ -68,7 +76,7 @@ public void testCleanFileWithBomAndUmlauts() throws IOException { var utf8Bom = "\uFEFF"; var inputString = utf8Bom + expected; var inputStream = IOUtils.toInputStream(inputString, StandardCharsets.UTF_8); - var resultStream = FileManager.cleanFile(inputStream, "CSV"); + var resultStream = fileManager.cleanFile(inputStream, "CSV"); var resultString = IOUtils.toString(resultStream, StandardCharsets.UTF_8); assertEquals(expected, resultString); @@ -77,57 +85,57 @@ public void testCleanFileWithBomAndUmlauts() throws IOException { @Test public void sanitizeFilename_replacesNonAlphanumericCharactersWithUnderscore() { var filename = "file@name#with$special%characters"; - var sanitizedFilename = FileManager.sanitizeFilename(filename); + var sanitizedFilename = fileManager.sanitizeFilename(filename); assertEquals("file_name_with_special_characters", sanitizedFilename); } @Test public void sanitizeFilename_keepsAlphanumericAndDotAndHyphenCharacters() { var filename = "file.name-with_alphanumeric123"; - var sanitizedFilename = FileManager.sanitizeFilename(filename); + var sanitizedFilename = fileManager.sanitizeFilename(filename); assertEquals(filename, sanitizedFilename); } @Test public void sanitizeFilename_returnsUnderscoreForFilenameWithAllSpecialCharacters() { var filename = "@#$%^&*()"; - var sanitizedFilename = FileManager.sanitizeFilename(filename); + var sanitizedFilename = fileManager.sanitizeFilename(filename); assertEquals("_________", sanitizedFilename); } @Test public void sanitizeFilename_returnsEmptyStringForEmptyFilename() { var filename = ""; - var sanitizedFilename = FileManager.sanitizeFilename(filename); + var sanitizedFilename = fileManager.sanitizeFilename(filename); assertEquals("", sanitizedFilename); } @Test public void sanitizeFilename_removesSingleParentDirectory() { var filename = "../file.csv"; - var sanitizedFilename = FileManager.sanitizeFilename(filename); + var sanitizedFilename = fileManager.sanitizeFilename(filename); assertEquals(".._file.csv", sanitizedFilename); } @Test public void sanitizeFilename_removesDoubleParentDirectoryy() { var filename = "../../file"; - var sanitizedFilename = FileManager.sanitizeFilename(filename); + var sanitizedFilename = fileManager.sanitizeFilename(filename); assertEquals(".._.._file", sanitizedFilename); } @Test public void validateFileName_returnsTrueForCsv() { - assertTrue(FileManager.validateFileType("file.csv")); + assertTrue(fileManager.validateFileType("file.csv")); } @Test public void validateFileName_returnsTrueForJson() { - assertTrue(FileManager.validateFileType("file.json")); + assertTrue(fileManager.validateFileType("file.json")); } @Test public void validateFileName_returnsFalseForSh() { - assertFalse(FileManager.validateFileType("file.sh")); + assertFalse(fileManager.validateFileType("file.sh")); } } \ No newline at end of file diff --git a/streampipes-rest/src/main/java/org/apache/streampipes/rest/ResetManagement.java b/streampipes-rest/src/main/java/org/apache/streampipes/rest/ResetManagement.java index 8319d0a4ee..d5e89b9671 100644 --- a/streampipes-rest/src/main/java/org/apache/streampipes/rest/ResetManagement.java +++ b/streampipes-rest/src/main/java/org/apache/streampipes/rest/ResetManagement.java @@ -131,8 +131,9 @@ private static void stopAndDeleteAllAdapters() { } private static void deleteAllFiles() { - List allFiles = FileManager.getAllFiles(); - allFiles.forEach(fileMetadata -> FileManager.deleteFile(fileMetadata.getFileId())); + var fileManager = new FileManager(); + List allFiles = fileManager.getAllFiles(); + allFiles.forEach(fileMetadata -> fileManager.deleteFile(fileMetadata.getFileId())); } private static void removeAllDataInDataLake() { diff --git a/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/AssetDashboardResource.java b/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/AssetDashboardResource.java index 4433d51ae4..4687f7c962 100644 --- a/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/AssetDashboardResource.java +++ b/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/AssetDashboardResource.java @@ -106,7 +106,7 @@ public ResponseEntity deleteAssetDashboard(@PathVariable("dashboardId") St @GetMapping(path = "/images/{imageName}") public ResponseEntity getDashboardImage(@PathVariable("imageName") String imageName) { try { - var sanitizedFileName = FileManager.sanitizeFilename(imageName); + var sanitizedFileName = new FileManager().sanitizeFilename(imageName); java.nio.file.Path path = Paths.get(getTargetFile(sanitizedFileName)); File file = new File(path.toString()); FileNameMap fileNameMap = URLConnection.getFileNameMap(); @@ -128,7 +128,7 @@ public ResponseEntity storeDashboardImage(@RequestPart("file_upload") Mult try { var fileName = fileDetail.getName(); - if (!FileManager.validateFileType(fileName)) { + if (!new FileManager().validateFileType(fileName)) { LOG.error("File type of file " + fileName + " is not supported"); fail(); } @@ -148,7 +148,7 @@ private void storeFile(String fileName, InputStream fileInputStream) { targetDirectory.mkdirs(); } - var sanitizedFileName = FileManager.sanitizeFilename(fileName); + var sanitizedFileName = new FileManager().sanitizeFilename(fileName); var targetFile = new File(getTargetFile(sanitizedFileName)); diff --git a/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/PipelineElementFile.java b/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/PipelineElementFile.java index 89373cce05..006f3f955d 100644 --- a/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/PipelineElementFile.java +++ b/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/PipelineElementFile.java @@ -53,6 +53,12 @@ @RequestMapping("/api/v2/files") public class PipelineElementFile extends AbstractAuthGuardedRestResource { + private final FileManager fileManager; + + public PipelineElementFile() { + this.fileManager = new FileManager(); + } + @PostMapping( consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @@ -60,7 +66,7 @@ public class PipelineElementFile extends AbstractAuthGuardedRestResource { public ResponseEntity storeFile(@RequestPart("file_upload") MultipartFile fileDetail) { try { FileMetadata metadata = - FileManager.storeFile( + fileManager.storeFile( getAuthenticatedUsername(), fileDetail.getOriginalFilename(), fileDetail.getInputStream() @@ -74,7 +80,7 @@ public ResponseEntity storeFile(@RequestPart("file_upload") MultipartFile fil @DeleteMapping(path = "{fileId}") @PreAuthorize(AuthConstants.IS_ADMIN_ROLE) public ResponseEntity deleteFile(@PathVariable("fileId") String fileId) { - FileManager.deleteFile(fileId); + fileManager.deleteFile(fileId); return ok(); } @@ -83,7 +89,7 @@ public ResponseEntity deleteFile(@PathVariable("fileId") String fileId) { public ResponseEntity> getFileInfo( @RequestParam(value = "filetypes", required = false) String filetypes ) { - return ok(FileManager.getAllFiles(filetypes)); + return ok(fileManager.getAllFiles(filetypes)); } @GetMapping(path = "/{filename}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) @@ -109,7 +115,7 @@ public ResponseEntity getFile( @PathVariable("filename") String filename ) { try { - return ok(getFileContents(FileManager.getFile(filename))); + return ok(getFileContents(fileManager.getFile(filename))); } catch (IOException e) { throw new SpMessageException( NOT_FOUND, @@ -125,7 +131,7 @@ private byte[] getFileContents(File file) throws IOException { @GetMapping(path = "/allFilenames", produces = MediaType.APPLICATION_JSON_VALUE) @PreAuthorize(AuthConstants.HAS_READ_FILE_PRIVILEGE) public ResponseEntity> getAllOriginalFilenames() { - return ok(FileManager.getAllFiles() + return ok(fileManager.getAllFiles() .stream() .map(fileMetadata -> fileMetadata.getFilename() .toLowerCase()) @@ -141,7 +147,7 @@ public ResponseEntity checkFileContentChanged( @PathVariable(value = "hash") String hash ) { try { - return ok(FileManager.checkFileContentChanged(filename, hash)); + return ok(fileManager.checkFileContentChanged(filename, hash)); } catch (IOException e) { throw new SpMessageException( NOT_FOUND,