Search in sources :

Example 1 with UriRoute

use of io.micronaut.web.router.UriRoute in project micronaut-core by micronaut-projects.

the class AnnotatedFunctionRouteBuilder method process.

@SuppressWarnings("unchecked")
@Override
public void process(BeanDefinition<?> beanDefinition, ExecutableMethod<?, ?> method) {
    if (beanDefinition.hasAnnotation(FunctionBean.class)) {
        String methodName = method.getMethodName();
        Class<?> declaringType = method.getDeclaringType();
        String functionName = beanDefinition.stringValue(FunctionBean.class).orElse(methodName);
        String functionMethod = beanDefinition.stringValue(FunctionBean.class, "method").orElse(null);
        List<UriRoute> routes = new ArrayList<>(2);
        MediaType[] consumes = Arrays.stream(method.stringValues(Consumes.class)).map(MediaType::of).toArray(MediaType[]::new);
        MediaType[] produces = Arrays.stream(method.stringValues(Produces.class)).map(MediaType::of).toArray(MediaType[]::new);
        if (Stream.of(java.util.function.Function.class, Consumer.class, BiFunction.class, BiConsumer.class).anyMatch(type -> type.isAssignableFrom(declaringType))) {
            if (methodName.equals("accept") || methodName.equals("apply") || methodName.equals(functionMethod)) {
                String functionPath = resolveFunctionPath(methodName, declaringType, functionName);
                String[] argumentNames = method.getArgumentNames();
                String argumentName = argumentNames[0];
                int argCount = argumentNames.length;
                UriRoute route = POST(functionPath, beanDefinition, method);
                routes.add(route);
                if (argCount == 1) {
                    route.body(argumentName);
                }
                List<Argument<?>> typeArguments = beanDefinition.getTypeArguments();
                if (!typeArguments.isEmpty()) {
                    int size = typeArguments.size();
                    Argument<?> firstArgument = typeArguments.get(0);
                    if (size < 3 && ClassUtils.isJavaLangType(firstArgument.getType()) && consumes == null) {
                        consumes = new MediaType[] { MediaType.TEXT_PLAIN_TYPE, MediaType.APPLICATION_JSON_TYPE };
                    }
                    if (size < 3) {
                        route.body(Argument.of(firstArgument.getType(), argumentName));
                    }
                    if (size > 1) {
                        Argument<?> argument = typeArguments.get(size == 3 ? 2 : 1);
                        if (ClassUtils.isJavaLangType(argument.getType()) && produces == null) {
                            produces = new MediaType[] { MediaType.TEXT_PLAIN_TYPE, MediaType.APPLICATION_JSON_TYPE };
                        }
                    }
                } else {
                    if (argCount == 1 && ClassUtils.isJavaLangType(method.getArgumentTypes()[0]) && consumes == null) {
                        consumes = new MediaType[] { MediaType.TEXT_PLAIN_TYPE, MediaType.APPLICATION_JSON_TYPE };
                    }
                }
            }
        } else if (Supplier.class.isAssignableFrom(declaringType) && methodName.equals("get")) {
            String functionPath = resolveFunctionPath(methodName, declaringType, functionName);
            routes.add(GET(functionPath, beanDefinition, method));
            routes.add(HEAD(functionPath, beanDefinition, method));
        } else {
            if (StringUtils.isNotEmpty(functionMethod) && functionMethod.equals(methodName)) {
                Argument[] argumentTypes = method.getArguments();
                int argCount = argumentTypes.length;
                if (argCount < 3) {
                    String functionPath = resolveFunctionPath(methodName, declaringType, functionName);
                    if (argCount == 0) {
                        routes.add(GET(functionPath, beanDefinition, method));
                        routes.add(HEAD(functionPath, beanDefinition, method));
                    } else {
                        UriRoute route = POST(functionPath, beanDefinition, method);
                        routes.add(route);
                        if (argCount == 2 || !ClassUtils.isJavaLangType(argumentTypes[0].getType())) {
                            if (consumes == null) {
                                consumes = new MediaType[] { MediaType.APPLICATION_JSON_TYPE };
                            }
                        } else {
                            route.body(method.getArgumentNames()[0]).consumesAll();
                        }
                    }
                }
            }
        }
        if (!routes.isEmpty()) {
            for (UriRoute route : routes) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Created Route to Function: {}", route);
                }
                if (consumes != null) {
                    route.consumes(consumes);
                }
                if (produces != null) {
                    route.produces(produces);
                }
            }
            String functionPath = resolveFunctionPath(methodName, declaringType, functionName);
            availableFunctions.put(functionName, URI.create(functionPath));
            ((ExecutableMethodProcessor) localFunctionRegistry).process(beanDefinition, method);
        }
    }
}
Also used : FunctionBean(io.micronaut.function.FunctionBean) Argument(io.micronaut.core.type.Argument) BiFunction(java.util.function.BiFunction) UriRoute(io.micronaut.web.router.UriRoute) java.util(java.util) BiConsumer(java.util.function.BiConsumer) Consumer(java.util.function.Consumer) Consumes(io.micronaut.http.annotation.Consumes) Produces(io.micronaut.http.annotation.Produces) BiFunction(java.util.function.BiFunction) MediaType(io.micronaut.http.MediaType) BiConsumer(java.util.function.BiConsumer) ExecutableMethodProcessor(io.micronaut.context.processor.ExecutableMethodProcessor)

Example 2 with UriRoute

use of io.micronaut.web.router.UriRoute in project micronaut-core by micronaut-projects.

the class DeleteEndpointRouteBuilder method registerRoute.

@Override
protected void registerRoute(ExecutableMethod<?, ?> method, String id, Integer port) {
    Class<?> declaringType = method.getDeclaringType();
    UriTemplate template = buildUriTemplate(method, id);
    UriRoute uriRoute = DELETE(template.toString(), declaringType, method.getMethodName(), method.getArgumentTypes());
    if (port != null) {
        uriRoute = uriRoute.exposedPort(port);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Created Route to @Endpoint {}: {}", method.getDeclaringType().getName(), uriRoute);
    }
}
Also used : UriRoute(io.micronaut.web.router.UriRoute) UriTemplate(io.micronaut.http.uri.UriTemplate)

Example 3 with UriRoute

use of io.micronaut.web.router.UriRoute in project micronaut-core by micronaut-projects.

the class ReadEndpointRouteBuilder method registerRoute.

@Override
protected void registerRoute(ExecutableMethod<?, ?> method, String id, Integer port) {
    Class<?> declaringType = method.getDeclaringType();
    UriTemplate template = buildUriTemplate(method, id);
    UriRoute uriRoute = GET(template.toString(), declaringType, method.getMethodName(), method.getArgumentTypes());
    if (port != null) {
        uriRoute = uriRoute.exposedPort(port);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Created Route to @Endpoint {}: {}", method.getDeclaringType().getName(), uriRoute);
    }
    uriRoute = HEAD(template.toString(), declaringType, method.getMethodName(), method.getArgumentTypes());
    if (port != null) {
        uriRoute = uriRoute.exposedPort(port);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Created Route to @Endpoint {}: {}", method.getDeclaringType().getName(), uriRoute);
    }
}
Also used : UriRoute(io.micronaut.web.router.UriRoute) UriTemplate(io.micronaut.http.uri.UriTemplate)

Example 4 with UriRoute

use of io.micronaut.web.router.UriRoute in project micronaut-core by micronaut-projects.

the class WriteEndpointRouteBuilder method registerRoute.

@Override
protected void registerRoute(ExecutableMethod<?, ?> method, String id, Integer port) {
    Class<?> declaringType = method.getDeclaringType();
    UriTemplate template = buildUriTemplate(method, id);
    String[] consumes = method.stringValues(Write.class, "consumes");
    UriRoute uriRoute = POST(template.toString(), declaringType, method.getMethodName(), method.getArgumentTypes()).consumes(MediaType.of(consumes));
    if (port != null) {
        uriRoute = uriRoute.exposedPort(port);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Created Route to @Endpoint {}: {}", method.getDeclaringType().getName(), uriRoute);
    }
}
Also used : UriRoute(io.micronaut.web.router.UriRoute) UriTemplate(io.micronaut.http.uri.UriTemplate)

Example 5 with UriRoute

use of io.micronaut.web.router.UriRoute in project micronaut-core by micronaut-projects.

the class ServerWebSocketProcessor method process.

@Override
public void process(BeanDefinition<?> beanDefinition, ExecutableMethod<?, ?> method) {
    Class<?> beanType = beanDefinition.getBeanType();
    if (mappedWebSockets.contains(beanType)) {
        return;
    }
    if (method.isAnnotationPresent(OnMessage.class) || method.isAnnotationPresent(OnOpen.class)) {
        mappedWebSockets.add(beanType);
        String uri = beanDefinition.stringValue(ServerWebSocket.class).orElse("/ws");
        UriRoute route = GET(uri, method);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Created WebSocket: {}", route);
        }
    }
}
Also used : ServerWebSocket(io.micronaut.websocket.annotation.ServerWebSocket) UriRoute(io.micronaut.web.router.UriRoute) OnMessage(io.micronaut.websocket.annotation.OnMessage) OnOpen(io.micronaut.websocket.annotation.OnOpen)

Aggregations

UriRoute (io.micronaut.web.router.UriRoute)6 UriTemplate (io.micronaut.http.uri.UriTemplate)3 InvalidRequestEventException (com.amazonaws.serverless.exceptions.InvalidRequestEventException)1 ExecutableMethodProcessor (io.micronaut.context.processor.ExecutableMethodProcessor)1 Argument (io.micronaut.core.type.Argument)1 FunctionBean (io.micronaut.function.FunctionBean)1 MediaType (io.micronaut.http.MediaType)1 Consumes (io.micronaut.http.annotation.Consumes)1 Produces (io.micronaut.http.annotation.Produces)1 UriRouteMatch (io.micronaut.web.router.UriRouteMatch)1 OnMessage (io.micronaut.websocket.annotation.OnMessage)1 OnOpen (io.micronaut.websocket.annotation.OnOpen)1 ServerWebSocket (io.micronaut.websocket.annotation.ServerWebSocket)1 java.util (java.util)1 BiConsumer (java.util.function.BiConsumer)1 BiFunction (java.util.function.BiFunction)1 Consumer (java.util.function.Consumer)1