Created
February 15, 2018 22:06
-
-
Save trmsmy/8502f8672b1c684e5d073c617cbdb3b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lca.phoenix.workq.rest.config; | |
import java.util.Comparator; | |
import java.util.HashSet; | |
import java.util.Set; | |
import java.util.TreeSet; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import org.glassfish.jersey.server.model.Resource; | |
import org.glassfish.jersey.server.model.ResourceMethod; | |
import org.glassfish.jersey.server.model.ResourceModel; | |
import org.glassfish.jersey.server.monitoring.ApplicationEvent; | |
import org.glassfish.jersey.server.monitoring.ApplicationEventListener; | |
import org.glassfish.jersey.server.monitoring.RequestEvent; | |
import org.glassfish.jersey.server.monitoring.RequestEventListener; | |
import com.fasterxml.classmate.ResolvedType; | |
import com.fasterxml.classmate.TypeResolver; | |
public class ListOfAllJerseyEndpoint implements ApplicationEventListener { | |
private static final TypeResolver TYPE_RESOLVER = new TypeResolver(); | |
private final String applicationPath; | |
private boolean withOptions = false; | |
private boolean withWadl = false; | |
public ListOfAllJerseyEndpoint(String applicationPath) { | |
this.applicationPath = applicationPath; | |
} | |
@Override | |
public void onEvent(ApplicationEvent event) { | |
if (event.getType() == ApplicationEvent.Type.INITIALIZATION_APP_FINISHED) { | |
final ResourceModel resourceModel = event.getResourceModel(); | |
final ResourceLogDetails logDetails = new ResourceLogDetails(); | |
resourceModel.getResources().stream().forEach((resource) -> { | |
logDetails.addEndpointLogLines(getLinesFromResource(resource)); | |
}); | |
logDetails.log(); | |
} | |
} | |
@Override | |
public RequestEventListener onRequest(RequestEvent requestEvent) { | |
return null; | |
} | |
public ListOfAllJerseyEndpoint withOptions() { | |
this.withOptions = true; | |
return this; | |
} | |
public ListOfAllJerseyEndpoint withWadl() { | |
this.withWadl = true; | |
return this; | |
} | |
private Set<EndpointLogLine> getLinesFromResource(Resource resource) { | |
Set<EndpointLogLine> logLines = new HashSet<>(); | |
populate(this.applicationPath, false, resource, logLines); | |
return logLines; | |
} | |
private void populate(String basePath, Class<?> klass, boolean isLocator, | |
Set<EndpointLogLine> endpointLogLines) { | |
populate(basePath, isLocator, Resource.from(klass), endpointLogLines); | |
} | |
private void populate(String basePath, boolean isLocator, Resource resource, | |
Set<EndpointLogLine> endpointLogLines) { | |
if (!isLocator) { | |
basePath = normalizePath(basePath, resource.getPath()); | |
} | |
for (ResourceMethod method : resource.getResourceMethods()) { | |
if (!withOptions && method.getHttpMethod().equalsIgnoreCase("OPTIONS")) { | |
continue; | |
} | |
if (!withWadl && basePath.contains(".wadl")) { | |
continue; | |
} | |
endpointLogLines.add(new EndpointLogLine(method.getHttpMethod(), basePath, null)); | |
} | |
for (Resource childResource : resource.getChildResources()) { | |
for (ResourceMethod method : childResource.getAllMethods()) { | |
if (method.getType() == ResourceMethod.JaxrsType.RESOURCE_METHOD) { | |
final String path = normalizePath(basePath, childResource.getPath()); | |
if (!withOptions && method.getHttpMethod().equalsIgnoreCase("OPTIONS")) { | |
continue; | |
} | |
if (!withWadl && path.contains(".wadl")) { | |
continue; | |
} | |
endpointLogLines.add(new EndpointLogLine(method.getHttpMethod(), path, null)); | |
} else if (method.getType() == ResourceMethod.JaxrsType.SUB_RESOURCE_LOCATOR) { | |
final String path = normalizePath(basePath, childResource.getPath()); | |
final ResolvedType responseType = TYPE_RESOLVER | |
.resolve(method.getInvocable().getResponseType()); | |
final Class<?> erasedType = !responseType.getTypeBindings().isEmpty() | |
? responseType.getTypeBindings().getBoundType(0).getErasedType() | |
: responseType.getErasedType(); | |
populate(path, erasedType, true, endpointLogLines); | |
} | |
} | |
} | |
} | |
private static String normalizePath(String basePath, String path) { | |
if (path == null) { | |
return basePath; | |
} | |
if (basePath.endsWith("/")) { | |
return path.startsWith("/") ? basePath + path.substring(1) : basePath + path; | |
} | |
return path.startsWith("/") ? basePath + path : basePath + "/" + path; | |
} | |
private static class ResourceLogDetails { | |
private static final Logger logger = LogManager.getLogger(); | |
private static final Comparator<EndpointLogLine> COMPARATOR | |
= Comparator.comparing((EndpointLogLine e) -> e.path) | |
.thenComparing((EndpointLogLine e) -> e.httpMethod); | |
private final Set<EndpointLogLine> logLines = new TreeSet<>(COMPARATOR); | |
private void log() { | |
StringBuilder sb = new StringBuilder("\nAll endpoints for Jersey application\n"); | |
logLines.stream().forEach((line) -> { | |
sb.append(line).append("\n"); | |
}); | |
logger.info(sb.toString()); | |
} | |
private void addEndpointLogLines(Set<EndpointLogLine> logLines) { | |
this.logLines.addAll(logLines); | |
} | |
} | |
private static class EndpointLogLine { | |
private static final String DEFAULT_FORMAT = " %-7s %s"; | |
final String httpMethod; | |
final String path; | |
final String format; | |
private EndpointLogLine(String httpMethod, String path, String format) { | |
this.httpMethod = httpMethod; | |
this.path = path; | |
this.format = format == null ? DEFAULT_FORMAT : format; | |
} | |
@Override | |
public String toString() { | |
return String.format(format, httpMethod, path); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment