use of io.micronaut.http.uri.UriMatchTemplate in project micronaut-core by micronaut-projects.
the class WebSocketVisitor method visitMethod.
@Override
public void visitMethod(MethodElement element, VisitorContext context) {
if (skipValidation) {
return;
}
String uri = element.stringValue(WEB_SOCKET_COMPONENT).orElse("/ws");
UriMatchTemplate template = uriCache.computeIfAbsent(uri, UriMatchTemplate::of);
List<String> variables = template.getVariableNames();
ParameterElement[] parameters = element.getParameters();
if (ArrayUtils.isNotEmpty(parameters)) {
if (element.hasAnnotation(ON_OPEN)) {
for (ParameterElement parameter : parameters) {
if (isInvalidParameter(variables, parameter, WEB_SOCKET_SESSION, HTTP_REQUEST)) {
context.fail("Parameter to @OnOpen must either be a URI variable, a WebSocketSession , the HttpRequest, or annotated with an HTTP binding annotation (such as @Header)", parameter);
break;
}
}
} else if (element.hasAnnotation(ON_CLOSE)) {
for (ParameterElement parameter : parameters) {
if (isInvalidParameter(variables, parameter, WEB_SOCKET_SESSION, CLOSE_REASON)) {
context.fail("Parameter to @OnClose must either be a URI variable, a CloseReason, a WebSocketSession or annotated with an HTTP binding annotation (such as @Header)", parameter);
break;
}
}
} else if (element.hasAnnotation(ON_ERROR)) {
for (ParameterElement parameter : parameters) {
if (isInvalidParameter(variables, parameter, WEB_SOCKET_SESSION, Throwable.class.getName())) {
context.fail("Parameter to @OnError must either be a URI variable, a Throwable, a WebSocketSession or annotated with an HTTP binding annotation (such as @Header)", parameter);
break;
}
}
} else if (element.hasAnnotation(ON_MESSAGE)) {
List<ParameterElement> bodyParameters = new ArrayList<>(3);
for (ParameterElement parameter : parameters) {
if (isInvalidParameter(variables, parameter, WEB_SOCKET_SESSION)) {
// potential body parameter
bodyParameters.add(parameter);
}
}
if (bodyParameters.size() > 1) {
context.fail("WebSocket @OnMessage handler has multiple possible message body arguments. : " + bodyParameters, element);
}
}
}
}
Aggregations