Search in sources :

Example 6 with WebSocketSession

use of io.micronaut.websocket.WebSocketSession in project micronaut-core by micronaut-projects.

the class ClientWebSocketInterceptor method intercept.

@Override
public Object intercept(MethodInvocationContext<Object, Object> context) {
    Class<?> declaringType = context.getDeclaringType();
    if (declaringType == WebSocketSessionAware.class) {
        Object[] values = context.getParameterValues();
        if (ArrayUtils.isNotEmpty(values)) {
            Object o = values[0];
            if (o instanceof WebSocketSession) {
                this.webSocketSession = (WebSocketSession) o;
                return null;
            }
        }
    }
    if (declaringType == Closeable.class || declaringType == AutoCloseable.class) {
        // must be close method
        if (webSocketSession != null) {
            webSocketSession.close();
        }
        return null;
    } else {
        String methodName = context.getMethodName();
        if (methodName.startsWith("send") || methodName.startsWith("broadcast")) {
            MediaType mediaType = context.stringValue(Produces.class).map(MediaType::of).orElse(MediaType.APPLICATION_JSON_TYPE);
            validateSession();
            InterceptedMethod interceptedMethod = InterceptedMethod.of(context);
            Class<?> javaReturnType = context.getReturnType().getType();
            if (interceptedMethod.resultType() == InterceptedMethod.ResultType.SYNCHRONOUS && javaReturnType != void.class) {
                return context.proceed();
            }
            try {
                Object[] parameterValues = context.getParameterValues();
                switch(parameterValues.length) {
                    case 0:
                        throw new IllegalArgumentException("At least 1 parameter is required to a send method");
                    case 1:
                        Object value = parameterValues[0];
                        if (value == null) {
                            throw new IllegalArgumentException("Parameter cannot be null");
                        }
                        return send(interceptedMethod, value, mediaType);
                    default:
                        return send(interceptedMethod, context.getParameterValueMap(), mediaType);
                }
            } catch (Exception e) {
                return interceptedMethod.handleException(e);
            }
        }
    }
    return context.proceed();
}
Also used : Closeable(java.io.Closeable) InterceptedMethod(io.micronaut.aop.InterceptedMethod) MediaType(io.micronaut.http.MediaType) WebSocketClientException(io.micronaut.websocket.exceptions.WebSocketClientException) WebSocketSession(io.micronaut.websocket.WebSocketSession)

Aggregations

WebSocketSession (io.micronaut.websocket.WebSocketSession)6 MediaType (io.micronaut.http.MediaType)2 Channel (io.netty.channel.Channel)2 InterceptedMethod (io.micronaut.aop.InterceptedMethod)1 Requires (io.micronaut.context.annotation.Requires)1 Nullable (io.micronaut.core.annotation.Nullable)1 ConvertibleValues (io.micronaut.core.convert.value.ConvertibleValues)1 NettyWebSocketSession (io.micronaut.http.netty.websocket.NettyWebSocketSession)1 CloseReason (io.micronaut.websocket.CloseReason)1 WebSocketBroadcaster (io.micronaut.websocket.WebSocketBroadcaster)1 OnClose (io.micronaut.websocket.annotation.OnClose)1 OnMessage (io.micronaut.websocket.annotation.OnMessage)1 OnOpen (io.micronaut.websocket.annotation.OnOpen)1 WebSocketClientException (io.micronaut.websocket.exceptions.WebSocketClientException)1 WebSocketSessionException (io.micronaut.websocket.exceptions.WebSocketSessionException)1 ChannelGroupException (io.netty.channel.group.ChannelGroupException)1 WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)1 Attribute (io.netty.util.Attribute)1 Singleton (jakarta.inject.Singleton)1 Closeable (java.io.Closeable)1