mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2024-11-18 21:30:10 +01:00
Fix metrics
This commit is contained in:
parent
57a0cca595
commit
1798ce002a
@ -1,4 +1,5 @@
|
|||||||
package stirling.software.SPDF.config;
|
package stirling.software.SPDF.config;
|
||||||
|
import java.util.List;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@ -7,10 +8,22 @@ import org.springframework.web.servlet.ModelAndView;
|
|||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
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 {
|
public class CleanUrlInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
private static final Pattern LANG_PATTERN = Pattern.compile("&?lang=([^&]+)");
|
|
||||||
|
private static final List<String> ALLOWED_PARAMS = Arrays.asList("lang", "param2", "param3");
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
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()) {
|
if (queryString != null && !queryString.isEmpty()) {
|
||||||
String requestURI = request.getRequestURI();
|
String requestURI = request.getRequestURI();
|
||||||
|
|
||||||
// Keep the lang parameter if it exists
|
Map<String, String> parameters = new HashMap<>();
|
||||||
Matcher langMatcher = LANG_PATTERN.matcher(queryString);
|
|
||||||
String langQueryString = langMatcher.find() ? "lang=" + langMatcher.group(1) : "";
|
|
||||||
|
|
||||||
// Check if there are any other query parameters besides the lang parameter
|
// Keep only the allowed parameters
|
||||||
String remainingQueryString = queryString.replaceAll(LANG_PATTERN.pattern(), "").replaceAll("&+", "&").replaceAll("^&|&$", "");
|
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()) {
|
// If there are any other query parameters besides the allowed ones
|
||||||
// Redirect to the URL without other query parameters
|
if (parameters.size() > 0) {
|
||||||
String redirectUrl = requestURI + (langQueryString.isEmpty() ? "" : "?" + langQueryString);
|
// Construct new query string
|
||||||
|
StringBuilder newQueryString = new StringBuilder();
|
||||||
|
for (Map.Entry<String, String> 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);
|
response.sendRedirect(redirectUrl);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@ public class MetricsFilter extends OncePerRequestFilter {
|
|||||||
throws ServletException, IOException {
|
throws ServletException, IOException {
|
||||||
String uri = request.getRequestURI();
|
String uri = request.getRequestURI();
|
||||||
|
|
||||||
|
//System.out.println("uri="+uri + ", method=" + request.getMethod() );
|
||||||
// Ignore static resources
|
// 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"))) {
|
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")
|
Counter counter = Counter.builder("http.requests")
|
||||||
@ -36,6 +37,7 @@ public class MetricsFilter extends OncePerRequestFilter {
|
|||||||
.register(meterRegistry);
|
.register(meterRegistry);
|
||||||
|
|
||||||
counter.increment();
|
counter.increment();
|
||||||
|
//System.out.println("Counted");
|
||||||
}
|
}
|
||||||
|
|
||||||
filterChain.doFilter(request, response);
|
filterChain.doFilter(request, response);
|
||||||
|
@ -37,12 +37,12 @@ public class FilterController {
|
|||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/contains-image")
|
@PostMapping(consumes = "multipart/form-data", value = "/contains-image")
|
||||||
@Operation(summary = "Checks if a PDF contains an image", description = "Input:PDF Output:Boolean Type:SISO")
|
@Operation(summary = "Checks if a PDF contains an image", description = "Input:PDF Output:Boolean Type:SISO")
|
||||||
public ResponseEntity<byte[]> 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,
|
@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)
|
@Parameter(description = "The page number to check for image on accepts 'All', ranges like '1-4'", required = false) String pageNumber)
|
||||||
throws IOException, InterruptedException {
|
throws IOException, InterruptedException {
|
||||||
PDDocument pdfDocument = PDDocument.load(inputFile.getInputStream());
|
PDDocument pdfDocument = PDDocument.load(inputFile.getInputStream());
|
||||||
return PdfUtils.hasImagesOnPage(null)
|
return PdfUtils.hasImagesOnPage(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(consumes = "multipart/form-data", value = "/page-count")
|
@PostMapping(consumes = "multipart/form-data", value = "/page-count")
|
||||||
|
@ -12,6 +12,7 @@ import io.micrometer.core.instrument.Counter;
|
|||||||
import io.micrometer.core.instrument.Meter;
|
import io.micrometer.core.instrument.Meter;
|
||||||
import io.micrometer.core.instrument.MeterRegistry;
|
import io.micrometer.core.instrument.MeterRegistry;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.Parameter;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@ -38,17 +39,31 @@ public class MetricsController {
|
|||||||
@GetMapping("/loads")
|
@GetMapping("/loads")
|
||||||
@Operation(summary = "GET request count",
|
@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.")
|
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<String> endpoint) {
|
public Double getPageLoads(@RequestParam(required = false, name = "endpoint") @Parameter(description = "endpoint") Optional<String> endpoint) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
double count = 0.0;
|
double count = 0.0;
|
||||||
|
|
||||||
for (Meter meter : meterRegistry.getMeters()) {
|
for (Meter meter : meterRegistry.getMeters()) {
|
||||||
if (meter.getId().getName().equals("http.requests")) {
|
if (meter.getId().getName().equals("http.requests")) {
|
||||||
String method = meter.getId().getTag("method");
|
String method = meter.getId().getTag("method");
|
||||||
if (method != null && method.equals("GET")) {
|
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")
|
@GetMapping("/requests")
|
||||||
@Operation(summary = "POST request count",
|
@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.")
|
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<String> endpoint) {
|
public Double getTotalRequests(@RequestParam(required = false, name = "endpoint") @Parameter(description = "endpoint") Optional<String> endpoint) {
|
||||||
try {
|
try {
|
||||||
Counter counter;
|
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")
|
counter = meterRegistry.get("http.requests")
|
||||||
.tags("method", "POST", "uri", endpoint.get()).counter();
|
.tags("method", "POST", "uri", endpoint.get()).counter();
|
||||||
} else {
|
} else {
|
||||||
@ -74,7 +94,8 @@ public class MetricsController {
|
|||||||
}
|
}
|
||||||
return counter.count();
|
return counter.count();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return -1.0;
|
e.printStackTrace();
|
||||||
|
return 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user