Class ChatController

java.lang.Object
vaultWeb.controllers.ChatController

@Controller public class ChatController extends Object
Controller responsible for handling WebSocket-based chat functionality.

Supports both group chat and private messages. Messages are first persisted via ChatService and then dispatched to the corresponding topics or users.

  • Constructor Details

    • ChatController

      public ChatController()
  • Method Details

    • sendMessage

      @MessageMapping("/chat.send") public void sendMessage(@Valid @Payload @Valid ChatMessageDto messageDto, Principal principal)
      Handles incoming group chat messages from clients and broadcasts them to all subscribers of the specified group topic.
      Parameters:
      messageDto - DTO containing message content, sender information, and target group
    • sendPrivateMessage

      @MessageMapping("/chat.private.send") public void sendPrivateMessage(@Valid @Payload @Valid ChatMessageDto messageDto, Principal principal)
      Handles incoming private chat messages from clients and sends them to both users of the private chat. The message content is end-to-end encrypted and never decrypted by the server.
      Parameters:
      messageDto - DTO containing message content, sender information, and private chat ID
    • deleteMessage

      @MessageMapping("/chat.delete") public void deleteMessage(@Payload String clientMessageId, Principal principal)
    • markPrivateMessageDelivered

      @MessageMapping("/chat.private.delivered") public void markPrivateMessageDelivered(@Payload String clientMessageId, Principal principal)
      Handles a delivery receipt from the recipient of a private message and notifies the sender on /user/queue/private/status. A message that is already READ is never downgraded.
      Parameters:
      clientMessageId - client-generated ID of the delivered message
    • markPrivateChatAsRead

      @MessageMapping("/chat.private.read") public void markPrivateChatAsRead(@Payload Long privateChatId, Principal principal)
      Marks all messages from the other participant in a private chat as READ and notifies them on /user/queue/private/status. The update carries no clientMessageId because it applies to the whole chat.
      Parameters:
      privateChatId - ID of the private chat the user opened
    • handleChatException

      @MessageExceptionHandler({jakarta.persistence.EntityNotFoundException.class,org.springframework.security.access.AccessDeniedException.class,UnauthorizedException.class,java.lang.IllegalArgumentException.class,PrivateChatNotFoundException.class,UserNotFoundException.class,PrivateMessageSendException.class}) @SendToUser("/queue/errors") public ChatErrorDto handleChatException(Exception ex, Principal principal)
      Reports failures from STOMP handlers in this controller back to the client.

      Unlike @RestController endpoints, @MessageMapping methods have no automatic exception-to-response translation: an uncaught exception here is only logged server-side and the caller's socket receives nothing, leaving the client's UI in a stale state (e.g. a message the user tried to delete silently stays visible after an already-deleted or not-the-sender failure). This handler catches the failure modes deleteMessage(java.lang.String, java.security.Principal), sendMessage(vaultWeb.dtos.ChatMessageDto, java.security.Principal) and the status receipt handlers can throw and relays them to the user's private error queue.