From 4cb0a19a7b61c75d6a321ad23cb938caa3a7de8d Mon Sep 17 00:00:00 2001 From: srishti Date: Mon, 12 Feb 2024 16:21:49 +0100 Subject: [PATCH] EA-3705 adapt interfaces --- .../client/BaseTranslationApiClient.java | 20 --- .../client/TranslationApiClient.java | 162 +++++++++++++++--- .../service/LanguageDetectionClient.java | 39 ----- .../service/TranslationApiRestClient.java | 14 +- .../client/service/TranslationClient.java | 60 ------- .../client/utils/TranslationClientUtils.java | 42 +++++ .../model/LanguageDetectionObj.java | 2 - .../definitions/model/LanguageObj.java | 9 - 8 files changed, 185 insertions(+), 163 deletions(-) delete mode 100644 translation-client/src/main/java/eu/europeana/api/translation/client/service/LanguageDetectionClient.java delete mode 100644 translation-client/src/main/java/eu/europeana/api/translation/client/service/TranslationClient.java diff --git a/translation-client/src/main/java/eu/europeana/api/translation/client/BaseTranslationApiClient.java b/translation-client/src/main/java/eu/europeana/api/translation/client/BaseTranslationApiClient.java index 1507c11b..2428c399 100644 --- a/translation-client/src/main/java/eu/europeana/api/translation/client/BaseTranslationApiClient.java +++ b/translation-client/src/main/java/eu/europeana/api/translation/client/BaseTranslationApiClient.java @@ -4,9 +4,7 @@ import com.fasterxml.jackson.databind.ObjectWriter; import eu.europeana.api.translation.client.config.TranslationClientConfiguration; import eu.europeana.api.translation.client.exception.TranslationApiException; -import eu.europeana.api.translation.client.service.LanguageDetectionClient; import eu.europeana.api.translation.client.service.TranslationApiRestClient; -import eu.europeana.api.translation.client.service.TranslationClient; import eu.europeana.api.translation.definitions.language.LanguagePair; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -26,12 +24,6 @@ public class BaseTranslationApiClient { private final ObjectWriter objectWriter; private static Set supportedLanguagesForDetection = new TreeSet<>(); private static Set supportedLanguagesForTranslation = new TreeSet<>(); - - // clients - private TranslationClient translationClient; - private LanguageDetectionClient languageDetectionClient; - - private TranslationApiRestClient translationApiRestClient; public BaseTranslationApiClient(TranslationClientConfiguration configuration) throws TranslationApiException { @@ -39,10 +31,6 @@ public BaseTranslationApiClient(TranslationClientConfiguration configuration) th if (this.configuration.getTranslationApiUrl() == null || this.configuration.getTranslationApiUrl().isEmpty()) { throw new TranslationApiException("Translation api endpoint not configured !!!"); } - - this.translationClient = new TranslationClient(); - this.languageDetectionClient = new LanguageDetectionClient(); - this.translationApiRestClient = new TranslationApiRestClient(getApiClient(this.configuration.getTranslationApiUrl())); this.objectWriter = new ObjectMapper().writer().withDefaultPrettyPrinter(); @@ -91,12 +79,4 @@ public Set getSupportedLanguagesForDetection() { public Set getSupportedLanguagesForTranslation() { return supportedLanguagesForTranslation; } - - public TranslationClient getTranslationClient() { - return this.translationClient; - } - - public LanguageDetectionClient getLanguageDetectionClient() { - return this.languageDetectionClient; - } } diff --git a/translation-client/src/main/java/eu/europeana/api/translation/client/TranslationApiClient.java b/translation-client/src/main/java/eu/europeana/api/translation/client/TranslationApiClient.java index c278f369..93fb50a7 100644 --- a/translation-client/src/main/java/eu/europeana/api/translation/client/TranslationApiClient.java +++ b/translation-client/src/main/java/eu/europeana/api/translation/client/TranslationApiClient.java @@ -3,47 +3,163 @@ import com.fasterxml.jackson.core.JsonProcessingException; import eu.europeana.api.translation.client.config.TranslationClientConfiguration; import eu.europeana.api.translation.client.exception.TranslationApiException; +import eu.europeana.api.translation.client.utils.TranslationClientUtils; import eu.europeana.api.translation.definitions.language.LanguagePair; -import eu.europeana.api.translation.definitions.model.LangDetectRequest; -import eu.europeana.api.translation.definitions.model.LangDetectResponse; -import eu.europeana.api.translation.definitions.model.TranslationRequest; -import eu.europeana.api.translation.definitions.model.TranslationResponse; +import eu.europeana.api.translation.definitions.model.*; +import eu.europeana.api.translation.service.LanguageDetectionService; +import eu.europeana.api.translation.service.TranslationService; +import eu.europeana.api.translation.service.exception.LanguageDetectionException; +import eu.europeana.api.translation.service.exception.TranslationException; +import java.util.List; + +/** + * Translation API client class + * Implements the interfaces of Language Detection and translation services + * @author srishti singh + */ public class TranslationApiClient extends BaseTranslationApiClient { + private static final String SERVICE_ID = "TRANSLATION_CLIENT"; + private static final String TOKEN_ERROR_MESSAGE = "Translation API client has not been initialized with a token or has been closed!"; + + public static final ThreadLocal token = new ThreadLocal<>(); + // clients + private TranslationClient translationClient; + private LanguageDetectionClient languageDetectionClient; + public TranslationApiClient(TranslationClientConfiguration configuration) throws TranslationApiException { super(configuration); + this.translationClient = new TranslationClient(); + this.languageDetectionClient = new LanguageDetectionClient(); } - public TranslationResponse translate(TranslationRequest request, String authToken) throws TranslationApiException { - return getTranslationApiRestClient().getTranslations(getJsonString(request), authToken); + public TranslationService getTranslationService() { + return this.translationClient; } + public LanguageDetectionService getLanguageDetectionService() { + return this.languageDetectionClient; + } - public LangDetectResponse detectLang(LangDetectRequest langDetectRequest, String authToken) throws TranslationApiException { - return getTranslationApiRestClient().getDetectedLanguages(getJsonString(langDetectRequest), authToken); - + public void setAuthToken(String authToken) { + token.set(authToken); } /** - * indicates if the given language is supported by the language detection service - * @param srcLang language hint - * @return true is supported + * Close / purge the token from memory */ - public boolean isSupported(String srcLang) { - return getSupportedLanguagesForDetection().contains(srcLang); + public void close() { + token.remove(); } - /** - * To validate the given pair of source and target language is valid for translation - * - * @param srcLang source language of the data to be translated - * @param trgLang target language in which data has to be translated - * @return true is the pair is valid - */ - public boolean isSupported(String srcLang, String trgLang) { - return getSupportedLanguagesForTranslation().contains(new LanguagePair(srcLang, trgLang)); + + // Language detection client + private class LanguageDetectionClient implements LanguageDetectionService { + + @Override + public boolean isSupported(String srcLang) { + return getSupportedLanguagesForDetection().contains(srcLang); + } + + @Override + public String getServiceId() { + return SERVICE_ID; + } + + @Override + public void setServiceId(String serviceId) { + // leave empty + } + + @Override + public void detectLang(List languageDetectionObjs) throws LanguageDetectionException { + if (languageDetectionObjs.isEmpty()) { + return; + } + String authToken = TranslationApiClient.token.get(); + if (authToken == null) { + throw new LanguageDetectionException(TOKEN_ERROR_MESSAGE); + } + // convert LanguageDetectionObj to LangDetectRequest for the POST request to translation API + LangDetectRequest langDetectRequest = TranslationClientUtils.createLangDetectRequest(languageDetectionObjs); + + try { + LangDetectResponse response = getTranslationApiRestClient().getDetectedLanguages(getJsonString(langDetectRequest), authToken); + List detectedLang = response.getLangs(); + for (int i = 0; i < detectedLang.size(); i++) { + languageDetectionObjs.get(i).setDetectedLang(detectedLang.get(i)); + } + } catch (TranslationApiException e) { + throw new LanguageDetectionException(e.getMessage()); + } + } + + @Override + public void close() { + TranslationApiClient.this.close(); + } + + @Override + public String getExternalServiceEndPoint() { + return null; + } + } + + + // Translation client + private class TranslationClient implements TranslationService { + + @Override + public String getServiceId() { + return SERVICE_ID; + } + + @Override + public void setServiceId(String serviceId) { + // leave empty + } + + @Override + public boolean isSupported(String srcLang, String trgLang) { + return getSupportedLanguagesForTranslation().contains(new LanguagePair(srcLang, trgLang)); + } + + @Override + public void translate(List translationStrings) throws TranslationException { + if (translationStrings.isEmpty()) { + return; + } + String authToken = TranslationApiClient.token.get(); + if (authToken == null) { + throw new TranslationException(TOKEN_ERROR_MESSAGE); + } + // convert TranslationObj to TranslationRequest for the POST request to translation API + TranslationRequest translationRequest = TranslationClientUtils.createTranslationRequest(translationStrings); + + try { + TranslationResponse response = getTranslationApiRestClient().getTranslations(getJsonString(translationRequest), authToken); + List translations = response.getTranslations(); + for (int i = 0; i < translations.size(); i++) { + translationStrings.get(i).setTranslation(translations.get(i)); + } + + } catch (TranslationApiException e) { + throw new TranslationException(e.getMessage()); + } + + } + + @Override + public void close() { + TranslationApiClient.this.close(); + } + + @Override + public String getExternalServiceEndPoint() { + return null; + } } private String getJsonString(T request) throws TranslationApiException { diff --git a/translation-client/src/main/java/eu/europeana/api/translation/client/service/LanguageDetectionClient.java b/translation-client/src/main/java/eu/europeana/api/translation/client/service/LanguageDetectionClient.java deleted file mode 100644 index 04c12803..00000000 --- a/translation-client/src/main/java/eu/europeana/api/translation/client/service/LanguageDetectionClient.java +++ /dev/null @@ -1,39 +0,0 @@ -package eu.europeana.api.translation.client.service; - -import eu.europeana.api.translation.definitions.model.LanguageDetectionObj; -import eu.europeana.api.translation.service.LanguageDetectionService; -import eu.europeana.api.translation.service.exception.LanguageDetectionException; - -import java.util.List; - -public class LanguageDetectionClient implements LanguageDetectionService { - @Override - public boolean isSupported(String srcLang) { - return false; - } - - @Override - public String getServiceId() { - return "TRANSLATION_CLIENT"; - } - - @Override - public void setServiceId(String serviceId) { - - } - - @Override - public void detectLang(List languageDetectionObjs) throws LanguageDetectionException { - - } - - @Override - public void close() { - // leave empty - } - - @Override - public String getExternalServiceEndPoint() { - return null; - } -} diff --git a/translation-client/src/main/java/eu/europeana/api/translation/client/service/TranslationApiRestClient.java b/translation-client/src/main/java/eu/europeana/api/translation/client/service/TranslationApiRestClient.java index 21115b4c..36b5e927 100644 --- a/translation-client/src/main/java/eu/europeana/api/translation/client/service/TranslationApiRestClient.java +++ b/translation-client/src/main/java/eu/europeana/api/translation/client/service/TranslationApiRestClient.java @@ -1,6 +1,5 @@ package eu.europeana.api.translation.client.service; -import eu.europeana.api.translation.client.exception.ExternalServiceException; import eu.europeana.api.translation.client.exception.TranslationApiException; import eu.europeana.api.translation.definitions.language.LanguagePair; import eu.europeana.api.translation.definitions.model.LangDetectResponse; @@ -8,7 +7,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.WebClient; @@ -115,10 +113,6 @@ public T getTranslationApiResponse(WebClient webClient, Function response.bodyToMono(String.class).map(ExternalServiceException::new)); + .retrieve(); +// .onStatus( +// HttpStatus.BAD_GATEWAY::equals, +// response -> response.bodyToMono(String.class).map(ExternalServiceException::new)); } private WebClient.ResponseSpec executeGet(WebClient webClient, Function uriBuilderURIFunction, String authToken) { diff --git a/translation-client/src/main/java/eu/europeana/api/translation/client/service/TranslationClient.java b/translation-client/src/main/java/eu/europeana/api/translation/client/service/TranslationClient.java deleted file mode 100644 index d230bfa1..00000000 --- a/translation-client/src/main/java/eu/europeana/api/translation/client/service/TranslationClient.java +++ /dev/null @@ -1,60 +0,0 @@ -package eu.europeana.api.translation.client.service; - -import eu.europeana.api.translation.definitions.model.TranslationObj; -import eu.europeana.api.translation.definitions.model.TranslationRequest; -import eu.europeana.api.translation.service.TranslationService; -import eu.europeana.api.translation.service.exception.TranslationException; - -import java.util.ArrayList; -import java.util.List; - -public class TranslationClient implements TranslationService { - - @Override - public String getServiceId() { - return "TRANSLATION_CLIENT"; - } - - @Override - public void setServiceId(String serviceId) { - // leave empty - } - - @Override - public boolean isSupported(String srcLang, String trgLang) { - return false; - } - - @Override - public void translate(List translationStrings) throws TranslationException { - if (translationStrings.isEmpty()) { - return; - } - // convert TranslationObj to TranslationRequest for the POST request to translation API - TranslationRequest translationRequest = new TranslationRequest(); - translationRequest.setSource(translationStrings.get(0).getSourceLang()); - translationRequest.setTarget(translationStrings.get(0).getTargetLang()); - - List text = new ArrayList<>(); - for (TranslationObj object : translationStrings) { - text.add(object.getText()); - } - translationRequest.setText(text); - -// getTranslationApiRestClient().getTranslations(getJsonString(translationRequest), authToken); - // return getTranslationApiRestClient().getDetectedLanguages(getJsonString(langDetectRequest), authToken); - - - } - - @Override - public void close() { - // leave empty - } - - @Override - public String getExternalServiceEndPoint() { - return null; - } - -} diff --git a/translation-client/src/main/java/eu/europeana/api/translation/client/utils/TranslationClientUtils.java b/translation-client/src/main/java/eu/europeana/api/translation/client/utils/TranslationClientUtils.java index ba7110b0..76764996 100644 --- a/translation-client/src/main/java/eu/europeana/api/translation/client/utils/TranslationClientUtils.java +++ b/translation-client/src/main/java/eu/europeana/api/translation/client/utils/TranslationClientUtils.java @@ -5,6 +5,10 @@ import com.fasterxml.jackson.databind.ObjectMapper; import eu.europeana.api.translation.client.exception.TranslationApiException; import eu.europeana.api.translation.definitions.language.LanguagePair; +import eu.europeana.api.translation.definitions.model.LangDetectRequest; +import eu.europeana.api.translation.definitions.model.LanguageDetectionObj; +import eu.europeana.api.translation.definitions.model.TranslationObj; +import eu.europeana.api.translation.definitions.model.TranslationRequest; import org.apache.commons.lang3.StringUtils; import org.springframework.web.util.UriBuilder; @@ -46,6 +50,44 @@ public static Function buildUrl(String path) { }; } + /** + * Creates LangDetectRequest from LanguageDetectionObj + * @param languageDetectionObjs + * @return + */ + public static LangDetectRequest createLangDetectRequest(List languageDetectionObjs) { + LangDetectRequest langDetectRequest = new LangDetectRequest(); + // hint is optional + String hint = languageDetectionObjs.get(0).getHint(); + if (StringUtils.isNotEmpty(hint)) { + langDetectRequest.setLang(hint); + } + List text = new ArrayList<>(languageDetectionObjs.size()); + for (LanguageDetectionObj object : languageDetectionObjs) { + text.add(object.getText()); + } + langDetectRequest.setText(text); + return langDetectRequest; + } + + /** + * Creates TranslationRequest from TranslationObj + * @param translationStrings + * @return + */ + public static TranslationRequest createTranslationRequest(List translationStrings) { + TranslationRequest translationRequest = new TranslationRequest(); + translationRequest.setSource(translationStrings.get(0).getSourceLang()); + translationRequest.setTarget(translationStrings.get(0).getTargetLang()); + + List text = new ArrayList<>(translationStrings.size()); + for (TranslationObj object : translationStrings) { + text.add(object.getText()); + } + translationRequest.setText(text); + return translationRequest; + } + private static JsonNode getConfigNode(String json, String nodeToFetch) throws TranslationApiException { if (!StringUtils.isEmpty(json)) { try { diff --git a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/model/LanguageDetectionObj.java b/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/model/LanguageDetectionObj.java index 704a9324..b94e5152 100644 --- a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/model/LanguageDetectionObj.java +++ b/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/model/LanguageDetectionObj.java @@ -18,12 +18,10 @@ public void setHint(String hint) { this.hint = hint; } - @Override public String getDetectedLang() { return detectedLang; } - @Override public void setDetectedLang(String detectedLang) { this.detectedLang = detectedLang; } diff --git a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/model/LanguageObj.java b/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/model/LanguageObj.java index 93902c83..e8193ad3 100644 --- a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/model/LanguageObj.java +++ b/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/model/LanguageObj.java @@ -8,7 +8,6 @@ public abstract class LanguageObj { private String text; - private String detectedLang; private String cacheKey; private boolean retrievedFromCache; @@ -30,14 +29,6 @@ public void setText(String text) { this.text = text; } - public String getDetectedLang() { - return detectedLang; - } - - public void setDetectedLang(String detectedLang) { - this.detectedLang = detectedLang; - } - public String getCacheKey() { return cacheKey; }