Java utils - apycazo/playground GitHub Wiki

Get network interfaces

import static java.lang.System.out;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

public class ListNets {

    public void listInterfaces()
    {
        try {
            Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
            for (NetworkInterface netint : Collections.list(nets)) {
                if (netint.isUp()) {
                    displayInterfaceInformation(netint);
                }
            }
        } catch (Exception e) {
            out.println("Captured exception: " + e.getClass().getSimpleName());
        }
    }

    public void displayInterfaceInformation(NetworkInterface netint) throws SocketException
    {
        out.printf("Display name: %s\n", netint.getDisplayName());
        out.printf("Name: %s\n", netint.getName());
        Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
        List<InetAddress> list = Collections.list(inetAddresses);
        list.forEach(addr -> {
            out.printf("InetAddress: %s\n", addr);
        });
        out.printf("\n");
    }
}

Generate mapping list

Note: Requires @Autowired private RequestMappingHandlerMapping mappings;

@RequestMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
public Map<String, Map<String,Object>> getMappings(
		HttpServletRequest request)
{
	final Map<String, Map<String,Object>> services = new LinkedHashMap<>();
	String base = request.getRequestURL().toString();
	if (base.endsWith("/")) base = base.substring(0, base.length()-1);
	String finalBase = base;

	Map<RequestMappingInfo, HandlerMethod> x = mappings.getHandlerMethods();
	x.keySet().forEach(rmi -> {
		String beanClassName = x.get(rmi).getBeanType().getSimpleName();
		HandlerMethod method = x.get(rmi);
		Set<String> maps = rmi.getPatternsCondition().getPatterns();
		String url = maps.isEmpty() ? "" : maps.iterator().next();
		Map<String,Object> entry = services.get(beanClassName);
		if (entry == null) {
			entry = new LinkedHashMap<>();
		}
		entry.put(method.getMethod().getName(), finalBase + url);            
		services.put(beanClassName, entry);
	});
	return services;
}
⚠️ **GitHub.com Fallback** ⚠️