From 0ebec74beda41fabdf4e63274dbd2e0235f89066 Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Fri, 28 Apr 2023 23:45:54 +0100 Subject: [PATCH] stop annoying fb identify tags in URL --- .../stirling/software/SPDF/config/Beans.java | 1 + .../SPDF/config/CleanUrlInterceptor.java | 44 +++++++++++++++++++ .../SPDF/controller/api/MergeController.java | 12 ++++- 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/main/java/stirling/software/SPDF/config/CleanUrlInterceptor.java diff --git a/src/main/java/stirling/software/SPDF/config/Beans.java b/src/main/java/stirling/software/SPDF/config/Beans.java index a35796ea..a65879d3 100644 --- a/src/main/java/stirling/software/SPDF/config/Beans.java +++ b/src/main/java/stirling/software/SPDF/config/Beans.java @@ -16,6 +16,7 @@ public class Beans implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()); + registry.addInterceptor(new CleanUrlInterceptor()); } @Bean diff --git a/src/main/java/stirling/software/SPDF/config/CleanUrlInterceptor.java b/src/main/java/stirling/software/SPDF/config/CleanUrlInterceptor.java new file mode 100644 index 00000000..3b917664 --- /dev/null +++ b/src/main/java/stirling/software/SPDF/config/CleanUrlInterceptor.java @@ -0,0 +1,44 @@ +package stirling.software.SPDF.config; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.ModelAndView; + +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class CleanUrlInterceptor implements HandlerInterceptor { + + private static final Pattern LANG_PATTERN = Pattern.compile("&?lang=([^&]+)"); + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + String queryString = request.getQueryString(); + 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) : ""; + + // Check if there are any other query parameters besides the lang parameter + String remainingQueryString = queryString.replaceAll(LANG_PATTERN.pattern(), "").replaceAll("&+", "&").replaceAll("^&|&$", ""); + + if (!remainingQueryString.isEmpty()) { + // Redirect to the URL without other query parameters + String redirectUrl = requestURI + (langQueryString.isEmpty() ? "" : "?" + langQueryString); + response.sendRedirect(redirectUrl); + return false; + } + } + return true; + } + + @Override + public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) { + } + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { + } +} diff --git a/src/main/java/stirling/software/SPDF/controller/api/MergeController.java b/src/main/java/stirling/software/SPDF/controller/api/MergeController.java index 186656ff..f8c343e7 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/MergeController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/MergeController.java @@ -37,6 +37,8 @@ public class MergeController { for (PDPage page : pages) { mergedDoc.addPage(page); } + + } // Return the merged document @@ -55,8 +57,16 @@ public class MergeController { PDDocument mergedDoc = mergeDocuments(documents); + // Return the merged PDF as a response - return PdfUtils.pdfDocToWebResponse(mergedDoc, files[0].getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_merged.pdf"); + ResponseEntity response = PdfUtils.pdfDocToWebResponse(mergedDoc, files[0].getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_merged.pdf"); + + for (PDDocument doc : documents) { + // Close the document after processing + doc.close(); + } + + return response; } } \ No newline at end of file