|
| 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 | +} |
0 commit comments