diff --git a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/CrudAasRepository.java b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/CrudAasRepository.java index 4200aea95..326e5dcf9 100644 --- a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/CrudAasRepository.java +++ b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/CrudAasRepository.java @@ -47,7 +47,6 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.data.repository.CrudRepository; - /** * Default Implementation for the {@link AasRepository} based on Spring * {@link CrudRepository} @@ -70,7 +69,7 @@ public CrudAasRepository(AasBackendProvider aasBackendProvider, AasServiceFactor public CrudAasRepository(AasBackendProvider aasBackendProvider, AasServiceFactory aasServiceFactory, @Value("${basyx.aasrepo.name:aas-repo}") String aasRepositoryName) { this(aasBackendProvider, aasServiceFactory); - + this.aasRepositoryName = aasRepositoryName; } @@ -192,44 +191,50 @@ public File getThumbnail(String aasId) { @Override public void setThumbnail(String aasId, String fileName, String contentType, InputStream inputStream) { Resource thumbnail = getAssetInformation(aasId).getDefaultThumbnail(); - if (thumbnail != null) { - String path = thumbnail.getPath(); - - ThumbnailHandler.deleteExistingFile(path); - } - - String filePath = ThumbnailHandler.createFilePath(ThumbnailHandler.getTemporaryDirectoryPath(), aasId, fileName); - - ThumbnailHandler.createFileAtSpecifiedPath(fileName, inputStream, filePath); if (thumbnail != null) { - ThumbnailHandler.updateThumbnail(this, aasId, contentType, filePath); - + updateThumbnailFile(aasId, fileName, contentType, inputStream, thumbnail); return; } + String filePath = createFile(aasId, fileName, inputStream); ThumbnailHandler.setNewThumbnail(this, aasId, contentType, filePath); } + private void updateThumbnailFile(String aasId, String fileName, String contentType, InputStream inputStream, Resource thumbnail) { + String path = thumbnail.getPath(); + ThumbnailHandler.deleteExistingFile(path); + String filePath = createFile(aasId, fileName, inputStream); + ThumbnailHandler.updateThumbnail(this, aasId, contentType, filePath); + } + private String createFile(String aasId, String fileName, InputStream inputStream) { + String filePath = ThumbnailHandler.createFilePath(ThumbnailHandler.getTemporaryDirectoryPath(), aasId, fileName); + ThumbnailHandler.createFileAtSpecifiedPath(fileName, inputStream, filePath); + return filePath; + } @Override public void deleteThumbnail(String aasId) { Resource thumbnail = getAssetInformation(aasId).getDefaultThumbnail(); - ThumbnailHandler.throwIfFileDoesNotExist(aasId, thumbnail); - String filePath = thumbnail.getPath(); - java.io.File tmpFile = new java.io.File(filePath); - tmpFile.delete(); + deleteThumbnailFile(thumbnail); + + updateThumbnailInAssetInformation(aasId); + } + private void updateThumbnailInAssetInformation(String aasId) { AssetInformation assetInfor = getAssetInformation(aasId); assetInfor.getDefaultThumbnail().setContentType(""); assetInfor.getDefaultThumbnail().setPath(""); setAssetInformation(aasId, assetInfor); - } - + private void deleteThumbnailFile(Resource thumbnail) { + String filePath = thumbnail.getPath(); + java.io.File tmpFile = new java.io.File(filePath); + tmpFile.delete(); + } } diff --git a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/ThumbnailHandler.java b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/ThumbnailHandler.java index 22b3fc319..267e367ba 100644 --- a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/ThumbnailHandler.java +++ b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/ThumbnailHandler.java @@ -39,9 +39,13 @@ import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository; import org.eclipse.digitaltwin.basyx.core.exceptions.FileDoesNotExistException; import org.eclipse.digitaltwin.basyx.core.exceptions.FileHandlingException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ThumbnailHandler { + private static Logger logger = LoggerFactory.getLogger(ThumbnailHandler.class); + public static void updateThumbnail(AasRepository aasRepo, String aasId, String contentType, String filePath) { AssetInformation assetInfor = aasRepo.getAssetInformation(aasId); assetInfor.getDefaultThumbnail().setContentType(contentType); @@ -63,19 +67,17 @@ public static void throwIfFileDoesNotExist(String aasId, Resource resource) { throw new FileDoesNotExistException(aasId); String filePath = resource.getPath(); - if (!isFilePathValid(filePath)) - throw new FileDoesNotExistException(aasId); + throwIfFilePathIsNotValid(aasId, filePath); } - private static boolean isFilePathValid(String filePath) { + private static void throwIfFilePathIsNotValid(String aasId, String filePath) { if (filePath.isEmpty()) - return false; + throw new FileDoesNotExistException(aasId); try { Paths.get(filePath); } catch (InvalidPathException | NullPointerException ex) { - return false; + throw new FileDoesNotExistException(aasId); } - return true; } public static String createFilePath(String tmpDirectory, String aasId, String fileName) { @@ -99,7 +101,7 @@ public static void deleteExistingFile(String path) { try { Files.deleteIfExists(Paths.get(path, "")); } catch (IOException e) { - e.printStackTrace(); + logger.error("Unable to delete the file having path '{}'", path); } } @@ -108,7 +110,7 @@ public static String getTemporaryDirectoryPath() { try { tempDirectoryPath = Files.createTempDirectory("basyx-temp-thumbnail").toAbsolutePath().toString(); } catch (IOException e) { - e.printStackTrace(); + logger.error("Unable to create file in the temporary path."); } return tempDirectoryPath; } diff --git a/basyx.aasrepository/basyx.aasrepository-core/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/AasRepositorySuite.java b/basyx.aasrepository/basyx.aasrepository-core/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/AasRepositorySuite.java index 8c4cc3d77..f39c93538 100644 --- a/basyx.aasrepository/basyx.aasrepository-core/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/AasRepositorySuite.java +++ b/basyx.aasrepository/basyx.aasrepository-core/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/AasRepositorySuite.java @@ -279,9 +279,8 @@ public void updateThumbnail() throws FileNotFoundException, IOException { // Set the thumbnail of the AAS for the first time aasRepo.setThumbnail(AAS2, THUMBNAIL_FILE_PATH_1, "", getInputStreamOfFileFromClasspath(THUMBNAIL_FILE_PATH_1)); - // Set the thumbnail of the AAS for the second time with a new figure - aasRepo.setThumbnail(AAS2, THUMBNAIL_FILE_PATH_1, "", getInputStreamOfFileFromClasspath(THUMBNAIL_FILE_PATH_2)); + aasRepo.setThumbnail(AAS2, THUMBNAIL_FILE_PATH_2, "", getInputStreamOfFileFromClasspath(THUMBNAIL_FILE_PATH_2)); File retrievedThumbnail = aasRepo.getThumbnail(AAS2); @@ -313,16 +312,12 @@ public void getNonExistingFile() { aasRepo.getThumbnail(AAS2); } - @Test + @Test(expected = FileDoesNotExistException.class) public void deleteThumbnail() throws FileNotFoundException, IOException { aasRepo.setThumbnail(AAS2, THUMBNAIL_FILE_PATH_1, "", getInputStreamOfFileFromClasspath(THUMBNAIL_FILE_PATH_1)); aasRepo.deleteThumbnail(AAS2); - try { - aasRepo.getThumbnail(AAS2); - fail(); - } catch (Exception e) { - } + aasRepo.getThumbnail(AAS2); } @Test(expected = FileDoesNotExistException.class) diff --git a/basyx.aasrepository/basyx.aasrepository-http/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/http/AasRepositoryApiHTTPController.java b/basyx.aasrepository/basyx.aasrepository-http/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/http/AasRepositoryApiHTTPController.java index b863481bd..c1a730a81 100644 --- a/basyx.aasrepository/basyx.aasrepository-http/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/http/AasRepositoryApiHTTPController.java +++ b/basyx.aasrepository/basyx.aasrepository-http/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/http/AasRepositoryApiHTTPController.java @@ -41,7 +41,6 @@ import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository; import org.eclipse.digitaltwin.basyx.aasrepository.http.pagination.GetAssetAdministrationShellsResult; import org.eclipse.digitaltwin.basyx.aasrepository.http.pagination.GetReferencesResult; -import org.eclipse.digitaltwin.basyx.core.exceptions.FileDoesNotExistException; import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult; import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo; import org.eclipse.digitaltwin.basyx.http.Base64UrlEncodedIdentifier; @@ -177,12 +176,8 @@ private String getEncodedCursorFromCursorResult(CursorResult cursorResult) { @Override public ResponseEntity deleteThumbnailAasRepository(Base64UrlEncodedIdentifier aasIdentifier) { - try { - aasRepository.deleteThumbnail(aasIdentifier.getIdentifier()); - return new ResponseEntity(HttpStatus.OK); - } catch (FileDoesNotExistException e) { - return new ResponseEntity(HttpStatus.NOT_FOUND); - } + aasRepository.deleteThumbnail(aasIdentifier.getIdentifier()); + return new ResponseEntity(HttpStatus.OK); } @Override @@ -191,8 +186,8 @@ public ResponseEntity getThumbnailAasRepository(Base64UrlEncodedIdenti FileInputStream fileInputStream = new FileInputStream(aasRepository.getThumbnail(aasIdentifier.getIdentifier())); Resource resource = new InputStreamResource(fileInputStream); return new ResponseEntity(resource, HttpStatus.OK); - } catch (FileDoesNotExistException | FileNotFoundException e) { - return new ResponseEntity(HttpStatus.NOT_FOUND); + } catch (FileNotFoundException e) { + return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR); } } diff --git a/basyx.aasrepository/basyx.aasrepository-http/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/http/AasRepositoryHTTPSuite.java b/basyx.aasrepository/basyx.aasrepository-http/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/http/AasRepositoryHTTPSuite.java index bb829ea7f..8295f20ff 100644 --- a/basyx.aasrepository/basyx.aasrepository-http/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/http/AasRepositoryHTTPSuite.java +++ b/basyx.aasrepository/basyx.aasrepository-http/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/http/AasRepositoryHTTPSuite.java @@ -337,10 +337,12 @@ public void deleteThumbnail() throws FileNotFoundException, IOException { response = BaSyxHttpTestUtils.executeGetOnURL(BaSyxHttpTestUtils.getThumbnailAccessURL(getURL(), dummyAasId)); assertEquals(HttpStatus.NOT_FOUND.value(), response.getCode()); + + response.close(); } @Test - public void deleteNoneExistingThumbnail() throws FileNotFoundException, UnsupportedEncodingException, ClientProtocolException, IOException { + public void deleteNonExistingThumbnail() throws FileNotFoundException, UnsupportedEncodingException, ClientProtocolException, IOException { CloseableHttpResponse response = BaSyxHttpTestUtils.executeDeleteOnURL(BaSyxHttpTestUtils.getThumbnailAccessURL(getURL(), dummyAasId)); assertEquals(HttpStatus.NOT_FOUND.value(), response.getCode()); diff --git a/basyx.common/basyx.http/src/main/java/org/eclipse/digitaltwin/basyx/http/BaSyxExceptionHandler.java b/basyx.common/basyx.http/src/main/java/org/eclipse/digitaltwin/basyx/http/BaSyxExceptionHandler.java index 8cecd1d0d..8e93228ef 100644 --- a/basyx.common/basyx.http/src/main/java/org/eclipse/digitaltwin/basyx/http/BaSyxExceptionHandler.java +++ b/basyx.common/basyx.http/src/main/java/org/eclipse/digitaltwin/basyx/http/BaSyxExceptionHandler.java @@ -31,6 +31,7 @@ import org.eclipse.digitaltwin.basyx.core.exceptions.CollidingIdentifierException; import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException; import org.eclipse.digitaltwin.basyx.core.exceptions.FeatureNotSupportedException; +import org.eclipse.digitaltwin.basyx.core.exceptions.FileDoesNotExistException; import org.eclipse.digitaltwin.basyx.core.exceptions.IdentificationMismatchException; import org.eclipse.digitaltwin.basyx.core.exceptions.NotInvokableException; import org.springframework.http.HttpStatus; @@ -88,4 +89,9 @@ public ResponseEntity handleFeatureNotSupportedException(FeatureNotSuppor public ResponseEntity handleNotInvokableException(NotInvokableException exception) { return new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED); } + + @ExceptionHandler(FileDoesNotExistException.class) + public ResponseEntity handleFileDoesNotExistException(FileDoesNotExistException exception) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } } diff --git a/pom.xml b/pom.xml index 8aeb28649..ef8775129 100644 --- a/pom.xml +++ b/pom.xml @@ -16,10 +16,10 @@ basyx.common basyx.submodelservice basyx.submodelrepository - + basyx.submodelregistry basyx.aasservice basyx.aasrepository - + basyx.aasregistry basyx.aasenvironment basyx.conceptdescriptionrepository basyx.aasxfileserver