From 158828d85467ad2d7b3c28eb3407b66e3033632e Mon Sep 17 00:00:00 2001 From: dizda13 Date: Sun, 16 Apr 2017 18:12:25 +0200 Subject: [PATCH] ServiceDiscoveryClient fix in upload/Download --- .../ba/steleks/service/ServiceConstants.java | 24 ------------------- config | 2 +- .../java/ba/steleks/EventsApplication.java | 4 ---- .../controller/EventGalleryController.java | 14 ++++------- .../controller/TeamGalleryController.java | 14 ++++------- .../controller/ProfilePictureController.java | 16 +++++++++---- 6 files changed, 22 insertions(+), 52 deletions(-) delete mode 100644 common/src/main/java/ba/steleks/service/ServiceConstants.java diff --git a/common/src/main/java/ba/steleks/service/ServiceConstants.java b/common/src/main/java/ba/steleks/service/ServiceConstants.java deleted file mode 100644 index 12164bd..0000000 --- a/common/src/main/java/ba/steleks/service/ServiceConstants.java +++ /dev/null @@ -1,24 +0,0 @@ -package ba.steleks.service; - -/** - * Created by ensar on 16/04/17. - */ - -import org.springframework.beans.factory.annotation.Value; - -import java.util.logging.Logger; - -public class ServiceConstants { - private static final Logger logger = - Logger.getLogger(ServiceConstants.class.getName()); - - static { - } - - @Value("${users.name}") - public static String USERS_SERVICE_NAME; - @Value("${events.name}") - public static String EVENTS_SERVICE_NAME; - @Value("${teams.name}") - public static String TEAMS_SERVICE_NAME; -} diff --git a/config b/config index 75a784b..0d02814 160000 --- a/config +++ b/config @@ -1 +1 @@ -Subproject commit 75a784ba6ed8f6e70ee91808d3752443e3a04d1d +Subproject commit 0d028147e90627b077c5435b557d6b3d7d19623c diff --git a/events/src/main/java/ba/steleks/EventsApplication.java b/events/src/main/java/ba/steleks/EventsApplication.java index 7ca5dec..7d3202a 100644 --- a/events/src/main/java/ba/steleks/EventsApplication.java +++ b/events/src/main/java/ba/steleks/EventsApplication.java @@ -1,20 +1,16 @@ package ba.steleks; -import ba.steleks.service.ServiceConstants; import ba.steleks.storage.StorageProperties; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; -import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.List; diff --git a/events/src/main/java/ba/steleks/controller/EventGalleryController.java b/events/src/main/java/ba/steleks/controller/EventGalleryController.java index dd4e2e0..0c80ae2 100644 --- a/events/src/main/java/ba/steleks/controller/EventGalleryController.java +++ b/events/src/main/java/ba/steleks/controller/EventGalleryController.java @@ -4,6 +4,8 @@ import ba.steleks.error.exception.ExternalServiceException; import ba.steleks.model.Media; import ba.steleks.repository.EventsJpaRepository; import ba.steleks.repository.MediaJpaRepository; +import ba.steleks.service.Service; +import ba.steleks.service.discovery.ServiceDiscoveryClient; import ba.steleks.storage.StorageFileNotFoundException; import ba.steleks.storage.StorageService; import org.springframework.beans.factory.annotation.Autowired; @@ -27,10 +29,10 @@ public class EventGalleryController { private final StorageService storageService; private final MediaJpaRepository repository; - private final DiscoveryClient discoveryClient; + private final ServiceDiscoveryClient discoveryClient; @Autowired - public EventGalleryController(StorageService storageService, MediaJpaRepository repository, DiscoveryClient discoveryClient) { + public EventGalleryController(StorageService storageService, MediaJpaRepository repository, ServiceDiscoveryClient discoveryClient) { this.storageService = storageService; this.repository = repository; this.discoveryClient=discoveryClient; @@ -51,14 +53,8 @@ public class EventGalleryController { public String handleFileUpload(@PathVariable Long mediaId, @RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) throws ExternalServiceException { - List usersInstances = discoveryClient.getInstances("events"); - if(usersInstances == null || usersInstances.size() == 0) { - System.err.print("Users service not found!"); - throw new ExternalServiceException(); - } - ServiceInstance usersService = usersInstances.get(0); - String mediaServiceBase = usersService.getUri().toString(); + String mediaServiceBase = discoveryClient.getServiceUrl(Service.EVENTS); String[] names = file.getOriginalFilename().split("\\."); String dest = String.valueOf("mediaEvent/" + mediaId + "_" + new Date().getTime()) + "." + names[names.length - 1]; diff --git a/teams/src/main/java/ba/steleks/controller/TeamGalleryController.java b/teams/src/main/java/ba/steleks/controller/TeamGalleryController.java index 3226051..1c21f33 100644 --- a/teams/src/main/java/ba/steleks/controller/TeamGalleryController.java +++ b/teams/src/main/java/ba/steleks/controller/TeamGalleryController.java @@ -3,6 +3,8 @@ package ba.steleks.controller; import ba.steleks.error.exception.ExternalServiceException; import ba.steleks.model.TeamMedia; import ba.steleks.repository.TeamsMediaJpaRepository; +import ba.steleks.service.Service; +import ba.steleks.service.discovery.ServiceDiscoveryClient; import ba.steleks.storage.StorageFileNotFoundException; import ba.steleks.storage.StorageService; import org.springframework.beans.factory.annotation.Autowired; @@ -26,10 +28,10 @@ public class TeamGalleryController { private final StorageService storageService; private final TeamsMediaJpaRepository repository; - private final DiscoveryClient discoveryClient; + private final ServiceDiscoveryClient discoveryClient; @Autowired - public TeamGalleryController(StorageService storageService, TeamsMediaJpaRepository repository, DiscoveryClient discoveryClient) { + public TeamGalleryController(StorageService storageService, TeamsMediaJpaRepository repository, ServiceDiscoveryClient discoveryClient) { this.storageService = storageService; this.repository = repository; this.discoveryClient=discoveryClient; @@ -50,14 +52,8 @@ public class TeamGalleryController { public String handleFileUpload(@PathVariable Long mediaId, @RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) throws ExternalServiceException { - List usersInstances = discoveryClient.getInstances("teams"); - if(usersInstances == null || usersInstances.size() == 0) { - System.err.print("Users service not found!"); - throw new ExternalServiceException(); - } - ServiceInstance usersService = usersInstances.get(0); - String mediaServiceBase = usersService.getUri().toString(); + String mediaServiceBase = discoveryClient.getServiceUrl(Service.TEAMS); String[] names = file.getOriginalFilename().split("\\."); String dest = String.valueOf("teamPictures/" + mediaId + "_" + new Date().getTime()) + "." + names[names.length - 1]; diff --git a/users/src/main/java/ba/steleks/controller/ProfilePictureController.java b/users/src/main/java/ba/steleks/controller/ProfilePictureController.java index 18eac2a..179fe2f 100644 --- a/users/src/main/java/ba/steleks/controller/ProfilePictureController.java +++ b/users/src/main/java/ba/steleks/controller/ProfilePictureController.java @@ -1,11 +1,13 @@ package ba.steleks.controller; +import ba.steleks.error.exception.ExternalServiceException; import ba.steleks.model.User; import ba.steleks.repository.UsersJpaRepository; +import ba.steleks.service.Service; +import ba.steleks.service.discovery.ServiceDiscoveryClient; 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.*; @@ -22,18 +24,20 @@ public class ProfilePictureController { private final StorageService storageService; private final UsersJpaRepository repository; + private final ServiceDiscoveryClient discoveryClient; @Autowired - public ProfilePictureController(StorageService storageService, UsersJpaRepository repository) { + public ProfilePictureController(StorageService storageService, UsersJpaRepository repository, ServiceDiscoveryClient discoveryClient) { this.storageService = storageService; this.repository = repository; + this.discoveryClient = discoveryClient; } @GetMapping("/profilePictures/{filename:.+}") @ResponseBody public ResponseEntity serveFile(@PathVariable String filename) { - Resource file = storageService.loadAsResource(filename); + Resource file = storageService.loadAsResource("profilePictures/"+ filename); return ResponseEntity .ok() .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+file.getFilename()+"\"") @@ -42,7 +46,9 @@ public class ProfilePictureController { @PostMapping("/users/{userId}/profilePicture") public String handleFileUpload(@PathVariable Long userId, @RequestParam("file") MultipartFile file, - RedirectAttributes redirectAttributes) { + RedirectAttributes redirectAttributes) throws ExternalServiceException { + + String usersServiceBase = discoveryClient.getServiceUrl(Service.USERS); String[] names = file.getOriginalFilename().split("\\."); String dest = String.valueOf("profilePictures/" + userId + "_" + new Date().getTime()) + "." + names[names.length - 1]; @@ -51,7 +57,7 @@ public class ProfilePictureController { "You successfully uploaded " + file.getOriginalFilename() + "!"); User user = repository.findOne(userId); - user.setProfilePictureUrl("http://localhost:8090" + dest); + user.setProfilePictureUrl(usersServiceBase + "/" + dest); repository.save(user); return "redirect:/";