Skip to content

Commit 6e331f6

Browse files
committed
Split translation suggestion into separate controller
1 parent 23136b0 commit 6e331f6

File tree

5 files changed

+123
-76
lines changed

5 files changed

+123
-76
lines changed

.settings/org.eclipse.jdt.core.prefs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
eclipse.preferences.version=1
22
org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore
3-
org.eclipse.jdt.core.compiler.annotation.nonnull=javax.annotation.Nonnull
4-
org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=javax.annotation.ParametersAreNonnullByDefault
5-
org.eclipse.jdt.core.compiler.annotation.nullable=javax.annotation.Nullable
6-
org.eclipse.jdt.core.compiler.annotation.nullanalysis=enabled
3+
org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull
4+
org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault
5+
org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable
6+
org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled
77
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
88
org.eclipse.jdt.core.compiler.codegen.methodParameters=generate
99
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
@@ -49,14 +49,14 @@ org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignor
4949
org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning
5050
org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning
5151
org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore
52-
org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=warning
52+
org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error
5353
org.eclipse.jdt.core.compiler.problem.nullReference=warning
54-
org.eclipse.jdt.core.compiler.problem.nullSpecViolation=warning
54+
org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error
5555
org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=ignore
5656
org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning
5757
org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore
5858
org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore
59-
org.eclipse.jdt.core.compiler.problem.potentialNullReference=warning
59+
org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore
6060
org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=ignore
6161
org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning
6262
org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning
@@ -70,7 +70,7 @@ org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled
7070
org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning
7171
org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled
7272
org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled
73-
org.eclipse.jdt.core.compiler.problem.syntacticNullAnalysisForFields=enabled
73+
org.eclipse.jdt.core.compiler.problem.syntacticNullAnalysisForFields=disabled
7474
org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore
7575
org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning
7676
org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=enabled

src/main/java/org/wise/portal/presentation/web/controllers/author/project/TranslatableText.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
@Getter
66
public class TranslatableText {
7-
private String srcLangCode;
8-
private String targetLangCode;
9-
private String srcText;
7+
protected String srcLangCode;
8+
protected String targetLangCode;
9+
protected String srcText;
1010

1111
public TranslatableText(String srcLang, String targetLang, String srcText) {
1212
this.srcLangCode = this.convertLanguageToAWSCode(srcLang);

src/main/java/org/wise/portal/presentation/web/controllers/author/project/TranslateProjectAPIController.java

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33
import java.io.IOException;
44

55
import org.springframework.beans.factory.annotation.Autowired;
6-
import org.springframework.beans.factory.annotation.Value;
7-
import org.springframework.http.HttpStatus;
86
import org.springframework.security.access.annotation.Secured;
97
import org.springframework.security.core.Authentication;
108
import org.springframework.web.bind.annotation.PathVariable;
119
import org.springframework.web.bind.annotation.PostMapping;
1210
import org.springframework.web.bind.annotation.RequestBody;
1311
import org.springframework.web.bind.annotation.RequestMapping;
1412
import org.springframework.web.bind.annotation.RestController;
15-
import org.springframework.web.server.ResponseStatusException;
1613
import org.wise.portal.domain.project.impl.ProjectImpl;
1714
import org.wise.portal.domain.user.User;
1815
import org.wise.portal.service.project.ProjectService;
@@ -21,15 +18,8 @@
2118

2219
import com.fasterxml.jackson.databind.node.ObjectNode;
2320

24-
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
25-
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
26-
import software.amazon.awssdk.regions.Region;
27-
import software.amazon.awssdk.services.translate.TranslateClient;
28-
import software.amazon.awssdk.services.translate.model.TranslateTextRequest;
29-
import software.amazon.awssdk.services.translate.model.TranslateTextResponse;
30-
3121
@RestController
32-
@RequestMapping("/api/author/project/translate")
22+
@RequestMapping("/api/author/project/translate/{projectId}/{locale}")
3323
@Secured({ "ROLE_AUTHOR" })
3424
public class TranslateProjectAPIController {
3525

@@ -42,16 +32,7 @@ public class TranslateProjectAPIController {
4232
@Autowired
4333
protected TranslateProjectService translateProjectService;
4434

45-
@Value("${aws.accessKeyId:}")
46-
private String accessKey;
47-
48-
@Value("${aws.secretAccessKey:}")
49-
private String secretKey;
50-
51-
@Value("${aws.region:}")
52-
private String region;
53-
54-
@PostMapping("{projectId}/{locale}")
35+
@PostMapping
5536
protected void saveTranslations(Authentication auth,
5637
@PathVariable("projectId") ProjectImpl project, @PathVariable("locale") String locale,
5738
@RequestBody ObjectNode translations) throws IOException {
@@ -60,48 +41,4 @@ protected void saveTranslations(Authentication auth,
6041
translateProjectService.saveTranslations(project, locale, translations.toString());
6142
}
6243
}
63-
64-
@PostMapping("suggest")
65-
protected String getSuggestedTranslation(Authentication auth, @RequestBody TranslatableText translatableText)
66-
throws IOException, IllegalArgumentException, ResponseStatusException {
67-
if (accessKey.equals("") || secretKey.equals("") || region.equals("")) {
68-
throw new ResponseStatusException(
69-
HttpStatus.INTERNAL_SERVER_ERROR,
70-
"Missing application properties necessary for AWS Translate"
71-
);
72-
} else {
73-
TranslateClient translateClient = buildTranslateClient();
74-
TranslateTextRequest request = buildTranslateTextRequest(translatableText);
75-
return this.translateText(translateClient, request);
76-
}
77-
}
78-
79-
private TranslateClient buildTranslateClient() {
80-
AwsBasicCredentials credentials = AwsBasicCredentials.create(accessKey, secretKey);
81-
return TranslateClient.builder()
82-
.region(Region.of(region))
83-
.credentialsProvider(StaticCredentialsProvider.create(credentials))
84-
.build();
85-
}
86-
87-
private TranslateTextRequest buildTranslateTextRequest(TranslatableText translatableText) {
88-
return TranslateTextRequest.builder()
89-
.text(translatableText.getSrcText())
90-
.sourceLanguageCode(translatableText.getSrcLangCode())
91-
.targetLanguageCode(translatableText.getTargetLangCode())
92-
.build();
93-
}
94-
95-
private String translateText(TranslateClient client, TranslateTextRequest request) throws ResponseStatusException {
96-
TranslateTextResponse textResponse;
97-
try {
98-
textResponse = client.translateText(request);
99-
} catch (Exception e) {
100-
throw new ResponseStatusException(
101-
HttpStatus.INTERNAL_SERVER_ERROR,
102-
"Translation failed"
103-
);
104-
}
105-
return textResponse.translatedText();
106-
}
10744
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.wise.portal.presentation.web.controllers.author.project;
2+
3+
import java.io.IOException;
4+
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.security.access.annotation.Secured;
8+
import org.springframework.security.core.Authentication;
9+
import org.springframework.web.bind.annotation.PostMapping;
10+
import org.springframework.web.bind.annotation.RequestBody;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RestController;
13+
import org.springframework.web.server.ResponseStatusException;
14+
15+
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
16+
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
17+
import software.amazon.awssdk.regions.Region;
18+
import software.amazon.awssdk.services.translate.TranslateClient;
19+
import software.amazon.awssdk.services.translate.model.TranslateTextRequest;
20+
import software.amazon.awssdk.services.translate.model.TranslateTextResponse;
21+
22+
@RestController
23+
@RequestMapping("/api/author/project/translate/suggest")
24+
@Secured({ "ROLE_AUTHOR" })
25+
public class TranslationSuggestionAPIController {
26+
27+
@Value("${aws.accessKeyId:}")
28+
private String accessKey;
29+
30+
@Value("${aws.secretAccessKey:}")
31+
private String secretKey;
32+
33+
@Value("${aws.region:}")
34+
private String region;
35+
36+
@PostMapping
37+
protected String getSuggestedTranslation(Authentication auth, @RequestBody TranslatableText translatableText)
38+
throws IOException, IllegalArgumentException, ResponseStatusException {
39+
if (accessKey.equals("") || secretKey.equals("") || region.equals("")) {
40+
throw new ResponseStatusException(
41+
HttpStatus.INTERNAL_SERVER_ERROR,
42+
"Missing application properties necessary for AWS Translate"
43+
);
44+
} else {
45+
TranslateClient translateClient = buildTranslateClient();
46+
TranslateTextRequest request = buildTranslateTextRequest(translatableText);
47+
return this.translateText(translateClient, request);
48+
}
49+
}
50+
51+
private TranslateClient buildTranslateClient() {
52+
AwsBasicCredentials credentials = AwsBasicCredentials.create(accessKey, secretKey);
53+
return TranslateClient.builder()
54+
.region(Region.of(region))
55+
.credentialsProvider(StaticCredentialsProvider.create(credentials))
56+
.build();
57+
}
58+
59+
private TranslateTextRequest buildTranslateTextRequest(TranslatableText translatableText) {
60+
return TranslateTextRequest.builder()
61+
.text(translatableText.getSrcText())
62+
.sourceLanguageCode(translatableText.getSrcLangCode())
63+
.targetLanguageCode(translatableText.getTargetLangCode())
64+
.build();
65+
}
66+
67+
private String translateText(TranslateClient client, TranslateTextRequest request) throws ResponseStatusException {
68+
TranslateTextResponse textResponse;
69+
try {
70+
textResponse = client.translateText(request);
71+
} catch (Exception e) {
72+
throw new ResponseStatusException(
73+
HttpStatus.INTERNAL_SERVER_ERROR,
74+
"Translation failed"
75+
);
76+
}
77+
return textResponse.translatedText();
78+
}
79+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.wise.portal.presentation.web.controllers.author.project;
2+
3+
import org.easymock.EasyMockExtension;
4+
import org.easymock.TestSubject;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.extension.ExtendWith;
8+
import org.springframework.test.util.ReflectionTestUtils;
9+
import org.springframework.web.server.ResponseStatusException;
10+
import org.wise.portal.presentation.web.controllers.APIControllerTest;
11+
12+
@ExtendWith(EasyMockExtension.class)
13+
public class TranslationSuggestionAPIControllerTest extends APIControllerTest {
14+
15+
@TestSubject
16+
private final TranslationSuggestionAPIController controller = new TranslationSuggestionAPIController();
17+
18+
@Test
19+
public void getSuggestedTranslation_ThrowIfPropertiesEmpty() throws Exception {
20+
ReflectionTestUtils.setField(controller, "accessKey", "");
21+
ReflectionTestUtils.setField(controller, "secretKey", "");
22+
ReflectionTestUtils.setField(controller, "region", "");
23+
24+
TranslatableText tt = new TranslatableText("English", "Spanish", "text to translate");
25+
26+
assertThrows(ResponseStatusException.class, () -> {
27+
controller.getSuggestedTranslation(teacherAuth, tt);
28+
});
29+
}
30+
}
31+

0 commit comments

Comments
 (0)