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();
}
Aggregations