Skip to content

Commit

Permalink
Optimize exception handling.
Browse files Browse the repository at this point in the history
Signed-off-by: Zai Müller-Zhang <[email protected]>
  • Loading branch information
zhangzai123 committed Nov 30, 2023
1 parent a4a2e6d commit 3b4659c
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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);
}
}

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -177,12 +176,8 @@ private String getEncodedCursorFromCursorResult(CursorResult<?> cursorResult) {

@Override
public ResponseEntity<Void> deleteThumbnailAasRepository(Base64UrlEncodedIdentifier aasIdentifier) {
try {
aasRepository.deleteThumbnail(aasIdentifier.getIdentifier());
return new ResponseEntity<Void>(HttpStatus.OK);
} catch (FileDoesNotExistException e) {
return new ResponseEntity<Void>(HttpStatus.NOT_FOUND);
}
aasRepository.deleteThumbnail(aasIdentifier.getIdentifier());
return new ResponseEntity<Void>(HttpStatus.OK);
}

@Override
Expand All @@ -191,8 +186,8 @@ public ResponseEntity<Resource> getThumbnailAasRepository(Base64UrlEncodedIdenti
FileInputStream fileInputStream = new FileInputStream(aasRepository.getThumbnail(aasIdentifier.getIdentifier()));
Resource resource = new InputStreamResource(fileInputStream);
return new ResponseEntity<Resource>(resource, HttpStatus.OK);
} catch (FileDoesNotExistException | FileNotFoundException e) {
return new ResponseEntity<Resource>(HttpStatus.NOT_FOUND);
} catch (FileNotFoundException e) {
return new ResponseEntity<Resource>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,4 +89,9 @@ public <T> ResponseEntity<T> handleFeatureNotSupportedException(FeatureNotSuppor
public <T> ResponseEntity<T> handleNotInvokableException(NotInvokableException exception) {
return new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED);
}

@ExceptionHandler(FileDoesNotExistException.class)
public <T> ResponseEntity<T> handleFileDoesNotExistException(FileDoesNotExistException exception) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
<module>basyx.common</module>
<module>basyx.submodelservice</module>
<module>basyx.submodelrepository</module>
<!--<module>basyx.submodelregistry</module>-->
<module>basyx.submodelregistry</module>
<module>basyx.aasservice</module>
<module>basyx.aasrepository</module>
<!--<module>basyx.aasregistry</module>-->
<module>basyx.aasregistry</module>
<module>basyx.aasenvironment</module>
<module>basyx.conceptdescriptionrepository</module>
<module>basyx.aasxfileserver</module>
Expand Down

0 comments on commit 3b4659c

Please sign in to comment.