Search in sources :

Example 6 with UriMatchTemplate

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);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ParameterElement(io.micronaut.inject.ast.ParameterElement) UriMatchTemplate(io.micronaut.http.uri.UriMatchTemplate)

Aggregations

UriMatchTemplate (io.micronaut.http.uri.UriMatchTemplate)6 List (java.util.List)4 ParameterElement (io.micronaut.inject.ast.ParameterElement)3 ArrayList (java.util.ArrayList)3 Bindable (io.micronaut.core.bind.annotation.Bindable)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Optional (java.util.Optional)2 InterceptedMethod (io.micronaut.aop.InterceptedMethod)1 MethodInterceptor (io.micronaut.aop.MethodInterceptor)1 MethodInvocationContext (io.micronaut.aop.MethodInvocationContext)1 BootstrapContextCompatible (io.micronaut.context.annotation.BootstrapContextCompatible)1 ConfigurationException (io.micronaut.context.exceptions.ConfigurationException)1 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)1 Internal (io.micronaut.core.annotation.Internal)1 NonNull (io.micronaut.core.annotation.NonNull)1 Nullable (io.micronaut.core.annotation.Nullable)1