Skip to content

Commit

Permalink
refactoring for review comments #EA-3575
Browse files Browse the repository at this point in the history
  • Loading branch information
gsergiu committed Oct 6, 2023
1 parent 9fcfb52 commit 1df68ba
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,5 @@ public class TranslationAppConstants {
public static final String BUILD_INFO = "build";
public static final String APP_INFO = "app";
public static final String CONFIG_INFO = "config";

//error keys for the exceptions messages
public static final String ERROR_MANDATORY_PARAM_EMPTY = "error.mandatory_param_empty";
public static final String ERROR_INVALID_PARAM_VALUE = "error.invalid_param_value";
public static final String ERROR_UNSUPPORTED_LANG = "error.unsupported_language";
public static final String ERROR_GOOGLE_QUOTA_LIMIT = "error.google_quota_limit_reached";
public static final String ERROR_TRANSLATION_SERVICE_CALL = "error.translation.external_service_call";
public static final String ERROR_LANG_DETECT_SERVICE_CALL = "error.detection.external_service_call";

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

public class GoogleTranslationServiceClientWrapper {

private static final String MOCK_CLIENT_PROJ_ID = "google-test";

private final Logger logger = LogManager.getLogger(getClass());

/**Client that will be used to send requests. This client only needs to be created
Expand All @@ -25,7 +27,7 @@ public GoogleTranslationServiceClientWrapper(String projectId, boolean useHttpCl

private void initClient(String projectId, boolean useHttpClient) throws IOException {
// allow service mocking
final boolean initClientConnection = !"google-test".equals(projectId);
final boolean initClientConnection = isMockService(projectId);
if(! initClientConnection) {
return;
}
Expand Down Expand Up @@ -56,6 +58,10 @@ private void initClient(String projectId, boolean useHttpClient) throws IOExcept
this.closed=false;
}

private boolean isMockService(String projectId) {
return !MOCK_CLIENT_PROJ_ID.equals(projectId);
}

public TranslationServiceClient getClient() {
return client;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package eu.europeana.api.translation.web;

public interface I18nErrorMessageKeys {

//error keys for the exceptions messages
static final String ERROR_MANDATORY_PARAM_EMPTY = "error.mandatory_param_empty";
static final String ERROR_INVALID_PARAM_VALUE = "error.invalid_param_value";
static final String ERROR_UNSUPPORTED_LANG = "error.unsupported_language";
static final String ERROR_GOOGLE_QUOTA_LIMIT = "error.google_quota_limit_reached";
static final String ERROR_TRANSLATION_SERVICE_CALL = "error.translation.external_service_call";
static final String ERROR_LANG_DETECT_SERVICE_CALL = "error.detection.external_service_call";
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package eu.europeana.api.translation.web;

import static eu.europeana.api.translation.definitions.vocabulary.TranslationAppConstants.ERROR_INVALID_PARAM_VALUE;
import static eu.europeana.api.translation.definitions.vocabulary.TranslationAppConstants.ERROR_MANDATORY_PARAM_EMPTY;
import static eu.europeana.api.translation.definitions.vocabulary.TranslationAppConstants.LANG;
import static eu.europeana.api.translation.definitions.vocabulary.TranslationAppConstants.TEXT;
import static eu.europeana.api.translation.web.I18nErrorMessageKeys.ERROR_INVALID_PARAM_VALUE;
import static eu.europeana.api.translation.web.I18nErrorMessageKeys.ERROR_MANDATORY_PARAM_EMPTY;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package eu.europeana.api.translation.web;

import static eu.europeana.api.translation.web.I18nErrorMessageKeys.ERROR_INVALID_PARAM_VALUE;
import static eu.europeana.api.translation.web.I18nErrorMessageKeys.ERROR_MANDATORY_PARAM_EMPTY;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -17,7 +19,6 @@
import eu.europeana.api.translation.web.exception.ParamValidationException;
import eu.europeana.api.translation.web.service.TranslationWebService;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController
@Tag(name = "Translation endpoint", description = "Perform text translation")
public class TranslationController extends BaseRest {
Expand Down Expand Up @@ -49,17 +50,17 @@ public ResponseEntity<String> translate(@RequestBody TranslationRequest translRe
private void validateRequest(TranslationRequest translationRequest) throws ParamValidationException {
// validate mandatory params
if (translationRequest.getText() == null) {
throw new ParamValidationException(null, null, TranslationAppConstants.ERROR_MANDATORY_PARAM_EMPTY, new String[] {TranslationAppConstants.TEXT});
throw new ParamValidationException(null, null, ERROR_MANDATORY_PARAM_EMPTY, new String[] {TranslationAppConstants.TEXT});
}

if (StringUtils.isEmpty(translationRequest.getTarget())) {
throw new ParamValidationException(null, null, TranslationAppConstants.ERROR_MANDATORY_PARAM_EMPTY, new String[] {TranslationAppConstants.TARGET_LANG});
throw new ParamValidationException(null, null, ERROR_MANDATORY_PARAM_EMPTY, new String[] {TranslationAppConstants.TARGET_LANG});
}

//validate language pair
final LanguagePair languagePair = new LanguagePair(translationRequest.getSource(), translationRequest.getTarget());
if(!translationService.isTranslationSupported(languagePair)) {
throw new ParamValidationException(null, null, TranslationAppConstants.ERROR_INVALID_PARAM_VALUE, new String[] {LanguagePair.generateKey(TranslationAppConstants.SOURCE_LANG, TranslationAppConstants.TARGET_LANG) , languagePair.toString()});
throw new ParamValidationException(null, null, ERROR_INVALID_PARAM_VALUE, new String[] {LanguagePair.generateKey(TranslationAppConstants.SOURCE_LANG, TranslationAppConstants.TARGET_LANG) , languagePair.toString()});
}
}

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

import static eu.europeana.api.translation.definitions.vocabulary.TranslationAppConstants.ERROR_GOOGLE_QUOTA_LIMIT;
import static eu.europeana.api.translation.definitions.vocabulary.TranslationAppConstants.ERROR_LANG_DETECT_SERVICE_CALL;
import static eu.europeana.api.translation.definitions.vocabulary.TranslationAppConstants.ERROR_TRANSLATION_SERVICE_CALL;
import static eu.europeana.api.translation.web.I18nErrorMessageKeys.ERROR_GOOGLE_QUOTA_LIMIT;
import static eu.europeana.api.translation.web.I18nErrorMessageKeys.ERROR_LANG_DETECT_SERVICE_CALL;
import static eu.europeana.api.translation.web.I18nErrorMessageKeys.ERROR_TRANSLATION_SERVICE_CALL;
import static org.springframework.http.HttpStatus.GATEWAY_TIMEOUT;
import com.google.api.gax.rpc.ResourceExhaustedException;
import eu.europeana.api.commons.error.EuropeanaI18nApiException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package eu.europeana.api.translation.web.service;

import static eu.europeana.api.translation.web.I18nErrorMessageKeys.ERROR_INVALID_PARAM_VALUE;
import static eu.europeana.api.translation.web.I18nErrorMessageKeys.ERROR_UNSUPPORTED_LANG;
import java.util.List;
import java.util.Locale;
import javax.annotation.PreDestroy;
Expand Down Expand Up @@ -88,11 +90,11 @@ private LanguageDetectionService getServiceInstance(final String requestedServic
if(detectService==null) {
final String paramName = isFallbackService? TranslationAppConstants.FALLBACK: TranslationAppConstants.SERVICE;
final String availableServices = translationServiceProvider.getLangDetectServices().keySet().toString();
throw new ParamValidationException(null, null, TranslationAppConstants.ERROR_INVALID_PARAM_VALUE, new String[] {paramName, requestedServiceId + " (available services: " + availableServices + ")"});
throw new ParamValidationException(null, null, ERROR_INVALID_PARAM_VALUE, new String[] {paramName, requestedServiceId + " (available services: " + availableServices + ")"});
}
//check if the "lang" is supported
if(languageHint!=null && !detectService.isSupported(languageHint)) {
throw new ParamValidationException(null, null, TranslationAppConstants.ERROR_UNSUPPORTED_LANG, new String[] {TranslationAppConstants.LANG, requestedServiceId});
throw new ParamValidationException(null, null, ERROR_UNSUPPORTED_LANG, new String[] {TranslationAppConstants.LANG, requestedServiceId});
}
return detectService;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.europeana.api.translation.web.service;

import static eu.europeana.api.translation.web.I18nErrorMessageKeys.*;
import java.util.List;
import javax.annotation.PreDestroy;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -102,10 +103,10 @@ private TranslationService getTranslationService(final String serviceId,
translationServiceProvider.getTranslationServices().get(serviceId);
String param = fallback ? TranslationAppConstants.FALLBACK : TranslationAppConstants.SERVICE;
if (result == null) {
throw new ParamValidationException(null, null, TranslationAppConstants.ERROR_INVALID_PARAM_VALUE, new String[] {param, serviceId + " (available services: " + String.join(", ", translationServiceProvider.getTranslationServices().keySet()) + ")"});
throw new ParamValidationException(null, null, ERROR_INVALID_PARAM_VALUE, new String[] {param, serviceId + " (available services: " + String.join(", ", translationServiceProvider.getTranslationServices().keySet()) + ")"});
}
if (!result.isSupported(languagePair.getSrcLang(), languagePair.getTargetLang())) {
throw new ParamValidationException(null, null, TranslationAppConstants.ERROR_INVALID_PARAM_VALUE, new String[] {LanguagePair.generateKey(TranslationAppConstants.SOURCE_LANG, TranslationAppConstants.TARGET_LANG) , languagePair.toString()});
throw new ParamValidationException(null, null, ERROR_INVALID_PARAM_VALUE, new String[] {LanguagePair.generateKey(TranslationAppConstants.SOURCE_LANG, TranslationAppConstants.TARGET_LANG) , languagePair.toString()});
}
return result;
}
Expand Down

0 comments on commit 1df68ba

Please sign in to comment.