Search in sources :

Example 1 with RestEndpointModel

use of fish.payara.appserver.rest.endpoints.RestEndpointModel in project Payara by payara.

the class ListRestEndpointsCommand method getEndpointMap.

/**
 * Returns a list of a map of endpoint -> list of methods.
 * @param appName the name of the application.
 * @throws IllegalArgumentException if the application does not contain endpoints. Specifies the reason.
 */
public Map<String, Set<String>> getEndpointMap(String appName) {
    // Create an initial array
    Map<String, Set<String>> endpoints = new TreeMap<>();
    // Get all deployed applications
    ApplicationRegistry appRegistry = habitat.getService(ApplicationRegistry.class);
    // Get the deployed application with the provided name
    ApplicationInfo appInfo = getSpecifiedApplication(appName, appRegistry);
    // Check if the given application exists
    if (appInfo == null) {
        throw new IllegalArgumentException("Application " + appName + " is not registered.");
    }
    // Get the web modules from the given application (e.g. multiple wars in an ear)
    List<WebModule> modules = getWebModules(appInfo);
    // Check if the given application exists
    if (modules.isEmpty()) {
        throw new IllegalArgumentException("Application " + appName + " contains no web modules.");
    }
    // Get the Jersey applications from all of the modules (or only the one matching the given component name)
    Map<ServletContainer, String> jerseyApplicationMap = getSpecifiedJerseyApplications(componentName, modules);
    // error out in the case of a non existent provided component or no components at all
    if (jerseyApplicationMap.isEmpty()) {
        if (componentName == null) {
            throw new IllegalArgumentException("Application " + appName + " has no deployed JAX-RS applications.");
        }
        throw new IllegalArgumentException("Component " + componentName + " could not be found.");
    }
    // loop through jersey components
    boolean hasEndpoints = false;
    for (ServletContainer jerseyApplication : jerseyApplicationMap.keySet()) {
        String appRoot = jerseyApplication.getServletContext().getContextPath();
        String jerseyAppRoot = jerseyApplicationMap.get(jerseyApplication);
        List<Class<?>> containedClasses = getClasses(jerseyApplication);
        // loop through all classes contained by given jersey application
        for (Class<?> containedClass : containedClasses) {
            List<RestEndpointModel> classEndpoints = getEndpointsForClass(containedClass);
            if (!classEndpoints.isEmpty()) {
                // loop through endpoints in given class
                for (RestEndpointModel endpoint : classEndpoints) {
                    String path = appRoot + jerseyAppRoot + endpoint.getPath();
                    String method = endpoint.getRequestMethod();
                    if (endpoints.keySet().contains(path)) {
                        Set<String> methods = endpoints.get(path);
                        methods.add(method);
                        endpoints.put(path, methods);
                    } else {
                        endpoints.put(path, new TreeSet<>(asList(method)));
                    }
                }
            }
        }
        // Jersey will automatically generate a wadl file for the endpoints, so add
        // it for every deployed application with endpoints
        endpoints.put(appRoot + jerseyAppRoot + jerseyWADL, new TreeSet<>(asList(HttpMethod.GET)));
        hasEndpoints = true;
    }
    if (!hasEndpoints) {
        return null;
    }
    return endpoints;
}
Also used : TreeSet(java.util.TreeSet) Set(java.util.Set) ApplicationInfo(org.glassfish.internal.data.ApplicationInfo) WebModule(com.sun.enterprise.web.WebModule) TreeMap(java.util.TreeMap) ApplicationRegistry(org.glassfish.internal.data.ApplicationRegistry) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) RestEndpointModel(fish.payara.appserver.rest.endpoints.RestEndpointModel)

Aggregations

WebModule (com.sun.enterprise.web.WebModule)1 RestEndpointModel (fish.payara.appserver.rest.endpoints.RestEndpointModel)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 ApplicationInfo (org.glassfish.internal.data.ApplicationInfo)1 ApplicationRegistry (org.glassfish.internal.data.ApplicationRegistry)1 ServletContainer (org.glassfish.jersey.servlet.ServletContainer)1