diff --git a/irs-api/src/main/java/org/eclipse/tractusx/irs/aaswrapper/job/CallbackResponderEventListener.java b/irs-api/src/main/java/org/eclipse/tractusx/irs/aaswrapper/job/CallbackResponderEventListener.java index 2eaefa31c..37e30a3aa 100644 --- a/irs-api/src/main/java/org/eclipse/tractusx/irs/aaswrapper/job/CallbackResponderEventListener.java +++ b/irs-api/src/main/java/org/eclipse/tractusx/irs/aaswrapper/job/CallbackResponderEventListener.java @@ -57,11 +57,13 @@ @Service class CallbackResponderEventListener { + public static final String INVALID_CALLBACK_URL = "Invalid callback url '{}'."; private final UrlValidator urlValidator; private final RestTemplate restTemplate; - /* package */ CallbackResponderEventListener(@Qualifier(NO_ERROR_REST_TEMPLATE) final RestTemplate noErrorRestTemplate) { - this.urlValidator = new UrlValidator(); + /* package */ CallbackResponderEventListener( + @Qualifier(NO_ERROR_REST_TEMPLATE) final RestTemplate noErrorRestTemplate) { + this.urlValidator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS); this.restTemplate = noErrorRestTemplate; } @@ -83,6 +85,8 @@ public void handleJobProcessingFinishedEvent(final JobProcessingFinishedEvent jo } catch (final ResourceAccessException resourceAccessException) { log.warn("Callback url is not reachable - connection timed out, jobId {}", jobProcessingFinishedEvent.jobId()); } + } else { + log.warn(INVALID_CALLBACK_URL, callbackUri); } } } @@ -105,6 +109,8 @@ public void handleBatchProcessingFinishedEvent(final BatchProcessingFinishedEven } catch (final ResourceAccessException resourceAccessException) { log.warn("Callback url is not reachable - connection timed out, orderId {} batchId {}", batchProcessingFinishedEvent.batchOrderId(), batchProcessingFinishedEvent.batchId()); } + } else { + log.warn(INVALID_CALLBACK_URL, callbackUri); } } } @@ -127,6 +133,8 @@ public void handleBatchOrderProcessingFinishedEvent(final BatchOrderProcessingFi } catch (final ResourceAccessException resourceAccessException) { log.warn("Callback url is not reachable - connection timed out, jobId {}", batchOrderProcessingFinishedEvent.batchOrderId()); } + } else { + log.warn(INVALID_CALLBACK_URL, callbackUri); } } } @@ -136,7 +144,7 @@ private boolean thereIsCallbackUrlRegistered(final String callbackUrl) { } @SuppressWarnings("PMD.UseConcurrentHashMap") - private URI buildCallbackUri(final String callbackUrl, final String jobId, final JobState jobState) { + private URI buildCallbackUri(final String callbackUrl, final String jobId, final JobState jobState) { final Map uriVariables = new HashMap<>(); uriVariables.put("id", jobId); uriVariables.put("state", jobState); @@ -145,7 +153,7 @@ private URI buildCallbackUri(final String callbackUrl, final String jobId, fina uriComponentsBuilder.uriVariables(uriVariables); return uriComponentsBuilder.build().toUri(); } - + @SuppressWarnings("PMD.UseConcurrentHashMap") private URI buildCallbackUri(final String callbackUrl, final UUID orderId, final UUID batchId, final ProcessingState orderState, final ProcessingState batchState) { final Map uriVariables = new HashMap<>(); diff --git a/irs-api/src/test/java/org/eclipse/tractusx/irs/aaswrapper/job/CallbackResponderEventListenerTest.java b/irs-api/src/test/java/org/eclipse/tractusx/irs/aaswrapper/job/CallbackResponderEventListenerTest.java index 6d1ab5f4e..079ddf736 100644 --- a/irs-api/src/test/java/org/eclipse/tractusx/irs/aaswrapper/job/CallbackResponderEventListenerTest.java +++ b/irs-api/src/test/java/org/eclipse/tractusx/irs/aaswrapper/job/CallbackResponderEventListenerTest.java @@ -176,6 +176,37 @@ void shouldNotCallCallbackUrlIfIsNotValidAndBatchOrderProcessingFinishedEvent() verifyNoInteractions(this.restTemplate); } + @Test + void shouldCallCallbackUrlIfIsInternalAndStateCompletedAndJobProcessingFinishedEvent() throws URISyntaxException { + // given + final String callbackUrlTemplate = "https://internal:1234/callback?id={id}&state={state}"; + final String jobId = UUID.randomUUID().toString(); + final JobState jobState = JobState.COMPLETED; + final JobProcessingFinishedEvent jobProcessingFinishedEvent = new JobProcessingFinishedEvent(jobId, + jobState.name(), callbackUrlTemplate, Optional.empty()); + // when + callbackResponderEventListener.handleJobProcessingFinishedEvent(jobProcessingFinishedEvent); + + // then + final String expectedCallbackUrl = "https://internal:1234/callback?id=" + jobId + "&state=" + jobState; + verify(this.restTemplate, times(1)).getForEntity(new URI(expectedCallbackUrl), Void.class); + } + + @Test + void shouldNotCallCallbackUrlIfTldIsNotValidAndStateCompletedAndJobProcessingFinishedEvent() { + // given + final String callbackUrlTemplateWithInvalidTld = "https://domain.unknown/callback?id={id}&state={state}"; + final String jobId = UUID.randomUUID().toString(); + final JobState jobState = JobState.COMPLETED; + final JobProcessingFinishedEvent jobProcessingFinishedEvent = new JobProcessingFinishedEvent(jobId, + jobState.name(), callbackUrlTemplateWithInvalidTld, Optional.empty()); + // when + callbackResponderEventListener.handleJobProcessingFinishedEvent(jobProcessingFinishedEvent); + + // then + verifyNoInteractions(this.restTemplate); + } + @Test void shouldNotCallCallbackUrlIfCallbackUrlIsMissing() { // given