Skip to content

Commit

Permalink
Merge branch 'EA-3543_reusable_services' of
Browse files Browse the repository at this point in the history
https://github.com/europeana/translation-api/ into
EA-3543_reusable_services
  • Loading branch information
GordeaS authored and GordeaS committed Oct 18, 2023
2 parents d4c66a9 + 6e37831 commit 32f06d2
Show file tree
Hide file tree
Showing 34 changed files with 731 additions and 477 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/build_test_analyse.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Build, Run Tests and Sonar Analysis
on: push

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
# Shallow clones should be disabled for a better relevancy of analysis
fetch-depth: 0
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11
- name: Cache Maven packages
uses: actions/cache@v1
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Cache SonarCloud packages
uses: actions/cache@v1
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Build, run tests and analyse
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Pcoverage -Dsonar.projectKey=europeana_translation-api
env:
# Needed to get some information about the pull request, if any
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# SonarCloud access token should be generated from https://sonarcloud.io/account/security/
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
19 changes: 15 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
<description>API for the language detection and translation</description>

<modules>
<module>translation-web</module>
<module>translation-definitions</module>
<module>translation-service</module>
<module>translation-web</module>
<module>translation-tests</module>
</modules>

Expand Down Expand Up @@ -54,13 +55,23 @@
<git-commit-id.version>5.0.0</git-commit-id.version>
<git-code-format.version>3.1</git-code-format.version>
<surefire.version>3.0.0-M5</surefire.version>
<!--
<corelib.version>2.16.2</corelib.version>
-->
<!-- sonar plugins -->
<jacoco-plugin.version>0.8.7</jacoco-plugin.version>
<pmd-plugin.version>3.15.0</pmd-plugin.version>
<spotbugs-plugin.version>4.5.2.0</spotbugs-plugin.version>
<sonar-plugin.version>3.7.0.1746</sonar-plugin.version>
<!--
<corelib.version>2.16.2</corelib.version>
-->
<sonar.java.pmd.reportPaths>${project.build.directory}/pmd.xml</sonar.java.pmd.reportPaths>
<sonar.java.spotbugs.reportPaths>${project.build.directory}/spotbugsXml.xml</sonar.java.spotbugs.reportPaths>
<sonar.organization>europeana</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<!-- Exclude POJOs code duplication analysis -->
<sonar.cpd.exclusions>**/model/**/*</sonar.cpd.exclusions>
<aggregate.report.xml>annotation-tests/target/site/jacoco-aggregate/jacoco.xml</aggregate.report.xml>
<sonar.coverage.jacoco.xmlReportPaths>${aggregate.report.xml}</sonar.coverage.jacoco.xmlReportPaths>


</properties>

Expand Down
5 changes: 5 additions & 0 deletions translation-definitions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
<artifactId>translation-definitions</artifactId>
<name>translation-definitions</name>
<description>The shared data model classes shared by the translation API.</description>

<properties>
<sonar.coverage.jacoco.xmlReportPaths>${basedir}/../${aggregate.report.xml}</sonar.coverage.jacoco.xmlReportPaths>
</properties>

<dependencies>
<dependency>
<groupId>eu.europeana.api.commons</groupId>
Expand Down
5 changes: 5 additions & 0 deletions translation-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
<artifactId>translation-service</artifactId>
<name>translation-service</name>
<description>The Java APIs for the language detection and translation services (part of Translation API)</description>

<properties>
<sonar.coverage.jacoco.xmlReportPaths>${basedir}/../${aggregate.report.xml}</sonar.coverage.jacoco.xmlReportPaths>
</properties>

<dependencies>
<dependency>
<groupId>eu.europeana.api</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,67 +31,85 @@ public class GoogleTranslationService implements TranslationService {
private LocationName locationName;

public GoogleTranslationService(String googleProjectId) {
this(googleProjectId, true);
this(googleProjectId, true, false);
}

public GoogleTranslationService(String googleProjectId, boolean initClientConnection) {
public GoogleTranslationService(String googleProjectId, boolean initClientConnection,
boolean useHttpClient) {
this.googleProjectId = googleProjectId;
if(initClientConnection) {
init();
if (initClientConnection) {
init(useHttpClient);
}
}

/**
* Creates a new client that can send translation requests to Google Cloud Translate. Note that
* the client needs to be closed when it's not used anymore
*
* @throws RuntimeException when there is a problem creating the client
*/
public void init() throws RuntimeException{
public void init(boolean useHttpClient) throws RuntimeException {
try {

// gRPC doesn't like communication via the socks proxy (throws an error) and also doesn't
// support the
// socksNonProxyHosts settings, so this is to tell it to by-pass the configured proxy
TransportChannelProvider transportChannelProvider = CloudTasksStubSettings
.defaultGrpcTransportProviderBuilder()
.setChannelConfigurator(
managedChannelBuilder -> managedChannelBuilder.proxyDetector(socketAddress -> null))
.build();
TranslationServiceSettings tss;
tss = TranslationServiceSettings.newBuilder()
.setTransportChannelProvider(transportChannelProvider).build();

this.client = TranslationServiceClient.create(tss);
if (useHttpClient) {
TranslationServiceSettings translationServiceSettings =
TranslationServiceSettings.newHttpJsonBuilder().build();
this.client = TranslationServiceClient.create(translationServiceSettings);
} else {
TransportChannelProvider transportChannelProvider = CloudTasksStubSettings
.defaultGrpcTransportProviderBuilder()
.setChannelConfigurator(
managedChannelBuilder -> managedChannelBuilder.proxyDetector(socketAddress -> null))
.build();

TranslationServiceSettings tss;
tss = TranslationServiceSettings.newBuilder()
.setTransportChannelProvider(transportChannelProvider).build();

this.client = TranslationServiceClient.create(tss);
}

this.locationName = LocationName.of(getGoogleProjectId(), "global");
LOG.info("GoogleTranslationService initialised, projectId = {}", getGoogleProjectId());
} catch (IOException e) {
throw new RuntimeException("Cannot instantiate Google TranslationServiceClient!", e);
}
}

/**
* used mainly for testing purposes.
* @param client
*/
public void init(TranslationServiceClient client) {
this.client = client;
this.locationName = LocationName.of(getGoogleProjectId(), "global");
}

// @PreDestroy
@Override
public void close() {
if (this.client != null) {
LOG.info("Shutting down GoogleTranslationService client...");
LOG.debug("Shutting down GoogleTranslationService client...");
this.client.close();
}
}

// public List<String> translate(List<String> texts, String targetLanguage,
// Language sourceLangHint) {
// TranslateTextRequest request = TranslateTextRequest.newBuilder()
// .setParent(locationName.toString()).setMimeType(MIME_TYPE_TEXT)
// .setTargetLanguageCode(targetLanguage).addAllContents(texts).build();
// TranslateTextResponse response = this.client.translateText(request);
// List<String> result = new ArrayList<>();
// for (Translation t : response.getTranslationsList()) {
// result.add(t.getTranslatedText());
// }
// return result;
// }
// public List<String> translate(List<String> texts, String targetLanguage,
// Language sourceLangHint) {
// TranslateTextRequest request = TranslateTextRequest.newBuilder()
// .setParent(locationName.toString()).setMimeType(MIME_TYPE_TEXT)
// .setTargetLanguageCode(targetLanguage).addAllContents(texts).build();
// TranslateTextResponse response = this.client.translateText(request);
// List<String> result = new ArrayList<>();
// for (Translation t : response.getTranslationsList()) {
// result.add(t.getTranslatedText());
// }
// return result;
// }

@Override
public List<String> translate(List<String> texts, String targetLanguage)
throws TranslationException {
Expand Down Expand Up @@ -119,9 +137,9 @@ public List<String> translate(List<String> text, String targetLanguage, String s

@Override
public boolean isSupported(String srcLang, String targetLanguage) {
if(srcLang == null) {
//automatic language detection
return isTargetSupported(targetLanguage);
if (srcLang == null) {
// automatic language detection
return isTargetSupported(targetLanguage);
}
return true;
}
Expand Down
Loading

0 comments on commit 32f06d2

Please sign in to comment.