2023-07-16 19:57:21 +02:00
|
|
|
package stirling.software.SPDF.config;
|
|
|
|
|
|
|
|
import java.util.HashSet;
|
2023-08-26 23:33:23 +02:00
|
|
|
import java.util.List;
|
2023-07-16 19:57:21 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2023-08-26 23:33:23 +02:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2023-07-16 19:57:21 +02:00
|
|
|
import org.springframework.stereotype.Service;
|
2023-08-26 23:33:23 +02:00
|
|
|
|
|
|
|
import stirling.software.SPDF.model.ApplicationProperties;
|
2023-07-16 19:57:21 +02:00
|
|
|
@Service
|
|
|
|
public class EndpointConfiguration {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(EndpointConfiguration.class);
|
|
|
|
private Map<String, Boolean> endpointStatuses = new ConcurrentHashMap<>();
|
|
|
|
private Map<String, Set<String>> endpointGroups = new ConcurrentHashMap<>();
|
|
|
|
|
2023-08-26 23:33:23 +02:00
|
|
|
private final ApplicationProperties applicationProperties;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
public EndpointConfiguration(ApplicationProperties applicationProperties) {
|
|
|
|
this.applicationProperties = applicationProperties;
|
2023-07-16 19:57:21 +02:00
|
|
|
init();
|
|
|
|
processEnvironmentConfigs();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void enableEndpoint(String endpoint) {
|
|
|
|
endpointStatuses.put(endpoint, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void disableEndpoint(String endpoint) {
|
|
|
|
if(!endpointStatuses.containsKey(endpoint) || endpointStatuses.get(endpoint) != false) {
|
|
|
|
logger.info("Disabling {}", endpoint);
|
|
|
|
endpointStatuses.put(endpoint, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isEndpointEnabled(String endpoint) {
|
|
|
|
if (endpoint.startsWith("/")) {
|
|
|
|
endpoint = endpoint.substring(1);
|
|
|
|
}
|
|
|
|
return endpointStatuses.getOrDefault(endpoint, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addEndpointToGroup(String group, String endpoint) {
|
|
|
|
endpointGroups.computeIfAbsent(group, k -> new HashSet<>()).add(endpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void enableGroup(String group) {
|
|
|
|
Set<String> endpoints = endpointGroups.get(group);
|
|
|
|
if (endpoints != null) {
|
|
|
|
for (String endpoint : endpoints) {
|
|
|
|
enableEndpoint(endpoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void disableGroup(String group) {
|
|
|
|
Set<String> endpoints = endpointGroups.get(group);
|
|
|
|
if (endpoints != null) {
|
|
|
|
for (String endpoint : endpoints) {
|
|
|
|
disableEndpoint(endpoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void init() {
|
|
|
|
// Adding endpoints to "PageOps" group
|
|
|
|
addEndpointToGroup("PageOps", "remove-pages");
|
|
|
|
addEndpointToGroup("PageOps", "merge-pdfs");
|
|
|
|
addEndpointToGroup("PageOps", "split-pdfs");
|
|
|
|
addEndpointToGroup("PageOps", "pdf-organizer");
|
|
|
|
addEndpointToGroup("PageOps", "rotate-pdf");
|
|
|
|
addEndpointToGroup("PageOps", "multi-page-layout");
|
|
|
|
addEndpointToGroup("PageOps", "scale-pages");
|
|
|
|
addEndpointToGroup("PageOps", "adjust-contrast");
|
|
|
|
addEndpointToGroup("PageOps", "crop");
|
|
|
|
addEndpointToGroup("PageOps", "auto-split-pdf");
|
2023-08-06 23:14:37 +02:00
|
|
|
addEndpointToGroup("PageOps", "extract-page");
|
|
|
|
addEndpointToGroup("PageOps", "pdf-to-single-page");
|
2023-12-18 15:52:18 +01:00
|
|
|
addEndpointToGroup("PageOps", "split-by-size-or-count");
|
|
|
|
addEndpointToGroup("PageOps", "overlay-pdf");
|
|
|
|
addEndpointToGroup("PageOps", "split-pdf-by-sections");
|
|
|
|
|
2023-07-16 19:57:21 +02:00
|
|
|
|
|
|
|
// Adding endpoints to "Convert" group
|
|
|
|
addEndpointToGroup("Convert", "pdf-to-img");
|
|
|
|
addEndpointToGroup("Convert", "img-to-pdf");
|
|
|
|
addEndpointToGroup("Convert", "pdf-to-pdfa");
|
|
|
|
addEndpointToGroup("Convert", "file-to-pdf");
|
|
|
|
addEndpointToGroup("Convert", "xlsx-to-pdf");
|
|
|
|
addEndpointToGroup("Convert", "pdf-to-word");
|
|
|
|
addEndpointToGroup("Convert", "pdf-to-presentation");
|
|
|
|
addEndpointToGroup("Convert", "pdf-to-text");
|
|
|
|
addEndpointToGroup("Convert", "pdf-to-html");
|
|
|
|
addEndpointToGroup("Convert", "pdf-to-xml");
|
2023-07-23 01:03:25 +02:00
|
|
|
addEndpointToGroup("Convert", "html-to-pdf");
|
|
|
|
addEndpointToGroup("Convert", "url-to-pdf");
|
2023-08-06 23:14:37 +02:00
|
|
|
addEndpointToGroup("Convert", "markdown-to-pdf");
|
2023-12-18 15:52:18 +01:00
|
|
|
addEndpointToGroup("Convert", "pdf-to-csv");
|
|
|
|
|
2023-07-23 01:03:25 +02:00
|
|
|
|
2023-07-16 19:57:21 +02:00
|
|
|
// Adding endpoints to "Security" group
|
|
|
|
addEndpointToGroup("Security", "add-password");
|
|
|
|
addEndpointToGroup("Security", "remove-password");
|
|
|
|
addEndpointToGroup("Security", "change-permissions");
|
|
|
|
addEndpointToGroup("Security", "add-watermark");
|
|
|
|
addEndpointToGroup("Security", "cert-sign");
|
|
|
|
addEndpointToGroup("Security", "sanitize-pdf");
|
2023-12-18 15:52:18 +01:00
|
|
|
addEndpointToGroup("Security", "auto-redact");
|
2023-07-16 19:57:21 +02:00
|
|
|
|
2023-08-06 23:14:37 +02:00
|
|
|
|
2023-07-16 19:57:21 +02:00
|
|
|
// Adding endpoints to "Other" group
|
|
|
|
addEndpointToGroup("Other", "ocr-pdf");
|
|
|
|
addEndpointToGroup("Other", "add-image");
|
|
|
|
addEndpointToGroup("Other", "compress-pdf");
|
|
|
|
addEndpointToGroup("Other", "extract-images");
|
|
|
|
addEndpointToGroup("Other", "change-metadata");
|
|
|
|
addEndpointToGroup("Other", "extract-image-scans");
|
|
|
|
addEndpointToGroup("Other", "sign");
|
|
|
|
addEndpointToGroup("Other", "flatten");
|
|
|
|
addEndpointToGroup("Other", "repair");
|
|
|
|
addEndpointToGroup("Other", "remove-blanks");
|
2023-12-23 19:47:21 +01:00
|
|
|
addEndpointToGroup("Other", "remove-annotations");
|
2023-07-16 19:57:21 +02:00
|
|
|
addEndpointToGroup("Other", "compare");
|
|
|
|
addEndpointToGroup("Other", "add-page-numbers");
|
|
|
|
addEndpointToGroup("Other", "auto-rename");
|
2023-08-06 23:14:37 +02:00
|
|
|
addEndpointToGroup("Other", "get-info-on-pdf");
|
|
|
|
addEndpointToGroup("Other", "show-javascript");
|
2023-07-16 19:57:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//CLI
|
|
|
|
addEndpointToGroup("CLI", "compress-pdf");
|
|
|
|
addEndpointToGroup("CLI", "extract-image-scans");
|
|
|
|
addEndpointToGroup("CLI", "remove-blanks");
|
|
|
|
addEndpointToGroup("CLI", "repair");
|
|
|
|
addEndpointToGroup("CLI", "pdf-to-pdfa");
|
|
|
|
addEndpointToGroup("CLI", "file-to-pdf");
|
|
|
|
addEndpointToGroup("CLI", "xlsx-to-pdf");
|
|
|
|
addEndpointToGroup("CLI", "pdf-to-word");
|
|
|
|
addEndpointToGroup("CLI", "pdf-to-presentation");
|
|
|
|
addEndpointToGroup("CLI", "pdf-to-text");
|
|
|
|
addEndpointToGroup("CLI", "pdf-to-html");
|
|
|
|
addEndpointToGroup("CLI", "pdf-to-xml");
|
|
|
|
addEndpointToGroup("CLI", "ocr-pdf");
|
2023-07-23 01:03:25 +02:00
|
|
|
addEndpointToGroup("CLI", "html-to-pdf");
|
|
|
|
addEndpointToGroup("CLI", "url-to-pdf");
|
|
|
|
|
2023-07-16 19:57:21 +02:00
|
|
|
|
|
|
|
//python
|
|
|
|
addEndpointToGroup("Python", "extract-image-scans");
|
|
|
|
addEndpointToGroup("Python", "remove-blanks");
|
2023-07-23 01:03:25 +02:00
|
|
|
addEndpointToGroup("Python", "html-to-pdf");
|
|
|
|
addEndpointToGroup("Python", "url-to-pdf");
|
2023-07-16 19:57:21 +02:00
|
|
|
|
|
|
|
//openCV
|
|
|
|
addEndpointToGroup("OpenCV", "extract-image-scans");
|
|
|
|
addEndpointToGroup("OpenCV", "remove-blanks");
|
|
|
|
|
|
|
|
//LibreOffice
|
|
|
|
addEndpointToGroup("LibreOffice", "repair");
|
|
|
|
addEndpointToGroup("LibreOffice", "file-to-pdf");
|
|
|
|
addEndpointToGroup("LibreOffice", "xlsx-to-pdf");
|
|
|
|
addEndpointToGroup("LibreOffice", "pdf-to-word");
|
|
|
|
addEndpointToGroup("LibreOffice", "pdf-to-presentation");
|
|
|
|
addEndpointToGroup("LibreOffice", "pdf-to-text");
|
|
|
|
addEndpointToGroup("LibreOffice", "pdf-to-html");
|
|
|
|
addEndpointToGroup("LibreOffice", "pdf-to-xml");
|
|
|
|
|
|
|
|
|
|
|
|
//OCRmyPDF
|
|
|
|
addEndpointToGroup("OCRmyPDF", "compress-pdf");
|
|
|
|
addEndpointToGroup("OCRmyPDF", "pdf-to-pdfa");
|
|
|
|
addEndpointToGroup("OCRmyPDF", "ocr-pdf");
|
|
|
|
|
|
|
|
//Java
|
|
|
|
addEndpointToGroup("Java", "merge-pdfs");
|
|
|
|
addEndpointToGroup("Java", "remove-pages");
|
|
|
|
addEndpointToGroup("Java", "split-pdfs");
|
|
|
|
addEndpointToGroup("Java", "pdf-organizer");
|
|
|
|
addEndpointToGroup("Java", "rotate-pdf");
|
|
|
|
addEndpointToGroup("Java", "pdf-to-img");
|
|
|
|
addEndpointToGroup("Java", "img-to-pdf");
|
|
|
|
addEndpointToGroup("Java", "add-password");
|
|
|
|
addEndpointToGroup("Java", "remove-password");
|
|
|
|
addEndpointToGroup("Java", "change-permissions");
|
|
|
|
addEndpointToGroup("Java", "add-watermark");
|
|
|
|
addEndpointToGroup("Java", "add-image");
|
|
|
|
addEndpointToGroup("Java", "extract-images");
|
|
|
|
addEndpointToGroup("Java", "change-metadata");
|
|
|
|
addEndpointToGroup("Java", "cert-sign");
|
|
|
|
addEndpointToGroup("Java", "multi-page-layout");
|
|
|
|
addEndpointToGroup("Java", "scale-pages");
|
|
|
|
addEndpointToGroup("Java", "add-page-numbers");
|
|
|
|
addEndpointToGroup("Java", "auto-rename");
|
|
|
|
addEndpointToGroup("Java", "auto-split-pdf");
|
|
|
|
addEndpointToGroup("Java", "sanitize-pdf");
|
|
|
|
addEndpointToGroup("Java", "crop");
|
2023-08-06 23:14:37 +02:00
|
|
|
addEndpointToGroup("Java", "get-info-on-pdf");
|
|
|
|
addEndpointToGroup("Java", "extract-page");
|
|
|
|
addEndpointToGroup("Java", "pdf-to-single-page");
|
|
|
|
addEndpointToGroup("Java", "markdown-to-pdf");
|
|
|
|
addEndpointToGroup("Java", "show-javascript");
|
2023-12-18 15:52:18 +01:00
|
|
|
addEndpointToGroup("Java", "auto-redact");
|
|
|
|
addEndpointToGroup("Java", "pdf-to-csv");
|
|
|
|
addEndpointToGroup("Java", "split-by-size-or-count");
|
|
|
|
addEndpointToGroup("Java", "overlay-pdf");
|
|
|
|
addEndpointToGroup("Java", "split-pdf-by-sections");
|
2023-07-16 19:57:21 +02:00
|
|
|
|
|
|
|
//Javascript
|
|
|
|
addEndpointToGroup("Javascript", "pdf-organizer");
|
|
|
|
addEndpointToGroup("Javascript", "sign");
|
|
|
|
addEndpointToGroup("Javascript", "compare");
|
|
|
|
addEndpointToGroup("Javascript", "adjust-contrast");
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2023-08-26 23:33:23 +02:00
|
|
|
|
2023-07-16 19:57:21 +02:00
|
|
|
private void processEnvironmentConfigs() {
|
2023-08-26 23:33:23 +02:00
|
|
|
List<String> endpointsToRemove = applicationProperties.getEndpoints().getToRemove();
|
|
|
|
List<String> groupsToRemove = applicationProperties.getEndpoints().getGroupsToRemove();
|
2023-07-16 19:57:21 +02:00
|
|
|
|
|
|
|
if (endpointsToRemove != null) {
|
2023-08-26 23:33:23 +02:00
|
|
|
for (String endpoint : endpointsToRemove) {
|
2023-07-16 19:57:21 +02:00
|
|
|
disableEndpoint(endpoint.trim());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupsToRemove != null) {
|
2023-08-26 23:33:23 +02:00
|
|
|
for (String group : groupsToRemove) {
|
2023-07-16 19:57:21 +02:00
|
|
|
disableGroup(group.trim());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|