diff --git a/src/main/java/toy/bookchat/bookchat/BookChatApplication.java b/src/main/java/toy/bookchat/bookchat/BookChatApplication.java index cf31e382..c704a1d6 100644 --- a/src/main/java/toy/bookchat/bookchat/BookChatApplication.java +++ b/src/main/java/toy/bookchat/bookchat/BookChatApplication.java @@ -7,14 +7,14 @@ import toy.bookchat.bookchat.config.token.OAuth2Properties; import toy.bookchat.bookchat.config.web.BookSearchProperties; import toy.bookchat.bookchat.config.websocket.ExternalBrokerProperties; -import toy.bookchat.bookchat.infrastructure.aws.StorageProperties; +import toy.bookchat.bookchat.infrastructure.s3.StorageProperties; @SpringBootApplication @EnableConfigurationProperties({JwtTokenProperties.class, OAuth2Properties.class, StorageProperties.class, ExternalBrokerProperties.class, BookSearchProperties.class}) public class BookChatApplication { - - public static void main(String[] args) { - SpringApplication.run(BookChatApplication.class, args); - } + + public static void main(String[] args) { + SpringApplication.run(BookChatApplication.class, args); + } } diff --git a/src/main/java/toy/bookchat/bookchat/domain/chat/service/ChatService.java b/src/main/java/toy/bookchat/bookchat/domain/chat/service/ChatService.java index 87320005..2d1090ff 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/chat/service/ChatService.java +++ b/src/main/java/toy/bookchat/bookchat/domain/chat/service/ChatService.java @@ -1,6 +1,6 @@ package toy.bookchat.bookchat.domain.chat.service; -import static toy.bookchat.bookchat.infrastructure.push.PushType.CHAT; +import static toy.bookchat.bookchat.infrastructure.fcm.PushType.CHAT; import java.util.List; import org.springframework.data.domain.Pageable; @@ -16,11 +16,11 @@ import toy.bookchat.bookchat.domain.participant.service.ParticipantValidator; import toy.bookchat.bookchat.domain.user.User; import toy.bookchat.bookchat.domain.user.service.UserReader; -import toy.bookchat.bookchat.infrastructure.broker.MessagePublisher; -import toy.bookchat.bookchat.infrastructure.broker.message.CommonMessage; -import toy.bookchat.bookchat.infrastructure.push.ChatMessageBody; -import toy.bookchat.bookchat.infrastructure.push.PushMessageBody; -import toy.bookchat.bookchat.infrastructure.push.service.PushService; +import toy.bookchat.bookchat.infrastructure.rabbitmq.MessagePublisher; +import toy.bookchat.bookchat.infrastructure.rabbitmq.message.CommonMessage; +import toy.bookchat.bookchat.infrastructure.fcm.ChatMessageBody; +import toy.bookchat.bookchat.infrastructure.fcm.PushMessageBody; +import toy.bookchat.bookchat.infrastructure.fcm.service.PushService; @Service public class ChatService { diff --git a/src/main/java/toy/bookchat/bookchat/domain/chatroom/service/ChatRoomService.java b/src/main/java/toy/bookchat/bookchat/domain/chatroom/service/ChatRoomService.java index 17ab9eb5..92d02be4 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/chatroom/service/ChatRoomService.java +++ b/src/main/java/toy/bookchat/bookchat/domain/chatroom/service/ChatRoomService.java @@ -32,11 +32,11 @@ import toy.bookchat.bookchat.domain.participant.service.ParticipantCleaner; import toy.bookchat.bookchat.domain.participant.service.ParticipantReader; import toy.bookchat.bookchat.domain.participant.service.ParticipantValidator; -import toy.bookchat.bookchat.domain.storage.StorageService; +import toy.bookchat.bookchat.infrastructure.s3.StorageService; import toy.bookchat.bookchat.domain.user.User; import toy.bookchat.bookchat.domain.user.service.UserReader; -import toy.bookchat.bookchat.infrastructure.broker.MessagePublisher; -import toy.bookchat.bookchat.infrastructure.broker.message.NotificationMessage; +import toy.bookchat.bookchat.infrastructure.rabbitmq.MessagePublisher; +import toy.bookchat.bookchat.infrastructure.rabbitmq.message.NotificationMessage; @Service public class ChatRoomService { diff --git a/src/main/java/toy/bookchat/bookchat/domain/participant/service/ParticipantService.java b/src/main/java/toy/bookchat/bookchat/domain/participant/service/ParticipantService.java index 93df7719..24ee1255 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/participant/service/ParticipantService.java +++ b/src/main/java/toy/bookchat/bookchat/domain/participant/service/ParticipantService.java @@ -15,8 +15,8 @@ import toy.bookchat.bookchat.domain.participant.ParticipantAdmin; import toy.bookchat.bookchat.domain.participant.ParticipantStatus; import toy.bookchat.bookchat.domain.participant.ParticipantWithChatRoom; -import toy.bookchat.bookchat.infrastructure.broker.MessagePublisher; -import toy.bookchat.bookchat.infrastructure.broker.message.NotificationMessage; +import toy.bookchat.bookchat.infrastructure.rabbitmq.MessagePublisher; +import toy.bookchat.bookchat.infrastructure.rabbitmq.message.NotificationMessage; @Service public class ParticipantService { diff --git a/src/main/java/toy/bookchat/bookchat/domain/user/service/UserService.java b/src/main/java/toy/bookchat/bookchat/domain/user/service/UserService.java index eae376a0..70a60aa6 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/user/service/UserService.java +++ b/src/main/java/toy/bookchat/bookchat/domain/user/service/UserService.java @@ -1,6 +1,6 @@ package toy.bookchat.bookchat.domain.user.service; -import static toy.bookchat.bookchat.infrastructure.push.PushType.LOGIN; +import static toy.bookchat.bookchat.infrastructure.fcm.PushType.LOGIN; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @@ -14,7 +14,7 @@ import toy.bookchat.bookchat.db_module.device.repository.DeviceRepository; import toy.bookchat.bookchat.db_module.user.UserEntity; import toy.bookchat.bookchat.db_module.user.repository.UserRepository; -import toy.bookchat.bookchat.domain.storage.StorageService; +import toy.bookchat.bookchat.infrastructure.s3.StorageService; import toy.bookchat.bookchat.domain.user.UserProfile; import toy.bookchat.bookchat.domain.user.api.v1.request.ChangeUserNicknameRequest; import toy.bookchat.bookchat.domain.user.api.v1.request.UserSignInRequest; @@ -22,8 +22,8 @@ import toy.bookchat.bookchat.domain.user.api.v1.response.MemberProfileResponse; import toy.bookchat.bookchat.exception.badrequest.user.UserAlreadySignUpException; import toy.bookchat.bookchat.exception.conflict.device.DeviceAlreadyRegisteredException; -import toy.bookchat.bookchat.infrastructure.push.PushMessageBody; -import toy.bookchat.bookchat.infrastructure.push.service.PushService; +import toy.bookchat.bookchat.infrastructure.fcm.PushMessageBody; +import toy.bookchat.bookchat.infrastructure.fcm.service.PushService; @Service public class UserService { diff --git a/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/ChatMessageBody.java b/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/ChatMessageBody.java index c06101aa..0d125cc7 100644 --- a/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/ChatMessageBody.java +++ b/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/ChatMessageBody.java @@ -1,4 +1,4 @@ -package toy.bookchat.bookchat.infrastructure.push; +package toy.bookchat.bookchat.infrastructure.fcm; import lombok.Builder; import lombok.Getter; @@ -6,12 +6,12 @@ @Getter public class ChatMessageBody { - private Long chatId; - private Long chatRoomId; + private Long chatId; + private Long chatRoomId; - @Builder - private ChatMessageBody(Long chatId, Long chatRoomId) { - this.chatId = chatId; - this.chatRoomId = chatRoomId; - } + @Builder + private ChatMessageBody(Long chatId, Long chatRoomId) { + this.chatId = chatId; + this.chatRoomId = chatRoomId; + } } diff --git a/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/FCMConfig.java b/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/FCMConfig.java index 15607f1c..b799ce07 100644 --- a/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/FCMConfig.java +++ b/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/FCMConfig.java @@ -1,4 +1,4 @@ -package toy.bookchat.bookchat.infrastructure.push; +package toy.bookchat.bookchat.infrastructure.fcm; import com.google.auth.oauth2.GoogleCredentials; import com.google.firebase.FirebaseApp; @@ -13,19 +13,19 @@ @Configuration public class FCMConfig { - @Bean - public FirebaseMessaging firebaseMessaging() throws IOException { - ClassPathResource resource = new ClassPathResource("firebase/bookchat-firebase-private.json"); - InputStream refreshToken = resource.getInputStream(); + @Bean + public FirebaseMessaging firebaseMessaging() throws IOException { + ClassPathResource resource = new ClassPathResource("firebase/bookchat-firebase-private.json"); + InputStream refreshToken = resource.getInputStream(); - FirebaseOptions options = FirebaseOptions.builder() - .setCredentials(GoogleCredentials.fromStream(refreshToken)) - .build(); + FirebaseOptions options = FirebaseOptions.builder() + .setCredentials(GoogleCredentials.fromStream(refreshToken)) + .build(); - if (FirebaseApp.getApps().isEmpty()) { - return FirebaseMessaging.getInstance(FirebaseApp.initializeApp(options)); - } else { - return FirebaseMessaging.getInstance(FirebaseApp.getInstance()); - } + if (FirebaseApp.getApps().isEmpty()) { + return FirebaseMessaging.getInstance(FirebaseApp.initializeApp(options)); + } else { + return FirebaseMessaging.getInstance(FirebaseApp.getInstance()); } + } } diff --git a/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/PushMessageBody.java b/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/PushMessageBody.java index 8c6443ae..e211a63d 100644 --- a/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/PushMessageBody.java +++ b/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/PushMessageBody.java @@ -1,19 +1,19 @@ -package toy.bookchat.bookchat.infrastructure.push; +package toy.bookchat.bookchat.infrastructure.fcm; import lombok.Getter; @Getter public class PushMessageBody { - private PushType pushType; - private Object body; + private PushType pushType; + private Object body; - private PushMessageBody(PushType pushType, Object body) { - this.pushType = pushType; - this.body = body; - } + private PushMessageBody(PushType pushType, Object body) { + this.pushType = pushType; + this.body = body; + } - public static PushMessageBody of(PushType pushType, Object body) { - return new PushMessageBody(pushType, body); - } + public static PushMessageBody of(PushType pushType, Object body) { + return new PushMessageBody(pushType, body); + } } diff --git a/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/PushType.java b/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/PushType.java index da8cc472..0cbfd51f 100644 --- a/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/PushType.java +++ b/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/PushType.java @@ -1,7 +1,7 @@ -package toy.bookchat.bookchat.infrastructure.push; +package toy.bookchat.bookchat.infrastructure.fcm; public enum PushType { - LOGIN, - CHAT; + LOGIN, + CHAT; } diff --git a/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/service/FcmService.java b/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/service/FcmService.java index f583b506..a8f8795c 100644 --- a/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/service/FcmService.java +++ b/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/service/FcmService.java @@ -1,4 +1,4 @@ -package toy.bookchat.bookchat.infrastructure.push.service; +package toy.bookchat.bookchat.infrastructure.fcm.service; import static com.google.firebase.ErrorCode.NOT_FOUND; @@ -16,46 +16,46 @@ import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import toy.bookchat.bookchat.exception.serviceunavailable.push.PushServiceCallException; -import toy.bookchat.bookchat.infrastructure.push.PushMessageBody; +import toy.bookchat.bookchat.infrastructure.fcm.PushMessageBody; @Service public class FcmService implements PushService { - private final String BOOK_CHAT = "Book Chat"; - private final FirebaseMessaging firebaseMessaging; - private final ObjectMapper objectMapper; - - public FcmService(FirebaseMessaging firebaseMessaging, ObjectMapper objectMapper) { - this.firebaseMessaging = firebaseMessaging; - this.objectMapper = objectMapper; - } - - @Async - @Retryable(value = PushServiceCallException.class, maxAttempts = 5, backoff = @Backoff(delay = 2000L, multiplier = 2.0)) - @Override - public void send(String fcmToken, PushMessageBody messageBody) { - try { - Map data = new HashMap<>(); - data.put("title", BOOK_CHAT); - data.put("body", objectMapper.writeValueAsString(messageBody)); - - Message message = Message.builder() - .setToken(fcmToken) - .setAndroidConfig(AndroidConfig.builder() - .setDirectBootOk(true) - .setPriority(Priority.HIGH) - .build()) - .putAllData(data) - .build(); - - firebaseMessaging.send(message); - } catch (FirebaseMessagingException e) { - if (e.getErrorCode() == NOT_FOUND) { - return; - } - throw new PushServiceCallException(); - } catch (JsonProcessingException e) { - throw new IllegalArgumentException(e); - } + private final String BOOK_CHAT = "Book Chat"; + private final FirebaseMessaging firebaseMessaging; + private final ObjectMapper objectMapper; + + public FcmService(FirebaseMessaging firebaseMessaging, ObjectMapper objectMapper) { + this.firebaseMessaging = firebaseMessaging; + this.objectMapper = objectMapper; + } + + @Async + @Retryable(value = PushServiceCallException.class, maxAttempts = 5, backoff = @Backoff(delay = 2000L, multiplier = 2.0)) + @Override + public void send(String fcmToken, PushMessageBody messageBody) { + try { + Map data = new HashMap<>(); + data.put("title", BOOK_CHAT); + data.put("body", objectMapper.writeValueAsString(messageBody)); + + Message message = Message.builder() + .setToken(fcmToken) + .setAndroidConfig(AndroidConfig.builder() + .setDirectBootOk(true) + .setPriority(Priority.HIGH) + .build()) + .putAllData(data) + .build(); + + firebaseMessaging.send(message); + } catch (FirebaseMessagingException e) { + if (e.getErrorCode() == NOT_FOUND) { + return; + } + throw new PushServiceCallException(); + } catch (JsonProcessingException e) { + throw new IllegalArgumentException(e); } + } } diff --git a/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/service/PushService.java b/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/service/PushService.java index 3affd893..08fa8a8d 100644 --- a/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/service/PushService.java +++ b/src/main/java/toy/bookchat/bookchat/infrastructure/fcm/service/PushService.java @@ -1,8 +1,8 @@ -package toy.bookchat.bookchat.infrastructure.push.service; +package toy.bookchat.bookchat.infrastructure.fcm.service; -import toy.bookchat.bookchat.infrastructure.push.PushMessageBody; +import toy.bookchat.bookchat.infrastructure.fcm.PushMessageBody; public interface PushService { - void send(String fcmToken, PushMessageBody pushMessageBody); + void send(String fcmToken, PushMessageBody pushMessageBody); } diff --git a/src/main/java/toy/bookchat/bookchat/infrastructure/rabbitmq/MessagePublisher.java b/src/main/java/toy/bookchat/bookchat/infrastructure/rabbitmq/MessagePublisher.java index c6346a80..234cc2e5 100644 --- a/src/main/java/toy/bookchat/bookchat/infrastructure/rabbitmq/MessagePublisher.java +++ b/src/main/java/toy/bookchat/bookchat/infrastructure/rabbitmq/MessagePublisher.java @@ -1,26 +1,26 @@ -package toy.bookchat.bookchat.infrastructure.broker; +package toy.bookchat.bookchat.infrastructure.rabbitmq; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.stereotype.Component; -import toy.bookchat.bookchat.infrastructure.broker.message.CommonMessage; -import toy.bookchat.bookchat.infrastructure.broker.message.NotificationMessage; +import toy.bookchat.bookchat.infrastructure.rabbitmq.message.CommonMessage; +import toy.bookchat.bookchat.infrastructure.rabbitmq.message.NotificationMessage; @Component public class MessagePublisher { - private final String DESTINATION_PREFIX = "/topic/"; + private final String DESTINATION_PREFIX = "/topic/"; - private final SimpMessagingTemplate messagingTemplate; + private final SimpMessagingTemplate messagingTemplate; - public MessagePublisher(SimpMessagingTemplate messagingTemplate) { - this.messagingTemplate = messagingTemplate; - } + public MessagePublisher(SimpMessagingTemplate messagingTemplate) { + this.messagingTemplate = messagingTemplate; + } - public void sendCommonMessage(String roomSid, CommonMessage commonMessage) { - messagingTemplate.convertAndSend(DESTINATION_PREFIX + roomSid, commonMessage); - } + public void sendCommonMessage(String roomSid, CommonMessage commonMessage) { + messagingTemplate.convertAndSend(DESTINATION_PREFIX + roomSid, commonMessage); + } - public void sendNotificationMessage(String roomSid, NotificationMessage notificationMessage) { - messagingTemplate.convertAndSend(DESTINATION_PREFIX + roomSid, notificationMessage); - } + public void sendNotificationMessage(String roomSid, NotificationMessage notificationMessage) { + messagingTemplate.convertAndSend(DESTINATION_PREFIX + roomSid, notificationMessage); + } } diff --git a/src/main/java/toy/bookchat/bookchat/infrastructure/rabbitmq/message/CommonMessage.java b/src/main/java/toy/bookchat/bookchat/infrastructure/rabbitmq/message/CommonMessage.java index 933503b2..f93db33e 100644 --- a/src/main/java/toy/bookchat/bookchat/infrastructure/rabbitmq/message/CommonMessage.java +++ b/src/main/java/toy/bookchat/bookchat/infrastructure/rabbitmq/message/CommonMessage.java @@ -1,4 +1,4 @@ -package toy.bookchat.bookchat.infrastructure.broker.message; +package toy.bookchat.bookchat.infrastructure.rabbitmq.message; import lombok.AccessLevel; import lombok.Builder; @@ -12,31 +12,32 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) public class CommonMessage { - private Long chatRoomId; - private Long chatId; - private Long senderId; - private Integer receiptId; - private String message; - private String dispatchTime; + private Long chatRoomId; + private Long chatId; + private Long senderId; + private Integer receiptId; + private String message; + private String dispatchTime; - @Builder - private CommonMessage(Long chatRoomId, Long chatId, Long senderId, Integer receiptId, String dispatchTime, String message) { - this.chatRoomId = chatRoomId; - this.chatId = chatId; - this.senderId = senderId; - this.receiptId = receiptId; - this.message = message; - this.dispatchTime = dispatchTime; - } + @Builder + private CommonMessage(Long chatRoomId, Long chatId, Long senderId, Integer receiptId, String dispatchTime, + String message) { + this.chatRoomId = chatRoomId; + this.chatId = chatId; + this.senderId = senderId; + this.receiptId = receiptId; + this.message = message; + this.dispatchTime = dispatchTime; + } - public static CommonMessage from(Chat chat, Integer receiptId) { - return CommonMessage.builder() - .chatRoomId(chat.getChatRoomId()) - .chatId(chat.getId()) - .senderId(chat.getSenderId()) - .receiptId(receiptId) - .message(chat.getMessage()) - .dispatchTime(chat.getDispatchTime().toString()) - .build(); - } + public static CommonMessage from(Chat chat, Integer receiptId) { + return CommonMessage.builder() + .chatRoomId(chat.getChatRoomId()) + .chatId(chat.getId()) + .senderId(chat.getSenderId()) + .receiptId(receiptId) + .message(chat.getMessage()) + .dispatchTime(chat.getDispatchTime().toString()) + .build(); + } } diff --git a/src/main/java/toy/bookchat/bookchat/infrastructure/rabbitmq/message/NotificationMessage.java b/src/main/java/toy/bookchat/bookchat/infrastructure/rabbitmq/message/NotificationMessage.java index d3892cd9..52bea14a 100644 --- a/src/main/java/toy/bookchat/bookchat/infrastructure/rabbitmq/message/NotificationMessage.java +++ b/src/main/java/toy/bookchat/bookchat/infrastructure/rabbitmq/message/NotificationMessage.java @@ -1,4 +1,4 @@ -package toy.bookchat.bookchat.infrastructure.broker.message; +package toy.bookchat.bookchat.infrastructure.rabbitmq.message; import javax.validation.constraints.NotBlank; import lombok.AccessLevel; diff --git a/src/main/java/toy/bookchat/bookchat/infrastructure/rabbitmq/message/NotificationMessageType.java b/src/main/java/toy/bookchat/bookchat/infrastructure/rabbitmq/message/NotificationMessageType.java index 17e3d323..fd37a646 100644 --- a/src/main/java/toy/bookchat/bookchat/infrastructure/rabbitmq/message/NotificationMessageType.java +++ b/src/main/java/toy/bookchat/bookchat/infrastructure/rabbitmq/message/NotificationMessageType.java @@ -1,12 +1,12 @@ -package toy.bookchat.bookchat.infrastructure.broker.message; +package toy.bookchat.bookchat.infrastructure.rabbitmq.message; public enum NotificationMessageType { - NOTICE_ENTER, - NOTICE_EXIT, - NOTICE_HOST_EXIT, - NOTICE_HOST_DELEGATE, - NOTICE_KICK, - NOTICE_SUB_HOST_DISMISS, - NOTICE_SUB_HOST_DELEGATE; + NOTICE_ENTER, + NOTICE_EXIT, + NOTICE_HOST_EXIT, + NOTICE_HOST_DELEGATE, + NOTICE_KICK, + NOTICE_SUB_HOST_DISMISS, + NOTICE_SUB_HOST_DELEGATE; } diff --git a/src/main/java/toy/bookchat/bookchat/infrastructure/s3/AWSConfig.java b/src/main/java/toy/bookchat/bookchat/infrastructure/s3/AWSConfig.java index 0834d76b..f2f46931 100644 --- a/src/main/java/toy/bookchat/bookchat/infrastructure/s3/AWSConfig.java +++ b/src/main/java/toy/bookchat/bookchat/infrastructure/s3/AWSConfig.java @@ -1,4 +1,4 @@ -package toy.bookchat.bookchat.infrastructure.aws; +package toy.bookchat.bookchat.infrastructure.s3; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; @@ -10,21 +10,21 @@ @Configuration public class AWSConfig { - private final StorageProperties storageProperties; + private final StorageProperties storageProperties; - public AWSConfig(StorageProperties storageProperties) { - this.storageProperties = storageProperties; - } + public AWSConfig(StorageProperties storageProperties) { + this.storageProperties = storageProperties; + } - @Bean - public AmazonS3Client amazonS3Client() { - BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials( - storageProperties.getAccessKey(), - storageProperties.getSecretKey()); - return (AmazonS3Client) AmazonS3ClientBuilder - .standard() - .withRegion(storageProperties.getRegion()) - .withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials)) - .build(); - } + @Bean + public AmazonS3Client amazonS3Client() { + BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials( + storageProperties.getAccessKey(), + storageProperties.getSecretKey()); + return (AmazonS3Client) AmazonS3ClientBuilder + .standard() + .withRegion(storageProperties.getRegion()) + .withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials)) + .build(); + } } diff --git a/src/main/java/toy/bookchat/bookchat/infrastructure/s3/ChatRoomStorageService.java b/src/main/java/toy/bookchat/bookchat/infrastructure/s3/ChatRoomStorageService.java index 3a98412b..b89c54d8 100644 --- a/src/main/java/toy/bookchat/bookchat/infrastructure/s3/ChatRoomStorageService.java +++ b/src/main/java/toy/bookchat/bookchat/infrastructure/s3/ChatRoomStorageService.java @@ -1,75 +1,73 @@ -package toy.bookchat.bookchat.domain.storage; +package toy.bookchat.bookchat.infrastructure.s3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.ObjectMetadata; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; -import toy.bookchat.bookchat.domain.storage.image.ImageValidator; import toy.bookchat.bookchat.exception.internalserver.ImageUploadToStorageException; -import toy.bookchat.bookchat.infrastructure.aws.StorageProperties; @Service public class ChatRoomStorageService implements StorageService { - // TODO: 2023/03/08 앞단에서 정확한 이미지 사이즈 제한을 지정하기전까지는 일시적으로 제한을 해제한다. - private static final int WIDTH_LIMIT = Integer.MAX_VALUE / 2; - // private static final int WIDTH_LIMIT = 200; - private static final int HEIGHT_LIMIT = Integer.MAX_VALUE / 2; - // private static final int HEIGHT_LIMIT = 200; + // TODO: 2023/03/08 앞단에서 정확한 이미지 사이즈 제한을 지정하기전까지는 일시적으로 제한을 해제한다. + private static final int WIDTH_LIMIT = Integer.MAX_VALUE / 2; + // private static final int WIDTH_LIMIT = 200; + private static final int HEIGHT_LIMIT = Integer.MAX_VALUE / 2; + // private static final int HEIGHT_LIMIT = 200; - private final AmazonS3Client amazonS3Client; - private final StorageProperties storageProperties; - private final ImageValidator imageValidator; + private final AmazonS3Client amazonS3Client; + private final StorageProperties storageProperties; + private final ImageValidator imageValidator; - public ChatRoomStorageService(AmazonS3Client amazonS3Client, - StorageProperties storageProperties, ImageValidator imageValidator) { - this.amazonS3Client = amazonS3Client; - this.storageProperties = storageProperties; - this.imageValidator = imageValidator; - } + public ChatRoomStorageService(AmazonS3Client amazonS3Client, + StorageProperties storageProperties, ImageValidator imageValidator) { + this.amazonS3Client = amazonS3Client; + this.storageProperties = storageProperties; + this.imageValidator = imageValidator; + } - @Override - public String upload(MultipartFile multipartFile, String uuid, String date) { - try { - String fileName = createFileName(multipartFile, uuid, date); - amazonS3Client.putObject(storageProperties.getBucketName(), fileName, - multipartFile.getInputStream(), abstractObjectMetadataFrom(multipartFile)); + @Override + public String upload(MultipartFile multipartFile, String uuid, String date) { + try { + String fileName = createFileName(multipartFile, uuid, date); + amazonS3Client.putObject(storageProperties.getBucketName(), fileName, + multipartFile.getInputStream(), abstractObjectMetadataFrom(multipartFile)); - return getFileUrl(fileName); - } catch (Exception exception) { - throw new ImageUploadToStorageException(); - } + return getFileUrl(fileName); + } catch (Exception exception) { + throw new ImageUploadToStorageException(); } + } - private ObjectMetadata abstractObjectMetadataFrom(MultipartFile multipartFile) { - ObjectMetadata objectMetadata = new ObjectMetadata(); - objectMetadata.setContentType(multipartFile.getContentType()); - objectMetadata.setContentLength(multipartFile.getSize()); - return objectMetadata; - } + private ObjectMetadata abstractObjectMetadataFrom(MultipartFile multipartFile) { + ObjectMetadata objectMetadata = new ObjectMetadata(); + objectMetadata.setContentType(multipartFile.getContentType()); + objectMetadata.setContentLength(multipartFile.getSize()); + return objectMetadata; + } - private String getFileUrl(String fileName) { - return storageProperties.getImageBucketUrl() + fileName; - } + private String getFileUrl(String fileName) { + return storageProperties.getImageBucketUrl() + fileName; + } - /** - * '날짜 역순' + UUID로 저장 - S3가 prefix를 사용하여 partitioning을 하기 때문에 - */ - private String createFileName(MultipartFile file, String uuidFileName, - String currentTime) { - imageValidator.hasValidImage(file, WIDTH_LIMIT, HEIGHT_LIMIT); - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append(currentTime).reverse(); - stringBuilder.append(uuidFileName); - stringBuilder.append("."); - stringBuilder.append(getFileExtension(file)); - stringBuilder.insert(0, storageProperties.getChatRoomImageFolder()); - return stringBuilder.toString(); - } + /** + * '날짜 역순' + UUID로 저장 - S3가 prefix를 사용하여 partitioning을 하기 때문에 + */ + private String createFileName(MultipartFile file, String uuidFileName, + String currentTime) { + imageValidator.hasValidImage(file, WIDTH_LIMIT, HEIGHT_LIMIT); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(currentTime).reverse(); + stringBuilder.append(uuidFileName); + stringBuilder.append("."); + stringBuilder.append(getFileExtension(file)); + stringBuilder.insert(0, storageProperties.getChatRoomImageFolder()); + return stringBuilder.toString(); + } - private String getFileExtension(MultipartFile image) { - return image.getOriginalFilename() - .substring(image.getOriginalFilename().lastIndexOf(".") + 1); - } + private String getFileExtension(MultipartFile image) { + return image.getOriginalFilename() + .substring(image.getOriginalFilename().lastIndexOf(".") + 1); + } } diff --git a/src/main/java/toy/bookchat/bookchat/infrastructure/s3/ImageReaderAdapter.java b/src/main/java/toy/bookchat/bookchat/infrastructure/s3/ImageReaderAdapter.java index 0e51bf83..e467d0ee 100644 --- a/src/main/java/toy/bookchat/bookchat/infrastructure/s3/ImageReaderAdapter.java +++ b/src/main/java/toy/bookchat/bookchat/infrastructure/s3/ImageReaderAdapter.java @@ -1,12 +1,12 @@ -package toy.bookchat.bookchat.domain.storage.image; +package toy.bookchat.bookchat.infrastructure.s3; import javax.imageio.stream.ImageInputStream; public interface ImageReaderAdapter { - void setInput(ImageInputStream imageInputStream); + void setInput(ImageInputStream imageInputStream); - int getWidth(); + int getWidth(); - int getHeight(); + int getHeight(); } diff --git a/src/main/java/toy/bookchat/bookchat/infrastructure/s3/ImageReaderAdapterImpl.java b/src/main/java/toy/bookchat/bookchat/infrastructure/s3/ImageReaderAdapterImpl.java index 53a05195..52960e51 100644 --- a/src/main/java/toy/bookchat/bookchat/infrastructure/s3/ImageReaderAdapterImpl.java +++ b/src/main/java/toy/bookchat/bookchat/infrastructure/s3/ImageReaderAdapterImpl.java @@ -1,6 +1,6 @@ -package toy.bookchat.bookchat.domain.storage.image; +package toy.bookchat.bookchat.infrastructure.s3; -import static toy.bookchat.bookchat.domain.storage.image.SupportedFileExtension.WEBP; +import static toy.bookchat.bookchat.infrastructure.s3.SupportedFileExtension.WEBP; import com.luciad.imageio.webp.WebPImageReaderSpi; import java.io.IOException; @@ -12,36 +12,36 @@ @Component public class ImageReaderAdapterImpl implements ImageReaderAdapter { - private final ImageReader readerInstance; + private final ImageReader readerInstance; - public ImageReaderAdapterImpl() { - try { - this.readerInstance = new WebPImageReaderSpi().createReaderInstance(WEBP.getValue()); - } catch (IOException exception) { - throw new ImageInputStreamException(); - } + public ImageReaderAdapterImpl() { + try { + this.readerInstance = new WebPImageReaderSpi().createReaderInstance(WEBP.getValue()); + } catch (IOException exception) { + throw new ImageInputStreamException(); } - - @Override - public void setInput(ImageInputStream imageInputStream) { - readerInstance.setInput(imageInputStream); - } - - @Override - public int getWidth() { - try { - return readerInstance.getWidth(0); - } catch (IOException exception) { - throw new ImageInputStreamException(); - } + } + + @Override + public void setInput(ImageInputStream imageInputStream) { + readerInstance.setInput(imageInputStream); + } + + @Override + public int getWidth() { + try { + return readerInstance.getWidth(0); + } catch (IOException exception) { + throw new ImageInputStreamException(); } - - @Override - public int getHeight() { - try { - return readerInstance.getHeight(0); - } catch (IOException exception) { - throw new ImageInputStreamException(); - } + } + + @Override + public int getHeight() { + try { + return readerInstance.getHeight(0); + } catch (IOException exception) { + throw new ImageInputStreamException(); } + } } diff --git a/src/main/java/toy/bookchat/bookchat/infrastructure/s3/ImageValidator.java b/src/main/java/toy/bookchat/bookchat/infrastructure/s3/ImageValidator.java index 4a1a2e54..f40778b5 100644 --- a/src/main/java/toy/bookchat/bookchat/infrastructure/s3/ImageValidator.java +++ b/src/main/java/toy/bookchat/bookchat/infrastructure/s3/ImageValidator.java @@ -1,4 +1,4 @@ -package toy.bookchat.bookchat.domain.storage.image; +package toy.bookchat.bookchat.infrastructure.s3; import javax.imageio.ImageIO; import org.springframework.stereotype.Component; @@ -9,48 +9,48 @@ @Component public class ImageValidator { - private final ImageReaderAdapter imageReaderAdapter; + private final ImageReaderAdapter imageReaderAdapter; - public ImageValidator(ImageReaderAdapter imageReaderAdapter) { - this.imageReaderAdapter = imageReaderAdapter; - } + public ImageValidator(ImageReaderAdapter imageReaderAdapter) { + this.imageReaderAdapter = imageReaderAdapter; + } - private static void hasFileName(MultipartFile multipartFile) { - if (!StringUtils.hasText(multipartFile.getOriginalFilename())) { - throw new IllegalArgumentException("Can't Handle Blank Name File"); - } + private static void hasFileName(MultipartFile multipartFile) { + if (!StringUtils.hasText(multipartFile.getOriginalFilename())) { + throw new IllegalArgumentException("Can't Handle Blank Name File"); } - - private String getFileExtension(MultipartFile multipartFile) { - return multipartFile.getOriginalFilename() - .substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1); + } + + private String getFileExtension(MultipartFile multipartFile) { + return multipartFile.getOriginalFilename() + .substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1); + } + + public void hasValidImage(MultipartFile multipartFile, int widthLimit, int hegithLimit) { + isNotEmptyFile(multipartFile); + hasFileName(multipartFile); + SupportedFileExtension.isSupport(getFileExtension(multipartFile)); + isValidFileSize(multipartFile, widthLimit, hegithLimit); + } + + private void isNotEmptyFile(MultipartFile multipartFile) { + if (multipartFile == null || multipartFile.isEmpty()) { + throw new IllegalArgumentException("Can't Handle Empty File"); } - - public void hasValidImage(MultipartFile multipartFile, int widthLimit, int hegithLimit) { - isNotEmptyFile(multipartFile); - hasFileName(multipartFile); - SupportedFileExtension.isSupport(getFileExtension(multipartFile)); - isValidFileSize(multipartFile, widthLimit, hegithLimit); - } - - private void isNotEmptyFile(MultipartFile multipartFile) { - if (multipartFile == null || multipartFile.isEmpty()) { - throw new IllegalArgumentException("Can't Handle Empty File"); - } + } + + private void isValidFileSize(MultipartFile multipartFile, int widthLimit, int heightLimit) { + try { + imageReaderAdapter.setInput( + ImageIO.createImageInputStream(multipartFile.getInputStream())); + } catch (Exception exception) { + throw new ImageInputStreamException(); } - private void isValidFileSize(MultipartFile multipartFile, int widthLimit, int heightLimit) { - try { - imageReaderAdapter.setInput( - ImageIO.createImageInputStream(multipartFile.getInputStream())); - } catch (Exception exception) { - throw new ImageInputStreamException(); - } - - if (imageReaderAdapter.getWidth() > widthLimit - || imageReaderAdapter.getHeight() > heightLimit) { - throw new IllegalArgumentException("Not Supplied File Size"); - } + if (imageReaderAdapter.getWidth() > widthLimit + || imageReaderAdapter.getHeight() > heightLimit) { + throw new IllegalArgumentException("Not Supplied File Size"); } + } } diff --git a/src/main/java/toy/bookchat/bookchat/infrastructure/s3/StorageProperties.java b/src/main/java/toy/bookchat/bookchat/infrastructure/s3/StorageProperties.java index 3f9634f1..e4170531 100644 --- a/src/main/java/toy/bookchat/bookchat/infrastructure/s3/StorageProperties.java +++ b/src/main/java/toy/bookchat/bookchat/infrastructure/s3/StorageProperties.java @@ -1,4 +1,4 @@ -package toy.bookchat.bookchat.infrastructure.aws; +package toy.bookchat.bookchat.infrastructure.s3; import lombok.Getter; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -9,22 +9,22 @@ @ConfigurationProperties(prefix = "aws.s3") public class StorageProperties { - private final String accessKey; - private final String secretKey; - private final String region; - private final String bucketName; - private final String userProfileImageFolder; - private final String chatRoomImageFolder; - private final String imageBucketUrl; + private final String accessKey; + private final String secretKey; + private final String region; + private final String bucketName; + private final String userProfileImageFolder; + private final String chatRoomImageFolder; + private final String imageBucketUrl; - public StorageProperties(String accessKey, String secretKey, String region, String bucketName, - String userProfileImageFolder, String chatRoomImageFolder, String imageBucketUrl) { - this.accessKey = accessKey; - this.secretKey = secretKey; - this.region = region; - this.bucketName = bucketName; - this.userProfileImageFolder = userProfileImageFolder; - this.chatRoomImageFolder = chatRoomImageFolder; - this.imageBucketUrl = imageBucketUrl; - } + public StorageProperties(String accessKey, String secretKey, String region, String bucketName, + String userProfileImageFolder, String chatRoomImageFolder, String imageBucketUrl) { + this.accessKey = accessKey; + this.secretKey = secretKey; + this.region = region; + this.bucketName = bucketName; + this.userProfileImageFolder = userProfileImageFolder; + this.chatRoomImageFolder = chatRoomImageFolder; + this.imageBucketUrl = imageBucketUrl; + } } diff --git a/src/main/java/toy/bookchat/bookchat/infrastructure/s3/StorageService.java b/src/main/java/toy/bookchat/bookchat/infrastructure/s3/StorageService.java index 7d8d599b..a196aa4e 100644 --- a/src/main/java/toy/bookchat/bookchat/infrastructure/s3/StorageService.java +++ b/src/main/java/toy/bookchat/bookchat/infrastructure/s3/StorageService.java @@ -1,8 +1,8 @@ -package toy.bookchat.bookchat.domain.storage; +package toy.bookchat.bookchat.infrastructure.s3; import org.springframework.web.multipart.MultipartFile; public interface StorageService { - String upload(MultipartFile multipartFile, String uuid, String date); + String upload(MultipartFile multipartFile, String uuid, String date); } diff --git a/src/main/java/toy/bookchat/bookchat/infrastructure/s3/SupportedFileExtension.java b/src/main/java/toy/bookchat/bookchat/infrastructure/s3/SupportedFileExtension.java index 2506ae8b..14eb9271 100644 --- a/src/main/java/toy/bookchat/bookchat/infrastructure/s3/SupportedFileExtension.java +++ b/src/main/java/toy/bookchat/bookchat/infrastructure/s3/SupportedFileExtension.java @@ -1,24 +1,24 @@ -package toy.bookchat.bookchat.domain.storage.image; +package toy.bookchat.bookchat.infrastructure.s3; public enum SupportedFileExtension { - WEBP("webp"); + WEBP("webp"); - private final String fileExtension; + private final String fileExtension; - SupportedFileExtension(String fileExtension) { - this.fileExtension = fileExtension; - } + SupportedFileExtension(String fileExtension) { + this.fileExtension = fileExtension; + } - public static void isSupport(String fileExtension) { - for (SupportedFileExtension supportedFileExtension : SupportedFileExtension.values()) { - if (supportedFileExtension.fileExtension.equals(fileExtension)) { - return; - } - } - throw new IllegalArgumentException("Not Supported File Extension"); + public static void isSupport(String fileExtension) { + for (SupportedFileExtension supportedFileExtension : SupportedFileExtension.values()) { + if (supportedFileExtension.fileExtension.equals(fileExtension)) { + return; + } } + throw new IllegalArgumentException("Not Supported File Extension"); + } - public String getValue() { - return this.fileExtension; - } + public String getValue() { + return this.fileExtension; + } } diff --git a/src/main/java/toy/bookchat/bookchat/infrastructure/s3/UserProfileStorageService.java b/src/main/java/toy/bookchat/bookchat/infrastructure/s3/UserProfileStorageService.java index d78b4913..bb5eb2ef 100644 --- a/src/main/java/toy/bookchat/bookchat/infrastructure/s3/UserProfileStorageService.java +++ b/src/main/java/toy/bookchat/bookchat/infrastructure/s3/UserProfileStorageService.java @@ -1,74 +1,72 @@ -package toy.bookchat.bookchat.domain.storage; +package toy.bookchat.bookchat.infrastructure.s3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.ObjectMetadata; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; -import toy.bookchat.bookchat.domain.storage.image.ImageValidator; import toy.bookchat.bookchat.exception.internalserver.ImageUploadToStorageException; -import toy.bookchat.bookchat.infrastructure.aws.StorageProperties; @Service public class UserProfileStorageService implements StorageService { - // TODO: 2023/03/08 앞단에서 정확한 이미지 사이즈 제한을 지정하기전까지는 일시적으로 제한을 해제한다. - private static final int WIDTH_LIMIT = Integer.MAX_VALUE / 2; - // private static final int WIDTH_LIMIT = 200; - private static final int HEIGHT_LIMIT = Integer.MAX_VALUE / 2; - // private static final int HEIGHT_LIMIT = 200; + // TODO: 2023/03/08 앞단에서 정확한 이미지 사이즈 제한을 지정하기전까지는 일시적으로 제한을 해제한다. + private static final int WIDTH_LIMIT = Integer.MAX_VALUE / 2; + // private static final int WIDTH_LIMIT = 200; + private static final int HEIGHT_LIMIT = Integer.MAX_VALUE / 2; + // private static final int HEIGHT_LIMIT = 200; - private final AmazonS3Client amazonS3Client; - private final StorageProperties storageProperties; - private final ImageValidator imageValidator; + private final AmazonS3Client amazonS3Client; + private final StorageProperties storageProperties; + private final ImageValidator imageValidator; - public UserProfileStorageService(AmazonS3Client amazonS3Client, - StorageProperties storageProperties, ImageValidator imageValidator) { - this.amazonS3Client = amazonS3Client; - this.storageProperties = storageProperties; - this.imageValidator = imageValidator; - } + public UserProfileStorageService(AmazonS3Client amazonS3Client, + StorageProperties storageProperties, ImageValidator imageValidator) { + this.amazonS3Client = amazonS3Client; + this.storageProperties = storageProperties; + this.imageValidator = imageValidator; + } - @Override - public String upload(MultipartFile multipartFile, String uuid, String date) { - try { - String fileName = createFileName(multipartFile, uuid, date); - amazonS3Client.putObject(storageProperties.getBucketName(), fileName, - multipartFile.getInputStream(), abstractObjectMetadataFrom(multipartFile)); + @Override + public String upload(MultipartFile multipartFile, String uuid, String date) { + try { + String fileName = createFileName(multipartFile, uuid, date); + amazonS3Client.putObject(storageProperties.getBucketName(), fileName, + multipartFile.getInputStream(), abstractObjectMetadataFrom(multipartFile)); - return getFileUrl(fileName); - } catch (Exception exception) { - throw new ImageUploadToStorageException(); - } + return getFileUrl(fileName); + } catch (Exception exception) { + throw new ImageUploadToStorageException(); } + } - private ObjectMetadata abstractObjectMetadataFrom(MultipartFile multipartFile) { - ObjectMetadata objectMetadata = new ObjectMetadata(); - objectMetadata.setContentType(multipartFile.getContentType()); - objectMetadata.setContentLength(multipartFile.getSize()); - return objectMetadata; - } + private ObjectMetadata abstractObjectMetadataFrom(MultipartFile multipartFile) { + ObjectMetadata objectMetadata = new ObjectMetadata(); + objectMetadata.setContentType(multipartFile.getContentType()); + objectMetadata.setContentLength(multipartFile.getSize()); + return objectMetadata; + } - private String getFileUrl(String fileName) { - return storageProperties.getImageBucketUrl() + fileName; - } + private String getFileUrl(String fileName) { + return storageProperties.getImageBucketUrl() + fileName; + } - /** - * '날짜 역순' + UUID로 저장 - S3가 prefix를 사용하여 partitioning을 하기 때문에 - */ - private String createFileName(MultipartFile file, String uuidFileName, - String currentTime) { - imageValidator.hasValidImage(file, WIDTH_LIMIT, HEIGHT_LIMIT); - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append(currentTime).reverse(); - stringBuilder.append(uuidFileName); - stringBuilder.append("."); - stringBuilder.append(getFileExtension(file)); - stringBuilder.insert(0, storageProperties.getUserProfileImageFolder()); - return stringBuilder.toString(); - } + /** + * '날짜 역순' + UUID로 저장 - S3가 prefix를 사용하여 partitioning을 하기 때문에 + */ + private String createFileName(MultipartFile file, String uuidFileName, + String currentTime) { + imageValidator.hasValidImage(file, WIDTH_LIMIT, HEIGHT_LIMIT); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(currentTime).reverse(); + stringBuilder.append(uuidFileName); + stringBuilder.append("."); + stringBuilder.append(getFileExtension(file)); + stringBuilder.insert(0, storageProperties.getUserProfileImageFolder()); + return stringBuilder.toString(); + } - private String getFileExtension(MultipartFile image) { - return image.getOriginalFilename() - .substring(image.getOriginalFilename().lastIndexOf(".") + 1); - } + private String getFileExtension(MultipartFile image) { + return image.getOriginalFilename() + .substring(image.getOriginalFilename().lastIndexOf(".") + 1); + } }