19 package org.sleuthkit.autopsy.texttranslation.translators;
21 import com.google.gson.JsonArray;
22 import com.google.gson.JsonObject;
23 import com.google.gson.JsonParser;
24 import com.squareup.okhttp.MediaType;
25 import com.squareup.okhttp.OkHttpClient;
26 import com.squareup.okhttp.Request;
27 import com.squareup.okhttp.RequestBody;
28 import com.squareup.okhttp.Response;
29 import java.io.IOException;
30 import javax.swing.JPanel;
31 import org.openide.util.NbBundle.Messages;
32 import org.openide.util.lookup.ServiceProvider;
47 private static final String BASE_URL =
"https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=";
48 private static final int MAX_STRING_LENGTH = 5000;
52 private final OkHttpClient CLIENT =
new OkHttpClient();
70 static String getTranlatorUrl(String languageCode) {
71 return BASE_URL + languageCode;
86 MediaType mediaType = MediaType.parse(
"application/json");
88 JsonArray jsonArray =
new JsonArray();
89 JsonObject jsonObject =
new JsonObject();
90 jsonObject.addProperty(
"Text",
string);
91 jsonArray.add(jsonObject);
92 String bodyString = jsonArray.toString();
94 RequestBody body = RequestBody.create(mediaType,
96 Request request =
new Request.Builder()
97 .url(getTranlatorUrl(settings.getTargetLanguageCode())).post(body)
98 .addHeader(
"Ocp-Apim-Subscription-Key", settings.getAuthenticationKey())
99 .addHeader(
"Content-type",
"application/json").build();
100 Response response = CLIENT.newCall(request).execute();
101 return response.body().string();
106 if (settings.getAuthenticationKey() == null || settings.getAuthenticationKey().isEmpty()) {
107 throw new TranslationException(
"Bing Translator has not been configured, authentication key needs to be specified");
109 String toTranslate =
string.trim();
117 if (toTranslate.length() > MAX_STRING_LENGTH) {
118 toTranslate = toTranslate.substring(0, MAX_STRING_LENGTH);
122 String response = postTranslationRequest(toTranslate);
123 return parseJSONResponse(response);
125 throw new TranslationException(
"Exception while attempting to translate using BingTranslator", ex);
129 @Messages({
"BingTranslator.name.text=Bing Translator"})
132 return Bundle.BingTranslator_name_text();
137 return settingsPanel;
142 settings.setAuthenticationKey(settingsPanel.getAuthenticationKey());
143 settings.setTargetLanguageCode(settingsPanel.getTargetLanguageCode());
144 settings.saveSettings();
163 JsonParser parser =
new JsonParser();
165 JsonArray responses = parser.parse(json_text).getAsJsonArray();
167 JsonObject response0 = responses.get(0).getAsJsonObject();
168 JsonArray translations = response0.getAsJsonArray(
"translations");
169 JsonObject translation0 = translations.get(0).getAsJsonObject();
170 return translation0.get(
"text").getAsString();
171 }
catch (IllegalStateException | ClassCastException | NullPointerException | IndexOutOfBoundsException e) {
178 return MAX_STRING_LENGTH;
final BingTranslatorSettingsPanel settingsPanel
String translate(String string)
String postTranslationRequest(String string)
JPanel getSettingsPanel()
String parseJSONResponse(String json_text)