mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-16 11:23:10 +02:00
Inform AI engine which endpoints are disabled on the backend (#6251)
# Description of Changes Have the Java send a list of enabled endpoints to the AI engine so it can intelligently respond to the user that the tool does exist but is disabled on the server so it can't acutally run the operation, instead of the current behaviour where it sends the API call back and then 503 errors because the execution fails when the URL is disabled. <img width="380" height="208" alt="image" src="https://github.com/user-attachments/assets/5842fb2e-2e55-45a5-8205-25515636daae" /> --------- Co-authored-by: EthanHealy01 <[email protected]>
This commit is contained in:
co-authored by
EthanHealy01
parent
5541dd666c
commit
51f5345151
@@ -21,28 +21,7 @@ public class EndpointInterceptor implements HandlerInterceptor {
|
||||
HttpServletRequest request, HttpServletResponse response, Object handler)
|
||||
throws Exception {
|
||||
String requestURI = request.getRequestURI();
|
||||
boolean isEnabled;
|
||||
|
||||
// Extract the specific endpoint name (e.g: /api/v1/general/remove-pages -> remove-pages)
|
||||
if (requestURI.contains("/api/v1") && requestURI.split("/").length > 4) {
|
||||
|
||||
String[] requestURIParts = requestURI.split("/");
|
||||
String requestEndpoint;
|
||||
|
||||
// Endpoint: /api/v1/convert/pdf/img becomes pdf-to-img
|
||||
if ("convert".equals(requestURIParts[3]) && requestURIParts.length > 5) {
|
||||
requestEndpoint = requestURIParts[4] + "-to-" + requestURIParts[5];
|
||||
} else {
|
||||
requestEndpoint = requestURIParts[4];
|
||||
}
|
||||
|
||||
log.debug("Request endpoint: {}", requestEndpoint);
|
||||
isEnabled = endpointConfiguration.isEndpointEnabled(requestEndpoint);
|
||||
log.debug("Is endpoint enabled: {}", isEnabled);
|
||||
} else {
|
||||
isEnabled = endpointConfiguration.isEndpointEnabled(requestURI);
|
||||
}
|
||||
|
||||
boolean isEnabled = endpointConfiguration.isEndpointEnabledForUri(requestURI);
|
||||
if (!isEnabled) {
|
||||
response.sendError(HttpServletResponse.SC_FORBIDDEN, "This endpoint is disabled");
|
||||
return false;
|
||||
|
||||
@@ -27,60 +27,19 @@ class EndpointInterceptorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void preHandleAllowsEnabledApiEndpoint() throws Exception {
|
||||
void preHandleAllowsEnabledEndpoint() throws Exception {
|
||||
when(request.getRequestURI()).thenReturn("/api/v1/general/remove-pages");
|
||||
when(endpointConfiguration.isEndpointEnabled("remove-pages")).thenReturn(true);
|
||||
when(endpointConfiguration.isEndpointEnabledForUri("/api/v1/general/remove-pages"))
|
||||
.thenReturn(true);
|
||||
assertTrue(interceptor.preHandle(request, response, new Object()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void preHandleBlocksDisabledApiEndpoint() throws Exception {
|
||||
void preHandleBlocksDisabledEndpoint() throws Exception {
|
||||
when(request.getRequestURI()).thenReturn("/api/v1/general/remove-pages");
|
||||
when(endpointConfiguration.isEndpointEnabled("remove-pages")).thenReturn(false);
|
||||
when(endpointConfiguration.isEndpointEnabledForUri("/api/v1/general/remove-pages"))
|
||||
.thenReturn(false);
|
||||
assertFalse(interceptor.preHandle(request, response, new Object()));
|
||||
verify(response).sendError(HttpServletResponse.SC_FORBIDDEN, "This endpoint is disabled");
|
||||
}
|
||||
|
||||
@Test
|
||||
void preHandleExtractsConvertEndpointCorrectly() throws Exception {
|
||||
when(request.getRequestURI()).thenReturn("/api/v1/convert/pdf/img");
|
||||
when(endpointConfiguration.isEndpointEnabled("pdf-to-img")).thenReturn(true);
|
||||
assertTrue(interceptor.preHandle(request, response, new Object()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void preHandleBlocksDisabledConvertEndpoint() throws Exception {
|
||||
when(request.getRequestURI()).thenReturn("/api/v1/convert/pdf/img");
|
||||
when(endpointConfiguration.isEndpointEnabled("pdf-to-img")).thenReturn(false);
|
||||
assertFalse(interceptor.preHandle(request, response, new Object()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void preHandleUsesFullUriForNonApiPaths() throws Exception {
|
||||
when(request.getRequestURI()).thenReturn("/some-page");
|
||||
when(endpointConfiguration.isEndpointEnabled("/some-page")).thenReturn(true);
|
||||
assertTrue(interceptor.preHandle(request, response, new Object()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void preHandleBlocksDisabledNonApiPath() throws Exception {
|
||||
when(request.getRequestURI()).thenReturn("/some-page");
|
||||
when(endpointConfiguration.isEndpointEnabled("/some-page")).thenReturn(false);
|
||||
assertFalse(interceptor.preHandle(request, response, new Object()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void preHandleUsesFullUriForShortApiPath() throws Exception {
|
||||
// URI with /api/v1 but not enough segments (split length <= 4)
|
||||
when(request.getRequestURI()).thenReturn("/api/v1/general");
|
||||
when(endpointConfiguration.isEndpointEnabled("/api/v1/general")).thenReturn(true);
|
||||
assertTrue(interceptor.preHandle(request, response, new Object()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void preHandleExtractsNonConvertApiEndpoint() throws Exception {
|
||||
when(request.getRequestURI()).thenReturn("/api/v1/security/add-watermark");
|
||||
when(endpointConfiguration.isEndpointEnabled("add-watermark")).thenReturn(true);
|
||||
assertTrue(interceptor.preHandle(request, response, new Object()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user