Skip to content

Commit

Permalink
EA-3543 sonarq suggestaion
Browse files Browse the repository at this point in the history
  • Loading branch information
SrishtiSingh-eu committed Sep 12, 2023
1 parent 90248d1 commit 6e37831
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
import eu.europeana.api.commons.web.exception.ParamValidationException;
import eu.europeana.api.commons.web.http.HttpHeaders;
import eu.europeana.api.commons.web.model.vocabulary.Operations;
import eu.europeana.api.translation.config.I18nConstants;
import eu.europeana.api.translation.definitions.vocabulary.TranslationAppConstants;
import eu.europeana.api.translation.model.LangDetectRequest;
import eu.europeana.api.translation.model.LangDetectResponse;
import eu.europeana.api.translation.web.service.LangDetectionWebService;
import io.swagger.v3.oas.annotations.tags.Tag;
import static eu.europeana.api.translation.config.I18nConstants.EMPTY_PARAM_MANDATORY;
import static eu.europeana.api.translation.config.I18nConstants.INVALID_SERVICE_PARAM;

@RestController
@Tag(name = "Language Detection endoints", description = "Perform language detection")
@Tag(name = "Language Detection endpoints", description = "Perform language detection")
public class LangDetectionController extends BaseRest {

private final LangDetectionWebService langDetectionService;
Expand All @@ -43,16 +44,22 @@ public ResponseEntity<String> detectLang(@RequestBody LangDetectRequest langDete
return generateResponseEntity(request, resultJson);
}

/**
* Validate the Language detection request.
*
* @param langDetectRequest detection request
* @throws ParamValidationException exception
*/
private void validateRequest(LangDetectRequest langDetectRequest)
throws ParamValidationException {
// validate mandatory params
if (langDetectRequest.getText() == null) {
throw new ParamValidationException(null, I18nConstants.EMPTY_PARAM_MANDATORY,
throw new ParamValidationException(null, EMPTY_PARAM_MANDATORY,
new String[] {TranslationAppConstants.TEXT});
}
//validate language hint if provided
if(langDetectRequest.getLang() != null && !langDetectionService.isLangDetectionSupported(langDetectRequest.getLang())) {
throw new ParamValidationException(null, I18nConstants.INVALID_SERVICE_PARAM,
throw new ParamValidationException(null, INVALID_SERVICE_PARAM,
new String[] {TranslationAppConstants.LANG, langDetectRequest.getLang()});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
import eu.europeana.api.commons.web.exception.ParamValidationException;
import eu.europeana.api.commons.web.http.HttpHeaders;
import eu.europeana.api.commons.web.model.vocabulary.Operations;
import eu.europeana.api.translation.config.I18nConstants;
import eu.europeana.api.translation.definitions.language.LanguagePair;
import eu.europeana.api.translation.definitions.vocabulary.TranslationAppConstants;
import eu.europeana.api.translation.model.TranslationRequest;
import eu.europeana.api.translation.model.TranslationResponse;
import eu.europeana.api.translation.web.service.TranslationWebService;
import io.swagger.v3.oas.annotations.tags.Tag;

import static eu.europeana.api.translation.config.I18nConstants.EMPTY_PARAM_MANDATORY;
import static eu.europeana.api.translation.config.I18nConstants.INVALID_SERVICE_PARAM;

@RestController
@Tag(name = "Translation endoint", description = "Perform text translation")
@Tag(name = "Translation endpoint", description = "Perform text translation")
public class TranslationController extends BaseRest {

private final TranslationWebService translationService;
Expand Down Expand Up @@ -49,23 +51,19 @@ public ResponseEntity<String> translate(@RequestBody TranslationRequest translRe
private void validateRequest(TranslationRequest translationRequest) throws ParamValidationException {
// validate mandatory params
if (translationRequest.getText() == null) {
throw new ParamValidationException( I18nConstants.EMPTY_PARAM_MANDATORY, I18nConstants.EMPTY_PARAM_MANDATORY,
throw new ParamValidationException( EMPTY_PARAM_MANDATORY, EMPTY_PARAM_MANDATORY,
new String[] {TranslationAppConstants.TEXT});
}
//source is optional
// if (translRequest.getSource() == null) {
// throw new ParamValidationException( I18nConstants.EMPTY_PARAM_MANDATORY, I18nConstants.EMPTY_PARAM_MANDATORY,
// new String[] {TranslationAppConstants.SOURCE_LANG});
// }
if (translationRequest.getTarget() == I18nConstants.EMPTY_PARAM_MANDATORY) {
throw new ParamValidationException(I18nConstants.EMPTY_PARAM_MANDATORY, I18nConstants.EMPTY_PARAM_MANDATORY,

if (translationRequest.getTarget() == EMPTY_PARAM_MANDATORY) {
throw new ParamValidationException(EMPTY_PARAM_MANDATORY, EMPTY_PARAM_MANDATORY,
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, I18nConstants.INVALID_SERVICE_PARAM, new String[] {languagePair.toString()});
throw new ParamValidationException(null, INVALID_SERVICE_PARAM, new String[] {languagePair.toString()});
}
}

Expand Down

0 comments on commit 6e37831

Please sign in to comment.