Skip to content

Commit

Permalink
Merge pull request #60 from L1nkWave/feature/LWB-40_rename-subroute
Browse files Browse the repository at this point in the history
feature/LWB-40_rename-subroute
  • Loading branch information
borjom1 committed May 2, 2024
2 parents adc1a2c + 2e87c35 commit e6512d6
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.linkwave.ws.websocket.routing.Box;
import org.linkwave.ws.websocket.routing.Payload;
import org.linkwave.ws.websocket.routing.bpp.Broadcast;
import org.linkwave.ws.websocket.routing.bpp.SubRoute;
import org.linkwave.ws.websocket.routing.bpp.Endpoint;
import org.linkwave.ws.websocket.routing.bpp.WebSocketRoute;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -38,7 +38,7 @@ public class ChatRoutes {

@SneakyThrows
@Broadcast
@SubRoute("/{id}/send")
@Endpoint("/{id}/send")
public Box<OutcomeMessage> sendTextMessage(@PathVariable String id,
@Payload IncomeMessage message,
@NonNull WebSocketSession session,
Expand Down Expand Up @@ -82,7 +82,7 @@ public Box<OutcomeMessage> sendTextMessage(@PathVariable String id,
.build());
}

@SubRoute("/edit_text_message/{messageId}")
@Endpoint("/edit_text_message/{messageId}")
@Broadcast(value = "chat:{chatId}", analyzeMessage = true)
public Box<OutcomeMessage> editTextMessage(@PathVariable String messageId,
@Payload String text,
Expand Down Expand Up @@ -111,7 +111,7 @@ public Box<OutcomeMessage> editTextMessage(@PathVariable String messageId,
}

@Broadcast
@SubRoute("/{id}/send_file")
@Endpoint("/{id}/send_file")
public Box<OutcomeFileMessage> sendFileMessage(@PathVariable String id,
@Payload CreatedFileMessage message,
@NonNull UserPrincipal principal,
Expand Down Expand Up @@ -150,7 +150,7 @@ public Box<OutcomeFileMessage> sendFileMessage(@PathVariable String id,
.build());
}

@SubRoute("/remove_message/{messageId}")
@Endpoint("/remove_message/{messageId}")
@Broadcast(value = "chat:{chatId}", analyzeMessage = true)
public Box<IdentifiedMessage> removeMessage(@PathVariable String messageId,
@NonNull UserPrincipal principal,
Expand Down Expand Up @@ -185,7 +185,7 @@ public Box<IdentifiedMessage> removeMessage(@PathVariable String messageId,
}

@Broadcast
@SubRoute("/{id}/clear_history")
@Endpoint("/{id}/clear_history")
public Box<ChatMessage> clearChatHistory(@PathVariable String id,
@NonNull UserPrincipal principal,
@NonNull String path) {
Expand All @@ -211,7 +211,7 @@ public Box<ChatMessage> clearChatHistory(@PathVariable String id,
.build());
}

@SubRoute("/unread_messages")
@Endpoint("/unread_messages")
public UnreadMessages getUnreadMessages(@NonNull UserPrincipal principal) {
final Long userId = principal.token().userId();
return UnreadMessages.builder()
Expand All @@ -220,7 +220,7 @@ public UnreadMessages getUnreadMessages(@NonNull UserPrincipal principal) {
}

@Broadcast
@SubRoute("/{id}/read")
@Endpoint("/{id}/read")
public Box<ReadMessage> readMessages(@PathVariable String id,
@Payload LastReadMessage message,
@NonNull UserPrincipal principal,
Expand Down Expand Up @@ -264,7 +264,7 @@ public Box<ReadMessage> readMessages(@PathVariable String id,
return ok();
}

@SubRoute(value = "/{id}/{recipientId}/add", disabled = true)
@Endpoint(value = "/{id}/{recipientId}/add", disabled = true)
public Box<Void> addChat(@PathVariable String id,
@PathVariable Long recipientId,
@NonNull UserPrincipal principal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.linkwave.ws.websocket.routing.Box;
import org.linkwave.ws.websocket.routing.Payload;
import org.linkwave.ws.websocket.routing.bpp.Broadcast;
import org.linkwave.ws.websocket.routing.bpp.SubRoute;
import org.linkwave.ws.websocket.routing.bpp.Endpoint;
import org.linkwave.ws.websocket.routing.bpp.WebSocketRoute;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -31,7 +31,7 @@ public class GroupChatRoutes {
private final ChatRepository<Long, String> chatRepository;
private final ChatServiceClient chatClient;

@SubRoute(value = "/create", disabled = true)
@Endpoint(value = "/create", disabled = true)
public Box<GroupChatDto> createChat(@NonNull UserPrincipal principal,
@Payload NewGroupChat body,
@NonNull String path) {
Expand All @@ -52,7 +52,7 @@ public Box<GroupChatDto> createChat(@NonNull UserPrincipal principal,
return ok(groupChat);
}

@SubRoute(value = "/{id}/add", disabled = true)
@Endpoint(value = "/{id}/add", disabled = true)
public Box<Void> addChat(@PathVariable String id,
@NonNull UserPrincipal principal,
@NonNull String path) {
Expand All @@ -76,7 +76,7 @@ public Box<Void> addChat(@PathVariable String id,
}

@Broadcast
@SubRoute("/{id}/join")
@Endpoint("/{id}/join")
public Box<MemberMessage> join(@PathVariable String id,
@NonNull UserPrincipal principal,
@NonNull String path) {
Expand Down Expand Up @@ -113,7 +113,7 @@ public Box<MemberMessage> join(@PathVariable String id,

@Broadcast
@Broadcast(value = "user:{senderId}", analyzeMessage = true)
@SubRoute("/{id}/leave")
@Endpoint("/{id}/leave")
public Box<ChatMessage> leaveChat(@PathVariable String id,
@NonNull UserPrincipal principal,
@NonNull String path) {
Expand Down Expand Up @@ -146,7 +146,7 @@ public Box<ChatMessage> leaveChat(@PathVariable String id,
}

@Broadcast
@SubRoute("/{id}/add_member/{userId}")
@Endpoint("/{id}/add_member/{userId}")
public Box<MemberMessage> addMember(@PathVariable String id,
@PathVariable Long userId,
@NonNull UserPrincipal principal,
Expand Down Expand Up @@ -193,7 +193,7 @@ public Box<MemberMessage> addMember(@PathVariable String id,

@Broadcast
@Broadcast("user:{memberId}")
@SubRoute("/{id}/kick/{memberId}")
@Endpoint("/{id}/kick/{memberId}")
public Box<MemberMessage> removeMember(@PathVariable String id,
@PathVariable Long memberId,
@NonNull UserPrincipal principal,
Expand Down Expand Up @@ -238,7 +238,7 @@ public Box<MemberMessage> removeMember(@PathVariable String id,
}

@Broadcast
@SubRoute("/{id}/set_role/{memberId}")
@Endpoint("/{id}/set_role/{memberId}")
public Box<ChatRoleMessage> changeMemberRole(@PathVariable String id,
@PathVariable Long memberId,
@NonNull UserPrincipal principal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import java.lang.annotation.Target;

import org.linkwave.ws.websocket.routing.args.RouteHandlerArgumentResolver;
import org.linkwave.ws.websocket.routing.bpp.SubRoute;
import org.linkwave.ws.websocket.routing.bpp.Endpoint;

/**
* Used to indicate parameter as message payload in {@link SubRoute} (route handler).
* Used to indicate parameter as message payload in method annotated with {@link Endpoint} (i.e. route handler).
* Moreover, the annotated parameter is involved in {@link RouteHandlerArgumentResolver}
* in message injection.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import java.lang.reflect.Method;

import org.linkwave.ws.websocket.routing.bpp.WebSocketRoute;
import org.linkwave.ws.websocket.routing.bpp.SubRoute;
import org.linkwave.ws.websocket.routing.bpp.Endpoint;

/**
* Just a convenient container that holds reference to object or bean annotated
* with {@link WebSocketRoute} is responsible for handling several routes and
* one of handler that's declared in that bean.
*
* @param beanRoute component with route handlers declaration
* @param routeHandler one of the declared handlers ({@link SubRoute})
* @param routeHandler one of the declared handlers ({@link Endpoint})
*/
public record RouteComponent(Object beanRoute, Method routeHandler) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.linkwave.ws.websocket.routing.exception.InvalidPathException;
import org.springframework.lang.NonNull;
import org.springframework.web.socket.WebSocketSession;
import org.linkwave.ws.websocket.routing.bpp.SubRoute;
import org.linkwave.ws.websocket.routing.bpp.Endpoint;

import java.util.List;
import java.util.Map;
Expand All @@ -14,7 +14,7 @@
* Describes the way {@link WebSocketRouter} should resolve route handler's parameters
* in order to construct his successful invocation. The message has a chance to be handled only if
* it passed this stage, i.e. path that specified in message is valid and payload type is compatible with
* the type of {@link Payload} declared in target {@link SubRoute} (route handler).
* the type of {@link Payload} declared in target {@link Endpoint} (route handler).
*/
public interface RouteHandlerArgumentResolver {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* This annotation is used to enable message broadcast for route handler.
*
* @see BroadcastManager
* @see SubRoute
* @see Endpoint
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
Expand All @@ -23,7 +23,7 @@
*
* <ul>
* <li>
* a path variable from route that is specified in {@link SubRoute} annotation that
* a path variable from endpoint that is specified in {@link Endpoint} annotation that
* attached to specific route handler.
* </li>
* <li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import java.lang.annotation.Target;

/**
* This annotation is used to mark methods of {@code @WebSocketRoute} beans
* aimed to handle received message by defined route at value property
* This annotation is used to mark methods of {@code @WebSocketRoute} beans that are
* aimed to handle received messages for specific endpoint.
*
* @see WebSocketRoute
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SubRoute {
public @interface Endpoint {
String value() default "";
boolean disabled() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

/**
* This annotation is used to define bean that can hold
* route handlers, i.e. methods marked with {@code @SubRoute}
* route handlers, i.e. methods marked with {@link Endpoint}
*
* @see SubRoute
* @see Endpoint
*/
@Component
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public Object postProcessAfterInitialization(@NonNull Object bean,
String rootPath = routeCls.getAnnotation(WebSocketRoute.class).value();
sb.append(rootPath);

// scan all sub-routes
// scan all endpoints inside bean
for (Method method : routeCls.getDeclaredMethods()) {
if (method.isAnnotationPresent(SubRoute.class)) {
SubRoute ann = method.getAnnotation(SubRoute.class);
if (method.isAnnotationPresent(Endpoint.class)) {
Endpoint ann = method.getAnnotation(Endpoint.class);

// omit disabled route handler
// omit disabled endpoint
if (ann.disabled()) {
continue;
}
Expand All @@ -64,7 +64,7 @@ public Object postProcessAfterInitialization(@NonNull Object bean,

String combinedPath = sb.toString();
if (routes.containsKey(combinedPath)) {
throw new RuntimeException("found duplicate routes");
throw new RuntimeException("Found duplicate routes");
}

// check broadcast options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/**
* Built-in component that is used by router to automatically broadcast message
* using {@code @Broadcast} annotation. Broadcast manager is informed about what, how and where
* using {@link Broadcast} annotation. Broadcast manager is informed about what, how and where
* the message must be delivered.
*
* @see Broadcast
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.linkwave.ws.websocket.routing.RoutingMessage;
import org.linkwave.ws.websocket.routing.args.DefaultRouteHandlerArgumentResolver;
import org.linkwave.ws.websocket.routing.args.RouteHandlerArgumentResolver;
import org.linkwave.ws.websocket.routing.bpp.SubRoute;
import org.linkwave.ws.websocket.routing.bpp.Endpoint;
import org.linkwave.ws.websocket.routing.exception.InvalidMessageFormatException;
import org.linkwave.ws.websocket.routing.exception.InvalidPathException;
import org.linkwave.ws.websocket.routing.exception.RoutingException;
Expand Down Expand Up @@ -204,37 +204,37 @@ void shouldThrowExceptionWhenPathVariableCantBeResolved() {

private static class RouteHandlers {

@SubRoute("/send")
@Endpoint("/send")
void send() {
}

@SubRoute("/echo")
@Endpoint("/echo")
void echoPath(String path) {

}

@SubRoute("/echo")
@Endpoint("/echo")
void echoPrincipal(UserPrincipal userPrincipal, Principal principal) {

}

@SubRoute("/chat/{id}/send")
@Endpoint("/chat/{id}/send")
void send(WebSocketSession session, @Payload String message, @PathVariable long id, int abc) {
}

@SubRoute("/chat/{id}/send")
@Endpoint("/chat/{id}/send")
void send2(@Payload int message, @PathVariable long id) {
}

@SubRoute("/chat/{ID}/send")
@Endpoint("/chat/{ID}/send")
void send(@PathVariable long id) {
}

@SubRoute("/chat/{objId}/send")
@Endpoint("/chat/{objId}/send")
void send(@PathVariable Object objId) {
}

@SubRoute("/chat/{intId}/send/{stringId}")
@Endpoint("/chat/{intId}/send/{stringId}")
void send(@PathVariable int intId, @PathVariable String stringId) {
}

Expand All @@ -246,7 +246,7 @@ private Map.Entry<String, RouteComponent> findRouteHandler(final String route, f
final List<Method> routeHandlers = Arrays.stream(handlers.getClass().getDeclaredMethods())
.filter(method -> method.getName().equals(handlerName))
.filter(method -> {
final var ann = method.getAnnotation(SubRoute.class);
final var ann = method.getAnnotation(Endpoint.class);
return ann != null && ann.value().equals(route);
})
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.linkwave.ws.websocket.routing.bpp.Broadcast;
import org.linkwave.ws.websocket.routing.bpp.SubRoute;
import org.linkwave.ws.websocket.routing.bpp.Endpoint;
import org.linkwave.ws.websocket.routing.broadcast.BroadcastRepositoryResolver;
import org.linkwave.ws.websocket.routing.broadcast.FlexBroadcastManager;
import org.linkwave.ws.websocket.routing.broadcast.WebSocketMessageBroadcast;
Expand Down Expand Up @@ -122,14 +122,14 @@ static class Routes {

@Broadcast
@Broadcast(value = "user:{senderId}", analyzeMessage = true)
@SubRoute("/chat/{id}")
@Endpoint("/chat/{id}")
Message send(@PathVariable String id) {
return new Message(id, 1L);
}

@Broadcast
@Broadcast
@SubRoute("/chat/{id}")
@Endpoint("/chat/{id}")
Message sendDuplicates(@PathVariable String id) {
return new Message(id, 1L);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.linkwave.ws.websocket.routing.*;
import org.linkwave.ws.websocket.routing.args.DefaultRouteHandlerArgumentResolver;
import org.linkwave.ws.websocket.routing.bpp.Broadcast;
import org.linkwave.ws.websocket.routing.bpp.SubRoute;
import org.linkwave.ws.websocket.routing.bpp.Endpoint;
import org.linkwave.ws.websocket.routing.bpp.WebSocketRoute;
import org.linkwave.ws.websocket.routing.broadcast.BroadcastManager;
import org.linkwave.ws.websocket.routing.broadcast.SimpleBroadcastManager;
Expand Down Expand Up @@ -202,7 +202,7 @@ void everyoneInChatMustReceiveTextMessage() {
@WebSocketRoute("/group-chat")
private static class ChatRoutesBroadcastT {

@SubRoute("/{id}/send")
@Endpoint("/{id}/send")
@Broadcast("chat:{id}")
Box<OutcomeMessage> sendMessage(@PathVariable String id,
@NonNull WebSocketSession session,
Expand Down
Loading

0 comments on commit e6512d6

Please sign in to comment.