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;
48 private static final String BASE_URL =
"https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=";
49 private static final int MAX_STRING_LENGTH = 5000;
53 private final OkHttpClient CLIENT =
new OkHttpClient();
71 static String getTranlatorUrl(String languageCode) {
72 return BASE_URL + languageCode;
87 MediaType mediaType = MediaType.parse(
"application/json");
89 JsonArray jsonArray =
new JsonArray();
90 JsonObject jsonObject =
new JsonObject();
91 jsonObject.addProperty(
"Text",
string);
92 jsonArray.add(jsonObject);
93 String bodyString = jsonArray.toString();
95 RequestBody body = RequestBody.create(mediaType,
97 Request request =
new Request.Builder()
98 .url(getTranlatorUrl(settings.getTargetLanguageCode())).post(body)
99 .addHeader(
"Ocp-Apim-Subscription-Key", settings.getAuthenticationKey())
100 .addHeader(
"Content-type",
"application/json").build();
101 Response response = CLIENT.newCall(request).execute();
102 return response.body().string();
107 if (settings.getAuthenticationKey() == null || settings.getAuthenticationKey().isEmpty()) {
108 throw new TranslationException(
"Bing Translator has not been configured, authentication key needs to be specified");
110 String toTranslate =
string.trim();
118 if (toTranslate.length() > MAX_STRING_LENGTH) {
119 toTranslate = toTranslate.substring(0, MAX_STRING_LENGTH);
123 String response = postTranslationRequest(toTranslate);
124 return parseJSONResponse(response);
126 throw new TranslationException(
"Exception while attempting to translate using BingTranslator", ex);
130 @Messages({
"BingTranslator.name.text=Bing Translator"})
133 return Bundle.BingTranslator_name_text();
138 public JPanel getSettingsPanel() {
139 if(settingsPanel == null) {
142 return settingsPanel;
147 settings.setAuthenticationKey(settingsPanel.getAuthenticationKey());
148 settings.setTargetLanguageCode(settingsPanel.getTargetLanguageCode());
149 settings.saveSettings();
169 JsonArray responses = JsonParser.parseString(json_text).getAsJsonArray();
171 JsonObject response0 = responses.get(0).getAsJsonObject();
172 JsonArray translations = response0.getAsJsonArray(
"translations");
173 JsonObject translation0 = translations.get(0).getAsJsonObject();
174 return translation0.get(
"text").getAsString();
175 }
catch (IllegalStateException | ClassCastException | NullPointerException | IndexOutOfBoundsException e) {
182 return MAX_STRING_LENGTH;
BingTranslatorSettingsPanel settingsPanel
String translate(String string)
String postTranslationRequest(String string)
String parseJSONResponse(String json_text)