Skip to content

Commit

Permalink
throw interruption exception #EA-3859
Browse files Browse the repository at this point in the history
  • Loading branch information
gsergiu committed May 29, 2024
1 parent db537dc commit 31a5516
Showing 1 changed file with 57 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
import eu.europeana.api.translation.service.etranslation.ETranslationTranslationService;
import eu.europeana.api.translation.web.model.CachedTranslation;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController
@Tag(name = "ETranslation callback controller", description = "Receives the eTranslation response")
public class ETranslationCallbackController {

private static final Logger LOGGER = LogManager.getLogger(ETranslationCallbackController.class);

RedisTemplate<String, CachedTranslation> redisTemplate;

@Autowired
Expand All @@ -35,48 +36,50 @@ public void eTranslationCallbackPost(
@RequestParam(value = "translated-text", required = true) String translatedTextSnippet,
@RequestParam(value = "request-id", required = true) String requestId,
@RequestParam(value = "external-reference", required = true) String externalReference) {

if(LOGGER.isDebugEnabled()) {
LOGGER.debug("eTranslation callback has been received with the request-id: {}, and the"
+ " external-reference: {}", LoggingUtils.sanitizeUserInput(requestId), LoggingUtils.sanitizeUserInput(externalReference));

if (LOGGER.isDebugEnabled()) {
LOGGER.debug(
"eTranslation callback has been received with the request-id: {}, and the"
+ " external-reference: {}",
LoggingUtils.sanitizeUserInput(requestId),
LoggingUtils.sanitizeUserInput(externalReference));
}
if(externalReference!=null && translatedTextSnippet!=null) {
if (externalReference != null && translatedTextSnippet != null) {
redisTemplate.convertAndSend(externalReference, translatedTextSnippet);
}
}
}

@Tag(description = "ETranslation callback endpoint", name = "eTranslationCallback")
@GetMapping(value = ETranslationTranslationService.eTranslationCallbackRelativeUrl)
public ResponseEntity<String> eTranslationCallbackGet(
@RequestParam(value = "target-language", required = false) String targetLanguage,
@RequestParam(value = "translated-text", required = false) String translatedTextSnippet,
@RequestParam(value = "request-id", required = false) String requestId,
@RequestParam(value = "external-reference", required = false) String externalReference,
@RequestParam(value = "timeout", required = false) int timeout,
@RequestBody(required = false) String body) {
if(timeout > 0) {
try {
//for simulation purposes, wait for $timeout seconds
Thread.sleep(timeout * 1000);
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
} catch (InterruptedException e) {
//should not happen
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.toString());
}
}
@RequestParam(value = "timeout", required = false) Integer timeout,
@RequestBody(required = false) String body) throws InterruptedException {

if(LOGGER.isDebugEnabled()) {
LOGGER.debug("eTranslation callback has been received with the request-id: {}, and the"
+ " external-reference: {}", LoggingUtils.sanitizeUserInput(""+ requestId), LoggingUtils.sanitizeUserInput(""+externalReference));
if (timeout != null && timeout > 0) {
// for simulation purposes, wait for $timeout seconds
Thread.sleep(timeout * 1000);
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
}

if (LOGGER.isDebugEnabled()) {
LOGGER.debug(
"eTranslation callback has been received with the request-id: {}, and the"
+ " external-reference: {}",
LoggingUtils.sanitizeUserInput("" + requestId),
LoggingUtils.sanitizeUserInput("" + externalReference));
}
if(externalReference!=null && translatedTextSnippet!=null) {
if (externalReference != null && translatedTextSnippet != null) {
redisTemplate.convertAndSend(externalReference, translatedTextSnippet);
}

return ResponseEntity.status(HttpStatus.ACCEPTED).build();


}

}

@Tag(description = "ETranslation error callback endpoint", name = "eTranslationErrorCallback")
@PostMapping(value = ETranslationTranslationService.eTranslationErrorCallbackRelativeUrl)
Expand All @@ -87,17 +90,22 @@ public void eTranslationErrorCallbackPost(
@RequestParam(value = "request-id", required = false) String requestId,
@RequestParam(value = "external-reference", required = false) String externalReference,
@RequestBody(required = false) String body) {
if(LOGGER.isDebugEnabled()) {
LOGGER.debug("eTranslation error callback has been received with the following parameters: error-code: {},"
+ "error-message: {}, request-id: {}, external-reference: {}", LoggingUtils.sanitizeUserInput(errorCode),
LoggingUtils.sanitizeUserInput(errorMessage), LoggingUtils.sanitizeUserInput(requestId), LoggingUtils.sanitizeUserInput(externalReference));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(
"eTranslation error callback has been received with the following parameters: error-code: {},"
+ "error-message: {}, request-id: {}, external-reference: {}",
LoggingUtils.sanitizeUserInput(errorCode), LoggingUtils.sanitizeUserInput(errorMessage),
LoggingUtils.sanitizeUserInput(requestId),
LoggingUtils.sanitizeUserInput(externalReference));
}
if(externalReference!=null) {
redisTemplate.convertAndSend(externalReference, String.format("%s: error-code=%s, error-message=%s",
ETranslationTranslationService.eTranslationErrorCallbackIndicator, errorCode, errorMessage));
if (externalReference != null) {
redisTemplate.convertAndSend(externalReference,
String.format("%s: error-code=%s, error-message=%s",
ETranslationTranslationService.eTranslationErrorCallbackIndicator, errorCode,
errorMessage));
}
}
}

@Tag(description = "ETranslation error callback endpoint", name = "eTranslationErrorCallback")
@GetMapping(value = ETranslationTranslationService.eTranslationErrorCallbackRelativeUrl)
public void eTranslationErrorCallbackGet(
Expand All @@ -107,16 +115,21 @@ public void eTranslationErrorCallbackGet(
@RequestParam(value = "request-id", required = false) String requestId,
@RequestParam(value = "external-reference", required = false) String externalReference,
@RequestBody(required = false) String body) {
if(LOGGER.isDebugEnabled()) {
LOGGER.debug("eTranslation error callback has been received with the following parameters: error-code: {},"
+ "error-message: {}, request-id: {}, external-reference: {}", LoggingUtils.sanitizeUserInput(errorCode),
LoggingUtils.sanitizeUserInput(errorMessage), LoggingUtils.sanitizeUserInput(requestId), LoggingUtils.sanitizeUserInput(externalReference));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(
"eTranslation error callback has been received with the following parameters: error-code: {},"
+ "error-message: {}, request-id: {}, external-reference: {}",
LoggingUtils.sanitizeUserInput(errorCode), LoggingUtils.sanitizeUserInput(errorMessage),
LoggingUtils.sanitizeUserInput(requestId),
LoggingUtils.sanitizeUserInput(externalReference));
}
if(externalReference!=null) {
redisTemplate.convertAndSend(externalReference, String.format("%s: error-code=%s, error-message=%s",
ETranslationTranslationService.eTranslationErrorCallbackIndicator, errorCode, errorMessage));
if (externalReference != null) {
redisTemplate.convertAndSend(externalReference,
String.format("%s: error-code=%s, error-message=%s",
ETranslationTranslationService.eTranslationErrorCallbackIndicator, errorCode,
errorMessage));
}
}
}


}

0 comments on commit 31a5516

Please sign in to comment.