Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix NPE in case eTransl response does not come #53

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public class ETranslationTranslationService extends AbstractTranslationService {
private String serviceId;
private final String baseUrl;
private final String domain;
//this is the base url of the translation api (without the request handler (or the controller) endpoint)
private final String callbackUrl;
private final String callbackErrorUrl;
private final String credentialUsername;
Expand All @@ -49,36 +48,35 @@ public class ETranslationTranslationService extends AbstractTranslationService {
public static final String markupDelimiter="\ndeenPVsaOg\n";//base64 encoded string (as in generateRedisKey()) with new lines
public static final String markupDelimiterWithoutNewline="deenPVsaOg";
public static final String eTranslationErrorCallbackIndicator="eTranslationErrorCallback";
public static final String eTranslationCallbackRelativeUrl="/etranslation/callback";
public static final String eTranslationErrorCallbackRelativeUrl="/etranslation/error-callback";

public ETranslationTranslationService(String baseUrl, String domain, String callbackUrl, String callbackErrorUrl, int maxWaitMillisec,
public ETranslationTranslationService(String baseUrl, String domain, String translationApiBaseUrl, int maxWaitMillisec,
String username, String password, RedisMessageListenerContainer redisMessageListenerContainer) throws TranslationException {
if(!baseUrlTests.equals(baseUrl)) {
validateETranslConfigParams(baseUrl, domain, callbackUrl, callbackErrorUrl, maxWaitMillisec, username, password);
validateETranslConfigParams(baseUrl, domain, translationApiBaseUrl, maxWaitMillisec, username, password);
}
this.baseUrl = baseUrl;
this.domain = domain;
this.callbackUrl=callbackUrl;
this.callbackErrorUrl=callbackErrorUrl;
this.callbackUrl=translationApiBaseUrl + eTranslationCallbackRelativeUrl;
this.callbackErrorUrl=translationApiBaseUrl + eTranslationErrorCallbackRelativeUrl;
this.maxWaitMillisec=maxWaitMillisec;
this.credentialUsername=username;
this.credentialPwd=password;
this.redisMessageListenerContainer=redisMessageListenerContainer;
}

private void validateETranslConfigParams(String baseUrl, String domain, String callbackUrl,
String callbackErrorUrl, int maxWaitMillisec, String username, String password) throws TranslationException {
private void validateETranslConfigParams(String baseUrl, String domain, String translationApiBaseUrl,
int maxWaitMillisec, String username, String password) throws TranslationException {
List<String> missingParams= new ArrayList<>(6);
if(StringUtils.isBlank(baseUrl)) {
missingParams.add("baseUrl");
}
if(StringUtils.isBlank(domain)) {
missingParams.add("domain");
}
if(StringUtils.isBlank(callbackUrl)) {
missingParams.add("callbackUrl");
}
if(StringUtils.isBlank(callbackErrorUrl)) {
missingParams.add("callbackErrorUrl");
if(StringUtils.isBlank(translationApiBaseUrl)) {
missingParams.add("translationApiBaseUrl");
}
if(maxWaitMillisec<=0) {
missingParams.add("maxWaitMillisec (must be >0)");
Expand Down Expand Up @@ -166,13 +164,16 @@ private void createRedisMessageListenerAndWaitForResults(List<TranslationObj> tr
if(LOGGER.isDebugEnabled()) {
LOGGER.debug("Received message from redis message listener is: {}", response);
}
if(response.contains(ETranslationTranslationService.eTranslationErrorCallbackIndicator)) {
//eTtransl error callback received
throw new TranslationException(response);
}
else if(response!=null) {
//extractTranslationsFromETranslationHtmlResponse(translationObjs, redisMessageListenerAdapter, response);
extractTranslationsFromETranslationResponse(translationObjs, redisMessageListenerAdapter, response);

if(response!=null) {
if(response.contains(ETranslationTranslationService.eTranslationErrorCallbackIndicator)) {
//eTtransl error callback received
throw new TranslationException(response);
}
else {
//extractTranslationsFromETranslationHtmlResponse(translationObjs, redisMessageListenerAdapter, response);
extractTranslationsFromETranslationResponse(translationObjs, redisMessageListenerAdapter, response);
}
}
/* unsubscibe this listener which automatically deletes the created pub/sub channel,
* which also gets deleted if the app is stopped or anyhow broken.
Expand Down Expand Up @@ -320,6 +321,9 @@ private long createHttpRequest(String content) throws TranslationException, IOEx
long requestNumber;
try{
requestNumber = Long.parseLong(respBody);
if(LOGGER.isDebugEnabled()) {
LOGGER.debug("eTranslation request sent with the request-id: {} .", requestNumber);
}
if(requestNumber < 0) {
throw wrapETranslationErrorResponse(respBody);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void translationETranslation() throws Exception {

mockMvc
.perform(
post("/etranslation/callback").characterEncoding(StandardCharsets.UTF_8)
post(ETranslationTranslationService.eTranslationCallbackRelativeUrl).characterEncoding(StandardCharsets.UTF_8)
.param("external-reference", eTranslRef)
.param("translated-text", translatedText.toString()))
.andExpect(status().isOk());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ public ETranslationTranslationService getETranslationService(
return new ETranslationTranslationService(
translationConfig.getEtranslationBaseUrl(),
translationConfig.getEtranslationDomain(),
translationConfig.getEtranslationCallback(),
translationConfig.getEtranslationErrorCallback(),
translationConfig.getTranslationApiBaseUrl(),
translationConfig.getEtranslationMaxWaitMillisec(),
translationConfig.getEtranslationUsername(),
translationConfig.getEtranslationPassword(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class TranslationConfig{

public static final String CONFIG_FOLDER = "/opt/app/config";

@Value("${translation.api.baseUrl:#{null}}")
private String translationApiBaseUrl;

@Value("${europeana.apikey.jwttoken.signaturekey:}")
private String apiKeyPublicKey;

Expand Down Expand Up @@ -63,13 +66,7 @@ public class TranslationConfig{

@Value("${translation.eTranslation.domain:#{null}}")
private String etranslationDomain;

@Value("${translation.eTranslation.callback:#{null}}")
private String etranslationCallback;

@Value("${translation.eTranslation.error.callback:#{null}}")
private String etranslationErrorCallback;


@Value("${translation.eTranslation.maxWaitMillisec:30000}")
private int etranslationMaxWaitMillisec;

Expand All @@ -83,6 +80,10 @@ public TranslationConfig() {
super();
}

public String getTranslationApiBaseUrl() {
return translationApiBaseUrl;
}

public String getApiKeyPublicKey() {
return apiKeyPublicKey;
}
Expand Down Expand Up @@ -173,14 +174,6 @@ public String getEtranslationDomain() {
return etranslationDomain;
}

public String getEtranslationCallback() {
return etranslationCallback;
}

public String getEtranslationErrorCallback() {
return etranslationErrorCallback;
}

public int getEtranslationMaxWaitMillisec() {
return etranslationMaxWaitMillisec;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ETranslationCallbackController(RedisTemplate<String, CachedTranslation> r
}

@Tag(description = "ETranslation callback endpoint", name = "eTranslationCallback")
@PostMapping(value = "/etranslation/callback")
@PostMapping(value = ETranslationTranslationService.eTranslationCallbackRelativeUrl)
public void eTranslationCallback(
@RequestParam(value = "target-language", required = false) String targetLanguage,
@RequestParam(value = "translated-text", required = false) String translatedTextSnippet,
Expand All @@ -43,7 +43,7 @@ public void eTranslationCallback(
}

@Tag(description = "ETranslation error callback endpoint", name = "eTranslationErrorCallback")
@PostMapping(value = "/etranslation/error-callback")
@PostMapping(value = ETranslationTranslationService.eTranslationErrorCallbackRelativeUrl)
public void eTranslationErrorCallback(
@RequestParam(value = "error-code", required = false) String errorCode,
@RequestParam(value = "error-message", required = false) String errorMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ truststore.password=

translation.runtime=application

translation.api.baseUrl=

translation.eTranslation.baseUrl=
translation.eTranslation.username=
translation.eTranslation.password=
# eTranslation domain could be "SPD" (for neural and speed-optimized statistical engines on the cloud. This is the default.) else "Europeana"
translation.eTranslation.domain=
translation.eTranslation.callback=
translation.eTranslation.error.callback=
translation.eTranslation.maxWaitMillisec=

Loading