Skip to content

Commit

Permalink
addressing code quality issues, remove warnings which are false
Browse files Browse the repository at this point in the history
positives #EA-3626
  • Loading branch information
GordeaS authored and GordeaS committed Nov 30, 2023
1 parent c6523c8 commit 812b3eb
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,65 @@
import eu.europeana.api.translation.definitions.vocabulary.TranslationAppConstants;

/**
* Class to hold the Language pair values supported by the Translation services
* Mostly for the future Translation API, when we have more than one translation service
* Class to hold the Language pair values supported by the Translation services Mostly for the
* future Translation API, when we have more than one translation service
*
* @author Hugo
* @since 5 Apr 2023
*/
public class LanguagePair implements Comparable<LanguagePair> {

private String srcLang;
private String targetLang;
private String srcLang;
private String targetLang;

public LanguagePair(String srcLang, @NotNull String targetLang) {
this.srcLang = srcLang;
this.targetLang = targetLang;
public LanguagePair(String srcLang, @NotNull String targetLang) {
this.srcLang = srcLang;
this.targetLang = targetLang;
}

public String getSrcLang() {
return srcLang;
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}

public String getSrcLang() {
return srcLang;
if (!(this.getClass() == obj.getClass())) {
return false;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof LanguagePair)) {
return false;
}
LanguagePair pair = (LanguagePair) obj;
return StringUtils.equals(targetLang, pair.targetLang)
&& StringUtils.equals(srcLang, pair.srcLang);
}

LanguagePair pair = (LanguagePair) obj;
return StringUtils.equals(targetLang, pair.targetLang)
&& StringUtils.equals(srcLang, pair.srcLang);
@Override
public int compareTo(LanguagePair pair) {
int ret = targetLang.compareTo(pair.targetLang);
if (ret == 0) {
ret = StringUtils.compare(srcLang, pair.srcLang);
}
return ret;
}

@Override
public int compareTo(LanguagePair pair) {
int ret = targetLang.compareTo(pair.targetLang);
if(ret == 0) {
ret = StringUtils.compare(srcLang, pair.srcLang);
}
return ret;
}

@Override
public String toString() {
return generateKey(srcLang, targetLang);
}
@Override
public String toString() {
return generateKey(srcLang, targetLang);
}

public static String generateKey(String srcLang, String targetLang) {
return srcLang + TranslationAppConstants.LANG_DELIMITER + targetLang;
}

@Override
public int hashCode() {
return srcLang==null ? targetLang.hashCode() : srcLang.hashCode() + targetLang.hashCode();
}
public static String generateKey(String srcLang, String targetLang) {
return srcLang + TranslationAppConstants.LANG_DELIMITER + targetLang;
}

public String getTargetLang() {
return targetLang;
}
@Override
public int hashCode() {
return srcLang == null ? targetLang.hashCode() : srcLang.hashCode() + targetLang.hashCode();
}

public String getTargetLang() {
return targetLang;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.cloud.translate.v3.TranslateTextRequest.Builder;
import com.google.cloud.translate.v3.TranslateTextResponse;
import com.google.cloud.translate.v3.Translation;
import com.google.cloud.translate.v3.TranslationOrBuilder;
import eu.europeana.api.translation.definitions.model.TranslationObj;
import eu.europeana.api.translation.service.AbstractTranslationService;
import eu.europeana.api.translation.service.exception.TranslationException;
Expand Down Expand Up @@ -71,7 +72,7 @@ public void translate(List<TranslationObj> translationObjs) throws TranslationEx

}

private void updateFromTranslation( TranslationObj translationObj, Translation translation) {
private void updateFromTranslation( TranslationObj translationObj, TranslationOrBuilder translation) {
if(translationObj.getSourceLang()==null) {
translationObj.setSourceLang(translation.getDetectedLanguageCode());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ private LettuceConnectionFactory getRedisConnectionFactory() {
// if redis secure protocol is used (rediss vs. redis)
boolean sslEnabled = translationConfig.getRedisConnectionUrl().startsWith("rediss");
if (sslEnabled) {
@SuppressWarnings("external_findsecbugs:PATH_TRAVERSAL_IN") // the trustore path is not user input but application config
final File truststore = new File(FilenameUtils.normalize(translationConfig.getTruststorePath()));
SslOptions sslOptions = SslOptions.builder().jdkSslProvider()
.truststore(truststore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@PropertySource(value = "translation.user.properties", ignoreResourceNotFound = true)})
public class TranslationConfig{

private final Logger LOG = LogManager.getLogger(TranslationConfig.class);
private static final Logger LOG = LogManager.getLogger(TranslationConfig.class);
/** Matches spring.profiles.active property in test/resource application.properties file */
public static final String ACTIVE_TEST_PROFILE = "test";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class WebMvcConfig implements WebMvcConfigurer {

/** Setup CORS for all GET, HEAD and OPTIONS, requests. */
@Override
@SuppressWarnings("external_findsecbugs:PERMISSIVE_CORS") //the API is public
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/**")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public CachedTranslation deserialize(byte[] bytes) throws SerializationException
try {
return om.readValue(bytes, CachedTranslation.class);
} catch (IOException e) {
throw new SerializationException(e.getMessage(), e);
throw new SerializationException("Cannot deserialize redis response: " + e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import eu.europeana.api.commons.web.http.HttpHeaders;
Expand All @@ -21,7 +22,7 @@ public TranslationErrorController(ErrorAttributes errorAttributes) {
}


@RequestMapping(value = "/error", produces = {HttpHeaders.CONTENT_TYPE_JSON_UTF8, HttpHeaders.CONTENT_TYPE_JSONLD})
@RequestMapping(value = "/error", produces = {HttpHeaders.CONTENT_TYPE_JSON_UTF8, HttpHeaders.CONTENT_TYPE_JSONLD}, method = {RequestMethod.GET, RequestMethod.POST})
@ResponseBody
public Map<String, Object> error(final HttpServletRequest request) {
return this.getErrorAttributes(request, ErrorAttributeOptions.defaults());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.europeana.api.translation.web.service;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
Expand Down Expand Up @@ -159,7 +160,7 @@ public void deleteAll() {
public String generateRedisKey(String inputText, String sourceLang, String targetLang) {
StringBuilder builder = (new StringBuilder()).append(sourceLang).append(targetLang);
byte[] hash = Base64.getEncoder().withoutPadding().encode(Ints.toByteArray(inputText.hashCode()));
builder.append(new String(hash));
builder.append(new String(hash, StandardCharsets.UTF_8));
return builder.toString();
}

Expand Down

0 comments on commit 812b3eb

Please sign in to comment.