diff --git a/src/main/java/stirling/software/SPDF/config/CleanUrlInterceptor.java b/src/main/java/stirling/software/SPDF/config/CleanUrlInterceptor.java index 81cbc43f..d53d9fa2 100644 --- a/src/main/java/stirling/software/SPDF/config/CleanUrlInterceptor.java +++ b/src/main/java/stirling/software/SPDF/config/CleanUrlInterceptor.java @@ -1,4 +1,5 @@ package stirling.software.SPDF.config; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -7,10 +8,22 @@ import org.springframework.web.servlet.ModelAndView; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.Arrays; +import java.util.List; +import java.util.HashMap; +import java.util.Map; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.ModelAndView; public class CleanUrlInterceptor implements HandlerInterceptor { - private static final Pattern LANG_PATTERN = Pattern.compile("&?lang=([^&]+)"); + + private static final List ALLOWED_PARAMS = Arrays.asList("lang", "param2", "param3"); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { @@ -18,16 +31,33 @@ public class CleanUrlInterceptor implements HandlerInterceptor { if (queryString != null && !queryString.isEmpty()) { String requestURI = request.getRequestURI(); - // Keep the lang parameter if it exists - Matcher langMatcher = LANG_PATTERN.matcher(queryString); - String langQueryString = langMatcher.find() ? "lang=" + langMatcher.group(1) : ""; + Map parameters = new HashMap<>(); - // Check if there are any other query parameters besides the lang parameter - String remainingQueryString = queryString.replaceAll(LANG_PATTERN.pattern(), "").replaceAll("&+", "&").replaceAll("^&|&$", ""); + // Keep only the allowed parameters + String[] queryParameters = queryString.split("&"); + for (String param : queryParameters) { + String[] keyValue = param.split("="); + if (keyValue.length != 2) { + continue; + } + if (ALLOWED_PARAMS.contains(keyValue[0])) { + parameters.put(keyValue[0], keyValue[1]); + } + } - if (!remainingQueryString.isEmpty()) { - // Redirect to the URL without other query parameters - String redirectUrl = requestURI + (langQueryString.isEmpty() ? "" : "?" + langQueryString); + // If there are any other query parameters besides the allowed ones + if (parameters.size() > 0) { + // Construct new query string + StringBuilder newQueryString = new StringBuilder(); + for (Map.Entry entry : parameters.entrySet()) { + if (newQueryString.length() > 0) { + newQueryString.append("&"); + } + newQueryString.append(entry.getKey()).append("=").append(entry.getValue()); + } + + // Redirect to the URL with only allowed query parameters + String redirectUrl = requestURI + "?" + newQueryString; response.sendRedirect(redirectUrl); return false; } diff --git a/src/main/java/stirling/software/SPDF/config/MetricsFilter.java b/src/main/java/stirling/software/SPDF/config/MetricsFilter.java index d4f64596..87edc0f6 100644 --- a/src/main/java/stirling/software/SPDF/config/MetricsFilter.java +++ b/src/main/java/stirling/software/SPDF/config/MetricsFilter.java @@ -27,7 +27,8 @@ public class MetricsFilter extends OncePerRequestFilter { protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String uri = request.getRequestURI(); - + + //System.out.println("uri="+uri + ", method=" + request.getMethod() ); // Ignore static resources if (!(uri.startsWith("/js") || uri.startsWith("/images") || uri.endsWith(".ico") || uri.endsWith(".css") || uri.endsWith(".svg")|| uri.endsWith(".js") || uri.contains("swagger") || uri.startsWith("/api"))) { Counter counter = Counter.builder("http.requests") @@ -36,6 +37,7 @@ public class MetricsFilter extends OncePerRequestFilter { .register(meterRegistry); counter.increment(); + //System.out.println("Counted"); } filterChain.doFilter(request, response); diff --git a/src/main/java/stirling/software/SPDF/controller/api/filters/FilterController.java b/src/main/java/stirling/software/SPDF/controller/api/filters/FilterController.java index 3a87d566..41ba71d5 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/filters/FilterController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/filters/FilterController.java @@ -37,12 +37,12 @@ public class FilterController { @PostMapping(consumes = "multipart/form-data", value = "/contains-image") @Operation(summary = "Checks if a PDF contains an image", description = "Input:PDF Output:Boolean Type:SISO") - public ResponseEntity containsImage( + public Boolean containsImage( @RequestPart(required = true, value = "fileInput") @Parameter(description = "The input PDF file to be converted to a PDF/A file", required = true) MultipartFile inputFile, @Parameter(description = "The page number to check for image on accepts 'All', ranges like '1-4'", required = false) String pageNumber) throws IOException, InterruptedException { PDDocument pdfDocument = PDDocument.load(inputFile.getInputStream()); - return PdfUtils.hasImagesOnPage(null) + return PdfUtils.hasImagesOnPage(null); } @PostMapping(consumes = "multipart/form-data", value = "/page-count") diff --git a/src/main/java/stirling/software/SPDF/controller/web/MetricsController.java b/src/main/java/stirling/software/SPDF/controller/web/MetricsController.java index 1bea8821..70235df3 100644 --- a/src/main/java/stirling/software/SPDF/controller/web/MetricsController.java +++ b/src/main/java/stirling/software/SPDF/controller/web/MetricsController.java @@ -12,6 +12,7 @@ import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.Meter; import io.micrometer.core.instrument.MeterRegistry; import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; @RestController @@ -38,17 +39,31 @@ public class MetricsController { @GetMapping("/loads") @Operation(summary = "GET request count", description = "This endpoint returns the total count of GET requests or the count of GET requests for a specific endpoint.") - public Double getPageLoads(@RequestParam Optional endpoint) { + public Double getPageLoads(@RequestParam(required = false, name = "endpoint") @Parameter(description = "endpoint") Optional endpoint) { try { - double count = 0.0; + double count = 0.0; + for (Meter meter : meterRegistry.getMeters()) { if (meter.getId().getName().equals("http.requests")) { String method = meter.getId().getTag("method"); if (method != null && method.equals("GET")) { - if (meter instanceof Counter) { - count += ((Counter) meter).count(); - } + + if (endpoint.isPresent() && !endpoint.get().isBlank()) { + if(!endpoint.get().startsWith("/")) { + endpoint = Optional.of("/" + endpoint.get()); + } + System.out.println("loads " + endpoint.get() + " vs " + meter.getId().getTag("uri")); + if(endpoint.get().equals(meter.getId().getTag("uri"))){ + if (meter instanceof Counter) { + count += ((Counter) meter).count(); + } + } + } else { + if (meter instanceof Counter) { + count += ((Counter) meter).count(); + } + } } } } @@ -62,10 +77,15 @@ public class MetricsController { @GetMapping("/requests") @Operation(summary = "POST request count", description = "This endpoint returns the total count of POST requests or the count of POST requests for a specific endpoint.") - public Double getTotalRequests(@RequestParam Optional endpoint) { + public Double getTotalRequests(@RequestParam(required = false, name = "endpoint") @Parameter(description = "endpoint") Optional endpoint) { try { Counter counter; - if (endpoint.isPresent()) { + if (endpoint.isPresent() && !endpoint.get().isBlank()) { + if(!endpoint.get().startsWith("/")) { + endpoint = Optional.of("/" + endpoint.get()); + } + + System.out.println("loads " + endpoint.get() + " vs " + meterRegistry.get("http.requests").tags("uri", endpoint.get()).toString()); counter = meterRegistry.get("http.requests") .tags("method", "POST", "uri", endpoint.get()).counter(); } else { @@ -74,7 +94,8 @@ public class MetricsController { } return counter.count(); } catch (Exception e) { - return -1.0; + e.printStackTrace(); + return 0.0; } }