From 119e49338ad5a957259d795d562798f2f8e59fcc Mon Sep 17 00:00:00 2001 From: esensar Date: Sun, 16 Apr 2017 18:05:22 +0200 Subject: [PATCH] Move storage exception handling to common module --- .../storage/FileSystemStorageService.java | 3 ++- .../storage/error/StorageErrorHandler.java | 21 +++++++++++++++++++ .../exception}/StorageException.java | 2 +- .../StorageFileNotFoundException.java | 2 +- .../controller/EventGalleryController.java | 3 +-- .../controller/ProfilePictureController.java | 7 ------- 6 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 common/src/main/java/ba/steleks/storage/error/StorageErrorHandler.java rename common/src/main/java/ba/steleks/storage/{ => error/exception}/StorageException.java (83%) rename common/src/main/java/ba/steleks/storage/{ => error/exception}/StorageFileNotFoundException.java (85%) diff --git a/common/src/main/java/ba/steleks/storage/FileSystemStorageService.java b/common/src/main/java/ba/steleks/storage/FileSystemStorageService.java index 9e91224..f7dea03 100644 --- a/common/src/main/java/ba/steleks/storage/FileSystemStorageService.java +++ b/common/src/main/java/ba/steleks/storage/FileSystemStorageService.java @@ -1,5 +1,7 @@ package ba.steleks.storage; +import ba.steleks.storage.error.exception.StorageException; +import ba.steleks.storage.error.exception.StorageFileNotFoundException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; @@ -12,7 +14,6 @@ import java.net.MalformedURLException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.nio.file.attribute.FileOwnerAttributeView; import java.util.stream.Stream; @Service diff --git a/common/src/main/java/ba/steleks/storage/error/StorageErrorHandler.java b/common/src/main/java/ba/steleks/storage/error/StorageErrorHandler.java new file mode 100644 index 0000000..80a904f --- /dev/null +++ b/common/src/main/java/ba/steleks/storage/error/StorageErrorHandler.java @@ -0,0 +1,21 @@ +package ba.steleks.storage.error;/** + * Created by ensar on 16/04/17. + */ + +import ba.steleks.storage.error.exception.StorageFileNotFoundException; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +import java.util.logging.Logger; + +@ControllerAdvice +public class StorageErrorHandler { + private static final Logger logger = + Logger.getLogger(StorageErrorHandler.class.getName()); + + @ExceptionHandler(StorageFileNotFoundException.class) + public ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) { + return ResponseEntity.notFound().build(); + } +} diff --git a/common/src/main/java/ba/steleks/storage/StorageException.java b/common/src/main/java/ba/steleks/storage/error/exception/StorageException.java similarity index 83% rename from common/src/main/java/ba/steleks/storage/StorageException.java rename to common/src/main/java/ba/steleks/storage/error/exception/StorageException.java index 013fdb5..190f9a6 100644 --- a/common/src/main/java/ba/steleks/storage/StorageException.java +++ b/common/src/main/java/ba/steleks/storage/error/exception/StorageException.java @@ -1,4 +1,4 @@ -package ba.steleks.storage; +package ba.steleks.storage.error.exception; public class StorageException extends RuntimeException { diff --git a/common/src/main/java/ba/steleks/storage/StorageFileNotFoundException.java b/common/src/main/java/ba/steleks/storage/error/exception/StorageFileNotFoundException.java similarity index 85% rename from common/src/main/java/ba/steleks/storage/StorageFileNotFoundException.java rename to common/src/main/java/ba/steleks/storage/error/exception/StorageFileNotFoundException.java index 2222d76..b1b8e08 100644 --- a/common/src/main/java/ba/steleks/storage/StorageFileNotFoundException.java +++ b/common/src/main/java/ba/steleks/storage/error/exception/StorageFileNotFoundException.java @@ -1,4 +1,4 @@ -package ba.steleks.storage; +package ba.steleks.storage.error.exception; public class StorageFileNotFoundException extends StorageException { diff --git a/events/src/main/java/ba/steleks/controller/EventGalleryController.java b/events/src/main/java/ba/steleks/controller/EventGalleryController.java index 06e8945..13c73b6 100644 --- a/events/src/main/java/ba/steleks/controller/EventGalleryController.java +++ b/events/src/main/java/ba/steleks/controller/EventGalleryController.java @@ -2,9 +2,8 @@ package ba.steleks.controller; import ba.steleks.error.exception.ExternalServiceException; import ba.steleks.model.Media; -import ba.steleks.repository.EventsJpaRepository; import ba.steleks.repository.MediaJpaRepository; -import ba.steleks.storage.StorageFileNotFoundException; +import ba.steleks.storage.error.exception.StorageFileNotFoundException; import ba.steleks.storage.StorageService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; diff --git a/users/src/main/java/ba/steleks/controller/ProfilePictureController.java b/users/src/main/java/ba/steleks/controller/ProfilePictureController.java index fadc321..9a33a1e 100644 --- a/users/src/main/java/ba/steleks/controller/ProfilePictureController.java +++ b/users/src/main/java/ba/steleks/controller/ProfilePictureController.java @@ -2,10 +2,8 @@ package ba.steleks.controller; import ba.steleks.model.User; import ba.steleks.repository.UsersJpaRepository; -import ba.steleks.storage.StorageFileNotFoundException; import ba.steleks.storage.StorageService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.rest.webmvc.RepositoryRestController; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -56,9 +54,4 @@ public class ProfilePictureController { return "redirect:/"; } - - @ExceptionHandler(StorageFileNotFoundException.class) - public ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) { - return ResponseEntity.notFound().build(); - } }