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;
}
Aggregations