19 package org.sleuthkit.autopsy.texttranslation.translators;
21 import com.google.gson.JsonArray;
22 import com.google.gson.JsonElement;
23 import com.google.gson.JsonParser;
24 import com.google.gson.JsonObject;
25 import com.squareup.okhttp.MediaType;
26 import com.squareup.okhttp.OkHttpClient;
27 import com.squareup.okhttp.Request;
28 import com.squareup.okhttp.RequestBody;
29 import com.squareup.okhttp.Response;
30 import java.io.IOException;
31 import java.util.logging.Level;
33 import javax.swing.event.DocumentEvent;
34 import javax.swing.event.DocumentListener;
35 import org.apache.commons.lang3.StringUtils;
36 import org.openide.util.NbBundle.Messages;
45 private static final String
GET_TARGET_LANGUAGES_URL =
"https://api.cognitive.microsofttranslator.com/languages?api-version=3.0&scope=translation";
57 public void insertUpdate(DocumentEvent e) {
58 firePropertyChange(
"SettingChanged",
true,
false);
62 public void removeUpdate(DocumentEvent e) {
63 firePropertyChange(
"SettingChanged",
true,
false);
67 public void changedUpdate(DocumentEvent e) {
68 firePropertyChange(
"SettingChanged",
true,
false);
74 targetLanguageCode = code;
80 @Messages({
"BingTranslatorSettingsPanel.warning.targetLanguageFailure=Unable to get list of target languages or parse the result that was received"})
82 Request get_request =
new Request.Builder()
83 .url(GET_TARGET_LANGUAGES_URL).build();
85 Response response =
new OkHttpClient().newCall(get_request).execute();
86 JsonParser parser =
new JsonParser();
87 String responseBody = response.body().string();
88 JsonElement elementBody = parser.parse(responseBody);
89 JsonObject asObject = elementBody.getAsJsonObject();
90 JsonElement translationElement = asObject.get(
"translation");
91 JsonObject responses = translationElement.getAsJsonObject();
92 responses.entrySet().forEach((entry) -> {
93 targetLanguageComboBox.addItem(
new LanguageWrapper(entry.getKey(), entry.getValue().getAsJsonObject().get(
"name").getAsString()));
96 }
catch (IOException | IllegalStateException | ClassCastException | NullPointerException | IndexOutOfBoundsException ex) {
97 logger.log(Level.SEVERE, Bundle.BingTranslatorSettingsPanel_warning_targetLanguageFailure(), ex);
98 warningLabel.setText(Bundle.BingTranslatorSettingsPanel_warning_targetLanguageFailure());
124 @SuppressWarnings(
"unchecked")
143 warningLabel.setForeground(
new java.awt.Color(255, 0, 0));
147 testButton.addActionListener(
new java.awt.event.ActionListener() {
148 public void actionPerformed(java.awt.event.ActionEvent evt) {
157 public void itemStateChanged(java.awt.event.ItemEvent evt) {
173 instructionsScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
185 javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(
this);
186 this.setLayout(layout);
187 layout.setHorizontalGroup(
188 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
189 .addGroup(layout.createSequentialGroup()
191 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
193 .addGroup(layout.createSequentialGroup()
194 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
195 .addGroup(layout.createSequentialGroup()
196 .addComponent(
authenticationKeyLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
197 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
198 .addComponent(
authenticationKeyField, javax.swing.GroupLayout.PREFERRED_SIZE, 486, javax.swing.GroupLayout.PREFERRED_SIZE))
199 .addComponent(
warningLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 551, javax.swing.GroupLayout.PREFERRED_SIZE)
200 .addGroup(layout.createSequentialGroup()
201 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
203 .addComponent(
testButton, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
205 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
206 .addGroup(layout.createSequentialGroup()
207 .addComponent(
untranslatedLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
208 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
209 .addComponent(
testUntranslatedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
210 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
212 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
213 .addComponent(
testResultValueLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
214 .addGroup(layout.createSequentialGroup()
216 .addGap(276, 276, 276)))))
217 .addGap(0, 0, Short.MAX_VALUE)))
220 layout.setVerticalGroup(
221 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
222 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
224 .addComponent(
instructionsScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
225 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
226 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
227 .addComponent(
authenticationKeyField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
229 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
230 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
232 .addComponent(
targetLanguageComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
233 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
234 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
236 .addComponent(
testUntranslatedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
240 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
241 .addComponent(
warningLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
242 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
246 @Messages({
"BingTranslatorSettingsPanel.warning.invalidKey=Invalid translation authentication key"})
251 warningLabel.setText(Bundle.BingTranslatorSettingsPanel_warning_invalidKey());
257 if (!StringUtils.isBlank(selectedCode) && !selectedCode.equals(targetLanguageCode)) {
258 targetLanguageCode = selectedCode;
259 firePropertyChange(
"SettingChanged",
true,
false);
287 MediaType mediaType = MediaType.parse(
"application/json");
288 JsonArray jsonArray =
new JsonArray();
289 JsonObject jsonObject =
new JsonObject();
291 jsonArray.add(jsonObject);
292 String bodyString = jsonArray.toString();
294 RequestBody body = RequestBody.create(mediaType,
296 Request request =
new Request.Builder()
297 .url(
BingTranslator.getTranlatorUrl(targetLanguageCode)).post(body)
299 .addHeader(
"Content-type",
"application/json").build();
301 Response response =
new OkHttpClient().newCall(request).execute();
302 JsonParser parser =
new JsonParser();
303 JsonArray responses = parser.parse(response.body().string()).getAsJsonArray();
305 JsonObject response0 = responses.get(0).getAsJsonObject();
306 JsonArray translations = response0.getAsJsonArray(
"translations");
307 JsonObject translation0 = translations.get(0).getAsJsonObject();
310 }
catch (IOException | IllegalStateException | ClassCastException | NullPointerException | IndexOutOfBoundsException e) {
311 logger.log(Level.WARNING,
"Test of Bing Translator failed due to exception", e);
322 String getAuthenticationKey() {
331 String getTargetLanguageCode() {
javax.swing.JTextField testUntranslatedTextField
javax.swing.JLabel testResultValueLabel
javax.swing.JLabel targetLanguageLabel
javax.swing.JLabel resultLabel
static final long serialVersionUID
void testButtonActionPerformed(java.awt.event.ActionEvent evt)
static final String GET_TARGET_LANGUAGES_URL
boolean testTranslationSetup()
javax.swing.JLabel warningLabel
javax.swing.JTextArea instructionsTextArea
javax.swing.JScrollPane instructionsScrollPane
void selectLanguageByCode(String code)
javax.swing.JComboBox< LanguageWrapper > targetLanguageComboBox
javax.swing.JLabel authenticationKeyLabel
static final String DEFUALT_TEST_STRING
javax.swing.JTextField authenticationKeyField
synchronized static Logger getLogger(String name)
String targetLanguageCode
BingTranslatorSettingsPanel(String authenticationKey, String code)
void targetLanguageComboBoxSelected(java.awt.event.ItemEvent evt)
javax.swing.JButton testButton
static final Logger logger
javax.swing.JLabel untranslatedLabel