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