diff --git a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/language/LanguagePair.java b/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/language/LanguagePair.java index 7bd91e89..d73b8876 100644 --- a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/language/LanguagePair.java +++ b/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/language/LanguagePair.java @@ -5,61 +5,65 @@ import eu.europeana.api.translation.definitions.vocabulary.TranslationAppConstants; /** - * Class to hold the Language pair values supported by the Translation services - * Mostly for the future Translation API, when we have more than one translation service + * Class to hold the Language pair values supported by the Translation services Mostly for the + * future Translation API, when we have more than one translation service * * @author Hugo * @since 5 Apr 2023 */ public class LanguagePair implements Comparable { - private String srcLang; - private String targetLang; + private String srcLang; + private String targetLang; - public LanguagePair(String srcLang, @NotNull String targetLang) { - this.srcLang = srcLang; - this.targetLang = targetLang; + public LanguagePair(String srcLang, @NotNull String targetLang) { + this.srcLang = srcLang; + this.targetLang = targetLang; + } + + public String getSrcLang() { + return srcLang; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; } - public String getSrcLang() { - return srcLang; + if (!(this.getClass() == obj.getClass())) { + return false; } - @Override - public boolean equals(Object obj) { - if (!(obj instanceof LanguagePair)) { - return false; - } + LanguagePair pair = (LanguagePair) obj; + return StringUtils.equals(targetLang, pair.targetLang) + && StringUtils.equals(srcLang, pair.srcLang); + } - LanguagePair pair = (LanguagePair) obj; - return StringUtils.equals(targetLang, pair.targetLang) - && StringUtils.equals(srcLang, pair.srcLang); + @Override + public int compareTo(LanguagePair pair) { + int ret = targetLang.compareTo(pair.targetLang); + if (ret == 0) { + ret = StringUtils.compare(srcLang, pair.srcLang); } + return ret; + } - @Override - public int compareTo(LanguagePair pair) { - int ret = targetLang.compareTo(pair.targetLang); - if(ret == 0) { - ret = StringUtils.compare(srcLang, pair.srcLang); - } - return ret; - } - - @Override - public String toString() { - return generateKey(srcLang, targetLang); - } + @Override + public String toString() { + return generateKey(srcLang, targetLang); + } - public static String generateKey(String srcLang, String targetLang) { - return srcLang + TranslationAppConstants.LANG_DELIMITER + targetLang; - } - - @Override - public int hashCode() { - return srcLang==null ? targetLang.hashCode() : srcLang.hashCode() + targetLang.hashCode(); - } + public static String generateKey(String srcLang, String targetLang) { + return srcLang + TranslationAppConstants.LANG_DELIMITER + targetLang; + } - public String getTargetLang() { - return targetLang; - } + @Override + public int hashCode() { + return srcLang == null ? targetLang.hashCode() : srcLang.hashCode() + targetLang.hashCode(); + } + + public String getTargetLang() { + return targetLang; + } } 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 8ae0666a..600f15ef 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 @@ -7,6 +7,7 @@ import com.google.cloud.translate.v3.TranslateTextRequest.Builder; import com.google.cloud.translate.v3.TranslateTextResponse; import com.google.cloud.translate.v3.Translation; +import com.google.cloud.translate.v3.TranslationOrBuilder; import eu.europeana.api.translation.definitions.model.TranslationObj; import eu.europeana.api.translation.service.AbstractTranslationService; import eu.europeana.api.translation.service.exception.TranslationException; @@ -71,7 +72,7 @@ public void translate(List translationObjs) throws TranslationEx } - private void updateFromTranslation( TranslationObj translationObj, Translation translation) { + private void updateFromTranslation( TranslationObj translationObj, TranslationOrBuilder translation) { if(translationObj.getSourceLang()==null) { translationObj.setSourceLang(translation.getDetectedLanguageCode()); } 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 d87f62aa..5fda80ea 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 @@ -175,6 +175,7 @@ private LettuceConnectionFactory getRedisConnectionFactory() { // if redis secure protocol is used (rediss vs. redis) boolean sslEnabled = translationConfig.getRedisConnectionUrl().startsWith("rediss"); if (sslEnabled) { + @SuppressWarnings("external_findsecbugs:PATH_TRAVERSAL_IN") // the trustore path is not user input but application config final File truststore = new File(FilenameUtils.normalize(translationConfig.getTruststorePath())); SslOptions sslOptions = SslOptions.builder().jdkSslProvider() .truststore(truststore, diff --git a/translation-web/src/main/java/eu/europeana/api/translation/config/TranslationConfig.java b/translation-web/src/main/java/eu/europeana/api/translation/config/TranslationConfig.java index 114c043d..d321005f 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/config/TranslationConfig.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/config/TranslationConfig.java @@ -19,7 +19,7 @@ @PropertySource(value = "translation.user.properties", ignoreResourceNotFound = true)}) public class TranslationConfig{ - private final Logger LOG = LogManager.getLogger(TranslationConfig.class); + private static final Logger LOG = LogManager.getLogger(TranslationConfig.class); /** Matches spring.profiles.active property in test/resource application.properties file */ public static final String ACTIVE_TEST_PROFILE = "test"; diff --git a/translation-web/src/main/java/eu/europeana/api/translation/config/WebMvcConfig.java b/translation-web/src/main/java/eu/europeana/api/translation/config/WebMvcConfig.java index 017d27fe..ced49a62 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/config/WebMvcConfig.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/config/WebMvcConfig.java @@ -19,6 +19,7 @@ public class WebMvcConfig implements WebMvcConfigurer { /** Setup CORS for all GET, HEAD and OPTIONS, requests. */ @Override + @SuppressWarnings("external_findsecbugs:PERMISSIVE_CORS") //the API is public public void addCorsMappings(CorsRegistry registry) { registry .addMapping("/**") diff --git a/translation-web/src/main/java/eu/europeana/api/translation/serialization/JsonRedisSerializer.java b/translation-web/src/main/java/eu/europeana/api/translation/serialization/JsonRedisSerializer.java index 74ef586a..a67ecadf 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/serialization/JsonRedisSerializer.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/serialization/JsonRedisSerializer.java @@ -39,7 +39,7 @@ public CachedTranslation deserialize(byte[] bytes) throws SerializationException try { return om.readValue(bytes, CachedTranslation.class); } catch (IOException e) { - throw new SerializationException(e.getMessage(), e); + throw new SerializationException("Cannot deserialize redis response: " + e.getMessage()); } } diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/TranslationErrorController.java b/translation-web/src/main/java/eu/europeana/api/translation/web/TranslationErrorController.java index 596ce47b..216dfc71 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/web/TranslationErrorController.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/web/TranslationErrorController.java @@ -6,6 +6,7 @@ import org.springframework.boot.web.error.ErrorAttributeOptions; import org.springframework.boot.web.servlet.error.ErrorAttributes; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import eu.europeana.api.commons.web.http.HttpHeaders; @@ -21,7 +22,7 @@ public TranslationErrorController(ErrorAttributes errorAttributes) { } - @RequestMapping(value = "/error", produces = {HttpHeaders.CONTENT_TYPE_JSON_UTF8, HttpHeaders.CONTENT_TYPE_JSONLD}) + @RequestMapping(value = "/error", produces = {HttpHeaders.CONTENT_TYPE_JSON_UTF8, HttpHeaders.CONTENT_TYPE_JSONLD}, method = {RequestMethod.GET, RequestMethod.POST}) @ResponseBody public Map error(final HttpServletRequest request) { return this.getErrorAttributes(request, ErrorAttributeOptions.defaults()); diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/service/RedisCacheService.java b/translation-web/src/main/java/eu/europeana/api/translation/web/service/RedisCacheService.java index 712270f4..2f4bcb98 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/web/service/RedisCacheService.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/web/service/RedisCacheService.java @@ -1,5 +1,6 @@ package eu.europeana.api.translation.web.service; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Base64; import java.util.HashMap; @@ -159,7 +160,7 @@ public void deleteAll() { public String generateRedisKey(String inputText, String sourceLang, String targetLang) { StringBuilder builder = (new StringBuilder()).append(sourceLang).append(targetLang); byte[] hash = Base64.getEncoder().withoutPadding().encode(Ints.toByteArray(inputText.hashCode())); - builder.append(new String(hash)); + builder.append(new String(hash, StandardCharsets.UTF_8)); return builder.toString(); }