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

io.yawp.commons.http.HttpVerb Maven / Gradle / Ivy

There is a newer version: 2.08alpha
Show newest version
package io.yawp.commons.http;

import io.yawp.commons.http.annotation.GET;
import io.yawp.commons.http.annotation.PUT;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public enum HttpVerb {

	GET(GET.class), POST, PUT(PUT.class), PATCH, DELETE, OPTIONS;

	private Class annotationClazz;

	private HttpVerb() {
	}

	private HttpVerb(Class annotation) {
		this.annotationClazz = annotation;

	}

	public static HttpVerb fromString(String method) {
		String methodLowerCase = method.toUpperCase();
		return valueOf(methodLowerCase);
	}

	public boolean hasAnnotation(Method method) {
		if (annotationClazz == null) {
			return false;
		}
		return method.isAnnotationPresent(annotationClazz);
	}

	public String getAnnotationValue(Method method) {
		try {
			Annotation annotation = method.getAnnotation(annotationClazz);
			Method valueMethod = annotation.getClass().getMethod("value");
			return (String) valueMethod.invoke(annotation);
		} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			throw new RuntimeException(e);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy