From 52d4adc4738647601d3442ffe3e493dfe4c4ff9c Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com.> Date: Wed, 19 Mar 2025 10:50:39 +0000 Subject: [PATCH] fix and cleanups --- .gitignore | 1 - .../SPDF/config/EndpointInspector.java | 174 +++++++----------- 2 files changed, 64 insertions(+), 111 deletions(-) diff --git a/.gitignore b/.gitignore index 440bdd6f9..90d48ccea 100644 --- a/.gitignore +++ b/.gitignore @@ -25,7 +25,6 @@ clientWebUI/ !cucumber/ !cucumber/exampleFiles/ !cucumber/exampleFiles/example_html.zip -testing/file_snapshots exampleYmlFiles/stirling/ /testing/file_snapshots SwaggerDoc.json diff --git a/src/main/java/stirling/software/SPDF/config/EndpointInspector.java b/src/main/java/stirling/software/SPDF/config/EndpointInspector.java index fc7e03e7c..16027a768 100644 --- a/src/main/java/stirling/software/SPDF/config/EndpointInspector.java +++ b/src/main/java/stirling/software/SPDF/config/EndpointInspector.java @@ -5,6 +5,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.TreeSet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,6 +37,7 @@ public class EndpointInspector implements ApplicationListener entry : mappings.entrySet()) { - String beanName = entry.getKey(); RequestMappingHandlerMapping mapping = entry.getValue(); // Get all handler methods registered in this mapping Map handlerMethods = mapping.getHandlerMethods(); - int methodsWithEmptyMethodsCondition = 0; - int methodsWithGetMethod = 0; - int methodsWithGetOrEmpty = 0; // Process each handler method for (Map.Entry handlerEntry : @@ -62,74 +60,30 @@ public class EndpointInspector implements ApplicationListener {}", - mappingInfo, - handlerMethod.getMethod().getName()); - - boolean hasEmptyMethodsCondition = false; - boolean hasGetMethod = false; - - // Get methods through reflection if standard approach fails - Set methods = Collections.emptySet(); - + // Check if the method handles GET requests + boolean isGetHandler = false; try { - methods = mappingInfo.getMethodsCondition().getMethods(); - - // Standard approach - hasEmptyMethodsCondition = methods.isEmpty(); - hasGetMethod = methods.contains(RequestMethod.GET); - - logger.debug( - "Standard method detection: methods={}, isEmpty={}, hasGET={}", - methods, - hasEmptyMethodsCondition, - hasGetMethod); + Set methods = mappingInfo.getMethodsCondition().getMethods(); + // Either explicitly handles GET or handles all methods (empty set) + isGetHandler = methods.isEmpty() || methods.contains(RequestMethod.GET); } catch (Exception e) { - logger.warn( - "Error accessing methods through standard API: {}", e.getMessage()); + // If we can't determine methods, assume it could handle GET + isGetHandler = true; } - if (hasEmptyMethodsCondition) { - methodsWithEmptyMethodsCondition++; - } - - if (hasGetMethod) { - methodsWithGetMethod++; - } - - // Count any method that could potentially handle GET requests - if (hasEmptyMethodsCondition || hasGetMethod) { - methodsWithGetOrEmpty++; - - // Try to get patterns using reflection if direct approach fails - Set patterns = extractPatternsUsingReflection(mappingInfo); - + if (isGetHandler) { + // Since we know getDirectPaths works, use it directly + Set patterns = extractPatternsUsingDirectPaths(mappingInfo); + + // If that fails, try string parsing as fallback if (patterns.isEmpty()) { - // Fall back to toString parsing - String infoString = mappingInfo.toString(); - // Extract patterns from toString if possible - if (infoString.contains("{")) { - String patternsSection = - infoString.substring( - infoString.indexOf("{") + 1, - infoString.indexOf("}")); - - for (String pattern : patternsSection.split(",")) { - pattern = pattern.trim(); - if (!pattern.isEmpty()) { - patterns.add(pattern); - } - } - } + patterns = extractPatternsFromString(mappingInfo); } - - // Add all patterns - validGetEndpoints.addAll(patterns); + + // Add all valid patterns + validGetEndpoints.addAll(patterns); } } - } if (validGetEndpoints.isEmpty()) { @@ -144,58 +98,47 @@ public class EndpointInspector implements ApplicationListener extractPatternsUsingReflection(RequestMappingInfo mappingInfo) { + /** + * Extract patterns using the getDirectPaths method that works in this environment + */ + private Set extractPatternsUsingDirectPaths(RequestMappingInfo mappingInfo) { Set patterns = new HashSet<>(); - + try { - // First try standard API - if (mappingInfo.getPatternsCondition() != null) { - patterns.addAll(mappingInfo.getPatternsCondition().getPatterns()); + Method getDirectPathsMethod = mappingInfo.getClass().getMethod("getDirectPaths"); + Object result = getDirectPathsMethod.invoke(mappingInfo); + if (result instanceof Set) { + @SuppressWarnings("unchecked") + Set resultSet = (Set) result; + patterns.addAll(resultSet); } } catch (Exception e) { - logger.debug("Standard pattern access failed: {}", e.getMessage()); + // Just return empty set if method not found or fails } + + return patterns; + } - // If standard approach failed, try reflection - if (patterns.isEmpty()) { - try { - // Try to access patterns through reflection on different Spring versions - Method[] methods = mappingInfo.getClass().getMethods(); + private Set extractPatternsFromString(RequestMappingInfo mappingInfo) { + Set patterns = new HashSet<>(); + try { + String infoString = mappingInfo.toString(); + if (infoString.contains("{")) { + String patternsSection = + infoString.substring( + infoString.indexOf("{") + 1, + infoString.indexOf("}")); - // Look for methods that might return patterns - for (Method method : methods) { - String methodName = method.getName(); - if ((methodName.contains("pattern") || methodName.contains("Path")) - && method.getParameterCount() == 0) { - - logger.debug("Trying reflection method: {}", methodName); - try { - Object result = method.invoke(mappingInfo); - if (result instanceof Set) { - @SuppressWarnings("unchecked") - Set resultSet = (Set) result; - patterns.addAll(resultSet); - logger.debug( - "Found {} patterns using method {}", - resultSet.size(), - methodName); - } else if (result != null) { - logger.debug( - "Method {} returned non-Set result: {}", - methodName, - result); - } - } catch (Exception e) { - logger.debug( - "Method {} invocation failed: {}", methodName, e.getMessage()); - } + for (String pattern : patternsSection.split(",")) { + pattern = pattern.trim(); + if (!pattern.isEmpty()) { + patterns.add(pattern); } } - } catch (Exception e) { - logger.warn("Reflection-based pattern extraction failed: {}", e.getMessage()); } + } catch (Exception e) { + // Just return empty set if parsing fails } - return patterns; } @@ -211,8 +154,7 @@ public class EndpointInspector implements ApplicationListener(validGetEndpoints); } -} + + //For debugging when needed + private void logAllEndpoints() { + Set sortedEndpoints = new TreeSet<>(validGetEndpoints); + + logger.info("=== BEGIN: All discovered GET endpoints ==="); + for (String endpoint : sortedEndpoints) { + logger.info("Endpoint: {}", endpoint); + } + logger.info("=== END: All discovered GET endpoints ==="); + + } + +} \ No newline at end of file