Skip to content

Commit

Permalink
enhanced request validation, refactored services to service-common
Browse files Browse the repository at this point in the history
module #EA-3589
  • Loading branch information
GordeaS authored and GordeaS committed Nov 7, 2023
1 parent b3586b7 commit 5f2c00f
Show file tree
Hide file tree
Showing 33 changed files with 192 additions and 127 deletions.
6 changes: 4 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

<modules>
<module>translation-definitions</module>
<module>translation-web</module>
<module>translation-tests</module>
<module>translation-service-common</module>
<module>translation-service-google</module>
<module>translation-service-pangeanic</module>
<module>translation-web</module>
<module>translation-tests</module>

</modules>

<repositories>
Expand Down
12 changes: 12 additions & 0 deletions translation-service-common/AbstractTranslationService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package eu.europeana.api.translation.service.pangeanic;

import org.apache.commons.lang3.StringUtils;
import eu.europeana.api.translation.definitions.language.Language;

public class AbstractTranslationService {

public static boolean noTranslationRequired(String lang) {
return (lang == null || StringUtils.equals(lang, Language.NO_LINGUISTIC_CONTENT)
|| StringUtils.equals(lang, Language.ENGLISH));
}
}
31 changes: 31 additions & 0 deletions translation-service-common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>eu.europeana.api</groupId>
<artifactId>translation-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>translation-service-common</artifactId>
<name>translation-service-common</name>
<description>Common functionality to be reused by specific implementation of language detection and translation services</description>

<properties>
<sonar.coverage.jacoco.xmlReportPaths>${basedir}/../${aggregate.report.xml}</sonar.coverage.jacoco.xmlReportPaths>
</properties>

<dependencies>
<dependency>
<groupId>eu.europeana.api</groupId>
<artifactId>translation-definitions</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package eu.europeana.api.translation.service;

public abstract class AbstractTranslationService implements TranslationService {


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.europeana.api.translation.definitions.service;
package eu.europeana.api.translation.service;

import java.util.List;
import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException;
import eu.europeana.api.translation.service.exception.LanguageDetectionException;

public interface LanguageDetectionService {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.europeana.api.translation.definitions.service;
package eu.europeana.api.translation.service;

import java.util.List;
import eu.europeana.api.translation.definitions.service.exception.TranslationException;
import eu.europeana.api.translation.service.exception.TranslationException;

/**
* Generic translation service interface
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.europeana.api.translation.definitions.service.exception;
package eu.europeana.api.translation.service.exception;

/**
* Exception that is thrown when there is an error using the translation service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.europeana.api.translation.definitions.service.exception;
package eu.europeana.api.translation.service.exception;

/**
* Exception that is thrown when there is an error using the translation service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.europeana.api.translation.definitions.service.exception;
package eu.europeana.api.translation.service.exception;

/**
* Exception that is thrown when there is an error using the translation service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.europeana.api.translation.definitions.service.exception;
package eu.europeana.api.translation.service.exception;

/**
* Exception that is thrown when there is an error using the translation service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.europeana.api.translation.definitions.util;
package eu.europeana.api.translation.service.util;

/**
* Utilities for logging
Expand Down
7 changes: 1 addition & 6 deletions translation-service-google/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<dependencies>
<dependency>
<groupId>eu.europeana.api</groupId>
<artifactId>translation-definitions</artifactId>
<artifactId>translation-service-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

Expand All @@ -39,11 +39,6 @@
<version>2.19.0</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException;
import eu.europeana.api.translation.service.exception.LanguageDetectionException;

/**
* Dummy implementation preventing invocation of remote google service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.europeana.api.translation.service.google;

import java.util.List;
import eu.europeana.api.translation.definitions.service.exception.TranslationException;
import eu.europeana.api.translation.service.exception.TranslationException;

/**
* Dummy implementation preventing invocation of remote google service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import com.google.cloud.translate.v3.DetectLanguageRequest.Builder;
import com.google.cloud.translate.v3.DetectLanguageResponse;
import com.google.cloud.translate.v3.LocationName;
import eu.europeana.api.translation.definitions.service.LanguageDetectionService;
import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException;
import eu.europeana.api.translation.service.LanguageDetectionService;
import eu.europeana.api.translation.service.exception.LanguageDetectionException;

/**
* Translation service implementing remote invocation of google language detection service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import com.google.cloud.translate.v3.TranslateTextRequest.Builder;
import com.google.cloud.translate.v3.TranslateTextResponse;
import com.google.cloud.translate.v3.Translation;
import eu.europeana.api.translation.definitions.service.TranslationService;
import eu.europeana.api.translation.definitions.service.exception.TranslationException;
import eu.europeana.api.translation.service.AbstractTranslationService;
import eu.europeana.api.translation.service.TranslationService;
import eu.europeana.api.translation.service.exception.TranslationException;

/**
* Translation service implementing remote invocation of google language detection service
Expand All @@ -18,7 +19,7 @@
* @author GordeaS
*
*/
public class GoogleTranslationService implements TranslationService {
public class GoogleTranslationService extends AbstractTranslationService implements TranslationService {

private static final String MIME_TYPE_TEXT = "text/plain";
private final String googleProjectId;
Expand Down
7 changes: 1 addition & 6 deletions translation-service-pangeanic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@
<dependencies>
<dependency>
<groupId>eu.europeana.api</groupId>
<artifactId>translation-definitions</artifactId>
<artifactId>translation-service-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException;
import eu.europeana.api.translation.service.exception.LanguageDetectionException;

/**
* Dummy implementation preventing invocation of remote pangeanic service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.europeana.api.translation.service.pangeanic;

import java.util.List;
import eu.europeana.api.translation.definitions.service.exception.TranslationException;
import eu.europeana.api.translation.service.exception.TranslationException;

/**
* Dummy implementation preventing invocation of remote pangeanic service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import eu.europeana.api.translation.definitions.service.LanguageDetectionService;
import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException;
import eu.europeana.api.translation.service.LanguageDetectionService;
import eu.europeana.api.translation.service.exception.LanguageDetectionException;

public class PangeanicLangDetectService implements LanguageDetectionService {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import eu.europeana.api.translation.definitions.language.PangeanicLanguages;
import eu.europeana.api.translation.definitions.service.TranslationService;
import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException;
import eu.europeana.api.translation.definitions.service.exception.TranslationException;
import eu.europeana.api.translation.definitions.util.LoggingUtils;
import eu.europeana.api.translation.service.AbstractTranslationService;
import eu.europeana.api.translation.service.TranslationService;
import eu.europeana.api.translation.service.exception.LanguageDetectionException;
import eu.europeana.api.translation.service.exception.TranslationException;
import eu.europeana.api.translation.service.util.LoggingUtils;

/**
* Service to send data to translate to Pangeanic Translate API V2
*
* @author Srishti Singh
*/
// TODO get api key, for now passed empty
public class PangeanicTranslationService implements TranslationService {
public class PangeanicTranslationService extends AbstractTranslationService implements TranslationService {

private PangeanicLangDetectService langDetectService;

Expand Down Expand Up @@ -112,10 +113,12 @@ public List<String> translate(List<String> texts, String targetLanguage, String
// lang-detection first and later will translated
return translateWithLangDetect(texts, targetLanguage, sourceLanguage);
}

//regular invocation of external translation service
HttpPost post = PangeanicTranslationUtils.createTranslateRequest(getExternalServiceEndPoint(),
texts, targetLanguage, sourceLanguage, "");
return PangeanicTranslationUtils.getResults(texts,
sendTranslateRequestAndParse(post, sourceLanguage), false);
sendTranslateRequestAndParse(post, sourceLanguage));
} catch (JSONException e) {
throw new TranslationException("Exception occured during Pangeanic translation!",
HttpStatus.SC_BAD_GATEWAY, e);
Expand Down Expand Up @@ -148,8 +151,7 @@ private List<String> translateWithLangDetect(List<String> texts, String targetLa
List<String> detectedLanguages = detectLanguages(texts, langHint);
Map<String, String> translations =
computeTranslations(texts, targetLanguage, detectedLanguages, langHint);
return PangeanicTranslationUtils.getResults(texts, translations,
PangeanicTranslationUtils.nonTranslatedDataExists(detectedLanguages));
return PangeanicTranslationUtils.getResults(texts, translations);
} catch (JSONException | IOException e) {
throw new TranslationException("Exception occured during Pangeanic translation!",
HttpStatus.SC_BAD_GATEWAY, e);
Expand All @@ -173,18 +175,9 @@ private Map<String, String> computeTranslations(List<String> texts, String targe
}

for (Map.Entry<String, List<String>> entry : detectedLangValueMap.entrySet()) {
if (PangeanicTranslationUtils.noTranslationRequired(entry.getKey())) {
if (LOG.isDebugEnabled()) {
LOG.debug("NOT translating data for lang {} for detected values {} ",
LoggingUtils.sanitizeUserInput(entry.getKey()),
LoggingUtils.sanitizeUserInput(entry.getValue().toString()));
}
//TODO translations.put ... original value, this code should be refactored
} else {
HttpPost translateRequest = PangeanicTranslationUtils.createTranslateRequest(
getExternalServiceEndPoint(), entry.getValue(), targetLanguage, entry.getKey(), "");
translations.putAll(sendTranslateRequestAndParse(translateRequest, entry.getKey()));
}
translations.putAll(sendTranslateRequestAndParse(translateRequest, entry.getKey()));
}
return translations;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ public static Map<String, List<String>> getDetectedLangValueMap(List<String> tex

/**
* Returns the translations.
* If nonTranslatedDataExists is true then add non-translated values as it is
*
* SG: outdated documentation from record translations
*
* LOGIC :
* if there is a size mismatch then the order the translation according to text sequence is performed.
* As if multiple language were detected the order of translation result will vary.
Expand All @@ -185,37 +185,26 @@ public static Map<String, List<String>> getDetectedLangValueMap(List<String> tex
*
* @param texts original values sent for translations
* @param translateResult
* @param nonTranslatedDataExists if language zxx or na was detected
* @return
*/
public static List<String> getResults(List<String> texts, Map<String, String> translateResult, boolean nonTranslatedDataExists) {
public static List<String> getResults(List<String> texts, Map<String, String> translateResult) {
List<String> translations = new ArrayList<>();
if (texts.size() != translateResult.size()) {
for (String text : texts) {
if (translateResult.containsKey(text)) {
translations.add(translateResult.get(text));
} else if (nonTranslatedDataExists) {
// add non-translated values as it is. Only if "zxx" or no-lang detected responses were present.
translations.add(text);
}
}
} else {
// if (texts.size() != translateResult.size()) {
// for (String text : texts) {
// if (translateResult.containsKey(text)) {
// translations.add(translateResult.get(text));
// } else {
// //if (nonTranslatedDataExists) {
// // add non-translated values as it is. Only if "zxx" or no-lang detected responses were present.
// translations.add(text);
// }
// }
// } else {
for (Map.Entry<String, String> entry : translateResult.entrySet()) {
translations.add(entry.getValue());
}
}
// }
return translations;
}

public static boolean noTranslationRequired(String lang) {
return ( lang == null || StringUtils.equals(lang, Language.NO_LINGUISTIC_CONTENT)
|| StringUtils.equals(lang, Language.ENGLISH));
}

public static boolean nonTranslatedDataExists(List<String> detectedLanguages) {
return (detectedLanguages.contains(Language.NO_LINGUISTIC_CONTENT)
|| detectedLanguages.contains(null)
|| detectedLanguages.contains(Language.ENGLISH));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import org.springframework.web.context.WebApplicationContext;
import eu.europeana.api.translation.TranslationApp;
import eu.europeana.api.translation.config.TranslationServiceProvider;
import eu.europeana.api.translation.definitions.service.exception.LangDetectionServiceConfigurationException;
import eu.europeana.api.translation.definitions.service.exception.TranslationServiceConfigurationException;
import eu.europeana.api.translation.service.exception.LangDetectionServiceConfigurationException;
import eu.europeana.api.translation.service.exception.TranslationServiceConfigurationException;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import eu.europeana.api.commons.config.i18n.I18nService;
import eu.europeana.api.commons.config.i18n.I18nServiceImpl;
import eu.europeana.api.commons.oauth2.service.impl.EuropeanaClientDetailsService;
import eu.europeana.api.translation.definitions.service.exception.LangDetectionServiceConfigurationException;
import eu.europeana.api.translation.definitions.service.exception.TranslationServiceConfigurationException;
import eu.europeana.api.translation.service.exception.LangDetectionServiceConfigurationException;
import eu.europeana.api.translation.service.exception.TranslationServiceConfigurationException;
import eu.europeana.api.translation.service.google.DummyGLangDetectService;
import eu.europeana.api.translation.service.google.DummyGTranslateService;
import eu.europeana.api.translation.service.google.GoogleLangDetectService;
Expand Down
Loading

0 comments on commit 5f2c00f

Please sign in to comment.