Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eTransl improved logging #48

Merged
merged 6 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
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 All @@ -160,7 +186,7 @@ private void createRedisMessageListenerAndWaitForResults(List<TranslationObj> tr
* @throws TranslationException
*/
private String generateJointHtmlForTranslation(List<TranslationObj> translationObjs) throws TranslationException {
StringBuilder translJointString=new StringBuilder(100);
StringBuilder translJointString=new StringBuilder();
translJointString.append("<!DOCTYPE html>\n<htlm>\n<body>\n");
for(TranslationObj translObj : translationObjs) {
translJointString.append("<p>");
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,7 @@
@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 external-reference: {}", externalReference);

Check notice

Code scanning / SonarCloud

Logging should not be vulnerable to injection attacks Low

Change this code to not log user-controlled data. See more on SonarCloud
}
if(externalReference!=null && body!=null) {
redisTemplate.convertAndSend(externalReference, body);
Expand Down
Loading