From 5f2c00f10abe85ece5cf408a8d27158c26f39fdd Mon Sep 17 00:00:00 2001 From: GordeaS Date: Tue, 7 Nov 2023 16:53:15 +0100 Subject: [PATCH 1/2] enhanced request validation, refactored services to service-common module #EA-3589 --- pom.xml | 6 +- .../AbstractTranslationService.java | 12 ++++ translation-service-common/pom.xml | 31 ++++++++++ .../service/AbstractTranslationService.java | 6 ++ .../service/LanguageDetectionService.java | 4 +- .../service/TranslationService.java | 4 +- ...etectionServiceConfigurationException.java | 2 +- .../exception/LanguageDetectionException.java | 2 +- .../exception/TranslationException.java | 2 +- ...nslationServiceConfigurationException.java | 2 +- .../service}/util/LoggingUtils.java | 2 +- translation-service-google/pom.xml | 7 +-- .../google/DummyGLangDetectService.java | 2 +- .../google/DummyGTranslateService.java | 2 +- .../google/GoogleLangDetectService.java | 4 +- .../google/GoogleTranslationService.java | 7 ++- translation-service-pangeanic/pom.xml | 7 +-- .../pangeanic/DummyPangLangDetectService.java | 2 +- .../DummyPangTranslationService.java | 2 +- .../pangeanic/PangeanicLangDetectService.java | 4 +- .../PangeanicTranslationService.java | 29 ++++----- .../pangeanic/PangeanicTranslationUtils.java | 41 +++++-------- .../tests/BaseTranslationTest.java | 4 +- .../config/TranslationApiAutoconfig.java | 4 +- .../config/TranslationServiceProvider.java | 8 +-- .../api/translation/web/BaseRest.java | 12 ++++ .../web/LangDetectionController.java | 2 +- .../web/TranslationController.java | 9 +-- .../web/TranslationErrorController.java | 59 ++++++++++--------- .../web/exception/GlobalExceptionHandler.java | 27 +++++++++ .../web/service/BaseWebService.java | 4 +- .../web/service/LangDetectionWebService.java | 4 +- .../web/service/TranslationWebService.java | 6 +- 33 files changed, 192 insertions(+), 127 deletions(-) create mode 100644 translation-service-common/AbstractTranslationService.java create mode 100644 translation-service-common/pom.xml create mode 100644 translation-service-common/src/main/java/eu/europeana/api/translation/service/AbstractTranslationService.java rename {translation-definitions/src/main/java/eu/europeana/api/translation/definitions => translation-service-common/src/main/java/eu/europeana/api/translation}/service/LanguageDetectionService.java (85%) rename {translation-definitions/src/main/java/eu/europeana/api/translation/definitions => translation-service-common/src/main/java/eu/europeana/api/translation}/service/TranslationService.java (91%) rename {translation-definitions/src/main/java/eu/europeana/api/translation/definitions => translation-service-common/src/main/java/eu/europeana/api/translation}/service/exception/LangDetectionServiceConfigurationException.java (86%) rename {translation-definitions/src/main/java/eu/europeana/api/translation/definitions => translation-service-common/src/main/java/eu/europeana/api/translation}/service/exception/LanguageDetectionException.java (96%) rename {translation-definitions/src/main/java/eu/europeana/api/translation/definitions => translation-service-common/src/main/java/eu/europeana/api/translation}/service/exception/TranslationException.java (95%) rename {translation-definitions/src/main/java/eu/europeana/api/translation/definitions => translation-service-common/src/main/java/eu/europeana/api/translation}/service/exception/TranslationServiceConfigurationException.java (86%) rename {translation-definitions/src/main/java/eu/europeana/api/translation/definitions => translation-service-common/src/main/java/eu/europeana/api/translation/service}/util/LoggingUtils.java (86%) diff --git a/pom.xml b/pom.xml index 4128b8a3..b480a4df 100644 --- a/pom.xml +++ b/pom.xml @@ -17,10 +17,12 @@ translation-definitions - translation-web - translation-tests + translation-service-common translation-service-google translation-service-pangeanic + translation-web + translation-tests + diff --git a/translation-service-common/AbstractTranslationService.java b/translation-service-common/AbstractTranslationService.java new file mode 100644 index 00000000..8c6a1667 --- /dev/null +++ b/translation-service-common/AbstractTranslationService.java @@ -0,0 +1,12 @@ +package eu.europeana.api.translation.service.pangeanic; + +import org.apache.commons.lang3.StringUtils; +import eu.europeana.api.translation.definitions.language.Language; + +public class AbstractTranslationService { + + public static boolean noTranslationRequired(String lang) { + return (lang == null || StringUtils.equals(lang, Language.NO_LINGUISTIC_CONTENT) + || StringUtils.equals(lang, Language.ENGLISH)); + } +} diff --git a/translation-service-common/pom.xml b/translation-service-common/pom.xml new file mode 100644 index 00000000..1f6d2282 --- /dev/null +++ b/translation-service-common/pom.xml @@ -0,0 +1,31 @@ + + 4.0.0 + + eu.europeana.api + translation-api + 0.0.1-SNAPSHOT + + + translation-service-common + translation-service-common + Common functionality to be reused by specific implementation of language detection and translation services + + + ${basedir}/../${aggregate.report.xml} + + + + + eu.europeana.api + translation-definitions + 0.0.1-SNAPSHOT + + + + org.apache.logging.log4j + log4j-api + + + + + \ No newline at end of file diff --git a/translation-service-common/src/main/java/eu/europeana/api/translation/service/AbstractTranslationService.java b/translation-service-common/src/main/java/eu/europeana/api/translation/service/AbstractTranslationService.java new file mode 100644 index 00000000..03dceba9 --- /dev/null +++ b/translation-service-common/src/main/java/eu/europeana/api/translation/service/AbstractTranslationService.java @@ -0,0 +1,6 @@ +package eu.europeana.api.translation.service; + +public abstract class AbstractTranslationService implements TranslationService { + + +} diff --git a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/service/LanguageDetectionService.java b/translation-service-common/src/main/java/eu/europeana/api/translation/service/LanguageDetectionService.java similarity index 85% rename from translation-definitions/src/main/java/eu/europeana/api/translation/definitions/service/LanguageDetectionService.java rename to translation-service-common/src/main/java/eu/europeana/api/translation/service/LanguageDetectionService.java index 9e7a3455..6696aa94 100644 --- a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/service/LanguageDetectionService.java +++ b/translation-service-common/src/main/java/eu/europeana/api/translation/service/LanguageDetectionService.java @@ -1,7 +1,7 @@ -package eu.europeana.api.translation.definitions.service; +package eu.europeana.api.translation.service; import java.util.List; -import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException; +import eu.europeana.api.translation.service.exception.LanguageDetectionException; public interface LanguageDetectionService { diff --git a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/service/TranslationService.java b/translation-service-common/src/main/java/eu/europeana/api/translation/service/TranslationService.java similarity index 91% rename from translation-definitions/src/main/java/eu/europeana/api/translation/definitions/service/TranslationService.java rename to translation-service-common/src/main/java/eu/europeana/api/translation/service/TranslationService.java index d6f88dc3..d76d7062 100644 --- a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/service/TranslationService.java +++ b/translation-service-common/src/main/java/eu/europeana/api/translation/service/TranslationService.java @@ -1,7 +1,7 @@ -package eu.europeana.api.translation.definitions.service; +package eu.europeana.api.translation.service; import java.util.List; -import eu.europeana.api.translation.definitions.service.exception.TranslationException; +import eu.europeana.api.translation.service.exception.TranslationException; /** * Generic translation service interface diff --git a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/service/exception/LangDetectionServiceConfigurationException.java b/translation-service-common/src/main/java/eu/europeana/api/translation/service/exception/LangDetectionServiceConfigurationException.java similarity index 86% rename from translation-definitions/src/main/java/eu/europeana/api/translation/definitions/service/exception/LangDetectionServiceConfigurationException.java rename to translation-service-common/src/main/java/eu/europeana/api/translation/service/exception/LangDetectionServiceConfigurationException.java index ef0b9b57..5e02db7e 100644 --- a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/service/exception/LangDetectionServiceConfigurationException.java +++ b/translation-service-common/src/main/java/eu/europeana/api/translation/service/exception/LangDetectionServiceConfigurationException.java @@ -1,4 +1,4 @@ -package eu.europeana.api.translation.definitions.service.exception; +package eu.europeana.api.translation.service.exception; /** * Exception that is thrown when there is an error using the translation service diff --git a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/service/exception/LanguageDetectionException.java b/translation-service-common/src/main/java/eu/europeana/api/translation/service/exception/LanguageDetectionException.java similarity index 96% rename from translation-definitions/src/main/java/eu/europeana/api/translation/definitions/service/exception/LanguageDetectionException.java rename to translation-service-common/src/main/java/eu/europeana/api/translation/service/exception/LanguageDetectionException.java index c6c980dc..ee19b985 100644 --- a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/service/exception/LanguageDetectionException.java +++ b/translation-service-common/src/main/java/eu/europeana/api/translation/service/exception/LanguageDetectionException.java @@ -1,4 +1,4 @@ -package eu.europeana.api.translation.definitions.service.exception; +package eu.europeana.api.translation.service.exception; /** * Exception that is thrown when there is an error using the translation service diff --git a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/service/exception/TranslationException.java b/translation-service-common/src/main/java/eu/europeana/api/translation/service/exception/TranslationException.java similarity index 95% rename from translation-definitions/src/main/java/eu/europeana/api/translation/definitions/service/exception/TranslationException.java rename to translation-service-common/src/main/java/eu/europeana/api/translation/service/exception/TranslationException.java index d1e36331..62bf61a2 100644 --- a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/service/exception/TranslationException.java +++ b/translation-service-common/src/main/java/eu/europeana/api/translation/service/exception/TranslationException.java @@ -1,4 +1,4 @@ -package eu.europeana.api.translation.definitions.service.exception; +package eu.europeana.api.translation.service.exception; /** * Exception that is thrown when there is an error using the translation service diff --git a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/service/exception/TranslationServiceConfigurationException.java b/translation-service-common/src/main/java/eu/europeana/api/translation/service/exception/TranslationServiceConfigurationException.java similarity index 86% rename from translation-definitions/src/main/java/eu/europeana/api/translation/definitions/service/exception/TranslationServiceConfigurationException.java rename to translation-service-common/src/main/java/eu/europeana/api/translation/service/exception/TranslationServiceConfigurationException.java index 361e4ada..acfd45cf 100644 --- a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/service/exception/TranslationServiceConfigurationException.java +++ b/translation-service-common/src/main/java/eu/europeana/api/translation/service/exception/TranslationServiceConfigurationException.java @@ -1,4 +1,4 @@ -package eu.europeana.api.translation.definitions.service.exception; +package eu.europeana.api.translation.service.exception; /** * Exception that is thrown when there is an error using the translation service diff --git a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/util/LoggingUtils.java b/translation-service-common/src/main/java/eu/europeana/api/translation/service/util/LoggingUtils.java similarity index 86% rename from translation-definitions/src/main/java/eu/europeana/api/translation/definitions/util/LoggingUtils.java rename to translation-service-common/src/main/java/eu/europeana/api/translation/service/util/LoggingUtils.java index c0f28ee9..4b7e728c 100644 --- a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/util/LoggingUtils.java +++ b/translation-service-common/src/main/java/eu/europeana/api/translation/service/util/LoggingUtils.java @@ -1,4 +1,4 @@ -package eu.europeana.api.translation.definitions.util; +package eu.europeana.api.translation.service.util; /** * Utilities for logging diff --git a/translation-service-google/pom.xml b/translation-service-google/pom.xml index af19c35e..e1eb3be3 100644 --- a/translation-service-google/pom.xml +++ b/translation-service-google/pom.xml @@ -17,7 +17,7 @@ eu.europeana.api - translation-definitions + translation-service-common 0.0.1-SNAPSHOT @@ -39,11 +39,6 @@ 2.19.0 - - org.apache.logging.log4j - log4j-api - - \ No newline at end of file diff --git a/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/DummyGLangDetectService.java b/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/DummyGLangDetectService.java index 2e41671a..c81fc8e1 100644 --- a/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/DummyGLangDetectService.java +++ b/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/DummyGLangDetectService.java @@ -3,7 +3,7 @@ import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.StringUtils; -import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException; +import eu.europeana.api.translation.service.exception.LanguageDetectionException; /** * Dummy implementation preventing invocation of remote google service diff --git a/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/DummyGTranslateService.java b/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/DummyGTranslateService.java index 7fb0e3bb..06ee9914 100644 --- a/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/DummyGTranslateService.java +++ b/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/DummyGTranslateService.java @@ -1,7 +1,7 @@ package eu.europeana.api.translation.service.google; import java.util.List; -import eu.europeana.api.translation.definitions.service.exception.TranslationException; +import eu.europeana.api.translation.service.exception.TranslationException; /** * Dummy implementation preventing invocation of remote google service diff --git a/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleLangDetectService.java b/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleLangDetectService.java index a7cb1ef2..c4aa8018 100644 --- a/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleLangDetectService.java +++ b/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleLangDetectService.java @@ -9,8 +9,8 @@ import com.google.cloud.translate.v3.DetectLanguageRequest.Builder; import com.google.cloud.translate.v3.DetectLanguageResponse; import com.google.cloud.translate.v3.LocationName; -import eu.europeana.api.translation.definitions.service.LanguageDetectionService; -import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException; +import eu.europeana.api.translation.service.LanguageDetectionService; +import eu.europeana.api.translation.service.exception.LanguageDetectionException; /** * Translation service implementing remote invocation of google language detection service diff --git a/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleTranslationService.java b/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleTranslationService.java index 5a440ceb..1fede6c5 100644 --- a/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleTranslationService.java +++ b/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleTranslationService.java @@ -8,8 +8,9 @@ import com.google.cloud.translate.v3.TranslateTextRequest.Builder; import com.google.cloud.translate.v3.TranslateTextResponse; import com.google.cloud.translate.v3.Translation; -import eu.europeana.api.translation.definitions.service.TranslationService; -import eu.europeana.api.translation.definitions.service.exception.TranslationException; +import eu.europeana.api.translation.service.AbstractTranslationService; +import eu.europeana.api.translation.service.TranslationService; +import eu.europeana.api.translation.service.exception.TranslationException; /** * Translation service implementing remote invocation of google language detection service @@ -18,7 +19,7 @@ * @author GordeaS * */ -public class GoogleTranslationService implements TranslationService { +public class GoogleTranslationService extends AbstractTranslationService implements TranslationService { private static final String MIME_TYPE_TEXT = "text/plain"; private final String googleProjectId; diff --git a/translation-service-pangeanic/pom.xml b/translation-service-pangeanic/pom.xml index cabd1a32..c4b85f92 100644 --- a/translation-service-pangeanic/pom.xml +++ b/translation-service-pangeanic/pom.xml @@ -17,15 +17,10 @@ eu.europeana.api - translation-definitions + translation-service-common 0.0.1-SNAPSHOT - - org.apache.logging.log4j - log4j-api - - org.apache.httpcomponents httpclient diff --git a/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/DummyPangLangDetectService.java b/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/DummyPangLangDetectService.java index c339150d..bc2b8a81 100644 --- a/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/DummyPangLangDetectService.java +++ b/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/DummyPangLangDetectService.java @@ -3,7 +3,7 @@ import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.StringUtils; -import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException; +import eu.europeana.api.translation.service.exception.LanguageDetectionException; /** * Dummy implementation preventing invocation of remote pangeanic service diff --git a/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/DummyPangTranslationService.java b/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/DummyPangTranslationService.java index 8dea3f8f..e0eeee5a 100644 --- a/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/DummyPangTranslationService.java +++ b/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/DummyPangTranslationService.java @@ -1,7 +1,7 @@ package eu.europeana.api.translation.service.pangeanic; import java.util.List; -import eu.europeana.api.translation.definitions.service.exception.TranslationException; +import eu.europeana.api.translation.service.exception.TranslationException; /** * Dummy implementation preventing invocation of remote pangeanic service diff --git a/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/PangeanicLangDetectService.java b/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/PangeanicLangDetectService.java index a0d95d31..86bb985e 100644 --- a/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/PangeanicLangDetectService.java +++ b/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/PangeanicLangDetectService.java @@ -19,8 +19,8 @@ import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; -import eu.europeana.api.translation.definitions.service.LanguageDetectionService; -import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException; +import eu.europeana.api.translation.service.LanguageDetectionService; +import eu.europeana.api.translation.service.exception.LanguageDetectionException; public class PangeanicLangDetectService implements LanguageDetectionService { diff --git a/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/PangeanicTranslationService.java b/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/PangeanicTranslationService.java index 2e5f6aad..2eb5775f 100644 --- a/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/PangeanicTranslationService.java +++ b/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/PangeanicTranslationService.java @@ -21,10 +21,11 @@ import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; import eu.europeana.api.translation.definitions.language.PangeanicLanguages; -import eu.europeana.api.translation.definitions.service.TranslationService; -import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException; -import eu.europeana.api.translation.definitions.service.exception.TranslationException; -import eu.europeana.api.translation.definitions.util.LoggingUtils; +import eu.europeana.api.translation.service.AbstractTranslationService; +import eu.europeana.api.translation.service.TranslationService; +import eu.europeana.api.translation.service.exception.LanguageDetectionException; +import eu.europeana.api.translation.service.exception.TranslationException; +import eu.europeana.api.translation.service.util.LoggingUtils; /** * Service to send data to translate to Pangeanic Translate API V2 @@ -32,7 +33,7 @@ * @author Srishti Singh */ // TODO get api key, for now passed empty -public class PangeanicTranslationService implements TranslationService { +public class PangeanicTranslationService extends AbstractTranslationService implements TranslationService { private PangeanicLangDetectService langDetectService; @@ -112,10 +113,12 @@ public List translate(List texts, String targetLanguage, String // lang-detection first and later will translated return translateWithLangDetect(texts, targetLanguage, sourceLanguage); } + + //regular invocation of external translation service HttpPost post = PangeanicTranslationUtils.createTranslateRequest(getExternalServiceEndPoint(), texts, targetLanguage, sourceLanguage, ""); return PangeanicTranslationUtils.getResults(texts, - sendTranslateRequestAndParse(post, sourceLanguage), false); + sendTranslateRequestAndParse(post, sourceLanguage)); } catch (JSONException e) { throw new TranslationException("Exception occured during Pangeanic translation!", HttpStatus.SC_BAD_GATEWAY, e); @@ -148,8 +151,7 @@ private List translateWithLangDetect(List texts, String targetLa List detectedLanguages = detectLanguages(texts, langHint); Map translations = computeTranslations(texts, targetLanguage, detectedLanguages, langHint); - return PangeanicTranslationUtils.getResults(texts, translations, - PangeanicTranslationUtils.nonTranslatedDataExists(detectedLanguages)); + return PangeanicTranslationUtils.getResults(texts, translations); } catch (JSONException | IOException e) { throw new TranslationException("Exception occured during Pangeanic translation!", HttpStatus.SC_BAD_GATEWAY, e); @@ -173,18 +175,9 @@ private Map computeTranslations(List texts, String targe } for (Map.Entry> entry : detectedLangValueMap.entrySet()) { - if (PangeanicTranslationUtils.noTranslationRequired(entry.getKey())) { - if (LOG.isDebugEnabled()) { - LOG.debug("NOT translating data for lang {} for detected values {} ", - LoggingUtils.sanitizeUserInput(entry.getKey()), - LoggingUtils.sanitizeUserInput(entry.getValue().toString())); - } - //TODO translations.put ... original value, this code should be refactored - } else { HttpPost translateRequest = PangeanicTranslationUtils.createTranslateRequest( getExternalServiceEndPoint(), entry.getValue(), targetLanguage, entry.getKey(), ""); - translations.putAll(sendTranslateRequestAndParse(translateRequest, entry.getKey())); - } + translations.putAll(sendTranslateRequestAndParse(translateRequest, entry.getKey())); } return translations; } diff --git a/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/PangeanicTranslationUtils.java b/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/PangeanicTranslationUtils.java index d14a9479..a6b04255 100644 --- a/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/PangeanicTranslationUtils.java +++ b/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/PangeanicTranslationUtils.java @@ -172,8 +172,8 @@ public static Map> getDetectedLangValueMap(List tex /** * Returns the translations. - * If nonTranslatedDataExists is true then add non-translated values as it is - * + * SG: outdated documentation from record translations + * * LOGIC : * if there is a size mismatch then the order the translation according to text sequence is performed. * As if multiple language were detected the order of translation result will vary. @@ -185,37 +185,26 @@ public static Map> getDetectedLangValueMap(List tex * * @param texts original values sent for translations * @param translateResult - * @param nonTranslatedDataExists if language zxx or na was detected * @return */ - public static List getResults(List texts, Map translateResult, boolean nonTranslatedDataExists) { + public static List getResults(List texts, Map translateResult) { List translations = new ArrayList<>(); - if (texts.size() != translateResult.size()) { - for (String text : texts) { - if (translateResult.containsKey(text)) { - translations.add(translateResult.get(text)); - } else if (nonTranslatedDataExists) { - // add non-translated values as it is. Only if "zxx" or no-lang detected responses were present. - translations.add(text); - } - } - } else { +// if (texts.size() != translateResult.size()) { +// for (String text : texts) { +// if (translateResult.containsKey(text)) { +// translations.add(translateResult.get(text)); +// } else { +// //if (nonTranslatedDataExists) { +// // add non-translated values as it is. Only if "zxx" or no-lang detected responses were present. +// translations.add(text); +// } +// } +// } else { for (Map.Entry entry : translateResult.entrySet()) { translations.add(entry.getValue()); } - } +// } return translations; } - - public static boolean noTranslationRequired(String lang) { - return ( lang == null || StringUtils.equals(lang, Language.NO_LINGUISTIC_CONTENT) - || StringUtils.equals(lang, Language.ENGLISH)); - } - - public static boolean nonTranslatedDataExists(List detectedLanguages) { - return (detectedLanguages.contains(Language.NO_LINGUISTIC_CONTENT) - || detectedLanguages.contains(null) - || detectedLanguages.contains(Language.ENGLISH)); - } } diff --git a/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/BaseTranslationTest.java b/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/BaseTranslationTest.java index b4b84592..d1bb2e10 100644 --- a/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/BaseTranslationTest.java +++ b/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/BaseTranslationTest.java @@ -24,8 +24,8 @@ import org.springframework.web.context.WebApplicationContext; import eu.europeana.api.translation.TranslationApp; import eu.europeana.api.translation.config.TranslationServiceProvider; -import eu.europeana.api.translation.definitions.service.exception.LangDetectionServiceConfigurationException; -import eu.europeana.api.translation.definitions.service.exception.TranslationServiceConfigurationException; +import eu.europeana.api.translation.service.exception.LangDetectionServiceConfigurationException; +import eu.europeana.api.translation.service.exception.TranslationServiceConfigurationException; import okhttp3.mockwebserver.Dispatcher; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; diff --git a/translation-web/src/main/java/eu/europeana/api/translation/config/TranslationApiAutoconfig.java b/translation-web/src/main/java/eu/europeana/api/translation/config/TranslationApiAutoconfig.java index f29e396a..59ac4b05 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/config/TranslationApiAutoconfig.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/config/TranslationApiAutoconfig.java @@ -22,8 +22,8 @@ import eu.europeana.api.commons.config.i18n.I18nService; import eu.europeana.api.commons.config.i18n.I18nServiceImpl; import eu.europeana.api.commons.oauth2.service.impl.EuropeanaClientDetailsService; -import eu.europeana.api.translation.definitions.service.exception.LangDetectionServiceConfigurationException; -import eu.europeana.api.translation.definitions.service.exception.TranslationServiceConfigurationException; +import eu.europeana.api.translation.service.exception.LangDetectionServiceConfigurationException; +import eu.europeana.api.translation.service.exception.TranslationServiceConfigurationException; import eu.europeana.api.translation.service.google.DummyGLangDetectService; import eu.europeana.api.translation.service.google.DummyGTranslateService; import eu.europeana.api.translation.service.google.GoogleLangDetectService; diff --git a/translation-web/src/main/java/eu/europeana/api/translation/config/TranslationServiceProvider.java b/translation-web/src/main/java/eu/europeana/api/translation/config/TranslationServiceProvider.java index 925c0fa5..9007c1e7 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/config/TranslationServiceProvider.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/config/TranslationServiceProvider.java @@ -18,10 +18,10 @@ import eu.europeana.api.translation.config.services.TranslationServiceCfg; import eu.europeana.api.translation.config.services.TranslationServicesConfiguration; import eu.europeana.api.translation.definitions.language.LanguagePair; -import eu.europeana.api.translation.definitions.service.LanguageDetectionService; -import eu.europeana.api.translation.definitions.service.TranslationService; -import eu.europeana.api.translation.definitions.service.exception.LangDetectionServiceConfigurationException; -import eu.europeana.api.translation.definitions.service.exception.TranslationServiceConfigurationException; +import eu.europeana.api.translation.service.LanguageDetectionService; +import eu.europeana.api.translation.service.TranslationService; +import eu.europeana.api.translation.service.exception.LangDetectionServiceConfigurationException; +import eu.europeana.api.translation.service.exception.TranslationServiceConfigurationException; /** * Class used to read the traslation service configurations, validate them, initialize mapping for diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/BaseRest.java b/translation-web/src/main/java/eu/europeana/api/translation/web/BaseRest.java index 31de21ae..56355655 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/web/BaseRest.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/web/BaseRest.java @@ -1,6 +1,7 @@ package eu.europeana.api.translation.web; import java.io.IOException; +import java.util.List; import java.util.Optional; import javax.servlet.http.HttpServletRequest; import org.apache.logging.log4j.LogManager; @@ -104,4 +105,15 @@ public Authentication verifyReadAccess(HttpServletRequest request) } return null; } + + + protected boolean containsNullValues(List texts) { + for (String text : texts) { + if(text == null) { + return true; + } + } + return false; + } + } diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/LangDetectionController.java b/translation-web/src/main/java/eu/europeana/api/translation/web/LangDetectionController.java index 083dda53..13504fdb 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/web/LangDetectionController.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/web/LangDetectionController.java @@ -49,7 +49,7 @@ public ResponseEntity detectLang(@RequestBody LangDetectRequest langDete private void validateRequest(LangDetectRequest langDetectRequest) throws ParamValidationException { // validate mandatory params - if (langDetectRequest.getText() == null) { + if (langDetectRequest.getText() == null || containsNullValues(langDetectRequest.getText())) { throw new ParamValidationException(null, ERROR_MANDATORY_PARAM_EMPTY, ERROR_MANDATORY_PARAM_EMPTY, new String[] {TEXT}); } diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/TranslationController.java b/translation-web/src/main/java/eu/europeana/api/translation/web/TranslationController.java index f8b0db00..2fcac639 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/web/TranslationController.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/web/TranslationController.java @@ -2,6 +2,7 @@ import static eu.europeana.api.translation.web.I18nErrorMessageKeys.ERROR_INVALID_PARAM_VALUE; import static eu.europeana.api.translation.web.I18nErrorMessageKeys.ERROR_MANDATORY_PARAM_EMPTY; +import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -49,18 +50,18 @@ public ResponseEntity translate(@RequestBody TranslationRequest translRe private void validateRequest(TranslationRequest translationRequest) throws ParamValidationException { // validate mandatory params - if (translationRequest.getText() == null) { - throw new ParamValidationException(null, null, ERROR_MANDATORY_PARAM_EMPTY, new String[] {TranslationAppConstants.TEXT}); + if (translationRequest.getText() == null || containsNullValues(translationRequest.getText())) { + throw new ParamValidationException(null, ERROR_MANDATORY_PARAM_EMPTY, ERROR_MANDATORY_PARAM_EMPTY, new String[] {TranslationAppConstants.TEXT}); } if (StringUtils.isEmpty(translationRequest.getTarget())) { - throw new ParamValidationException(null, null, ERROR_MANDATORY_PARAM_EMPTY, new String[] {TranslationAppConstants.TARGET_LANG}); + throw new ParamValidationException(null, ERROR_MANDATORY_PARAM_EMPTY, ERROR_MANDATORY_PARAM_EMPTY, new String[] {TranslationAppConstants.TARGET_LANG}); } //validate language pair final LanguagePair languagePair = new LanguagePair(translationRequest.getSource(), translationRequest.getTarget()); if(!translationService.isTranslationSupported(languagePair)) { - throw new ParamValidationException(null, null, ERROR_INVALID_PARAM_VALUE, new String[] {LanguagePair.generateKey(TranslationAppConstants.SOURCE_LANG, TranslationAppConstants.TARGET_LANG) , languagePair.toString()}); + throw new ParamValidationException(null, ERROR_MANDATORY_PARAM_EMPTY, ERROR_INVALID_PARAM_VALUE, new String[] {LanguagePair.generateKey(TranslationAppConstants.SOURCE_LANG, TranslationAppConstants.TARGET_LANG) , languagePair.toString()}); } } diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/TranslationErrorController.java b/translation-web/src/main/java/eu/europeana/api/translation/web/TranslationErrorController.java index c24415ca..596ce47b 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/web/TranslationErrorController.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/web/TranslationErrorController.java @@ -1,29 +1,30 @@ -package eu.europeana.api.translation.web; - -import java.util.Map; -import javax.servlet.http.HttpServletRequest; -import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController; -import org.springframework.boot.web.error.ErrorAttributeOptions; -import org.springframework.boot.web.servlet.error.ErrorAttributes; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; -import eu.europeana.api.commons.web.http.HttpHeaders; - -/** - * Created by luthien on 2019-08-13. - */ -@RestController -public class TranslationErrorController extends AbstractErrorController { - - public TranslationErrorController(ErrorAttributes errorAttributes) { - super(errorAttributes); - } - - - @RequestMapping(value = "/error", produces = {HttpHeaders.CONTENT_TYPE_JSON_UTF8, HttpHeaders.CONTENT_TYPE_JSONLD}) - @ResponseBody - public Map error(final HttpServletRequest request) { - return this.getErrorAttributes(request, ErrorAttributeOptions.defaults()); - } -} +package eu.europeana.api.translation.web; + +import java.util.Map; +import javax.servlet.http.HttpServletRequest; +import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController; +import org.springframework.boot.web.error.ErrorAttributeOptions; +import org.springframework.boot.web.servlet.error.ErrorAttributes; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; +import eu.europeana.api.commons.web.http.HttpHeaders; + +/** + * Created by luthien on 2019-08-13. + */ +@RestController +public class TranslationErrorController extends AbstractErrorController { + + public TranslationErrorController(ErrorAttributes errorAttributes) { + super(errorAttributes); + } + + + @RequestMapping(value = "/error", produces = {HttpHeaders.CONTENT_TYPE_JSON_UTF8, HttpHeaders.CONTENT_TYPE_JSONLD}) + @ResponseBody + public Map error(final HttpServletRequest request) { + return this.getErrorAttributes(request, ErrorAttributeOptions.defaults()); + } + +} diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/exception/GlobalExceptionHandler.java b/translation-web/src/main/java/eu/europeana/api/translation/web/exception/GlobalExceptionHandler.java index c48eb027..dacba194 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/web/exception/GlobalExceptionHandler.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/web/exception/GlobalExceptionHandler.java @@ -1,10 +1,16 @@ package eu.europeana.api.translation.web.exception; +import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; import eu.europeana.api.commons.config.i18n.I18nService; +import eu.europeana.api.commons.error.EuropeanaApiErrorResponse; import eu.europeana.api.commons.web.exception.EuropeanaGlobalExceptionHandler; import eu.europeana.api.translation.config.BeanNames; import eu.europeana.api.translation.web.service.RequestPathMethodService; @@ -32,4 +38,25 @@ public I18nService getI18nService() { return i18nService; } + + /** + * HttpMessageNotReadableException thrown when a required parameter is not included in a request. + * @param e the exception indicating the request message parsing error + * @param httpRequest the request object + */ + @ExceptionHandler + public ResponseEntity handleInputValidationError(HttpMessageNotReadableException e, HttpServletRequest httpRequest) { + HttpStatus responseStatus = HttpStatus.BAD_REQUEST; + EuropeanaApiErrorResponse response = (new EuropeanaApiErrorResponse.Builder(httpRequest, e, stackTraceEnabled())) + .setStatus(responseStatus.value()) + .setError(responseStatus.getReasonPhrase()) + .setMessage("Invalid request body: " + e.getMessage()) + .setSeeAlso(getSeeAlso()) + .build(); + + return ResponseEntity + .status(responseStatus) + .headers(createHttpHeaders(httpRequest)) + .body(response); + } } diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/service/BaseWebService.java b/translation-web/src/main/java/eu/europeana/api/translation/web/service/BaseWebService.java index 37a4297b..f58cf4eb 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/web/service/BaseWebService.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/web/service/BaseWebService.java @@ -6,8 +6,8 @@ import static org.springframework.http.HttpStatus.GATEWAY_TIMEOUT; import com.google.api.gax.rpc.ResourceExhaustedException; import eu.europeana.api.commons.error.EuropeanaI18nApiException; -import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException; -import eu.europeana.api.translation.definitions.service.exception.TranslationException; +import eu.europeana.api.translation.service.exception.LanguageDetectionException; +import eu.europeana.api.translation.service.exception.TranslationException; import eu.europeana.api.translation.web.exception.ExternalServiceCallException; import eu.europeana.api.translation.web.exception.GoogleResourceExhaustedException; diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/service/LangDetectionWebService.java b/translation-web/src/main/java/eu/europeana/api/translation/web/service/LangDetectionWebService.java index 79fd67b3..6fe900de 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/web/service/LangDetectionWebService.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/web/service/LangDetectionWebService.java @@ -12,11 +12,11 @@ import org.springframework.stereotype.Service; import eu.europeana.api.commons.error.EuropeanaI18nApiException; import eu.europeana.api.translation.config.TranslationServiceProvider; -import eu.europeana.api.translation.definitions.service.LanguageDetectionService; -import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException; import eu.europeana.api.translation.definitions.vocabulary.TranslationAppConstants; import eu.europeana.api.translation.model.LangDetectRequest; import eu.europeana.api.translation.model.LangDetectResponse; +import eu.europeana.api.translation.service.LanguageDetectionService; +import eu.europeana.api.translation.service.exception.LanguageDetectionException; import eu.europeana.api.translation.web.exception.ParamValidationException; @Service diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/service/TranslationWebService.java b/translation-web/src/main/java/eu/europeana/api/translation/web/service/TranslationWebService.java index 0b50f18e..dbb868d8 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/web/service/TranslationWebService.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/web/service/TranslationWebService.java @@ -1,6 +1,6 @@ package eu.europeana.api.translation.web.service; -import static eu.europeana.api.translation.web.I18nErrorMessageKeys.*; +import static eu.europeana.api.translation.web.I18nErrorMessageKeys.ERROR_INVALID_PARAM_VALUE; import java.util.List; import javax.annotation.PreDestroy; import org.apache.logging.log4j.LogManager; @@ -11,11 +11,11 @@ import eu.europeana.api.translation.config.TranslationServiceProvider; import eu.europeana.api.translation.config.services.TranslationLangPairCfg; import eu.europeana.api.translation.definitions.language.LanguagePair; -import eu.europeana.api.translation.definitions.service.TranslationService; -import eu.europeana.api.translation.definitions.service.exception.TranslationException; import eu.europeana.api.translation.definitions.vocabulary.TranslationAppConstants; import eu.europeana.api.translation.model.TranslationRequest; import eu.europeana.api.translation.model.TranslationResponse; +import eu.europeana.api.translation.service.TranslationService; +import eu.europeana.api.translation.service.exception.TranslationException; import eu.europeana.api.translation.web.exception.ParamValidationException; @Service From 01f94c7cd5a6052f3a84ebaa896c32ac53deaa95 Mon Sep 17 00:00:00 2001 From: GordeaS Date: Tue, 7 Nov 2023 17:06:49 +0100 Subject: [PATCH 2/2] updated doc and method name #EA-3589 --- .../web/exception/GlobalExceptionHandler.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/exception/GlobalExceptionHandler.java b/translation-web/src/main/java/eu/europeana/api/translation/web/exception/GlobalExceptionHandler.java index dacba194..e649ea02 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/web/exception/GlobalExceptionHandler.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/web/exception/GlobalExceptionHandler.java @@ -33,19 +33,13 @@ public GlobalExceptionHandler(RequestPathMethodService requestPathMethodService, this.i18nService = i18nService; } - @Override - public I18nService getI18nService() { - return i18nService; - } - - /** - * HttpMessageNotReadableException thrown when a required parameter is not included in a request. + * HttpMessageNotReadableException thrown when the request body is not parsable to the declared input of the handler method * @param e the exception indicating the request message parsing error * @param httpRequest the request object */ @ExceptionHandler - public ResponseEntity handleInputValidationError(HttpMessageNotReadableException e, HttpServletRequest httpRequest) { + public ResponseEntity handleRequestBodyNotParsableError(HttpMessageNotReadableException e, HttpServletRequest httpRequest) { HttpStatus responseStatus = HttpStatus.BAD_REQUEST; EuropeanaApiErrorResponse response = (new EuropeanaApiErrorResponse.Builder(httpRequest, e, stackTraceEnabled())) .setStatus(responseStatus.value()) @@ -59,4 +53,9 @@ public ResponseEntity handleInputValidationError(Http .headers(createHttpHeaders(httpRequest)) .body(response); } + + @Override + public I18nService getI18nService() { + return i18nService; + } }