From 1942db9a4ec14ff38c9f0b607de6b492c0b09c22 Mon Sep 17 00:00:00 2001 From: SrdjanStevanetic Date: Tue, 3 Oct 2023 07:55:23 +0200 Subject: [PATCH 1/6] added dummy services for stress testing --- .../api/translation/config/BeanNames.java | 2 + .../config/TranslationApiAutoconfig.java | 62 +++++++++++++++++++ .../config/TranslationServiceProvider.java | 2 +- .../web/service/DummyLangDetectService.java | 46 ++++++++++++++ .../web/service/DummyTranslationService.java | 52 ++++++++++++++++ .../src/main/resources/translation.properties | 5 +- 6 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyLangDetectService.java create mode 100644 translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyTranslationService.java diff --git a/translation-web/src/main/java/eu/europeana/api/translation/config/BeanNames.java b/translation-web/src/main/java/eu/europeana/api/translation/config/BeanNames.java index 1bbc0c8e..ffef269c 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/config/BeanNames.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/config/BeanNames.java @@ -8,6 +8,8 @@ public interface BeanNames { String BEAN_GOOGLE_TRANSLATION_CLIENT_WRAPPER = "googleTranslationClientWrapper"; String BEAN_GOOGLE_TRANSLATION_SERVICE = "googleTranslationService"; String BEAN_GOOGLE_LANG_DETECT_SERVICE = "googleLangDetectService"; + String BEAN_DUMMY_TRANSLATION_SERVICE = "dummyTranslationService"; + String BEAN_DUMMY_LANG_DETECT_SERVICE = "dummyLangDetectService"; String BEAN_I18N_SERVICE = "i18nService"; String BEAN_CLIENT_DETAILS_SERVICE = "europeanaClientDetailsService"; 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 b675453c..6f7221a4 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 @@ -8,7 +8,9 @@ import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; @@ -16,10 +18,14 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.annotation.PropertySources; import org.springframework.context.support.ReloadableResourceBundleMessageSource; 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.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.google.GoogleLangDetectService; @@ -27,9 +33,16 @@ import eu.europeana.api.translation.service.google.GoogleTranslationServiceClientWrapper; import eu.europeana.api.translation.service.pangeanic.PangeanicLangDetectService; import eu.europeana.api.translation.service.pangeanic.PangeanicTranslationService; +import eu.europeana.api.translation.web.service.DummyLangDetectService; +import eu.europeana.api.translation.web.service.DummyTranslationService; @Configuration() +@PropertySources({@PropertySource("classpath:translation.properties"), +@PropertySource(value = "translation.user.properties", ignoreResourceNotFound = true)}) public class TranslationApiAutoconfig implements ApplicationListener{ + + @Value("${use.dummy.services:false}") + private boolean useDummyServices; private final TranslationConfig translationConfig; TranslationServiceProvider translationServiceConfigProvider; @@ -95,6 +108,18 @@ public GoogleTranslationService getGoogleTranslationService( @Qualifier(BeanNames.BEAN_GOOGLE_TRANSLATION_CLIENT_WRAPPER) GoogleTranslationServiceClientWrapper googleTranslationServiceClientWrapper) { return new GoogleTranslationService(translationConfig.getGoogleTranslateProjectId(), googleTranslationServiceClientWrapper); } + + @Bean(BeanNames.BEAN_DUMMY_LANG_DETECT_SERVICE) + @ConditionalOnProperty(value="use.dummy.services", havingValue = "true") + public DummyLangDetectService getDummyLangDetectService() { + return new DummyLangDetectService(); + } + + @Bean(BeanNames.BEAN_DUMMY_TRANSLATION_SERVICE) + @ConditionalOnProperty(value="use.dummy.services", havingValue = "true") + public DummyTranslationService getDummyTranslationService() { + return new DummyTranslationService(); + } @Bean(BeanNames.BEAN_SERVICE_PROVIDER) @DependsOn(value = {BeanNames.BEAN_PANGEANIC_LANG_DETECT_SERVICE, @@ -115,6 +140,16 @@ public void onApplicationEvent(ApplicationStartedEvent event) { printRegisteredBeans(event.getApplicationContext()); } + //load either normal or dummy services (used for stress testing) + if(! useDummyServices) { + loadServices(event); + } + else { + loadDummyServices(event); + } + } + + private void loadServices(ApplicationStartedEvent event) { try { // verify required configurations for initialization of translation services verifyMandatoryProperties(event.getApplicationContext()); @@ -131,6 +166,33 @@ public void onApplicationEvent(ApplicationStartedEvent event) { } } + private void loadDummyServices(ApplicationStartedEvent event) { + try { + TranslationServiceProvider translationServiceProvider = + (TranslationServiceProvider) event.getApplicationContext().getBean(BeanNames.BEAN_SERVICE_PROVIDER); + + //needed for the validation of the supported languages in the requests + translationServiceProvider.readServiceConfigurations(); + + TranslationService dummyTranslService = (TranslationService) event.getApplicationContext() + .getBean(BeanNames.BEAN_DUMMY_TRANSLATION_SERVICE, DummyTranslationService.class); + translationServiceProvider.getTranslationServices().put(dummyTranslService.getServiceId(), dummyTranslService); + + LanguageDetectionService dummyLangDetectService = (LanguageDetectionService) event.getApplicationContext() + .getBean(BeanNames.BEAN_DUMMY_LANG_DETECT_SERVICE, DummyLangDetectService.class); + translationServiceProvider.getLangDetectServices().put(dummyLangDetectService.getServiceId(), dummyLangDetectService); + } + catch (Exception e) { + // gracefully stop the application in case of configuration problems (code 1 means exception + // occured at startup) + logger.fatal( + "Stopping application. Dummy Translation Service initialization failed due to configuration errors or unavailable dummy service beans!", + e); + System.exit(SpringApplication.exit(event.getApplicationContext(), () -> 1)); + } + + } + public void initTranslationServices(ApplicationContext ctx) throws TranslationServiceConfigurationException, LangDetectionServiceConfigurationException { TranslationServiceProvider translationServiceProvider = (TranslationServiceProvider) ctx.getBean(BeanNames.BEAN_SERVICE_PROVIDER); 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 0cb22fa6..7094b235 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 @@ -80,7 +80,7 @@ public void initTranslationServicesConfiguration() validateAndInitServices(); } - private void readServiceConfigurations() + public void readServiceConfigurations() throws TranslationServiceConfigurationException { try(InputStream inputStream = getClass().getResourceAsStream(getServiceConfigFile())){ BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyLangDetectService.java b/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyLangDetectService.java new file mode 100644 index 00000000..b36c0b7e --- /dev/null +++ b/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyLangDetectService.java @@ -0,0 +1,46 @@ +package eu.europeana.api.translation.web.service; + +import java.util.List; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import eu.europeana.api.translation.definitions.service.LanguageDetectionService; +import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException; + +public class DummyLangDetectService implements LanguageDetectionService { + + protected static final Logger LOG = LogManager.getLogger(DummyLangDetectService.class); + private final String serviceId = "DUMMY"; + + public DummyLangDetectService() { + } + + @Override + public boolean isSupported(String srcLang) { + return true; + } + + @Override + public List detectLang(List texts, String langHint) + throws LanguageDetectionException { + return texts; + } + + @Override + public void close() { + } + + @Override + public String getServiceId() { + return serviceId; + } + + @Override + public void setServiceId(String serviceId) { + } + + @Override + public String getExternalServiceEndPoint() { + return null; + } + +} diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyTranslationService.java b/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyTranslationService.java new file mode 100644 index 00000000..8e31b78b --- /dev/null +++ b/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyTranslationService.java @@ -0,0 +1,52 @@ +package eu.europeana.api.translation.web.service; + +import java.util.List; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import eu.europeana.api.translation.definitions.service.TranslationService; +import eu.europeana.api.translation.definitions.service.exception.TranslationException; + +public class DummyTranslationService implements TranslationService { + + protected static final Logger LOG = LogManager.getLogger(DummyTranslationService.class); + private final String serviceId="DUMMY"; + + public DummyTranslationService() { + } + + @Override + public boolean isSupported(String srcLang, String targetLanguage) { + return true; + } + + @Override + public List translate(List texts, String targetLanguage, String sourceLanguage) + throws TranslationException { + return texts; + } + + @Override + public List translate(List texts, String targetLanguage) + throws TranslationException { + return translate(texts, targetLanguage, null); + } + + @Override + public void close() { + } + + @Override + public String getExternalServiceEndPoint() { + return null; + } + + @Override + public String getServiceId() { + return serviceId; + } + + @Override + public void setServiceId(String serviceId) { + } + +} diff --git a/translation-web/src/main/resources/translation.properties b/translation-web/src/main/resources/translation.properties index 2c547b75..df759cd4 100644 --- a/translation-web/src/main/resources/translation.properties +++ b/translation-web/src/main/resources/translation.properties @@ -13,4 +13,7 @@ auth.read.enabled=true auth.write.enabled=true # preffer http over the grpc protocol -translation.google.usehttpclient=false \ No newline at end of file +translation.google.usehttpclient=false + +# For using dummy services +#use.dummy.services=true \ No newline at end of file From 64e661af0a4cd08065007ca21070f6e3902a9ecf Mon Sep 17 00:00:00 2001 From: SrdjanStevanetic Date: Tue, 3 Oct 2023 08:01:07 +0200 Subject: [PATCH 2/6] fix sonar bugs --- .../translation/web/service/DummyLangDetectService.java | 7 +------ .../translation/web/service/DummyTranslationService.java | 7 +------ 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyLangDetectService.java b/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyLangDetectService.java index b36c0b7e..f48d7443 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyLangDetectService.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyLangDetectService.java @@ -1,16 +1,11 @@ package eu.europeana.api.translation.web.service; import java.util.List; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import eu.europeana.api.translation.definitions.service.LanguageDetectionService; import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException; public class DummyLangDetectService implements LanguageDetectionService { - protected static final Logger LOG = LogManager.getLogger(DummyLangDetectService.class); - private final String serviceId = "DUMMY"; - public DummyLangDetectService() { } @@ -31,7 +26,7 @@ public void close() { @Override public String getServiceId() { - return serviceId; + return "DUMMY"; } @Override diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyTranslationService.java b/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyTranslationService.java index 8e31b78b..a5144675 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyTranslationService.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyTranslationService.java @@ -1,16 +1,11 @@ package eu.europeana.api.translation.web.service; import java.util.List; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import eu.europeana.api.translation.definitions.service.TranslationService; import eu.europeana.api.translation.definitions.service.exception.TranslationException; public class DummyTranslationService implements TranslationService { - protected static final Logger LOG = LogManager.getLogger(DummyTranslationService.class); - private final String serviceId="DUMMY"; - public DummyTranslationService() { } @@ -42,7 +37,7 @@ public String getExternalServiceEndPoint() { @Override public String getServiceId() { - return serviceId; + return "DUMMY"; } @Override From a23a4b1d4542529f4fe8ab1570ac0d9e803bdc25 Mon Sep 17 00:00:00 2001 From: GordeaS <4517853+gsergiu@users.noreply.github.com> Date: Fri, 13 Oct 2023 14:36:27 +0200 Subject: [PATCH 3/6] dummy implementation of translation & detection services #EA-3587 --- .../google/DummyGLangDetectService.java | 9 + .../google/DummyGTranslateService.java | 11 ++ ...GoogleTranslationServiceClientWrapper.java | 4 +- .../config/TranslationApiAutoconfig.java | 58 +------ .../config/TranslationServiceProvider.java | 160 ++++++++++-------- 5 files changed, 116 insertions(+), 126 deletions(-) create mode 100644 translation-service-google/src/main/java/eu/europeana/api/translation/service/google/DummyGLangDetectService.java create mode 100644 translation-service-google/src/main/java/eu/europeana/api/translation/service/google/DummyGTranslateService.java 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 new file mode 100644 index 00000000..beab26d7 --- /dev/null +++ b/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/DummyGLangDetectService.java @@ -0,0 +1,9 @@ +package eu.europeana.api.translation.service.google; + +public class DummyGLangDetectService extends GoogleLangDetectService { + + public DummyGLangDetectService(String googleProjectId, + GoogleTranslationServiceClientWrapper clientWrapperBean) { + super(googleProjectId, clientWrapperBean); + } +} 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 new file mode 100644 index 00000000..22b1a89e --- /dev/null +++ b/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/DummyGTranslateService.java @@ -0,0 +1,11 @@ +package eu.europeana.api.translation.service.google; + +public class DummyGTranslateService extends GoogleTranslationService { + + public DummyGTranslateService(String googleProjectId, + GoogleTranslationServiceClientWrapper clientWrapperBean) { + super(googleProjectId, clientWrapperBean); + // TODO Auto-generated constructor stub + } + +} diff --git a/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleTranslationServiceClientWrapper.java b/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleTranslationServiceClientWrapper.java index e0879bb0..41950cbf 100644 --- a/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleTranslationServiceClientWrapper.java +++ b/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleTranslationServiceClientWrapper.java @@ -25,8 +25,8 @@ public GoogleTranslationServiceClientWrapper(String projectId, boolean useHttpCl private void initClient(String projectId, boolean useHttpClient) throws IOException { // allow service mocking - final boolean initClientConnection = !"google-test".equals(projectId); - if(! initClientConnection) { + boolean skipInitialization = "google-test".equals(projectId); + if(skipInitialization) { return; } 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 6f7221a4..d6523a34 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 @@ -19,13 +19,10 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; import org.springframework.context.annotation.PropertySource; -import org.springframework.context.annotation.PropertySources; import org.springframework.context.support.ReloadableResourceBundleMessageSource; 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.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.google.GoogleLangDetectService; @@ -37,8 +34,8 @@ import eu.europeana.api.translation.web.service.DummyTranslationService; @Configuration() -@PropertySources({@PropertySource("classpath:translation.properties"), -@PropertySource(value = "translation.user.properties", ignoreResourceNotFound = true)}) +@PropertySource("classpath:translation.properties") +@PropertySource(value = "translation.user.properties", ignoreResourceNotFound = true) public class TranslationApiAutoconfig implements ApplicationListener{ @Value("${use.dummy.services:false}") @@ -109,18 +106,6 @@ public GoogleTranslationService getGoogleTranslationService( return new GoogleTranslationService(translationConfig.getGoogleTranslateProjectId(), googleTranslationServiceClientWrapper); } - @Bean(BeanNames.BEAN_DUMMY_LANG_DETECT_SERVICE) - @ConditionalOnProperty(value="use.dummy.services", havingValue = "true") - public DummyLangDetectService getDummyLangDetectService() { - return new DummyLangDetectService(); - } - - @Bean(BeanNames.BEAN_DUMMY_TRANSLATION_SERVICE) - @ConditionalOnProperty(value="use.dummy.services", havingValue = "true") - public DummyTranslationService getDummyTranslationService() { - return new DummyTranslationService(); - } - @Bean(BeanNames.BEAN_SERVICE_PROVIDER) @DependsOn(value = {BeanNames.BEAN_PANGEANIC_LANG_DETECT_SERVICE, BeanNames.BEAN_PANGEANIC_TRANSLATION_SERVICE, BeanNames.BEAN_GOOGLE_TRANSLATION_SERVICE}) @@ -134,19 +119,13 @@ public TranslationServiceProvider getTranslationServiceProvider() { @Override public void onApplicationEvent(ApplicationStartedEvent event) { - // TODO Auto-generated method stub // log beans for debuging purposes if (logger.isDebugEnabled()) { printRegisteredBeans(event.getApplicationContext()); } - //load either normal or dummy services (used for stress testing) - if(! useDummyServices) { - loadServices(event); - } - else { - loadDummyServices(event); - } + //load either normal or dummy services (used for stress testing) + loadServices(event); } private void loadServices(ApplicationStartedEvent event) { @@ -166,37 +145,10 @@ private void loadServices(ApplicationStartedEvent event) { } } - private void loadDummyServices(ApplicationStartedEvent event) { - try { - TranslationServiceProvider translationServiceProvider = - (TranslationServiceProvider) event.getApplicationContext().getBean(BeanNames.BEAN_SERVICE_PROVIDER); - - //needed for the validation of the supported languages in the requests - translationServiceProvider.readServiceConfigurations(); - - TranslationService dummyTranslService = (TranslationService) event.getApplicationContext() - .getBean(BeanNames.BEAN_DUMMY_TRANSLATION_SERVICE, DummyTranslationService.class); - translationServiceProvider.getTranslationServices().put(dummyTranslService.getServiceId(), dummyTranslService); - - LanguageDetectionService dummyLangDetectService = (LanguageDetectionService) event.getApplicationContext() - .getBean(BeanNames.BEAN_DUMMY_LANG_DETECT_SERVICE, DummyLangDetectService.class); - translationServiceProvider.getLangDetectServices().put(dummyLangDetectService.getServiceId(), dummyLangDetectService); - } - catch (Exception e) { - // gracefully stop the application in case of configuration problems (code 1 means exception - // occured at startup) - logger.fatal( - "Stopping application. Dummy Translation Service initialization failed due to configuration errors or unavailable dummy service beans!", - e); - System.exit(SpringApplication.exit(event.getApplicationContext(), () -> 1)); - } - - } - public void initTranslationServices(ApplicationContext ctx) throws TranslationServiceConfigurationException, LangDetectionServiceConfigurationException { TranslationServiceProvider translationServiceProvider = (TranslationServiceProvider) ctx.getBean(BeanNames.BEAN_SERVICE_PROVIDER); - translationServiceProvider.initTranslationServicesConfiguration(); + translationServiceProvider.initTranslationServicesConfiguration(useDummyServices); } public void verifyMandatoryProperties(ApplicationContext ctx) { 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 7094b235..f52cd469 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 @@ -22,9 +22,13 @@ 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.web.service.DummyLangDetectService; +import eu.europeana.api.translation.web.service.DummyTranslationService; /** - * Class used to read the traslation service configurations, validate them, initialize mapping for language detection and translation services + * Class used to read the traslation service configurations, validate them, initialize mapping for + * language detection and translation services + * * @author GordeaS * */ @@ -50,6 +54,7 @@ public TranslationServiceProvider() { /** * Construtor using an atenitive config file + * * @param serviceConfigFile a config file available in classpath */ public TranslationServiceProvider(String serviceConfigFile) { @@ -69,33 +74,47 @@ public Map getTranslationServices() { } /** - * Initialization of language detection and translation services - * @throws TranslationServiceConfigurationException if translations services are not properly configured - * @throws LangDetectionServiceConfigurationException if language detection services are not properly configured + * Initialization of language detection and translation services + * + * @param useDummyServices boolean flag if the mock implementation for the services needs to be + * used + * @throws TranslationServiceConfigurationException if translations services are not properly + * configured + * @throws LangDetectionServiceConfigurationException if language detection services are not + * properly configured */ - public void initTranslationServicesConfiguration() + public void initTranslationServicesConfiguration(boolean useDummyServices) throws TranslationServiceConfigurationException, LangDetectionServiceConfigurationException { - // init translation services - readServiceConfigurations(); - validateAndInitServices(); + // init translation services + readServiceConfigurations(); + validateAndInitServices(useDummyServices); } - - public void readServiceConfigurations() - throws TranslationServiceConfigurationException { - try(InputStream inputStream = getClass().getResourceAsStream(getServiceConfigFile())){ + + public void readServiceConfigurations() throws TranslationServiceConfigurationException { + try (InputStream inputStream = getClass().getResourceAsStream(getServiceConfigFile())) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String content = reader.lines().collect(Collectors.joining(System.lineSeparator())); translationServicesConfig = new ObjectMapper().readValue(content, TranslationServicesConfiguration.class); } catch (IOException e) { - throw new TranslationServiceConfigurationException( - "Cannot read serviceConfigfile!", e); + throw new TranslationServiceConfigurationException("Cannot read serviceConfigfile!", e); } } - private void validateAndInitServices() throws TranslationServiceConfigurationException, LangDetectionServiceConfigurationException { - validateDetectServiceCfg(); - validateTranslationServiceCfg(); + private void validateAndInitServices(boolean useDummyServices) + throws TranslationServiceConfigurationException, LangDetectionServiceConfigurationException { + if (useDummyServices) { + + TranslationService dummyTranslationService = new DummyTranslationService(); + getTranslationServices().put(dummyTranslationService.getServiceId(), dummyTranslationService); + + LanguageDetectionService dummyLangDetectService = new DummyLangDetectService(); + getLangDetectServices().put(dummyLangDetectService.getServiceId(), dummyLangDetectService); + + } else { + validateDetectServiceCfg(); + validateTranslationServiceCfg(); + } } private void validateTranslationServiceCfg() throws TranslationServiceConfigurationException { @@ -105,20 +124,19 @@ private void validateTranslationServiceCfg() throws TranslationServiceConfigurat validateTranslationServices(); // check that a default service id is a valid one validateDefaultTranslationService(); - // init language mappings + // init language mappings validateAndInitLanguageMappings(); // validate all languages from the supported section are actually supported validateSupportedLanguagePairs(); } - private void validateSupportedLanguagePairs() - throws TranslationServiceConfigurationException { - final List supportedLanguagePairs = translationServicesConfig.getTranslationConfig() - .getSupported(); + private void validateSupportedLanguagePairs() throws TranslationServiceConfigurationException { + final List supportedLanguagePairs = + translationServicesConfig.getTranslationConfig().getSupported(); for (TranslationLangPairCfg langPair : supportedLanguagePairs) { - //iterate src lang list + // iterate src lang list for (String srcLang : langPair.getSrcLang()) { - //iterate src lang list + // iterate src lang list for (String trgLang : langPair.getTargetLang()) { validateSupportedLanguagePair(srcLang, trgLang); } @@ -128,28 +146,29 @@ private void validateSupportedLanguagePairs() private void validateSupportedLanguagePair(String srcLang, String trgLang) throws TranslationServiceConfigurationException { - //src and taret language must be different - if(srcLang.equals(trgLang)) { + // src and taret language must be different + if (srcLang.equals(trgLang)) { throw new TranslationServiceConfigurationException( - "Invalid configuration for supported language pairs by translation service! Target language must be different from the source language: " + srcLang); + "Invalid configuration for supported language pairs by translation service! Target language must be different from the source language: " + + srcLang); } - - //check if available in language mappings - boolean isSupported = langMappings4TranslateServices.containsKey(LanguagePair.generateKey(srcLang, trgLang)); - if(!isSupported) { - //check if supported by default service + + // check if available in language mappings + boolean isSupported = + langMappings4TranslateServices.containsKey(LanguagePair.generateKey(srcLang, trgLang)); + if (!isSupported) { + // check if supported by default service isSupported = getDefaultTranslationService().isSupported(srcLang, trgLang); } - - if(!isSupported) { + + if (!isSupported) { throw new TranslationServiceConfigurationException( "The translation services do not support all languages declared in the supported section."); } } private void validateDefaultTranslationService() throws TranslationServiceConfigurationException { - if (!getTranslationServices() - .containsKey(getDefaultTranslationServiceId())) { + if (!getTranslationServices().containsKey(getDefaultTranslationServiceId())) { throw new TranslationServiceConfigurationException( "Translation default service id is invalid."); } @@ -158,7 +177,7 @@ private void validateDefaultTranslationService() throws TranslationServiceConfig private String getDefaultTranslationServiceId() { return translationServicesConfig.getTranslationConfig().getDefaultServiceId(); } - + private TranslationService getDefaultTranslationService() { return getTranslationServices().getOrDefault(getDefaultTranslationServiceId(), null); } @@ -186,13 +205,13 @@ private void validateTranslationServices() throws TranslationServiceConfiguratio private void validateAndInitLanguageMappings() throws TranslationServiceConfigurationException { // validate that each service supports the languages declared in the mappings section -// List allMappingsLangPairs = new ArrayList<>(); + // List allMappingsLangPairs = new ArrayList<>(); for (TranslationMappingCfg translMapping : translationServicesConfig.getTranslationConfig() .getMappings()) { final String serviceId = translMapping.getServiceId(); final TranslationService translationService = verifyRegisteredService(serviceId); - - //register language mapping + + // register language mapping for (String srcLang : translMapping.getSrcLang()) { for (String trgLang : translMapping.getTrgLang()) { registerLanguageMapping(translationService, srcLang, trgLang); @@ -203,32 +222,33 @@ private void validateAndInitLanguageMappings() throws TranslationServiceConfigur private void registerLanguageMapping(final TranslationService translationService, String srcLang, String trgLang) throws TranslationServiceConfigurationException { - //for each language pair - if(srcLang.equals(trgLang)) { + // for each language pair + if (srcLang.equals(trgLang)) { throw new TranslationServiceConfigurationException( - "Invalid language mapping in service configurations! Target language must be different from the source language: " + srcLang + - " for service with id: " + translationService.getServiceId()); + "Invalid language mapping in service configurations! Target language must be different from the source language: " + + srcLang + " for service with id: " + translationService.getServiceId()); } - + String key = LanguagePair.generateKey(srcLang, trgLang); - if (!translationService - .isSupported(srcLang, trgLang)) { - throw new TranslationServiceConfigurationException("Invalid service configuration! Translation service: " - + translationService.getServiceId() + ", does not support the language pair: " + key - + ", declared in the mappings section."); + if (!translationService.isSupported(srcLang, trgLang)) { + throw new TranslationServiceConfigurationException( + "Invalid service configuration! Translation service: " + translationService.getServiceId() + + ", does not support the language pair: " + key + + ", declared in the mappings section."); } - - //prevent duplicate language pair mappings - if(getLangMappings4TranslateServices().containsKey(key)) { - throw new TranslationServiceConfigurationException("Dupplicate language mapping in service configurations for key: " + key); + + // prevent duplicate language pair mappings + if (getLangMappings4TranslateServices().containsKey(key)) { + throw new TranslationServiceConfigurationException( + "Dupplicate language mapping in service configurations for key: " + key); } - + getLangMappings4TranslateServices().put(key, translationService); } private TranslationService verifyRegisteredService(final String serviceId) throws TranslationServiceConfigurationException { - //verify if bean is available + // verify if bean is available final boolean isServiceBeanRegistered = getTranslationServices().containsKey(serviceId); if (!isServiceBeanRegistered) { throw new TranslationServiceConfigurationException( @@ -242,26 +262,25 @@ private void validateDetectServiceCfg() throws LangDetectionServiceConfiguration * Validate lang detection config */ validateDeclaredLangDetectionServices(); - // validate default lang detect service + // validate default lang detect service validateDefaultLangDetectServiceConfig(); } private void validateDefaultLangDetectServiceConfig() throws LangDetectionServiceConfigurationException { - final String defaultServiceId = translationServicesConfig.getLangDetectConfig().getDefaultServiceId(); - if (!getLangDetectServices() - .containsKey(defaultServiceId)) { + final String defaultServiceId = + translationServicesConfig.getLangDetectConfig().getDefaultServiceId(); + if (!getLangDetectServices().containsKey(defaultServiceId)) { throw new LangDetectionServiceConfigurationException( "Language detection default service id is invalid."); } - + // validate that the default service supports all languages from the supported section - final LanguageDetectionService defaultLanguageDetectionService = getLangDetectServices() - .get(defaultServiceId); - + final LanguageDetectionService defaultLanguageDetectionService = + getLangDetectServices().get(defaultServiceId); + for (String supportedLang : translationServicesConfig.getLangDetectConfig().getSupported()) { - if (!defaultLanguageDetectionService - .isSupported(supportedLang)) { + if (!defaultLanguageDetectionService.isSupported(supportedLang)) { throw new LangDetectionServiceConfigurationException( "The default language detection service does not support language: " + supportedLang + ", declared in the supported section"); @@ -271,7 +290,7 @@ private void validateDefaultLangDetectServiceConfig() private void validateDeclaredLangDetectionServices() throws LangDetectionServiceConfigurationException { - + for (DetectServiceCfg detectServiceCfg : translationServicesConfig.getLangDetectConfig() .getServices()) { // validate unique service ids @@ -279,18 +298,17 @@ private void validateDeclaredLangDetectionServices() throw new LangDetectionServiceConfigurationException( "Duplicate service id in the language detection config."); } - //find pre-registered bean + // find pre-registered bean LanguageDetectionService detectService; try { final Class beanClass = Class.forName(detectServiceCfg.getClassname()); - detectService = (LanguageDetectionService) applicationContext - .getBean(beanClass); + detectService = (LanguageDetectionService) applicationContext.getBean(beanClass); } catch (BeansException | ClassNotFoundException e) { throw new LangDetectionServiceConfigurationException( "Service bean not available: " + detectServiceCfg.getClassname(), e); } detectService.setServiceId(detectServiceCfg.getId()); - //add bean to service map + // add bean to service map getLangDetectServices().put(detectServiceCfg.getId(), detectService); } } From e87d13cde5adbefe98be47ccf3ca42527f6fb193 Mon Sep 17 00:00:00 2001 From: GordeaS <4517853+gsergiu@users.noreply.github.com> Date: Fri, 13 Oct 2023 15:28:55 +0200 Subject: [PATCH 4/6] dummy translation and detection services #EA-3587 --- .../service/google/DummyGLangDetectService.java | 17 +++++++++++++++++ .../service/google/DummyGTranslateService.java | 15 ++++++++++++++- .../google/GoogleTranslationService.java | 4 ++-- .../pangeanic/DummyPangLangDetectService.java | 9 +++++++++ .../pangeanic/DummyPangTranslationService.java | 9 +++++++++ .../pangeanic/PangeanicLangDetectService.java | 5 +++++ .../pangeanic/PangeanicTranslationService.java | 5 +++++ 7 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/DummyPangLangDetectService.java create mode 100644 translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/DummyPangTranslationService.java 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 beab26d7..3d50473d 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 @@ -1,9 +1,26 @@ package eu.europeana.api.translation.service.google; +import java.util.ArrayList; +import java.util.List; +import org.apache.commons.lang3.StringUtils; +import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException; + public class DummyGLangDetectService extends GoogleLangDetectService { public DummyGLangDetectService(String googleProjectId, GoogleTranslationServiceClientWrapper clientWrapperBean) { super(googleProjectId, clientWrapperBean); } + + @Override + public List detectLang(List texts, String langHint) + throws LanguageDetectionException { + String value = StringUtils.isNotBlank(langHint) ? langHint : "en"; + ArrayList ret = new ArrayList<>(); + + for (int i = 0; i < texts.size(); i++) { + ret.add(value); + } + return ret; + } } 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 22b1a89e..21d162c3 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,11 +1,24 @@ package eu.europeana.api.translation.service.google; +import java.util.List; +import eu.europeana.api.translation.definitions.service.exception.TranslationException; + public class DummyGTranslateService extends GoogleTranslationService { public DummyGTranslateService(String googleProjectId, GoogleTranslationServiceClientWrapper clientWrapperBean) { super(googleProjectId, clientWrapperBean); - // TODO Auto-generated constructor stub } + @Override + public List translate(List text, String targetLanguage, String sourceLanguage) + throws TranslationException { + return translate(text, null); + } + + @Override + public List translate(List text, String targetLanguage) + throws TranslationException { + return text; + } } 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 c70c9afd..9b49c723 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 @@ -40,9 +40,9 @@ public void init(GoogleTranslationServiceClientWrapper clientWrapper) { } @Override - public List translate(List texts, String targetLanguage) + public List translate(List text, String targetLanguage) throws TranslationException { - return translate(texts, targetLanguage, null); + return translate(text, targetLanguage, null); } @Override 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 new file mode 100644 index 00000000..2ae75460 --- /dev/null +++ b/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/DummyPangLangDetectService.java @@ -0,0 +1,9 @@ +package eu.europeana.api.translation.service.pangeanic; + +public class DummyPangLangDetectService extends PangeanicLangDetectService { + + public DummyPangLangDetectService(String endPoint) { + super(null); + } + +} 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 new file mode 100644 index 00000000..9d62ad9f --- /dev/null +++ b/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/DummyPangTranslationService.java @@ -0,0 +1,9 @@ +package eu.europeana.api.translation.service.pangeanic; + +public class DummyPangTranslationService extends PangeanicLangDetectService{ + + public DummyPangTranslationService(String endPoint) { + super(null); + } + +} 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 0ad97195..a0d95d31 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 @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; +import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; @@ -47,6 +48,10 @@ public PangeanicLangDetectService(String endPoint) { * @throws JSONException when there is a problem decoding the received token */ private void init() { + if(StringUtils.isBlank(getExternalServiceEndPoint())) { + return; + } + PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(PangeanicTranslationUtils.MAX_CONNECTIONS); cm.setDefaultMaxPerRoute(PangeanicTranslationUtils.MAX_CONNECTIONS_PER_ROUTE); 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 070606b0..2e5f6aad 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 @@ -5,6 +5,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; @@ -57,6 +58,10 @@ public PangeanicTranslationService(String externalServiceEndpoint, * @throws JSONException when there is a problem decoding the received token */ private void init() { + if(StringUtils.isBlank(getExternalServiceEndPoint())) { + return; + } + PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(PangeanicTranslationUtils.MAX_CONNECTIONS); cm.setDefaultMaxPerRoute(PangeanicTranslationUtils.MAX_CONNECTIONS_PER_ROUTE); From b61f1cf8c6ca82da526a15da280a214050a6eb57 Mon Sep 17 00:00:00 2001 From: GordeaS <4517853+gsergiu@users.noreply.github.com> Date: Fri, 13 Oct 2023 15:58:14 +0200 Subject: [PATCH 5/6] update autoconfiguration to use dummy services configurations #EA-3587 --- ...GoogleTranslationServiceClientWrapper.java | 2 +- .../pangeanic/DummyPangLangDetectService.java | 2 +- .../DummyPangTranslationService.java | 6 +- .../config/TranslationApiAutoconfig.java | 104 +++++++++++------- .../config/TranslationServiceProvider.java | 20 +--- .../web/service/DummyLangDetectService.java | 41 ------- .../web/service/DummyTranslationService.java | 47 -------- 7 files changed, 73 insertions(+), 149 deletions(-) delete mode 100644 translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyLangDetectService.java delete mode 100644 translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyTranslationService.java diff --git a/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleTranslationServiceClientWrapper.java b/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleTranslationServiceClientWrapper.java index 5b3f6be1..83a84457 100644 --- a/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleTranslationServiceClientWrapper.java +++ b/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleTranslationServiceClientWrapper.java @@ -10,7 +10,7 @@ public class GoogleTranslationServiceClientWrapper { - private static final String MOCK_CLIENT_PROJ_ID = "google-test"; + public static final String MOCK_CLIENT_PROJ_ID = "google-test"; private final Logger logger = LogManager.getLogger(getClass()); 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 2ae75460..6240a095 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 @@ -2,7 +2,7 @@ public class DummyPangLangDetectService extends PangeanicLangDetectService { - public DummyPangLangDetectService(String endPoint) { + public DummyPangLangDetectService() { super(null); } 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 9d62ad9f..89533682 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,9 +1,9 @@ package eu.europeana.api.translation.service.pangeanic; -public class DummyPangTranslationService extends PangeanicLangDetectService{ +public class DummyPangTranslationService extends PangeanicTranslationService{ - public DummyPangTranslationService(String endPoint) { - super(null); + public DummyPangTranslationService() { + super(null, null); } } 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 d6523a34..9cf9d5e7 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 @@ -1,5 +1,6 @@ package eu.europeana.api.translation.config; +import static eu.europeana.api.translation.service.google.GoogleTranslationServiceClientWrapper.MOCK_CLIENT_PROJ_ID; import java.io.IOException; import java.util.Arrays; import java.util.Locale; @@ -10,7 +11,6 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; @@ -25,26 +25,28 @@ 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.google.DummyGLangDetectService; +import eu.europeana.api.translation.service.google.DummyGTranslateService; import eu.europeana.api.translation.service.google.GoogleLangDetectService; import eu.europeana.api.translation.service.google.GoogleTranslationService; import eu.europeana.api.translation.service.google.GoogleTranslationServiceClientWrapper; +import eu.europeana.api.translation.service.pangeanic.DummyPangLangDetectService; +import eu.europeana.api.translation.service.pangeanic.DummyPangTranslationService; import eu.europeana.api.translation.service.pangeanic.PangeanicLangDetectService; import eu.europeana.api.translation.service.pangeanic.PangeanicTranslationService; -import eu.europeana.api.translation.web.service.DummyLangDetectService; -import eu.europeana.api.translation.web.service.DummyTranslationService; @Configuration() @PropertySource("classpath:translation.properties") @PropertySource(value = "translation.user.properties", ignoreResourceNotFound = true) -public class TranslationApiAutoconfig implements ApplicationListener{ - +public class TranslationApiAutoconfig implements ApplicationListener { + @Value("${use.dummy.services:false}") private boolean useDummyServices; private final TranslationConfig translationConfig; TranslationServiceProvider translationServiceConfigProvider; private final Logger logger = LogManager.getLogger(TranslationApiAutoconfig.class); - + public TranslationApiAutoconfig(@Autowired TranslationConfig translationConfig) { this.translationConfig = translationConfig; @@ -71,41 +73,63 @@ public MessageSource getMessageSource() { messageSource.setDefaultLocale(Locale.ENGLISH); return messageSource; } - + /** - * Creates a new client wrapper that can send translation requests to Google Cloud Translate. Note that - * the client needs to be closed when it's not used anymore - * @throws IOException + * Creates a new client wrapper that can send translation requests to Google Cloud Translate. Note + * that the client needs to be closed when it's not used anymore + * + * @throws IOException */ @Bean(BeanNames.BEAN_GOOGLE_TRANSLATION_CLIENT_WRAPPER) - public GoogleTranslationServiceClientWrapper getGoogleTranslationServiceClientWrapper() throws IOException { - return new GoogleTranslationServiceClientWrapper(translationConfig.getGoogleTranslateProjectId(), translationConfig.useGoogleHttpClient()); + public GoogleTranslationServiceClientWrapper getGoogleTranslationServiceClientWrapper() + throws IOException { + return new GoogleTranslationServiceClientWrapper( + translationConfig.getGoogleTranslateProjectId(), translationConfig.useGoogleHttpClient()); } @Bean(BeanNames.BEAN_PANGEANIC_LANG_DETECT_SERVICE) public PangeanicLangDetectService getPangeanicLangDetectService() { - return new PangeanicLangDetectService(translationConfig.getPangeanicDetectEndpoint()); + if (useDummyServices) { + return new DummyPangLangDetectService(); + } else { + return new PangeanicLangDetectService(translationConfig.getPangeanicDetectEndpoint()); + } } @Bean(BeanNames.BEAN_PANGEANIC_TRANSLATION_SERVICE) public PangeanicTranslationService getPangeanicTranslationService( @Qualifier(BeanNames.BEAN_PANGEANIC_LANG_DETECT_SERVICE) PangeanicLangDetectService pangeanicLangDetectService) { - return new PangeanicTranslationService(translationConfig.getPangeanicTranslateEndpoint(), - pangeanicLangDetectService); + if (useDummyServices) { + return new DummyPangTranslationService(); + } else { + return new PangeanicTranslationService(translationConfig.getPangeanicTranslateEndpoint(), + pangeanicLangDetectService); + } } @Bean(BeanNames.BEAN_GOOGLE_LANG_DETECT_SERVICE) public GoogleLangDetectService getGoogleLangDetectService( @Qualifier(BeanNames.BEAN_GOOGLE_TRANSLATION_CLIENT_WRAPPER) GoogleTranslationServiceClientWrapper googleTranslationServiceClientWrapper) { - return new GoogleLangDetectService(translationConfig.getGoogleTranslateProjectId(), googleTranslationServiceClientWrapper); + if (useDummyServices) { + return new DummyGLangDetectService(MOCK_CLIENT_PROJ_ID, + googleTranslationServiceClientWrapper); + } else { + return new GoogleLangDetectService(translationConfig.getGoogleTranslateProjectId(), + googleTranslationServiceClientWrapper); + } } @Bean(BeanNames.BEAN_GOOGLE_TRANSLATION_SERVICE) public GoogleTranslationService getGoogleTranslationService( @Qualifier(BeanNames.BEAN_GOOGLE_TRANSLATION_CLIENT_WRAPPER) GoogleTranslationServiceClientWrapper googleTranslationServiceClientWrapper) { - return new GoogleTranslationService(translationConfig.getGoogleTranslateProjectId(), googleTranslationServiceClientWrapper); + if (useDummyServices) { + return new DummyGTranslateService(MOCK_CLIENT_PROJ_ID, googleTranslationServiceClientWrapper); + } else { + return new GoogleTranslationService(translationConfig.getGoogleTranslateProjectId(), + googleTranslationServiceClientWrapper); + } } - + @Bean(BeanNames.BEAN_SERVICE_PROVIDER) @DependsOn(value = {BeanNames.BEAN_PANGEANIC_LANG_DETECT_SERVICE, BeanNames.BEAN_PANGEANIC_TRANSLATION_SERVICE, BeanNames.BEAN_GOOGLE_TRANSLATION_SERVICE}) @@ -123,39 +147,41 @@ public void onApplicationEvent(ApplicationStartedEvent event) { if (logger.isDebugEnabled()) { printRegisteredBeans(event.getApplicationContext()); } - - //load either normal or dummy services (used for stress testing) - loadServices(event); + + // load either normal or dummy services (used for stress testing) + loadServices(event); } - + private void loadServices(ApplicationStartedEvent event) { try { // verify required configurations for initialization of translation services verifyMandatoryProperties(event.getApplicationContext()); - + // init translation services initTranslationServices(event.getApplicationContext()); - } catch (Exception e) { - // gracefully stop the application in case of configuration problems (code 1 means exception - // occured at startup) - logger.fatal( - "Stopping application. Translation Service initialization failed due to configuration errors!", - e); - System.exit(SpringApplication.exit(event.getApplicationContext(), () -> 1)); - } + } catch (Exception e) { + // gracefully stop the application in case of configuration problems (code 1 means exception + // occured at startup) + logger.fatal( + "Stopping application. Translation Service initialization failed due to configuration errors!", + e); + System.exit(SpringApplication.exit(event.getApplicationContext(), () -> 1)); + } } - - public void initTranslationServices(ApplicationContext ctx) throws TranslationServiceConfigurationException, LangDetectionServiceConfigurationException { - TranslationServiceProvider translationServiceProvider = - (TranslationServiceProvider) ctx.getBean(BeanNames.BEAN_SERVICE_PROVIDER); - translationServiceProvider.initTranslationServicesConfiguration(useDummyServices); + + public void initTranslationServices(ApplicationContext ctx) + throws TranslationServiceConfigurationException, LangDetectionServiceConfigurationException { + TranslationServiceProvider translationServiceProvider = + (TranslationServiceProvider) ctx.getBean(BeanNames.BEAN_SERVICE_PROVIDER); + translationServiceProvider.initTranslationServicesConfiguration(); } public void verifyMandatoryProperties(ApplicationContext ctx) { - TranslationConfig translationConfig = (TranslationConfig) ctx.getBean(BeanNames.BEAN_TRANSLATION_CONFIG); - translationConfig.verifyRequiredProperties(); + TranslationConfig translationConfig = + (TranslationConfig) ctx.getBean(BeanNames.BEAN_TRANSLATION_CONFIG); + translationConfig.verifyRequiredProperties(); } - + private void printRegisteredBeans(ApplicationContext ctx) { String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); 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 f52cd469..f945277e 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 @@ -22,8 +22,6 @@ 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.web.service.DummyLangDetectService; -import eu.europeana.api.translation.web.service.DummyTranslationService; /** * Class used to read the traslation service configurations, validate them, initialize mapping for @@ -76,18 +74,16 @@ public Map getTranslationServices() { /** * Initialization of language detection and translation services * - * @param useDummyServices boolean flag if the mock implementation for the services needs to be - * used * @throws TranslationServiceConfigurationException if translations services are not properly * configured * @throws LangDetectionServiceConfigurationException if language detection services are not * properly configured */ - public void initTranslationServicesConfiguration(boolean useDummyServices) + public void initTranslationServicesConfiguration() throws TranslationServiceConfigurationException, LangDetectionServiceConfigurationException { // init translation services readServiceConfigurations(); - validateAndInitServices(useDummyServices); + validateAndInitServices(); } public void readServiceConfigurations() throws TranslationServiceConfigurationException { @@ -101,20 +97,10 @@ public void readServiceConfigurations() throws TranslationServiceConfigurationEx } } - private void validateAndInitServices(boolean useDummyServices) + private void validateAndInitServices() throws TranslationServiceConfigurationException, LangDetectionServiceConfigurationException { - if (useDummyServices) { - - TranslationService dummyTranslationService = new DummyTranslationService(); - getTranslationServices().put(dummyTranslationService.getServiceId(), dummyTranslationService); - - LanguageDetectionService dummyLangDetectService = new DummyLangDetectService(); - getLangDetectServices().put(dummyLangDetectService.getServiceId(), dummyLangDetectService); - - } else { validateDetectServiceCfg(); validateTranslationServiceCfg(); - } } private void validateTranslationServiceCfg() throws TranslationServiceConfigurationException { diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyLangDetectService.java b/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyLangDetectService.java deleted file mode 100644 index f48d7443..00000000 --- a/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyLangDetectService.java +++ /dev/null @@ -1,41 +0,0 @@ -package eu.europeana.api.translation.web.service; - -import java.util.List; -import eu.europeana.api.translation.definitions.service.LanguageDetectionService; -import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException; - -public class DummyLangDetectService implements LanguageDetectionService { - - public DummyLangDetectService() { - } - - @Override - public boolean isSupported(String srcLang) { - return true; - } - - @Override - public List detectLang(List texts, String langHint) - throws LanguageDetectionException { - return texts; - } - - @Override - public void close() { - } - - @Override - public String getServiceId() { - return "DUMMY"; - } - - @Override - public void setServiceId(String serviceId) { - } - - @Override - public String getExternalServiceEndPoint() { - return null; - } - -} diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyTranslationService.java b/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyTranslationService.java deleted file mode 100644 index a5144675..00000000 --- a/translation-web/src/main/java/eu/europeana/api/translation/web/service/DummyTranslationService.java +++ /dev/null @@ -1,47 +0,0 @@ -package eu.europeana.api.translation.web.service; - -import java.util.List; -import eu.europeana.api.translation.definitions.service.TranslationService; -import eu.europeana.api.translation.definitions.service.exception.TranslationException; - -public class DummyTranslationService implements TranslationService { - - public DummyTranslationService() { - } - - @Override - public boolean isSupported(String srcLang, String targetLanguage) { - return true; - } - - @Override - public List translate(List texts, String targetLanguage, String sourceLanguage) - throws TranslationException { - return texts; - } - - @Override - public List translate(List texts, String targetLanguage) - throws TranslationException { - return translate(texts, targetLanguage, null); - } - - @Override - public void close() { - } - - @Override - public String getExternalServiceEndPoint() { - return null; - } - - @Override - public String getServiceId() { - return "DUMMY"; - } - - @Override - public void setServiceId(String serviceId) { - } - -} From 3c2e2178aba48e372108179edbf6b2979800f20d Mon Sep 17 00:00:00 2001 From: GordeaS <4517853+gsergiu@users.noreply.github.com> Date: Fri, 13 Oct 2023 16:58:01 +0200 Subject: [PATCH 6/6] implementation of dummy services, added template for user.properties --- .../pangeanic/DummyPangLangDetectService.java | 16 ++++++++ .../DummyPangTranslationService.java | 14 +++++++ .../config/TranslationApiAutoconfig.java | 2 +- .../translation.user.properties.template | 37 +++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 translation-web/src/main/resources/translation.user.properties.template 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 6240a095..27ca2a33 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 @@ -1,9 +1,25 @@ package eu.europeana.api.translation.service.pangeanic; +import java.util.ArrayList; +import java.util.List; +import org.apache.commons.lang3.StringUtils; +import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException; + public class DummyPangLangDetectService extends PangeanicLangDetectService { public DummyPangLangDetectService() { super(null); } + @Override + public List detectLang(List texts, String langHint) + throws LanguageDetectionException { + String value = StringUtils.isNotBlank(langHint) ? langHint : "en"; + ArrayList ret = new ArrayList<>(); + + for (int i = 0; i < texts.size(); i++) { + ret.add(value); + } + return ret; + } } 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 89533682..75b9d7bb 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,9 +1,23 @@ package eu.europeana.api.translation.service.pangeanic; +import java.util.List; +import eu.europeana.api.translation.definitions.service.exception.TranslationException; + public class DummyPangTranslationService extends PangeanicTranslationService{ public DummyPangTranslationService() { super(null, null); } + @Override + public List translate(List text, String targetLanguage, String sourceLanguage) + throws TranslationException { + return translate(text, null); + } + + @Override + public List translate(List text, String targetLanguage) + throws TranslationException { + return text; + } } 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 9cf9d5e7..8c7befb3 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 @@ -40,7 +40,7 @@ @PropertySource(value = "translation.user.properties", ignoreResourceNotFound = true) public class TranslationApiAutoconfig implements ApplicationListener { - @Value("${use.dummy.services:false}") + @Value("${translation.dummy.services:false}") private boolean useDummyServices; private final TranslationConfig translationConfig; diff --git a/translation-web/src/main/resources/translation.user.properties.template b/translation-web/src/main/resources/translation.user.properties.template new file mode 100644 index 00000000..26d73f90 --- /dev/null +++ b/translation-web/src/main/resources/translation.user.properties.template @@ -0,0 +1,37 @@ +#========================================================================= +## test properties +#========================================================================= +# +# Copyright 2007-2012 The Europeana Foundation +# +# Licenced under the EUPL, Version 1.1 (the "Licence") and subsequent versions as approved +# by the European Commission; +# You may not use this work except in compliance with the Licence. +# +# You may obtain a copy of the Licence at: +# http://joinup.ec.europa.eu/software/page/eupl +# +# Unless required by applicable law or agreed to in writing, software distributed under +# the Licence is distributed on an "AS IS" basis, without warranties or conditions of +# any kind, either express or implied. +# See the Licence for the specific language governing permissions and limitations under +# the Licence. +# +translation.pangeanic.endpoint.detect=http://:/NexRelay/v1/detect_language +translation.pangeanic.endpoint.translate=http://:/NexRelay/v1/translate + +translation.google.projectId= + + +auth.write.enabled=true +auth.read.enabled=true + + +#the signature key used to verify JWT tokens +europeana.apikey.jwttoken.siganturekey= + +europeana.apikey.serviceurl= + + +# For using dummy services +#translation.dummy.services=false \ No newline at end of file