From a42e06c33b86a682ed7fe0aab959d182c50c2f28 Mon Sep 17 00:00:00 2001 From: pjhcsols Date: Fri, 15 Nov 2024 22:52:21 +0900 Subject: [PATCH] =?UTF-8?q?feat:[kakao-tech-campus-2nd-step3#28]=20Chat=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20WebSecurity=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ChatMessageController.java | 20 +++++---- .../chat/controller/ChatRoomController.java | 44 +++++++++++-------- .../controller/WebSocketChatController.java | 10 +---- .../global/config/WebConfig.java | 1 + .../global/security/WebSecurityConfig.java | 3 +- 5 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/helpmeCookies/chat/controller/ChatMessageController.java b/src/main/java/com/helpmeCookies/chat/controller/ChatMessageController.java index fcd9116..52b4d93 100644 --- a/src/main/java/com/helpmeCookies/chat/controller/ChatMessageController.java +++ b/src/main/java/com/helpmeCookies/chat/controller/ChatMessageController.java @@ -5,6 +5,8 @@ import com.helpmeCookies.chat.service.ChatMessageService; import com.helpmeCookies.chat.service.ChatRoomService; import com.helpmeCookies.chat.util.ImageStorageUtil; +import com.helpmeCookies.global.ApiResponse.ApiResponse; +import com.helpmeCookies.global.ApiResponse.SuccessCode; import com.helpmeCookies.global.exception.chat.ChatRoomIdNotFoundException; import com.helpmeCookies.user.service.UserService; import lombok.RequiredArgsConstructor; @@ -26,32 +28,34 @@ public class ChatMessageController { private final ImageStorageUtil imageStorageUtil; @GetMapping("/messages") - public List getAllMessages() { - return chatMessageService.getAllMessages(); + public ResponseEntity>> getAllMessages() { + List messages = chatMessageService.getAllMessages(); + return ResponseEntity.ok(ApiResponse.success(SuccessCode.OK, messages)); } @GetMapping("/rooms/{chatRoomId}/messages") - public List getMessagesByChatRoom(@PathVariable Long chatRoomId) { + public ResponseEntity>> getMessagesByChatRoom(@PathVariable Long chatRoomId) { ChatRoom chatRoom = chatRoomService.findById(chatRoomId) .orElseThrow(() -> new ChatRoomIdNotFoundException(chatRoomId)); - return chatMessageService.getMessagesByChatRoom(chatRoom); + List messages = chatMessageService.getMessagesByChatRoom(chatRoom); + return ResponseEntity.ok(ApiResponse.success(SuccessCode.OK, messages)); } @PostMapping("/image/convert") - public ResponseEntity convertImageUrlToBase64(@RequestParam String imagePath) throws IOException { - + public ResponseEntity> convertImageUrlToBase64(@RequestParam String imagePath) throws IOException { byte[] imageBytes = chatMessageService.convertImageUrlToBytes(imagePath); HttpHeaders headers = new HttpHeaders(); headers.set("Content-Type", "image/jpeg"); - return new ResponseEntity<>(imageBytes, headers, HttpStatus.OK); + return ResponseEntity.ok(ApiResponse.success(SuccessCode.OK, imageBytes)); } @DeleteMapping("/messages/{id}") - public void deleteMessage(@PathVariable Long id) { + public ResponseEntity> deleteMessage(@PathVariable Long id) { chatMessageService.deleteMessage(id); + return ResponseEntity.ok(ApiResponse.success(SuccessCode.OK)); } } diff --git a/src/main/java/com/helpmeCookies/chat/controller/ChatRoomController.java b/src/main/java/com/helpmeCookies/chat/controller/ChatRoomController.java index db3c288..9549447 100644 --- a/src/main/java/com/helpmeCookies/chat/controller/ChatRoomController.java +++ b/src/main/java/com/helpmeCookies/chat/controller/ChatRoomController.java @@ -1,6 +1,5 @@ package com.helpmeCookies.chat.controller; - import com.helpmeCookies.chat.dto.ChatMessageDto; import com.helpmeCookies.chat.dto.ChatRoomInfo; import com.helpmeCookies.chat.entity.ChatRoom; @@ -8,10 +7,14 @@ import com.helpmeCookies.chat.service.ChatMessageService; import com.helpmeCookies.chat.service.ChatRoomService; import com.helpmeCookies.chat.util.ImageStorageUtil; +import com.helpmeCookies.global.ApiResponse.ApiResponse; +import com.helpmeCookies.global.ApiResponse.SuccessCode; import com.helpmeCookies.global.exception.user.UserNotFoundException; import com.helpmeCookies.user.entity.User; import com.helpmeCookies.user.service.UserService; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -19,6 +22,7 @@ @RestController @RequiredArgsConstructor +@Slf4j @RequestMapping("/v1/chat/rooms") public class ChatRoomController { private final ChatRoomService chatRoomService; @@ -27,7 +31,7 @@ public class ChatRoomController { private final ChatMessageService chatMessageService; @PostMapping - public ChatRoom createChatRoom( + public ResponseEntity> createChatRoom( @RequestParam String userEmail1, @RequestParam String userEmail2 ) throws UserNotFoundException { @@ -38,45 +42,49 @@ public ChatRoom createChatRoom( ChatRoom chatRoom = chatRoomService.createChatRoom(user1, user2); - // 환영 메시지 생성 String welcomeMessageContent = "Welcome, " + user2.getEmail() + "!"; ChatMessageDto chatMessageDto = new ChatMessageDto(user1.getEmail(), welcomeMessageContent, MessageType.ENTER); chatMessageService.saveMessage(chatRoom.getId(), chatMessageDto); - return chatRoom; + return ResponseEntity.ok(ApiResponse.success(SuccessCode.OK, chatRoom)); } @GetMapping("/{chatRoomId}") - public ChatRoom getChatRoom(@PathVariable Long chatRoomId) { - return chatRoomService.getChatRoomById(chatRoomId); + public ResponseEntity> getChatRoom(@PathVariable Long chatRoomId) { + ChatRoom chatRoom = chatRoomService.getChatRoomById(chatRoomId); + return ResponseEntity.ok(ApiResponse.success(SuccessCode.OK, chatRoom)); } @DeleteMapping("/{chatRoomId}") - public void deleteChatRoom(@PathVariable Long chatRoomId) { + public ResponseEntity> deleteChatRoom(@PathVariable Long chatRoomId) { chatRoomService.deleteChatRoom(chatRoomId); - imageStorageUtil.deleteChatFolder("chatRoom_" + chatRoomId); + + return ResponseEntity.ok(ApiResponse.success(SuccessCode.OK)); } @GetMapping("/user/{userId}") - public List getUserChatRooms(@PathVariable Long userId) { - return chatRoomService.getChatRoomsByUserId(userId).stream() + public ResponseEntity>> getUserChatRooms(@PathVariable Long userId) { + List chatRooms = chatRoomService.getChatRoomsByUserId(userId).stream() .map(chatRoom -> new ChatRoomInfo(chatRoom.getId(), chatRoom.getTitle())) .collect(Collectors.toList()); + + return ResponseEntity.ok(ApiResponse.success(SuccessCode.OK, chatRooms)); } @PatchMapping("/{chatRoomId}/title") - public void setChatRoomTitle(@PathVariable Long chatRoomId, - @RequestParam Long userId, - @RequestParam String title) { - + public ResponseEntity> setChatRoomTitle( + @PathVariable Long chatRoomId, + @RequestParam Long userId, + @RequestParam String title + ) { chatRoomService.setChatRoomTitleIfUserMatches(chatRoomId, userId, title); + return ResponseEntity.ok(ApiResponse.success(SuccessCode.OK)); } @GetMapping - public List getAllChatRooms() { - return chatRoomService.getAllChatRooms(); + public ResponseEntity>> getAllChatRooms() { + List chatRooms = chatRoomService.getAllChatRooms(); + return ResponseEntity.ok(ApiResponse.success(SuccessCode.OK, chatRooms)); } - } - diff --git a/src/main/java/com/helpmeCookies/chat/controller/WebSocketChatController.java b/src/main/java/com/helpmeCookies/chat/controller/WebSocketChatController.java index 591ba34..c2f2d6a 100644 --- a/src/main/java/com/helpmeCookies/chat/controller/WebSocketChatController.java +++ b/src/main/java/com/helpmeCookies/chat/controller/WebSocketChatController.java @@ -42,10 +42,8 @@ public WebSocketChatController(SimpMessageSendingOperations messagingTemplate, @MessageMapping("/chat/{chatRoomId}") public void chat(@DestinationVariable Long chatRoomId, @RequestBody ChatMessageDto messageDto) { - // 메시지를 서비스로 처리 ChatMessage savedMessage = chatMessageService.saveMessage(chatRoomId, messageDto); - // WebSocket을 통해 메시지를 전송 messagingTemplate.convertAndSend("/v1/sub/chat/rooms/" + chatRoomId, savedMessage); } @@ -55,14 +53,11 @@ public void sendFile(@DestinationVariable Long chatRoomId, @RequestParam String fileBase64, @RequestParam String userEmail) { try { - // Base64 데이터를 이미지 파일로 변환 및 저장 ChatMessage message = chatMessageService.saveFileMessage(chatRoomId, userEmail, fileBase64); - // 이미지 바이트로 변환 후 Base64 인코딩 byte[] imageBytes = chatMessageService.convertImageUrlToBytes(message.getContent()); String encodedContent = ChatMessageDto.getImageContent(imageBytes); - // 채팅 메시지 DTO 생성 ChatMessageDto chatMessageDto = new ChatMessageDto( message.getChatRoom().getId(), message.getSender().getEmail(), @@ -71,10 +66,9 @@ public void sendFile(@DestinationVariable Long chatRoomId, MessageType.IMAGE ); - // WebSocket을 통해 채팅 메시지 전송 messagingTemplate.convertAndSend("/api/sub/chat/rooms/" + chatRoomId, chatMessageDto); } catch (UserNotFoundException | IOException e) { - // 에러 처리 및 로그 + log.error("파일 전송 중 오류 발생: " + e.getMessage(), e); } } @@ -82,10 +76,8 @@ public void sendFile(@DestinationVariable Long chatRoomId, @SubscribeMapping("/chat/rooms/{chatRoomId}/list") public List sendInitialMessages(@DestinationVariable Long chatRoomId) { - // chatRoomId로 ChatRoom 객체를 조회 ChatRoom chatRoom = chatRoomService.getChatRoomById(chatRoomId); - // 채팅방에 속한 메시지를 시간순으로 가져옴 return chatMessageService.getMessagesByChatRoom(chatRoom); } diff --git a/src/main/java/com/helpmeCookies/global/config/WebConfig.java b/src/main/java/com/helpmeCookies/global/config/WebConfig.java index a77ff6c..bd95452 100644 --- a/src/main/java/com/helpmeCookies/global/config/WebConfig.java +++ b/src/main/java/com/helpmeCookies/global/config/WebConfig.java @@ -1,4 +1,5 @@ package com.helpmeCookies.global.config; +import java.util.Arrays; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Configuration; diff --git a/src/main/java/com/helpmeCookies/global/security/WebSecurityConfig.java b/src/main/java/com/helpmeCookies/global/security/WebSecurityConfig.java index 55f9062..fce3512 100644 --- a/src/main/java/com/helpmeCookies/global/security/WebSecurityConfig.java +++ b/src/main/java/com/helpmeCookies/global/security/WebSecurityConfig.java @@ -77,7 +77,8 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { "/v1/products/**", "/v1/reviews/**", "/ws/**", - "/v1/artists/**" + "/v1/artists/**", + "v1/chat/**" ).permitAll() .anyRequest().authenticated() ).exceptionHandling((exception) -> exception