Skip to content

Commit

Permalink
eTransl improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
SrdjanStevanetic committed Apr 15, 2024
1 parent 25985c2 commit 8950eed
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
6 changes: 6 additions & 0 deletions translation-service-etranslation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>

<dependency>
<groupId>eu.europeana.api</groupId>
<artifactId>translation-service-common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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<TranslationObj> translationObjs) throws TranslationException {
Expand All @@ -86,10 +115,7 @@ public void translate(List<TranslationObj> 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) {
Expand Down Expand Up @@ -120,7 +146,7 @@ private void createRedisMessageListenerAndWaitForResults(List<TranslationObj> 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;
}
Expand All @@ -133,7 +159,7 @@ private void createRedisMessageListenerAndWaitForResults(List<TranslationObj> 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
Expand Down Expand Up @@ -206,15 +232,26 @@ 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();
HttpPost request = new HttpPost(baseUrl);
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8950eed

Please sign in to comment.