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

pl.morgwai.base.servlet.utils.EndpointUtils Maven / Gradle / Ivy

There is a newer version: 6.2-javax
Show newest version
// Copyright (c) Piotr Morgwai Kotarbinski, Licensed under the Apache License, Version 2.0
package pl.morgwai.base.servlet.utils;

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

import javax.websocket.*;



/** Some static helper functions related to {@link Endpoint}s. */
public interface EndpointUtils {



	/**
	 * Checks if {@code method} is either annotated with {@link OnOpen} or overrides
	 * {@link Endpoint#onOpen(Session, EndpointConfig)}.
	 */
	static boolean isOnOpen(Method method) {
		return isEndpointLifecycleMethod(method, OnOpen.class, "onOpen");
	}



	/**
	 * Checks if {@code method} is either annotated with {@link OnClose} or overrides
	 * {@link Endpoint#onClose(Session, CloseReason)}.
	 */
	static boolean isOnClose(Method method) {
		return isEndpointLifecycleMethod(method, OnClose.class, "onClose");
	}



	/**
	 * Checks if {@code method} is either annotated with {@link OnError} or overrides
	 * {@link Endpoint#onError(Session, Throwable)}.
	 */
	static boolean isOnError(Method method) {
		return isEndpointLifecycleMethod(method, OnError.class, "onError");
	}



	/**
	 * Checks if {@code method} either is annotated with {@code annotationClass} or overrides the
	 * {@link Endpoint} method given by {@code endpointMethodName}.
	 */
	private static boolean isEndpointLifecycleMethod(
		Method method,
		Class annotationClass,
		String endpointMethodName
	) {
		if (
			method.isAnnotationPresent(annotationClass)
			&& !Endpoint.class.isAssignableFrom(method.getDeclaringClass())
		) {
			return true;
		}

		if ( !method.getName().equals(endpointMethodName)) return false;
		try {
			Endpoint.class.getMethod(endpointMethodName, method.getParameterTypes());
			return true;
		} catch (NoSuchMethodException e) {
			return false;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy