From 8950eed5cb25fcb7c20d8c28e41a3a56b07a1bda Mon Sep 17 00:00:00 2001 From: SrdjanStevanetic Date: Mon, 15 Apr 2024 13:55:26 +0200 Subject: [PATCH 1/6] eTransl improved logging --- translation-service-etranslation/pom.xml | 6 ++ .../ETranslationTranslationService.java | 59 +++++++++++++++---- .../web/ETranslationCallbackController.java | 3 +- 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/translation-service-etranslation/pom.xml b/translation-service-etranslation/pom.xml index 93a3e803..42a8bd72 100644 --- a/translation-service-etranslation/pom.xml +++ b/translation-service-etranslation/pom.xml @@ -18,6 +18,12 @@ org.springframework.boot spring-boot-starter-data-redis + + + org.springframework + spring-web + + eu.europeana.api translation-service-common diff --git a/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java b/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java index fa80a7a3..f4b47adf 100644 --- a/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java +++ b/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java @@ -7,9 +7,11 @@ import java.util.Locale; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.StringUtils; +import org.apache.http.StatusLine; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.BasicCredentialsProvider; @@ -27,6 +29,7 @@ import org.springframework.data.redis.listener.ChannelTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; +import org.springframework.http.HttpStatus; import eu.europeana.api.translation.definitions.model.TranslationObj; import eu.europeana.api.translation.service.AbstractTranslationService; import eu.europeana.api.translation.service.exception.TranslationException; @@ -49,9 +52,8 @@ public class ETranslationTranslationService extends AbstractTranslationService { public ETranslationTranslationService(String baseUrl, String domain, String callbackUrl, int maxWaitMillisec, String username, String password, RedisMessageListenerContainer redisMessageListenerContainer) throws TranslationException { - if(!baseUrlTests.equals(baseUrl) && (StringUtils.isBlank(baseUrl) || StringUtils.isBlank(domain) || StringUtils.isBlank(callbackUrl) || - maxWaitMillisec<=0 || StringUtils.isBlank(username) || StringUtils.isBlank(password))) { - throw new TranslationException("Invalid eTranslation config parameters."); + if(!baseUrlTests.equals(baseUrl)) { + validateETranslConfigParams(baseUrl, domain, callbackUrl, maxWaitMillisec, username, password); } this.baseUrl = baseUrl; this.domain = domain; @@ -61,6 +63,33 @@ public ETranslationTranslationService(String baseUrl, String domain, String call this.credentialPwd=password; this.redisMessageListenerContainer=redisMessageListenerContainer; } + + private void validateETranslConfigParams(String baseUrl, String domain, String callbackUrl, + int maxWaitMillisec, String username, String password) throws TranslationException { + StringBuilder missingParams=new StringBuilder(100); + if(StringUtils.isBlank(baseUrl)) { + missingParams.append("baseUrl"); + } + if(StringUtils.isBlank(domain)) { + missingParams.append("domain"); + } + if(StringUtils.isBlank(callbackUrl)) { + missingParams.append("callbackUrl"); + } + if(maxWaitMillisec<=0) { + missingParams.append("maxWaitMillisec (must be >0)"); + } + if(StringUtils.isBlank(username)) { + missingParams.append("username"); + } + if(StringUtils.isBlank(password)) { + missingParams.append("password"); + } + + if(! missingParams.isEmpty()) { + throw new TranslationException("Invalid eTranslation config parameters: " + missingParams.toString()); + } + } @Override public void translate(List translationObjs) throws TranslationException { @@ -86,10 +115,7 @@ public void translate(List translationObjs) throws TranslationEx if(! baseUrlTests.equals(baseUrl)) { try { String body = createTranslationBody(eTranslJointStr,translationObjs.get(0).getSourceLang(),translationObjs.get(0).getTargetLang(),eTranslExtRef); - String eTranslRespNumber = createHttpRequest(body); - if(Integer.parseInt(eTranslRespNumber) < 0) { - throw new TranslationException("Invalid eTranslation http request."); - } + createHttpRequest(body); } catch (JSONException | UnsupportedEncodingException e) { throw new TranslationException("Exception during the eTranslation http request body creation.", 0, e); } catch (IOException e) { @@ -120,7 +146,7 @@ private void createRedisMessageListenerAndWaitForResults(List tr } else { if(LOGGER.isDebugEnabled()) { - LOGGER.debug("eTranslation response has not been received after waiting for: " + maxWaitMillisec + " milliseconds."); + LOGGER.debug("eTranslation response has not been received after waiting for: {} milliseconds.", maxWaitMillisec); } break; } @@ -133,7 +159,7 @@ private void createRedisMessageListenerAndWaitForResults(List tr String response=redisMessageListener.getMessage(); //message received, populate the translations if(LOGGER.isDebugEnabled()) { - LOGGER.debug("Received message from redis message listener is: " + response); + LOGGER.debug("Received message from redis message listener is: {}", response); } if(response!=null) { //first base64 decode @@ -206,7 +232,7 @@ private String createTranslationBody(String text, String sourceLang, String targ return jsonBody.toString(); } - private String createHttpRequest(String content) throws IOException { + private void createHttpRequest(String content) throws TranslationException, IOException { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(credentialUsername, credentialPwd)); CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build(); @@ -214,7 +240,18 @@ private String createHttpRequest(String content) throws IOException { StringEntity params = new StringEntity(content, "UTF-8"); request.addHeader("content-type", "application/json"); request.setEntity(params); - return EntityUtils.toString(httpClient.execute(request).getEntity(), "UTF-8"); + + CloseableHttpResponse response = httpClient.execute(request); + StatusLine respStatusLine = response.getStatusLine(); + String respBody=EntityUtils.toString(response.getEntity(), "UTF-8"); + + if(! HttpStatus.valueOf(respStatusLine.getStatusCode()).is2xxSuccessful()) { + throw new TranslationException("Invalid eTranslation http request (not successfull status code in the " + + "immediate response), status code: " + respStatusLine.getStatusCode() + ", reason phrase: " + respStatusLine.getReasonPhrase()); + } + if(Integer.parseInt(respBody) < 0) { + throw new TranslationException("Invalid eTranslation http request with the response code (<0): " + respBody); + } } @Override diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/ETranslationCallbackController.java b/translation-web/src/main/java/eu/europeana/api/translation/web/ETranslationCallbackController.java index 14a09a3e..89caa2e4 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/web/ETranslationCallbackController.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/web/ETranslationCallbackController.java @@ -32,7 +32,8 @@ public void eTranslationCallback( @RequestParam(value = "external-reference", required = true) String externalReference, @RequestBody String body) { if(LOGGER.isDebugEnabled()) { - LOGGER.debug("eTranslation callback on translation api has been received"); + LOGGER.debug("eTranslation callback on translation api has been received with the request-id: {}, " + + "and the external-reference: {}", requestId, externalReference); } if(externalReference!=null && body!=null) { redisTemplate.convertAndSend(externalReference, body); From 3eee219410b25d4af79d596966c5402d9d595091 Mon Sep 17 00:00:00 2001 From: SrdjanStevanetic Date: Mon, 15 Apr 2024 14:07:25 +0200 Subject: [PATCH 2/6] sonar cloud fixes --- .../service/etranslation/ETranslationTranslationService.java | 4 ++-- .../api/translation/web/ETranslationCallbackController.java | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java b/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java index f4b47adf..bf05c52a 100644 --- a/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java +++ b/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java @@ -66,7 +66,7 @@ public ETranslationTranslationService(String baseUrl, String domain, String call private void validateETranslConfigParams(String baseUrl, String domain, String callbackUrl, int maxWaitMillisec, String username, String password) throws TranslationException { - StringBuilder missingParams=new StringBuilder(100); + StringBuilder missingParams=new StringBuilder(); if(StringUtils.isBlank(baseUrl)) { missingParams.append("baseUrl"); } @@ -186,7 +186,7 @@ private void createRedisMessageListenerAndWaitForResults(List tr * @throws TranslationException */ private String generateJointHtmlForTranslation(List translationObjs) throws TranslationException { - StringBuilder translJointString=new StringBuilder(100); + StringBuilder translJointString=new StringBuilder(); translJointString.append("\n\n\n"); for(TranslationObj translObj : translationObjs) { translJointString.append("

"); diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/ETranslationCallbackController.java b/translation-web/src/main/java/eu/europeana/api/translation/web/ETranslationCallbackController.java index 89caa2e4..94ba1b6e 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/web/ETranslationCallbackController.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/web/ETranslationCallbackController.java @@ -32,8 +32,7 @@ public void eTranslationCallback( @RequestParam(value = "external-reference", required = true) String externalReference, @RequestBody String body) { if(LOGGER.isDebugEnabled()) { - LOGGER.debug("eTranslation callback on translation api has been received with the request-id: {}, " - + "and the external-reference: {}", requestId, externalReference); + LOGGER.debug("eTranslation callback on translation api has been received with the external-reference: {}", externalReference); } if(externalReference!=null && body!=null) { redisTemplate.convertAndSend(externalReference, body); From ba83cb4934732c14271f012b42d13e1ff74b206c Mon Sep 17 00:00:00 2001 From: SrdjanStevanetic Date: Mon, 15 Apr 2024 14:25:06 +0200 Subject: [PATCH 3/6] sonar cloud fixes --- .../api/translation/service/util/TranslationUtils.java | 2 ++ .../etranslation/ETranslationTranslationService.java | 9 +++++---- .../translation/web/ETranslationCallbackController.java | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/translation-service-common/src/main/java/eu/europeana/api/translation/service/util/TranslationUtils.java b/translation-service-common/src/main/java/eu/europeana/api/translation/service/util/TranslationUtils.java index 354ee1ab..3b9b3e3a 100644 --- a/translation-service-common/src/main/java/eu/europeana/api/translation/service/util/TranslationUtils.java +++ b/translation-service-common/src/main/java/eu/europeana/api/translation/service/util/TranslationUtils.java @@ -7,6 +7,8 @@ public class TranslationUtils { + public static final int STRING_BUILDER_INIT_SIZE=100; + private TranslationUtils() { } diff --git a/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java b/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java index bf05c52a..3bfc72bb 100644 --- a/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java +++ b/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java @@ -66,7 +66,7 @@ public ETranslationTranslationService(String baseUrl, String domain, String call private void validateETranslConfigParams(String baseUrl, String domain, String callbackUrl, int maxWaitMillisec, String username, String password) throws TranslationException { - StringBuilder missingParams=new StringBuilder(); + StringBuilder missingParams=new StringBuilder(TranslationUtils.STRING_BUILDER_INIT_SIZE); if(StringUtils.isBlank(baseUrl)) { missingParams.append("baseUrl"); } @@ -186,7 +186,7 @@ private void createRedisMessageListenerAndWaitForResults(List tr * @throws TranslationException */ private String generateJointHtmlForTranslation(List translationObjs) throws TranslationException { - StringBuilder translJointString=new StringBuilder(); + StringBuilder translJointString=new StringBuilder(TranslationUtils.STRING_BUILDER_INIT_SIZE); translJointString.append("\n\n\n"); for(TranslationObj translObj : translationObjs) { translJointString.append("

"); @@ -242,13 +242,14 @@ private void createHttpRequest(String content) throws TranslationException, IOEx request.setEntity(params); CloseableHttpResponse response = httpClient.execute(request); + StatusLine respStatusLine = response.getStatusLine(); - String respBody=EntityUtils.toString(response.getEntity(), "UTF-8"); - if(! HttpStatus.valueOf(respStatusLine.getStatusCode()).is2xxSuccessful()) { throw new TranslationException("Invalid eTranslation http request (not successfull status code in the " + "immediate response), status code: " + respStatusLine.getStatusCode() + ", reason phrase: " + respStatusLine.getReasonPhrase()); } + + String respBody=EntityUtils.toString(response.getEntity(), "UTF-8"); if(Integer.parseInt(respBody) < 0) { throw new TranslationException("Invalid eTranslation http request with the response code (<0): " + respBody); } diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/ETranslationCallbackController.java b/translation-web/src/main/java/eu/europeana/api/translation/web/ETranslationCallbackController.java index 94ba1b6e..5b63bd65 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/web/ETranslationCallbackController.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/web/ETranslationCallbackController.java @@ -32,7 +32,7 @@ public void eTranslationCallback( @RequestParam(value = "external-reference", required = true) String externalReference, @RequestBody String body) { if(LOGGER.isDebugEnabled()) { - LOGGER.debug("eTranslation callback on translation api has been received with the external-reference: {}", externalReference); + LOGGER.debug("eTranslation callback on translation api has been received."); } if(externalReference!=null && body!=null) { redisTemplate.convertAndSend(externalReference, body); From 0689f8911edf373c45453935a7ecd0a660ef9b75 Mon Sep 17 00:00:00 2001 From: SrdjanStevanetic Date: Mon, 15 Apr 2024 14:52:33 +0200 Subject: [PATCH 4/6] logging eTransl callback params --- .../service/util/LoggingUtils.java | 21 ------------------- .../web/ETranslationCallbackController.java | 4 +++- 2 files changed, 3 insertions(+), 22 deletions(-) delete mode 100644 translation-service-common/src/main/java/eu/europeana/api/translation/service/util/LoggingUtils.java diff --git a/translation-service-common/src/main/java/eu/europeana/api/translation/service/util/LoggingUtils.java b/translation-service-common/src/main/java/eu/europeana/api/translation/service/util/LoggingUtils.java deleted file mode 100644 index e8450d84..00000000 --- a/translation-service-common/src/main/java/eu/europeana/api/translation/service/util/LoggingUtils.java +++ /dev/null @@ -1,21 +0,0 @@ -package eu.europeana.api.translation.service.util; - -/** - * Utilities for logging - * @author GordeaS - * - */ -public class LoggingUtils { - - //hide default contructor - private LoggingUtils() {} - - /** - * Sanitize user input to prevent malicious code - * @param input user input - * @return input in which newlines are replaces by underscore - */ - public static String sanitizeUserInput(String input) { - return input.replaceAll("[\n\r]", "_"); - } -} diff --git a/translation-web/src/main/java/eu/europeana/api/translation/web/ETranslationCallbackController.java b/translation-web/src/main/java/eu/europeana/api/translation/web/ETranslationCallbackController.java index 5b63bd65..7a792a6d 100644 --- a/translation-web/src/main/java/eu/europeana/api/translation/web/ETranslationCallbackController.java +++ b/translation-web/src/main/java/eu/europeana/api/translation/web/ETranslationCallbackController.java @@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import eu.europeana.api.commons.definitions.utils.LoggingUtils; import eu.europeana.api.translation.web.model.CachedTranslation; import io.swagger.v3.oas.annotations.tags.Tag; @RestController @@ -32,7 +33,8 @@ public void eTranslationCallback( @RequestParam(value = "external-reference", required = true) String externalReference, @RequestBody String body) { if(LOGGER.isDebugEnabled()) { - LOGGER.debug("eTranslation callback on translation api has been received."); + LOGGER.debug("eTranslation callback on translation api has been received with the request-id: {}, and the" + + " external-reference: {}", LoggingUtils.sanitizeUserInput(requestId), LoggingUtils.sanitizeUserInput(externalReference)); } if(externalReference!=null && body!=null) { redisTemplate.convertAndSend(externalReference, body); From b8bf52621790578f20488a31db53fc5ce3e4541f Mon Sep 17 00:00:00 2001 From: SrdjanStevanetic Date: Mon, 15 Apr 2024 17:35:14 +0200 Subject: [PATCH 5/6] import LoggingUtils from api-commons --- .../ETranslationTranslationService.java | 1 + .../pangeanic/PangeanicTranslationService.java | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java b/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java index 3bfc72bb..85f2aaf1 100644 --- a/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java +++ b/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java @@ -242,6 +242,7 @@ private void createHttpRequest(String content) throws TranslationException, IOEx request.setEntity(params); CloseableHttpResponse response = httpClient.execute(request); +// System.out.print("eTransl request sent at: " + java.time.LocalTime.now()); StatusLine respStatusLine = response.getStatusLine(); if(! HttpStatus.valueOf(respStatusLine.getStatusCode()).is2xxSuccessful()) { 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 18441698..99169b41 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 @@ -1,9 +1,14 @@ package eu.europeana.api.translation.service.pangeanic; import java.io.IOException; -import java.util.*; - -import eu.europeana.api.translation.definitions.model.LanguageDetectionObj; +import java.util.ArrayList; +import java.util.EnumMap; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Objects; +import java.util.Properties; +import java.util.Set; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; @@ -19,12 +24,13 @@ import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import eu.europeana.api.commons.definitions.utils.LoggingUtils; +import eu.europeana.api.translation.definitions.model.LanguageDetectionObj; import eu.europeana.api.translation.definitions.model.TranslationObj; import eu.europeana.api.translation.service.AbstractTranslationService; import eu.europeana.api.translation.service.exception.LanguageDetectionException; import eu.europeana.api.translation.service.exception.TranslationException; import eu.europeana.api.translation.service.exception.TranslationServiceConfigurationException; -import eu.europeana.api.translation.service.util.LoggingUtils; /** * Service to send data to translate to Pangeanic Translate API V2 From 2bdbaa5be2d387730daed059ca40637f79a897c3 Mon Sep 17 00:00:00 2001 From: GordeaS Date: Tue, 16 Apr 2024 11:46:35 +0200 Subject: [PATCH 6/6] removed redundant dependency and improved error messages --- pom.xml | 2 +- translation-service-common/pom.xml | 5 +++ translation-service-etranslation/pom.xml | 2 + .../ETranslationTranslationService.java | 43 ++++++++++++------- .../tests/web/LangDetectionRestIT.java | 2 - .../tests/web/TranslationRestIT.java | 1 - 6 files changed, 35 insertions(+), 20 deletions(-) diff --git a/pom.xml b/pom.xml index b90a9a81..f052de4c 100644 --- a/pom.xml +++ b/pom.xml @@ -63,7 +63,7 @@ ${java.version} ${java.version} ${java.version} - 0.3.22 + 0.3.24-SNAPSHOT 1.3 2.5.14 1.6.14 diff --git a/translation-service-common/pom.xml b/translation-service-common/pom.xml index 169d82e9..158b98d9 100644 --- a/translation-service-common/pom.xml +++ b/translation-service-common/pom.xml @@ -16,6 +16,11 @@ + + eu.europeana.api.commons + commons-definitions + ${api-commons.version} + eu.europeana.api translation-definitions diff --git a/translation-service-etranslation/pom.xml b/translation-service-etranslation/pom.xml index 42a8bd72..c950ca37 100644 --- a/translation-service-etranslation/pom.xml +++ b/translation-service-etranslation/pom.xml @@ -19,10 +19,12 @@ spring-boot-starter-data-redis + eu.europeana.api diff --git a/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java b/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java index 85f2aaf1..94f1e427 100644 --- a/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java +++ b/translation-service-etranslation/src/main/java/eu/europeana/api/translation/service/etranslation/ETranslationTranslationService.java @@ -3,10 +3,12 @@ import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.List; import java.util.Locale; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpStatus; import org.apache.http.StatusLine; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; @@ -29,7 +31,6 @@ import org.springframework.data.redis.listener.ChannelTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; -import org.springframework.http.HttpStatus; import eu.europeana.api.translation.definitions.model.TranslationObj; import eu.europeana.api.translation.service.AbstractTranslationService; import eu.europeana.api.translation.service.exception.TranslationException; @@ -66,24 +67,24 @@ public ETranslationTranslationService(String baseUrl, String domain, String call private void validateETranslConfigParams(String baseUrl, String domain, String callbackUrl, int maxWaitMillisec, String username, String password) throws TranslationException { - StringBuilder missingParams=new StringBuilder(TranslationUtils.STRING_BUILDER_INIT_SIZE); + List missingParams= new ArrayList<>(5); if(StringUtils.isBlank(baseUrl)) { - missingParams.append("baseUrl"); + missingParams.add("baseUrl"); } if(StringUtils.isBlank(domain)) { - missingParams.append("domain"); + missingParams.add("domain"); } if(StringUtils.isBlank(callbackUrl)) { - missingParams.append("callbackUrl"); + missingParams.add("callbackUrl"); } if(maxWaitMillisec<=0) { - missingParams.append("maxWaitMillisec (must be >0)"); + missingParams.add("maxWaitMillisec (must be >0)"); } if(StringUtils.isBlank(username)) { - missingParams.append("username"); + missingParams.add("username"); } if(StringUtils.isBlank(password)) { - missingParams.append("password"); + missingParams.add("password"); } if(! missingParams.isEmpty()) { @@ -223,7 +224,6 @@ private String createTranslationBody(String text, String sourceLang, String targ .put("targetLanguages", new JSONArray().put(0, targetLang.toUpperCase(Locale.ENGLISH))) .put("domain", domain) .put("destinations", -// new JSONObject().put("emailDestinations", new JSONArray().put(0, "e-mail"))) new JSONObject().put("httpDestinations", new JSONArray().put(0, callbackUrl))) // .put("textToTranslate", text); .put("documentToTranslateBase64", @@ -232,7 +232,7 @@ private String createTranslationBody(String text, String sourceLang, String targ return jsonBody.toString(); } - private void createHttpRequest(String content) throws TranslationException, IOException { + private long createHttpRequest(String content) throws TranslationException, IOException { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(credentialUsername, credentialPwd)); CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build(); @@ -242,18 +242,29 @@ private void createHttpRequest(String content) throws TranslationException, IOEx request.setEntity(params); CloseableHttpResponse response = httpClient.execute(request); -// System.out.print("eTransl request sent at: " + java.time.LocalTime.now()); StatusLine respStatusLine = response.getStatusLine(); - if(! HttpStatus.valueOf(respStatusLine.getStatusCode()).is2xxSuccessful()) { - throw new TranslationException("Invalid eTranslation http request (not successfull status code in the " - + "immediate response), status code: " + respStatusLine.getStatusCode() + ", reason phrase: " + respStatusLine.getReasonPhrase()); + if(HttpStatus.SC_OK != respStatusLine.getStatusCode()) { + throw new TranslationException("The translation request could not be successfully registered. ETranslation response: " + + respStatusLine.getStatusCode() + ", reason phrase: " + respStatusLine.getReasonPhrase()); } String respBody=EntityUtils.toString(response.getEntity(), "UTF-8"); - if(Integer.parseInt(respBody) < 0) { - throw new TranslationException("Invalid eTranslation http request with the response code (<0): " + respBody); + long requestNumber; + try{ + requestNumber = Long.parseLong(respBody); + if(requestNumber < 0) { + throw wrapETranslationErrorResponse(respBody); + } + } catch (NumberFormatException e) { + throw wrapETranslationErrorResponse(respBody); } + + return requestNumber; + } + + TranslationException wrapETranslationErrorResponse(String respBody) { + return new TranslationException("The translation request could not be successfully registered. ETranslation error response: " + respBody); } @Override diff --git a/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/web/LangDetectionRestIT.java b/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/web/LangDetectionRestIT.java index 419b6f1b..856d3a1c 100644 --- a/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/web/LangDetectionRestIT.java +++ b/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/web/LangDetectionRestIT.java @@ -6,8 +6,6 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import java.io.IOException; -import java.util.Collections; -import java.util.List; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONObject; import org.junit.jupiter.api.Assertions; diff --git a/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/web/TranslationRestIT.java b/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/web/TranslationRestIT.java index 798e6e19..3d4eda42 100644 --- a/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/web/TranslationRestIT.java +++ b/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/web/TranslationRestIT.java @@ -31,7 +31,6 @@ import eu.europeana.api.translation.config.TranslationConfig; import eu.europeana.api.translation.definitions.model.TranslationObj; import eu.europeana.api.translation.definitions.vocabulary.TranslationAppConstants; -import eu.europeana.api.translation.service.etranslation.ETranslationTranslationService; import eu.europeana.api.translation.service.google.GoogleTranslationService; import eu.europeana.api.translation.service.google.GoogleTranslationServiceClientWrapper; import eu.europeana.api.translation.tests.BaseTranslationTest;