forked from solid-connection/solid-connect-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStompHandler.java
More file actions
69 lines (55 loc) · 2.62 KB
/
StompHandler.java
File metadata and controls
69 lines (55 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.example.solidconnection.chat.config;
import static com.example.solidconnection.common.exception.ErrorCode.AUTHENTICATION_FAILED;
import com.example.solidconnection.chat.service.ChatService;
import com.example.solidconnection.common.exception.CustomException;
import com.example.solidconnection.common.exception.ErrorCode;
import com.example.solidconnection.security.authentication.TokenAuthentication;
import com.example.solidconnection.security.userdetails.SiteUserDetails;
import java.security.Principal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.RequiredArgsConstructor;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class StompHandler implements ChannelInterceptor {
private static final Pattern ROOM_ID_PATTERN = Pattern.compile("^/topic/chat/(\\d+)$");
private final ChatService chatService;
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
final StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
if (StompCommand.CONNECT.equals(accessor.getCommand())) {
Principal user = accessor.getUser();
if (user == null) {
throw new CustomException(AUTHENTICATION_FAILED);
}
}
if (StompCommand.SUBSCRIBE.equals(accessor.getCommand())) {
Principal user = accessor.getUser();
if (user == null) {
throw new CustomException(AUTHENTICATION_FAILED);
}
TokenAuthentication tokenAuthentication = (TokenAuthentication) user;
SiteUserDetails siteUserDetails = (SiteUserDetails) tokenAuthentication.getPrincipal();
String destination = accessor.getDestination();
long roomId = Long.parseLong(extractRoomId(destination));
chatService.validateChatRoomParticipant(siteUserDetails.getSiteUser().getId(), roomId);
}
return message;
}
private String extractRoomId(String destination) {
if (destination == null) {
throw new CustomException(ErrorCode.INVALID_ROOM_ID);
}
Matcher matcher = ROOM_ID_PATTERN.matcher(destination);
if (!matcher.matches()) {
throw new CustomException(ErrorCode.INVALID_ROOM_ID);
}
return matcher.group(1);
}
}