All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.aesteve.vertx.nubes.routing.HttpMethodFactory Maven / Gradle / Ivy

The newest version!
package com.github.aesteve.vertx.nubes.routing;

import com.github.aesteve.vertx.nubes.annotations.routing.http.*;
import io.vertx.core.http.HttpMethod;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

public final class HttpMethodFactory {

  private static List> annotClasses = Arrays.asList(
      CONNECT.class,
      DELETE.class,
      GET.class,
      HEAD.class,
      OPTIONS.class,
      PATCH.class,
      POST.class,
      PUT.class,
      TRACE.class
  );

  private HttpMethodFactory() {}

  public static Map fromAnnotatedMethod(Method method) {
    Map methods = new EnumMap<>(HttpMethod.class);
    for (Annotation annot : method.getDeclaredAnnotations()) {
      Class annotClass = annot.annotationType();
      putIfHttpMethod(methods, annot, annotClass);
    }
    return methods;
  }

  public static boolean isRouteMethod(Method method) {
    return annotClasses.stream().anyMatch(method::isAnnotationPresent);
  }

  private static void putIfHttpMethod(Map methods, Annotation annot, Class annotClass) {
    if (annotClass.equals(CONNECT.class)) {
      CONNECT connect = (CONNECT) annot;
      methods.put(HttpMethod.CONNECT, connect.value());
    }
    if (annotClass.equals(DELETE.class)) {
      DELETE delete = (DELETE) annot;
      methods.put(HttpMethod.DELETE, delete.value());
    }
    if (annotClass.equals(GET.class)) {
      GET get = (GET) annot;
      methods.put(HttpMethod.GET, get.value());
    }
    if (annotClass.equals(HEAD.class)) {
      HEAD head = (HEAD) annot;
      methods.put(HttpMethod.HEAD, head.value());
    }
    if (annotClass.equals(OPTIONS.class)) {
      OPTIONS options = (OPTIONS) annot;
      methods.put(HttpMethod.OPTIONS, options.value());
    }
    if (annotClass.equals(PATCH.class)) {
      PATCH patch = (PATCH) annot;
      methods.put(HttpMethod.PATCH, patch.value());
    }
    if (annotClass.equals(POST.class)) {
      POST post = (POST) annot;
      methods.put(HttpMethod.POST, post.value());
    }
    if (annotClass.equals(PUT.class)) {
      PUT put = (PUT) annot;
      methods.put(HttpMethod.PUT, put.value());
    }
    if (annotClass.equals(TRACE.class)) {
      TRACE trace = (TRACE) annot;
      methods.put(HttpMethod.TRACE, trace.value());
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy