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..3d50473d --- /dev/null +++ b/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/DummyGLangDetectService.java @@ -0,0 +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 new file mode 100644 index 00000000..21d162c3 --- /dev/null +++ b/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/DummyGTranslateService.java @@ -0,0 +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); + } + + @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-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 9df131a2..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()); @@ -27,8 +27,8 @@ public GoogleTranslationServiceClientWrapper(String projectId, boolean useHttpCl private void initClient(String projectId, boolean useHttpClient) throws IOException { // allow service mocking - final boolean initClientConnection = isMockService(projectId); - if(! initClientConnection) { + final boolean skipInitialization = isMockService(projectId); + if(skipInitialization) { return; } @@ -59,7 +59,7 @@ private void initClient(String projectId, boolean useHttpClient) throws IOExcept } private boolean isMockService(String projectId) { - return !MOCK_CLIENT_PROJ_ID.equals(projectId); + return MOCK_CLIENT_PROJ_ID.equals(projectId); } public TranslationServiceClient getClient() { 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..27ca2a33 --- /dev/null +++ b/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/DummyPangLangDetectService.java @@ -0,0 +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 new file mode 100644 index 00000000..75b9d7bb --- /dev/null +++ b/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/DummyPangTranslationService.java @@ -0,0 +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-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); 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 8da2d1e9..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 @@ -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; @@ -8,6 +9,7 @@ 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.context.event.ApplicationStartedEvent; import org.springframework.context.ApplicationContext; @@ -16,25 +18,35 @@ 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.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.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; @Configuration() -public class TranslationApiAutoconfig implements ApplicationListener{ +@PropertySource("classpath:translation.properties") +@PropertySource(value = "translation.user.properties", ignoreResourceNotFound = true) +public class TranslationApiAutoconfig implements ApplicationListener { + + @Value("${translation.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; @@ -61,39 +73,61 @@ 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) @@ -113,34 +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); + } + + 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(); + + 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 0cb22fa6..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 @@ -24,7 +24,9 @@ import eu.europeana.api.translation.definitions.service.exception.TranslationServiceConfigurationException; /** - * 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 +52,7 @@ public TranslationServiceProvider() { /** * Construtor using an atenitive config file + * * @param serviceConfigFile a config file available in classpath */ public TranslationServiceProvider(String serviceConfigFile) { @@ -69,33 +72,35 @@ 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 + * + * @throws TranslationServiceConfigurationException if translations services are not properly + * configured + * @throws LangDetectionServiceConfigurationException if language detection services are not + * properly configured */ public void initTranslationServicesConfiguration() throws TranslationServiceConfigurationException, LangDetectionServiceConfigurationException { - // init translation services - readServiceConfigurations(); - validateAndInitServices(); + // init translation services + readServiceConfigurations(); + validateAndInitServices(); } - - private 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() + throws TranslationServiceConfigurationException, LangDetectionServiceConfigurationException { + validateDetectServiceCfg(); + validateTranslationServiceCfg(); } private void validateTranslationServiceCfg() throws TranslationServiceConfigurationException { @@ -105,20 +110,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 +132,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 +163,7 @@ private void validateDefaultTranslationService() throws TranslationServiceConfig private String getDefaultTranslationServiceId() { return translationServicesConfig.getTranslationConfig().getDefaultServiceId(); } - + private TranslationService getDefaultTranslationService() { return getTranslationServices().getOrDefault(getDefaultTranslationServiceId(), null); } @@ -186,13 +191,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 +208,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 +248,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 +276,7 @@ private void validateDefaultLangDetectServiceConfig() private void validateDeclaredLangDetectionServices() throws LangDetectionServiceConfigurationException { - + for (DetectServiceCfg detectServiceCfg : translationServicesConfig.getLangDetectConfig() .getServices()) { // validate unique service ids @@ -279,18 +284,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); } } 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 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