This commit is contained in:
Anthony Stirling 2023-12-31 13:34:35 +00:00
parent d83bd1ae94
commit 39045df785
5 changed files with 201 additions and 194 deletions

View File

@ -102,8 +102,9 @@ public class SecurityConfiguration {
|| trimmedUri.startsWith("/images/") || trimmedUri.startsWith("/images/")
|| trimmedUri.startsWith("/public/") || trimmedUri.startsWith("/public/")
|| trimmedUri.startsWith("/css/") || trimmedUri.startsWith("/css/")
|| trimmedUri.startsWith("/js/") || || trimmedUri.startsWith("/js/")
trimmedUri.startsWith("/api/v1/info/status"); || trimmedUri.startsWith(
"/api/v1/info/status");
}) })
.permitAll() .permitAll()
.anyRequest() .anyRequest()

View File

@ -95,16 +95,16 @@ public class UserAuthenticationFilter extends OncePerRequestFilter {
String uri = request.getRequestURI(); String uri = request.getRequestURI();
String contextPath = request.getContextPath(); String contextPath = request.getContextPath();
String[] permitAllPatterns = { String[] permitAllPatterns = {
contextPath + "/login", contextPath + "/login",
contextPath + "/register", contextPath + "/register",
contextPath + "/error", contextPath + "/error",
contextPath + "/images/", contextPath + "/images/",
contextPath + "/public/", contextPath + "/public/",
contextPath + "/css/", contextPath + "/css/",
contextPath + "/js/", contextPath + "/js/",
contextPath + "/pdfjs/", contextPath + "/pdfjs/",
contextPath + "/api/v1/info/status", contextPath + "/api/v1/info/status",
contextPath + "/site.webmanifest" contextPath + "/site.webmanifest"
}; };
for (String pattern : permitAllPatterns) { for (String pattern : permitAllPatterns) {

View File

@ -49,145 +49,153 @@ public class PipelineProcessor {
@Autowired(required = false) @Autowired(required = false)
private UserServiceInterface userService; private UserServiceInterface userService;
@Autowired @Autowired private ServletContext servletContext;
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();
return "http://localhost:" + port + contextPath + "/";
}
private String getApiKeyForUser() { List<Resource> runPipelineAgainstFiles(List<Resource> outputFiles, PipelineConfig config)
if (userService == null) throws Exception {
return "";
return userService.getApiKeyForUser(Role.INTERNAL_API_USER.getRoleId());
}
ByteArrayOutputStream logStream = new ByteArrayOutputStream();
PrintStream logPrintStream = new PrintStream(logStream);
private String getBaseUrl() { boolean hasErrors = false;
String contextPath = servletContext.getContextPath();
String port = SPdfApplication.getPort();
return "http://localhost:" + port + contextPath + "/"; for (PipelineOperation pipelineOperation : config.getOperations()) {
} String operation = pipelineOperation.getOperation();
boolean isMultiInputOperation = apiDocService.isMultiInput(operation);
logger.info(
"Running operation: {} isMultiInputOperation {}",
operation,
isMultiInputOperation);
Map<String, Object> parameters = pipelineOperation.getParameters();
String inputFileExtension = "";
// TODO
// if (operationNode.has("inputFileType")) {
// inputFileExtension = operationNode.get("inputFileType").asText();
// } else {
inputFileExtension = ".pdf";
// }
final String finalInputFileExtension = inputFileExtension;
List<Resource> runPipelineAgainstFiles(List<Resource> outputFiles, PipelineConfig config) throws Exception { String url = getBaseUrl() + operation;
ByteArrayOutputStream logStream = new ByteArrayOutputStream(); List<Resource> newOutputFiles = new ArrayList<>();
PrintStream logPrintStream = new PrintStream(logStream); if (!isMultiInputOperation) {
for (Resource file : outputFiles) {
boolean hasInputFileType = false;
if (file.getFilename().endsWith(inputFileExtension)) {
hasInputFileType = true;
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("fileInput", file);
boolean hasErrors = false; for (Entry<String, Object> entry : parameters.entrySet()) {
body.add(entry.getKey(), entry.getValue());
}
for (PipelineOperation pipelineOperation : config.getOperations()) { ResponseEntity<byte[]> response = sendWebRequest(url, body);
String operation = pipelineOperation.getOperation();
boolean isMultiInputOperation = apiDocService.isMultiInput(operation);
logger.info("Running operation: {} isMultiInputOperation {}", operation, isMultiInputOperation); // If the operation is filter and the response body is null or empty, skip
Map<String, Object> parameters = pipelineOperation.getParameters(); // this
String inputFileExtension = ""; // file
if (operation.startsWith("filter-")
&& (response.getBody() == null || response.getBody().length == 0)) {
logger.info("Skipping file due to failing {}", operation);
continue;
}
//TODO if (!response.getStatusCode().equals(HttpStatus.OK)) {
//if (operationNode.has("inputFileType")) { logPrintStream.println("Error: " + response.getBody());
// inputFileExtension = operationNode.get("inputFileType").asText(); hasErrors = true;
//} else { continue;
inputFileExtension = ".pdf"; }
//} processOutputFiles(operation, file.getFilename(), response, newOutputFiles);
final String finalInputFileExtension = inputFileExtension; }
String url = getBaseUrl() + operation; if (!hasInputFileType) {
logPrintStream.println(
"No files with extension "
+ inputFileExtension
+ " found for operation "
+ operation);
hasErrors = true;
}
}
List<Resource> newOutputFiles = new ArrayList<>(); } else {
if (!isMultiInputOperation) { // Filter and collect all files that match the inputFileExtension
for (Resource file : outputFiles) { List<Resource> matchingFiles =
boolean hasInputFileType = false; outputFiles.stream()
if (file.getFilename().endsWith(inputFileExtension)) { .filter(
hasInputFileType = true; file ->
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); file.getFilename()
body.add("fileInput", file); .endsWith(finalInputFileExtension))
.collect(Collectors.toList());
// Check if there are matching files
if (!matchingFiles.isEmpty()) {
// Create a new MultiValueMap for the request body
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
for(Entry<String, Object> entry : parameters.entrySet()) { // Add all matching files to the body
body.add(entry.getKey(), entry.getValue()); for (Resource file : matchingFiles) {
} body.add("fileInput", file);
}
ResponseEntity<byte[]> response = sendWebRequest(url, body); for (Entry<String, Object> entry : parameters.entrySet()) {
body.add(entry.getKey(), entry.getValue());
}
// If the operation is filter and the response body is null or empty, skip this ResponseEntity<byte[]> response = sendWebRequest(url, body);
// 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)) { // Handle the response
logPrintStream.println("Error: " + response.getBody()); if (response.getStatusCode().equals(HttpStatus.OK)) {
hasErrors = true; processOutputFiles(
continue; operation,
} matchingFiles.get(0).getFilename(),
processOutputFiles(operation, file.getFilename(), response, newOutputFiles); 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());
}
} return outputFiles;
}
if (!hasInputFileType) { private ResponseEntity<byte[]> sendWebRequest(String url, MultiValueMap<String, Object> body) {
logPrintStream.println( RestTemplate restTemplate = new RestTemplate();
"No files with extension " + inputFileExtension + " found for operation " + operation);
hasErrors = true;
}
// Set up headers, including API key
}
} else {
// Filter and collect all files that match the inputFileExtension
List<Resource> 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<String, Object> body = new LinkedMultiValueMap<>();
// Add all matching files to the body
for (Resource file : matchingFiles) {
body.add("fileInput", file);
}
for(Entry<String, Object> entry : parameters.entrySet()) {
body.add(entry.getKey(), entry.getValue());
}
ResponseEntity<byte[]> 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;
}
if (hasErrors) {
logger.error("Errors occurred during processing. Log: {}", logStream.toString());
}
return outputFiles;
}
private ResponseEntity<byte[]> sendWebRequest(String url, MultiValueMap<String, Object> body ){
RestTemplate restTemplate = new RestTemplate();
// Set up headers, including API key
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
String apiKey = getApiKeyForUser(); String apiKey = getApiKeyForUser();

View File

@ -33,66 +33,65 @@ import io.swagger.v3.oas.annotations.tags.Tag;
@Tag(name = "General", description = "General APIs") @Tag(name = "General", description = "General APIs")
public class GeneralWebController { public class GeneralWebController {
@GetMapping("/pipeline")
@Hidden
public String pipelineForm(Model model) {
model.addAttribute("currentPage", "pipeline");
@GetMapping("/pipeline") List<String> pipelineConfigs = new ArrayList<>();
@Hidden List<Map<String, String>> pipelineConfigsWithNames = new ArrayList<>();
public String pipelineForm(Model model) {
model.addAttribute("currentPage", "pipeline");
List<String> pipelineConfigs = new ArrayList<>(); if (new File("./pipeline/defaultWebUIConfigs/").exists()) {
List<Map<String, String>> pipelineConfigsWithNames = new ArrayList<>(); try (Stream<Path> paths = Files.walk(Paths.get("./pipeline/defaultWebUIConfigs/"))) {
List<Path> jsonFiles =
paths.filter(Files::isRegularFile)
.filter(p -> p.toString().endsWith(".json"))
.collect(Collectors.toList());
if(new File("./pipeline/defaultWebUIConfigs/").exists()) { for (Path jsonFile : jsonFiles) {
try (Stream<Path> paths = Files.walk(Paths.get("./pipeline/defaultWebUIConfigs/"))) { String content = Files.readString(jsonFile, StandardCharsets.UTF_8);
List<Path> jsonFiles = paths pipelineConfigs.add(content);
.filter(Files::isRegularFile) }
.filter(p -> p.toString().endsWith(".json"))
.collect(Collectors.toList());
for (Path jsonFile : jsonFiles) { for (String config : pipelineConfigs) {
String content = Files.readString(jsonFile, StandardCharsets.UTF_8); Map<String, Object> jsonContent =
pipelineConfigs.add(content); new ObjectMapper()
} .readValue(config, new TypeReference<Map<String, Object>>() {});
for (String config : pipelineConfigs) { String name = (String) jsonContent.get("name");
Map<String, Object> jsonContent = new ObjectMapper().readValue(config, new TypeReference<Map<String, Object>>(){}); if (name == null || name.length() < 1) {
String filename =
jsonFiles
.get(pipelineConfigs.indexOf(config))
.getFileName()
.toString();
name = filename.substring(0, filename.lastIndexOf('.'));
}
Map<String, String> configWithName = new HashMap<>();
System.out.println("json" + config);
String name = (String) jsonContent.get("name"); System.out.println("name" + name);
if(name == null || name.length() < 1) { configWithName.put("json", config);
String filename = jsonFiles.get(pipelineConfigs.indexOf(config)).getFileName().toString(); configWithName.put("name", name);
name = filename.substring(0, filename.lastIndexOf('.')); pipelineConfigsWithNames.add(configWithName);
} }
Map<String, String> 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<String, String> 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";
}
} catch (IOException e) {
e.printStackTrace();
}
}
if (pipelineConfigsWithNames.size() == 0) {
Map<String, String> 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") @GetMapping("/merge-pdfs")
@Hidden @Hidden

View File

@ -2,15 +2,14 @@ package stirling.software.SPDF.utils;
public class RequestUriUtils { public class RequestUriUtils {
public static boolean isStaticResource(String requestURI) { 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");
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");
} }
} }