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

io.github.hengyunabc.endpoints.autoconfigure2.EndPointsEndPoint Maven / Gradle / Ivy

The newest version!
package io.github.hengyunabc.endpoints.autoconfigure2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.WebEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpoint;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping;
import org.springframework.boot.actuate.endpoint.web.servlet.ControllerEndpointHandlerMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.AbstractHandlerMethodMapping;

/**
 *
 * see org.springframework.boot.actuate.endpoint.RequestMappingEndpoint
 */
@ControllerEndpoint(id = "endpoints")
public class EndPointsEndPoint {

	@Autowired
	private ManagementApplicationcontextHolder contextHolder;

	@Autowired(required = false)
	private WebEndpointsSupplier webEndpointsSupplier;
	@Autowired(required = false)
	private ServletEndpointsSupplier servletEndpointsSupplier;
	@Autowired(required = false)
	private ControllerEndpointsSupplier controllerEndpointsSupplier;

	public EndPointsEndPoint() {
	}

	@RequestMapping
	@ResponseBody
	public List> invoke() {
		List> result = new ArrayList>();
		List> allEndpoints = new ArrayList>();

		if (webEndpointsSupplier != null) {
			allEndpoints.addAll(webEndpointsSupplier.getEndpoints());
		}
		if (servletEndpointsSupplier != null) {
			allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
		}
		if (controllerEndpointsSupplier != null) {
			allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
		}

		for (ExposableEndpoint endpoint : allEndpoints) {
			Map endpointInfo = new HashMap();
			endpointInfo.put("id", endpoint.getId());
			endpointInfo.put("enableByDefault", endpoint.isEnableByDefault());
			result.add(endpointInfo);
		}

		return result;
	}

	@RequestMapping("mappings")
	@ResponseBody
	public Map mappings() {
		Map result = new LinkedHashMap();
		extractMethodMappings(contextHolder.getManagementApplicationContext(), result);
		return result;
	}

	@SuppressWarnings("rawtypes")
	private void extractMethodMappings(ApplicationContext applicationContext, Map result) {
		if (applicationContext != null) {
			for (Entry bean : applicationContext
					.getBeansOfType(AbstractHandlerMethodMapping.class).entrySet()) {
				AbstractHandlerMethodMapping methodMapping = bean.getValue();
				if (methodMapping instanceof AbstractWebMvcEndpointHandlerMapping
						|| methodMapping instanceof ControllerEndpointHandlerMapping) {
					@SuppressWarnings("unchecked")
					Map methods = bean.getValue().getHandlerMethods();
					for (Entry method : methods.entrySet()) {
						Map map = new LinkedHashMap();
						map.put("bean", bean.getKey());
						map.put("method", method.getValue().toString());
						result.put(method.getKey().toString(), map);
					}
				}
			}
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy