|
| 1 | +package com.taskagile.web.socket; |
| 2 | + |
| 3 | +import com.taskagile.utils.JsonUtils; |
| 4 | +import org.slf4j.Logger; |
| 5 | +import org.slf4j.LoggerFactory; |
| 6 | +import org.springframework.util.AntPathMatcher; |
| 7 | +import org.springframework.util.Assert; |
| 8 | + |
| 9 | +import java.lang.annotation.Annotation; |
| 10 | +import java.lang.reflect.Method; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +public class ChannelHandlerInvoker { |
| 15 | + |
| 16 | + private static final Logger log = LoggerFactory.getLogger(ChannelHandlerInvoker.class); |
| 17 | + |
| 18 | + private static final AntPathMatcher antPathMatcher = new AntPathMatcher(); |
| 19 | + |
| 20 | + private String channelPattern; |
| 21 | + private Object handler; |
| 22 | + // Key is the action, value is the method to handle that action |
| 23 | + private final Map<String, Method> actionMethods = new HashMap<>(); |
| 24 | + |
| 25 | + public ChannelHandlerInvoker(Object handler) { |
| 26 | + Assert.notNull(handler, "Parameter `handler` must not be null"); |
| 27 | + |
| 28 | + Class<?> handlerClass = handler.getClass(); |
| 29 | + ChannelHandler handlerAnnotation = handlerClass.getAnnotation(ChannelHandler.class); |
| 30 | + Assert.notNull(handlerAnnotation, "Parameter `handler` must have annotation @ChannelHandler"); |
| 31 | + |
| 32 | + Method[] methods = handlerClass.getMethods(); |
| 33 | + for (Method method : methods) { |
| 34 | + Action actionAnnotation = method.getAnnotation(Action.class); |
| 35 | + if (actionAnnotation == null) { |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + String action = actionAnnotation.value(); |
| 40 | + actionMethods.put(action, method); |
| 41 | + log.debug("Mapped action `{}` in channel handler `{}#{}`", action, handlerClass.getName(), method); |
| 42 | + } |
| 43 | + |
| 44 | + this.channelPattern = ChannelHandlers.getPattern(handlerAnnotation); |
| 45 | + this.handler = handler; |
| 46 | + } |
| 47 | + |
| 48 | + public boolean supports(String action) { |
| 49 | + return actionMethods.containsKey(action); |
| 50 | + } |
| 51 | + |
| 52 | + public void handle(IncomingMessage incomingMessage, RealTimeSession session) { |
| 53 | + Assert.isTrue(antPathMatcher.match(channelPattern, incomingMessage.getChannel()), "Channel of the handler must match"); |
| 54 | + Method actionMethod = actionMethods.get(incomingMessage.getAction()); |
| 55 | + Assert.notNull(actionMethod, "Action method for `" + incomingMessage.getAction() + "` must exist"); |
| 56 | + |
| 57 | + // Find all required parameters |
| 58 | + Class<?>[] parameterTypes = actionMethod.getParameterTypes(); |
| 59 | + // All the annotations for each parameter |
| 60 | + Annotation[][] allParameterAnnotations = actionMethod.getParameterAnnotations(); |
| 61 | + // The arguments that will be passed to the action method |
| 62 | + Object[] args = new Object[parameterTypes.length]; |
| 63 | + |
| 64 | + try { |
| 65 | + // Populate arguments |
| 66 | + for (int i = 0; i < parameterTypes.length; i++) { |
| 67 | + Class<?> parameterType = parameterTypes[i]; |
| 68 | + Annotation[] parameterAnnotations = allParameterAnnotations[i]; |
| 69 | + |
| 70 | + // No annotation applied on this parameter |
| 71 | + if (parameterAnnotations.length == 0) { |
| 72 | + if (parameterType.isInstance(session)) { |
| 73 | + args[i] = session; |
| 74 | + } else { |
| 75 | + args[i] = null; |
| 76 | + } |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + // Only use the first annotation applied on the parameter |
| 81 | + Annotation parameterAnnotation = parameterAnnotations[0]; |
| 82 | + if (parameterAnnotation instanceof Payload) { |
| 83 | + Object arg = JsonUtils.toObject(incomingMessage.getPayload(), parameterType); |
| 84 | + if (arg == null) { |
| 85 | + throw new IllegalArgumentException("Unable to instantiate parameter of type `" + |
| 86 | + parameterType.getName() + "`."); |
| 87 | + } |
| 88 | + args[i] = arg; |
| 89 | + } else if (parameterAnnotation instanceof ChannelValue) { |
| 90 | + args[i] = incomingMessage.getChannel(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + actionMethod.invoke(handler, args); |
| 95 | + } catch (Exception e) { |
| 96 | + String error = "Failed to invoker action method `" + incomingMessage.getAction() + |
| 97 | + "` at channel `" + incomingMessage.getChannel() + "` "; |
| 98 | + log.error(error, e); |
| 99 | + session.error(error); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments