Skip to content

Commit

Permalink
API Endpoint detection if handler is not object of Router in vertx
Browse files Browse the repository at this point in the history
  • Loading branch information
IshikaDawda committed Jul 29, 2024
1 parent 0e78306 commit 04249a6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public abstract class RouteImpl_Instrumentation {

synchronized void handleContext(RoutingContext context) {
try {
VertxApiEndpointUtils.getInstance().generateAPIEndpointsIfNotPresent(this.hashCode());
VertxApiEndpointUtils.getInstance().routeDetection(path, pattern);
} catch (Exception e) {
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_WHILE_GETTING_ROUTE_FOR_INCOMING_REQUEST, "VERTX-WEB-3.2.0", e.getMessage()), e, this.getClass().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class RouteImpl_Instrumentation {

void handleContext(RoutingContextImplBase context) {
try {
VertxApiEndpointUtils.getInstance().generateAPIEndpointsIfNotPresent(this.hashCode());
VertxApiEndpointUtils.getInstance().routeDetection(path, pattern);
} catch (Exception e) {
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_WHILE_GETTING_ROUTE_FOR_INCOMING_REQUEST, "VERTX-WEB-3.5.1", e.getMessage()), e, this.getClass().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
@Weave(originalName = "io.vertx.ext.web.impl.RouteState")
abstract class RouteState_Instrumentation {

private final RouteImpl route = Weaver.callOriginal();

void handleContext(RoutingContextImplBase context){
try {
VertxApiEndpointUtils.getInstance().generateAPIEndpointsIfNotPresent(route.hashCode());
VertxApiEndpointUtils.getInstance().routeDetection(getPath(), getPattern());
} catch (Exception e) {
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_WHILE_GETTING_ROUTE_FOR_INCOMING_REQUEST, "VERTX-WEB-3.8.3", e.getMessage()), e, this.getClass().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private void addRouteImpl(int routerHashCode, int routeHashCode) throws Exceptio
if (!routes.containsKey(routerHashCode)) {
routes.put(routerHashCode, new ConcurrentHashMap<>());
}
routes.get(routerHashCode).put(route.hashCode(), route);
routes.get(routerHashCode).put(routeHashCode, route);
}
} finally {
if (isLockAcquired) {
Expand All @@ -54,7 +54,7 @@ public void addRouteImpl(int routerHashCode, int routeHashCode, String path, Str
if (!routes.containsKey(routerHashCode)){
return;
}
VertxRoute route = routes.get(routerHashCode).get(Objects.hash(routerHashCode, routeHashCode));
VertxRoute route = routes.get(routerHashCode).get(routeHashCode);
if (route == null){
return;
}
Expand All @@ -77,7 +77,7 @@ public void addHandlerClass(int routerHashCode, int routeHashCode, String handle
if (!routes.containsKey(routerHashCode)){
return;
}
VertxRoute route = routes.get(routerHashCode).get(Objects.hash(routerHashCode, routeHashCode));
VertxRoute route = routes.get(routerHashCode).get(routeHashCode);
if (route == null || !Objects.isNull(route.getHandlerName())){
return;
}
Expand All @@ -99,7 +99,7 @@ public void resolveSubRoutes(int parentRouterHashCode, int childRouterHashCode,
String subRoutePath = getPath(route.getPath(), route.getPattern());
route.setPath(StringUtils.removeEnd(path, StringUtils.SEPARATOR) + StringUtils.prependIfMissing(subRoutePath, StringUtils.SEPARATOR));
route.setRouterHashCode(parentRouterHashCode);
routes.get(parentRouterHashCode).put(route.hashCode(), route);
routes.get(parentRouterHashCode).put(route.getRouteHashCode(), route);
}
} catch (Exception e) {
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_WHILE_GETTING_APP_ENDPOINTS, VERTX_FRAMEWORK, e.getMessage()), e, VertxApiEndpointUtils.class.getName());
Expand Down Expand Up @@ -155,16 +155,25 @@ public String getPath(String path, Object pattern) {

public void routeDetection(String path, Pattern pattern) {
if (NewRelicSecurity.isHookProcessingActive()){
if (URLMappingsHelper.getSegmentCount(NewRelicSecurity.getAgent().getSecurityMetaData().getRequest().getRoute()) == URLMappingsHelper.getSegmentCount(path)) {
return;
}
boolean isAlreadyServlet = Objects.equals(NewRelicSecurity.getAgent().getSecurityMetaData().getMetaData().getFramework(), Framework.SERVLET.name());
String route = StringUtils.EMPTY;
if (path != null){
NewRelicSecurity.getAgent().getSecurityMetaData().getRequest().setRoute(path, isAlreadyServlet);
route = path;
} else if (pattern != null){
NewRelicSecurity.getAgent().getSecurityMetaData().getRequest().setRoute(pattern.pattern(), isAlreadyServlet);
route = pattern.pattern();
}
if (URLMappingsHelper.getSegmentCount(NewRelicSecurity.getAgent().getSecurityMetaData().getRequest().getRoute()) == URLMappingsHelper.getSegmentCount(route)) {
return;
}
NewRelicSecurity.getAgent().getSecurityMetaData().getRequest().setRoute(route, Objects.equals(NewRelicSecurity.getAgent().getSecurityMetaData().getMetaData().getFramework(), Framework.SERVLET.name()));
NewRelicSecurity.getAgent().getSecurityMetaData().getMetaData().setFramework(Framework.VERTX);
}
}

public void generateAPIEndpointsIfNotPresent(int routeHashCode) {
for (Map.Entry<Integer, Map<Integer, VertxRoute>> routesSet : routes.entrySet()) {
if (routesSet.getValue().containsKey(routeHashCode)) {
generateAPIEndpoints(routesSet.getKey());
}
}
}
}

0 comments on commit 04249a6

Please sign in to comment.