Skip to content

Commit

Permalink
Using RowWebsocketSession instead of Spring WebsocketSession in class…
Browse files Browse the repository at this point in the history
…es methods
  • Loading branch information
sepehr committed Aug 13, 2020
1 parent f5ee805 commit ced0499
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/main/java/labs/psychogen/row/filter/RowFilter.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package labs.psychogen.row.filter;

import labs.psychogen.row.domain.RowWebsocketSession;
import labs.psychogen.row.domain.protocol.RequestDto;
import labs.psychogen.row.domain.protocol.ResponseDto;
import org.springframework.web.socket.WebSocketSession;

public interface RowFilter {
boolean filter(RequestDto requestDto, ResponseDto responseDto, WebSocketSession webSocketSession) throws Exception;
boolean filter(RequestDto requestDto, ResponseDto responseDto, RowWebsocketSession rowWebsocketSession) throws Exception;
}
5 changes: 3 additions & 2 deletions src/main/java/labs/psychogen/row/filter/RowFilterChain.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package labs.psychogen.row.filter;

import labs.psychogen.row.domain.RowWebsocketSession;
import labs.psychogen.row.domain.protocol.RequestDto;
import labs.psychogen.row.domain.protocol.ResponseDto;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -53,9 +54,9 @@ public void addFilterAfter(RowFilter rowFilter, Class after){
}
}

public void filter(RequestDto requestDto, ResponseDto response, WebSocketSession session) throws Exception {
public void filter(RequestDto requestDto, ResponseDto response, RowWebsocketSession rowWebsocketSession) throws Exception {
for (RowFilter filter : filters) {
if (!filter.filter(requestDto, response, session)) {
if (!filter.filter(requestDto, response, rowWebsocketSession)) {
return;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/labs/psychogen/row/filter/RowInvokerFiler.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package labs.psychogen.row.filter;

import labs.psychogen.row.domain.RowResponseStatus;
import labs.psychogen.row.domain.RowWebsocketSession;
import labs.psychogen.row.domain.protocol.RequestDto;
import labs.psychogen.row.domain.protocol.ResponseDto;
import labs.psychogen.row.exception.InvalidPathException;
import labs.psychogen.row.service.RowInvokerService;
import org.springframework.web.socket.WebSocketSession;

import java.lang.reflect.InvocationTargetException;

Expand All @@ -17,7 +17,7 @@ public RowInvokerFiler(RowInvokerService rowInvokerService) {
}

@Override
public boolean filter(RequestDto requestDto, ResponseDto responseDto, WebSocketSession webSocketSession) throws Exception {
public boolean filter(RequestDto requestDto, ResponseDto responseDto, RowWebsocketSession rowWebsocketSession) throws Exception {
try {
Object invoke = rowInvokerService.invoke(requestDto, responseDto);
responseDto.setBody(invoke);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/labs/psychogen/row/filter/SubscribeFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import labs.psychogen.row.config.Naming;
import labs.psychogen.row.domain.RowResponseStatus;
import labs.psychogen.row.domain.RowWebsocketSession;
import labs.psychogen.row.domain.protocol.RequestDto;
import labs.psychogen.row.domain.protocol.ResponseDto;
import labs.psychogen.row.event.Subscription;
Expand All @@ -23,7 +24,7 @@ public SubscribeFilter(SubscriberService subscriberService, boolean pre) {
}

@Override
public boolean filter(RequestDto requestDto, ResponseDto responseDto, WebSocketSession webSocketSession) throws Exception {
public boolean filter(RequestDto requestDto, ResponseDto responseDto, RowWebsocketSession rowWebsocketSession) throws Exception {
try {
if(requestDto.getHeaders() != null && requestDto.getHeaders().containsKey(Naming.UNSUBSCRIBE_HEADER_NAME)){
String value = requestDto.getHeaders().get(Naming.UNSUBSCRIBE_HEADER_NAME);
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/labs/psychogen/row/service/ProtocolService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import labs.psychogen.row.context.RowContextHolder;
import labs.psychogen.row.context.RowUser;
import labs.psychogen.row.domain.RowResponseStatus;
import labs.psychogen.row.domain.RowWebsocketSession;
import labs.psychogen.row.domain.protocol.RequestDto;
import labs.psychogen.row.domain.protocol.ResponseDto;
import labs.psychogen.row.filter.RowFilterChain;
Expand All @@ -34,10 +35,10 @@ public ProtocolService(RowFilterChain rowFilterChain) {
validator = Validation.buildDefaultValidatorFactory().getValidator();
}

public void handle(WebSocketSession webSocketSession, TextMessage textMessage){
public void handle(RowWebsocketSession webSocketSession, TextMessage textMessage){
log.trace("Received message: " + textMessage.getPayload());
String payload = textMessage.getPayload();
fillContext(webSocketSession);
fillContext(webSocketSession.getSession());
ResponseDto responseDto = ResponseDto.builder().status(RowResponseStatus.OK.getId()).build();
String requestId = null;
try {
Expand Down Expand Up @@ -67,7 +68,7 @@ public void handle(WebSocketSession webSocketSession, TextMessage textMessage){
try {
String responsePayload = objectMapper.writeValueAsString(responseDto);
log.trace("Sending response: "+ responsePayload);
webSocketSession.sendMessage(new TextMessage(responsePayload));
webSocketSession.getSession().sendMessage(new TextMessage(responsePayload));
} catch (IOException e) {
log.error("Failed to publish response to websocket", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void afterConnectionEstablished(WebSocketSession session) throws Exceptio

@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
protocolService.handle(session, message);
protocolService.handle(rowSessionRegistry.getSession(WebsocketSessionUtil.getUserId(session), session.getId()), message);
updateHeartbeat(session);
}

Expand Down

0 comments on commit ced0499

Please sign in to comment.