Skip to content

Commit

Permalink
EA-3525 fix the bug reported by sonar cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
SrishtiSingh-eu committed Sep 4, 2023
1 parent f585a2f commit f28fc7d
Showing 1 changed file with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package eu.europeana.api.translation.web;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -10,16 +12,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 +53,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 (StringUtils.equals(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 f28fc7d

Please sign in to comment.