From 39045df7858d31076530f70f7c58e27101156ede Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Sun, 31 Dec 2023 13:34:35 +0000 Subject: [PATCH] formats --- .../security/SecurityConfiguration.java | 5 +- .../security/UserAuthenticationFilter.java | 20 +- .../api/pipeline/PipelineProcessor.java | 238 +++++++++--------- .../controller/web/GeneralWebController.java | 113 +++++---- .../software/SPDF/utils/RequestUriUtils.java | 19 +- 5 files changed, 201 insertions(+), 194 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/config/security/SecurityConfiguration.java b/src/main/java/stirling/software/SPDF/config/security/SecurityConfiguration.java index a2908356..bf1e3661 100644 --- a/src/main/java/stirling/software/SPDF/config/security/SecurityConfiguration.java +++ b/src/main/java/stirling/software/SPDF/config/security/SecurityConfiguration.java @@ -102,8 +102,9 @@ public class SecurityConfiguration { || trimmedUri.startsWith("/images/") || trimmedUri.startsWith("/public/") || trimmedUri.startsWith("/css/") - || trimmedUri.startsWith("/js/") || - trimmedUri.startsWith("/api/v1/info/status"); + || trimmedUri.startsWith("/js/") + || trimmedUri.startsWith( + "/api/v1/info/status"); }) .permitAll() .anyRequest() diff --git a/src/main/java/stirling/software/SPDF/config/security/UserAuthenticationFilter.java b/src/main/java/stirling/software/SPDF/config/security/UserAuthenticationFilter.java index 2a4c68d5..d12fc72b 100644 --- a/src/main/java/stirling/software/SPDF/config/security/UserAuthenticationFilter.java +++ b/src/main/java/stirling/software/SPDF/config/security/UserAuthenticationFilter.java @@ -95,16 +95,16 @@ public class UserAuthenticationFilter extends OncePerRequestFilter { String uri = request.getRequestURI(); String contextPath = request.getContextPath(); String[] permitAllPatterns = { - contextPath + "/login", - contextPath + "/register", - contextPath + "/error", - contextPath + "/images/", - contextPath + "/public/", - contextPath + "/css/", - contextPath + "/js/", - contextPath + "/pdfjs/", - contextPath + "/api/v1/info/status", - contextPath + "/site.webmanifest" + contextPath + "/login", + contextPath + "/register", + contextPath + "/error", + contextPath + "/images/", + contextPath + "/public/", + contextPath + "/css/", + contextPath + "/js/", + contextPath + "/pdfjs/", + contextPath + "/api/v1/info/status", + contextPath + "/site.webmanifest" }; for (String pattern : permitAllPatterns) { diff --git a/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineProcessor.java b/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineProcessor.java index f87cf118..9bebb96f 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineProcessor.java +++ b/src/main/java/stirling/software/SPDF/controller/api/pipeline/PipelineProcessor.java @@ -49,145 +49,153 @@ public class PipelineProcessor { @Autowired(required = false) private UserServiceInterface userService; - @Autowired - private ServletContext servletContext; + @Autowired private ServletContext servletContext; - + private String getApiKeyForUser() { + if (userService == null) return ""; + return userService.getApiKeyForUser(Role.INTERNAL_API_USER.getRoleId()); + } + private String getBaseUrl() { + String contextPath = servletContext.getContextPath(); + String port = SPdfApplication.getPort(); - private String getApiKeyForUser() { - if (userService == null) - return ""; - return userService.getApiKeyForUser(Role.INTERNAL_API_USER.getRoleId()); - } + return "http://localhost:" + port + contextPath + "/"; + } + List runPipelineAgainstFiles(List outputFiles, PipelineConfig config) + throws Exception { - private String getBaseUrl() { - String contextPath = servletContext.getContextPath(); - String port = SPdfApplication.getPort(); + ByteArrayOutputStream logStream = new ByteArrayOutputStream(); + PrintStream logPrintStream = new PrintStream(logStream); - return "http://localhost:" + port + contextPath + "/"; - } + boolean hasErrors = false; - - - List runPipelineAgainstFiles(List outputFiles, PipelineConfig config) throws Exception { + for (PipelineOperation pipelineOperation : config.getOperations()) { + String operation = pipelineOperation.getOperation(); + boolean isMultiInputOperation = apiDocService.isMultiInput(operation); - ByteArrayOutputStream logStream = new ByteArrayOutputStream(); - PrintStream logPrintStream = new PrintStream(logStream); + logger.info( + "Running operation: {} isMultiInputOperation {}", + operation, + isMultiInputOperation); + Map parameters = pipelineOperation.getParameters(); + String inputFileExtension = ""; - boolean hasErrors = false; + // TODO + // if (operationNode.has("inputFileType")) { + // inputFileExtension = operationNode.get("inputFileType").asText(); + // } else { + inputFileExtension = ".pdf"; + // } + final String finalInputFileExtension = inputFileExtension; - for (PipelineOperation pipelineOperation : config.getOperations()) { - String operation = pipelineOperation.getOperation(); - boolean isMultiInputOperation = apiDocService.isMultiInput(operation); + String url = getBaseUrl() + operation; - logger.info("Running operation: {} isMultiInputOperation {}", operation, isMultiInputOperation); - Map parameters = pipelineOperation.getParameters(); - String inputFileExtension = ""; - - //TODO - //if (operationNode.has("inputFileType")) { - // inputFileExtension = operationNode.get("inputFileType").asText(); - //} else { - inputFileExtension = ".pdf"; - //} - final String finalInputFileExtension = inputFileExtension; - - String url = getBaseUrl() + operation; - - List newOutputFiles = new ArrayList<>(); - if (!isMultiInputOperation) { - for (Resource file : outputFiles) { - boolean hasInputFileType = false; - if (file.getFilename().endsWith(inputFileExtension)) { - hasInputFileType = true; - MultiValueMap body = new LinkedMultiValueMap<>(); - body.add("fileInput", file); + List newOutputFiles = new ArrayList<>(); + if (!isMultiInputOperation) { + for (Resource file : outputFiles) { + boolean hasInputFileType = false; + if (file.getFilename().endsWith(inputFileExtension)) { + hasInputFileType = true; + MultiValueMap body = new LinkedMultiValueMap<>(); + body.add("fileInput", file); - - for(Entry entry : parameters.entrySet()) { - body.add(entry.getKey(), entry.getValue()); - } + for (Entry entry : parameters.entrySet()) { + body.add(entry.getKey(), entry.getValue()); + } - ResponseEntity response = sendWebRequest(url, body); + ResponseEntity response = sendWebRequest(url, body); - // If the operation is filter and the response body is null or empty, skip this - // file - if (operation.startsWith("filter-") - && (response.getBody() == null || response.getBody().length == 0)) { - logger.info("Skipping file due to failing {}", operation); - continue; - } + // If the operation is filter and the response body is null or empty, skip + // this + // file + if (operation.startsWith("filter-") + && (response.getBody() == null || response.getBody().length == 0)) { + logger.info("Skipping file due to failing {}", operation); + continue; + } - if (!response.getStatusCode().equals(HttpStatus.OK)) { - logPrintStream.println("Error: " + response.getBody()); - hasErrors = true; - continue; - } - processOutputFiles(operation, file.getFilename(), response, newOutputFiles); - - } + if (!response.getStatusCode().equals(HttpStatus.OK)) { + logPrintStream.println("Error: " + response.getBody()); + hasErrors = true; + continue; + } + processOutputFiles(operation, file.getFilename(), response, newOutputFiles); + } - if (!hasInputFileType) { - logPrintStream.println( - "No files with extension " + inputFileExtension + " found for operation " + operation); - hasErrors = true; - } + if (!hasInputFileType) { + logPrintStream.println( + "No files with extension " + + inputFileExtension + + " found for operation " + + operation); + hasErrors = true; + } + } - - } + } else { + // Filter and collect all files that match the inputFileExtension + List matchingFiles = + outputFiles.stream() + .filter( + file -> + file.getFilename() + .endsWith(finalInputFileExtension)) + .collect(Collectors.toList()); - } else { - // Filter and collect all files that match the inputFileExtension - List matchingFiles = outputFiles.stream() - .filter(file -> file.getFilename().endsWith(finalInputFileExtension)) - .collect(Collectors.toList()); + // Check if there are matching files + if (!matchingFiles.isEmpty()) { + // Create a new MultiValueMap for the request body + MultiValueMap body = new LinkedMultiValueMap<>(); - // Check if there are matching files - if (!matchingFiles.isEmpty()) { - // Create a new MultiValueMap for the request body - MultiValueMap body = new LinkedMultiValueMap<>(); + // Add all matching files to the body + for (Resource file : matchingFiles) { + body.add("fileInput", file); + } - // Add all matching files to the body - for (Resource file : matchingFiles) { - body.add("fileInput", file); - } + for (Entry entry : parameters.entrySet()) { + body.add(entry.getKey(), entry.getValue()); + } - for(Entry entry : parameters.entrySet()) { - body.add(entry.getKey(), entry.getValue()); - } - - ResponseEntity response = sendWebRequest(url, body); + ResponseEntity response = sendWebRequest(url, body); - // Handle the response - if (response.getStatusCode().equals(HttpStatus.OK)) { - processOutputFiles(operation, matchingFiles.get(0).getFilename(), response, newOutputFiles); - } else { - // Log error if the response status is not OK - logPrintStream.println("Error in multi-input operation: " + response.getBody()); - hasErrors = true; - } - } else { - logPrintStream.println("No files with extension " + inputFileExtension + " found for multi-input operation " + operation); - hasErrors = true; - } - } - logPrintStream.close(); - outputFiles = newOutputFiles; + // Handle the response + if (response.getStatusCode().equals(HttpStatus.OK)) { + processOutputFiles( + operation, + matchingFiles.get(0).getFilename(), + response, + newOutputFiles); + } else { + // Log error if the response status is not OK + logPrintStream.println( + "Error in multi-input operation: " + response.getBody()); + hasErrors = true; + } + } else { + logPrintStream.println( + "No files with extension " + + inputFileExtension + + " found for multi-input operation " + + operation); + hasErrors = true; + } + } + logPrintStream.close(); + outputFiles = newOutputFiles; + } + if (hasErrors) { + logger.error("Errors occurred during processing. Log: {}", logStream.toString()); + } - } - if (hasErrors) { - logger.error("Errors occurred during processing. Log: {}", logStream.toString()); - } - - return outputFiles; - } + return outputFiles; + } - private ResponseEntity sendWebRequest(String url, MultiValueMap body ){ - RestTemplate restTemplate = new RestTemplate(); - - // Set up headers, including API key + private ResponseEntity sendWebRequest(String url, MultiValueMap body) { + RestTemplate restTemplate = new RestTemplate(); + + // Set up headers, including API key HttpHeaders headers = new HttpHeaders(); String apiKey = getApiKeyForUser(); diff --git a/src/main/java/stirling/software/SPDF/controller/web/GeneralWebController.java b/src/main/java/stirling/software/SPDF/controller/web/GeneralWebController.java index ecdbe22d..483053da 100644 --- a/src/main/java/stirling/software/SPDF/controller/web/GeneralWebController.java +++ b/src/main/java/stirling/software/SPDF/controller/web/GeneralWebController.java @@ -33,67 +33,66 @@ import io.swagger.v3.oas.annotations.tags.Tag; @Tag(name = "General", description = "General APIs") public class GeneralWebController { - - @GetMapping("/pipeline") - @Hidden - public String pipelineForm(Model model) { - model.addAttribute("currentPage", "pipeline"); + @GetMapping("/pipeline") + @Hidden + public String pipelineForm(Model model) { + model.addAttribute("currentPage", "pipeline"); - List pipelineConfigs = new ArrayList<>(); - List> pipelineConfigsWithNames = new ArrayList<>(); - - if(new File("./pipeline/defaultWebUIConfigs/").exists()) { - try (Stream paths = Files.walk(Paths.get("./pipeline/defaultWebUIConfigs/"))) { - List jsonFiles = paths - .filter(Files::isRegularFile) - .filter(p -> p.toString().endsWith(".json")) - .collect(Collectors.toList()); - - for (Path jsonFile : jsonFiles) { - String content = Files.readString(jsonFile, StandardCharsets.UTF_8); - pipelineConfigs.add(content); - } - - for (String config : pipelineConfigs) { - Map jsonContent = new ObjectMapper().readValue(config, new TypeReference>(){}); - - String name = (String) jsonContent.get("name"); - if(name == null || name.length() < 1) { - String filename = jsonFiles.get(pipelineConfigs.indexOf(config)).getFileName().toString(); - name = filename.substring(0, filename.lastIndexOf('.')); - } - Map configWithName = new HashMap<>(); - System.out.println("json" + config); - - System.out.println("name" + name); - configWithName.put("json", config); - configWithName.put("name", name); - pipelineConfigsWithNames.add(configWithName); - } - - - - - } catch (IOException e) { - e.printStackTrace(); - } - } - if(pipelineConfigsWithNames.size() == 0) { - Map configWithName = new HashMap<>(); - configWithName.put("json", ""); - configWithName.put("name", "No preloaded configs found"); - pipelineConfigsWithNames.add(configWithName); - } - model.addAttribute("pipelineConfigsWithNames", pipelineConfigsWithNames); + List pipelineConfigs = new ArrayList<>(); + List> pipelineConfigsWithNames = new ArrayList<>(); - model.addAttribute("pipelineConfigs", pipelineConfigs); + if (new File("./pipeline/defaultWebUIConfigs/").exists()) { + try (Stream paths = Files.walk(Paths.get("./pipeline/defaultWebUIConfigs/"))) { + List jsonFiles = + paths.filter(Files::isRegularFile) + .filter(p -> p.toString().endsWith(".json")) + .collect(Collectors.toList()); - return "pipeline"; - } + for (Path jsonFile : jsonFiles) { + String content = Files.readString(jsonFile, StandardCharsets.UTF_8); + pipelineConfigs.add(content); + } + + for (String config : pipelineConfigs) { + Map jsonContent = + new ObjectMapper() + .readValue(config, new TypeReference>() {}); + + String name = (String) jsonContent.get("name"); + if (name == null || name.length() < 1) { + String filename = + jsonFiles + .get(pipelineConfigs.indexOf(config)) + .getFileName() + .toString(); + name = filename.substring(0, filename.lastIndexOf('.')); + } + Map configWithName = new HashMap<>(); + System.out.println("json" + config); + + System.out.println("name" + name); + configWithName.put("json", config); + configWithName.put("name", name); + pipelineConfigsWithNames.add(configWithName); + } + + } catch (IOException e) { + e.printStackTrace(); + } + } + if (pipelineConfigsWithNames.size() == 0) { + Map configWithName = new HashMap<>(); + configWithName.put("json", ""); + configWithName.put("name", "No preloaded configs found"); + pipelineConfigsWithNames.add(configWithName); + } + model.addAttribute("pipelineConfigsWithNames", pipelineConfigsWithNames); + + model.addAttribute("pipelineConfigs", pipelineConfigs); + + return "pipeline"; + } - - - @GetMapping("/merge-pdfs") @Hidden public String mergePdfForm(Model model) { diff --git a/src/main/java/stirling/software/SPDF/utils/RequestUriUtils.java b/src/main/java/stirling/software/SPDF/utils/RequestUriUtils.java index e52547cf..21324a9c 100644 --- a/src/main/java/stirling/software/SPDF/utils/RequestUriUtils.java +++ b/src/main/java/stirling/software/SPDF/utils/RequestUriUtils.java @@ -2,15 +2,14 @@ package stirling.software.SPDF.utils; public class RequestUriUtils { - public static boolean isStaticResource(String requestURI) { - - return requestURI.startsWith("/css/") - || requestURI.startsWith("/js/") - || requestURI.startsWith("/images/") - || requestURI.startsWith("/public/") - || requestURI.startsWith("/pdfjs/") - || requestURI.endsWith(".svg") - || requestURI.startsWith("/api/v1/info/status"); - + public static boolean isStaticResource(String requestURI) { + + return requestURI.startsWith("/css/") + || requestURI.startsWith("/js/") + || requestURI.startsWith("/images/") + || requestURI.startsWith("/public/") + || requestURI.startsWith("/pdfjs/") + || requestURI.endsWith(".svg") + || requestURI.startsWith("/api/v1/info/status"); } }