Autopsy  4.19.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
BingTranslatorSettingsPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy
3  *
4  * Copyright 2019-2020 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.texttranslation.translators;
20 
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 javax.swing.event.HyperlinkEvent;
36 import javax.swing.event.HyperlinkListener;
37 import java.awt.Desktop;
38 import java.net.URISyntaxException;
39 import org.apache.commons.lang3.StringUtils;
40 import org.openide.util.NbBundle.Messages;
41 
45 public class BingTranslatorSettingsPanel extends javax.swing.JPanel {
46 
47  private static final Logger logger = Logger.getLogger(BingTranslatorSettingsPanel.class.getName());
48  private static final long serialVersionUID = 1L;
49  private static final String GET_TARGET_LANGUAGES_URL = "https://api.cognitive.microsofttranslator.com/languages?api-version=3.0&scope=translation";
50  private static final String DEFUALT_TEST_STRING = "traducción exitoso"; //spanish which should translate to something along the lines of "successful translation"
51  private String targetLanguageCode = "";
52 
56  public BingTranslatorSettingsPanel(String authenticationKey, String code) {
58  authenticationKeyField.setText(authenticationKey);
59  authenticationKeyField.getDocument().addDocumentListener(new DocumentListener() {
60  @Override
61  public void insertUpdate(DocumentEvent e) {
62  testResultValueLabel.setText("");
63  firePropertyChange("SettingChanged", true, false);
64  }
65 
66  @Override
67  public void removeUpdate(DocumentEvent e) {
68  testResultValueLabel.setText("");
69  firePropertyChange("SettingChanged", true, false);
70  }
71 
72  @Override
73  public void changedUpdate(DocumentEvent e) {
74  testResultValueLabel.setText("");
75  firePropertyChange("SettingChanged", true, false);
76  }
77 
78  });
81  targetLanguageCode = code;
82 
83  instructionsTextArea.addHyperlinkListener(new HyperlinkListener() {
84  @Override
85  public void hyperlinkUpdate(HyperlinkEvent e) {
86  if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
87  // Try to display the URL in the user's browswer.
88  if(Desktop.isDesktopSupported()) {
89  try {
90  Desktop.getDesktop().browse(e.getURL().toURI());
91  } catch (IOException | URISyntaxException ex) {
92  logger.log(Level.WARNING, "Failed to display URL in external viewer", ex);
93  }
94  }
95 
96  }
97  }
98  });
99  }
100 
104  @Messages({"BingTranslatorSettingsPanel.warning.targetLanguageFailure=Unable to get list of target languages or parse the result that was received"})
105  private void populateComboBox() {
106  Request get_request = new Request.Builder()
107  .url(GET_TARGET_LANGUAGES_URL).build();
108  try {
109  Response response = new OkHttpClient().newCall(get_request).execute();
110  JsonParser parser = new JsonParser();
111  String responseBody = response.body().string();
112  JsonElement elementBody = parser.parse(responseBody);
113  JsonObject asObject = elementBody.getAsJsonObject();
114  JsonElement translationElement = asObject.get("translation");
115  JsonObject responses = translationElement.getAsJsonObject();
116  responses.entrySet().forEach((entry) -> {
117  targetLanguageComboBox.addItem(new LanguageWrapper(entry.getKey(), entry.getValue().getAsJsonObject().get("name").getAsString()));
118  });
119  targetLanguageComboBox.setEnabled(true);
120  } catch (IOException | IllegalStateException | ClassCastException | NullPointerException | IndexOutOfBoundsException ex) {
121  logger.log(Level.SEVERE, Bundle.BingTranslatorSettingsPanel_warning_targetLanguageFailure(), ex);
122  warningLabel.setText(Bundle.BingTranslatorSettingsPanel_warning_targetLanguageFailure());
123  targetLanguageComboBox.setEnabled(false);
124  }
125 
126  }
127 
134  private void selectLanguageByCode(String code) {
135  for (int i = 0; i < targetLanguageComboBox.getModel().getSize(); i++) {
136  if (targetLanguageComboBox.getModel().getElementAt(i).getLanguageCode().equals(code)) {
137  targetLanguageComboBox.setSelectedIndex(i);
138  break;
139  }
140  }
141  }
142 
148  @SuppressWarnings("unchecked")
149  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
150  private void initComponents() {
151  java.awt.GridBagConstraints gridBagConstraints;
152 
153  authenticationKeyField = new javax.swing.JTextField();
154  warningLabel = new javax.swing.JLabel();
155  testButton = new javax.swing.JButton();
156  targetLanguageLabel = new javax.swing.JLabel();
157  targetLanguageComboBox = new javax.swing.JComboBox<>();
158  testUntranslatedTextField = new javax.swing.JTextField();
159  untranslatedLabel = new javax.swing.JLabel();
160  resultLabel = new javax.swing.JLabel();
161  testResultValueLabel = new javax.swing.JLabel();
162  authenticationKeyLabel = new javax.swing.JLabel();
163  instructionsScrollPane = new javax.swing.JScrollPane();
164  instructionsTextArea = new javax.swing.JTextPane();
165  javax.swing.Box.Filler filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
166 
167  setLayout(new java.awt.GridBagLayout());
168 
169  authenticationKeyField.setToolTipText(org.openide.util.NbBundle.getMessage(BingTranslatorSettingsPanel.class, "BingTranslatorSettingsPanel.authenticationKeyField.toolTipText")); // NOI18N
170  authenticationKeyField.setMaximumSize(new java.awt.Dimension(800, 22));
171  authenticationKeyField.setPreferredSize(new java.awt.Dimension(163, 22));
172  gridBagConstraints = new java.awt.GridBagConstraints();
173  gridBagConstraints.gridx = 3;
174  gridBagConstraints.gridy = 1;
175  gridBagConstraints.gridwidth = 7;
176  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
177  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
178  gridBagConstraints.insets = new java.awt.Insets(14, 5, 0, 12);
179  add(authenticationKeyField, gridBagConstraints);
180 
181  warningLabel.setForeground(new java.awt.Color(255, 0, 0));
182  org.openide.awt.Mnemonics.setLocalizedText(warningLabel, org.openide.util.NbBundle.getMessage(BingTranslatorSettingsPanel.class, "GoogleTranslatorSettingsPanel.warningLabel.text")); // NOI18N
183  gridBagConstraints = new java.awt.GridBagConstraints();
184  gridBagConstraints.gridx = 0;
185  gridBagConstraints.gridy = 4;
186  gridBagConstraints.gridwidth = 10;
187  gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
188  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
189  gridBagConstraints.weightx = 1.0;
190  gridBagConstraints.insets = new java.awt.Insets(7, 12, 6, 0);
191  add(warningLabel, gridBagConstraints);
192 
193  org.openide.awt.Mnemonics.setLocalizedText(testButton, org.openide.util.NbBundle.getMessage(BingTranslatorSettingsPanel.class, "BingTranslatorSettingsPanel.testButton.text")); // NOI18N
194  testButton.addActionListener(new java.awt.event.ActionListener() {
195  public void actionPerformed(java.awt.event.ActionEvent evt) {
197  }
198  });
199  gridBagConstraints = new java.awt.GridBagConstraints();
200  gridBagConstraints.gridx = 0;
201  gridBagConstraints.gridy = 3;
202  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
203  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
204  gridBagConstraints.insets = new java.awt.Insets(6, 12, 0, 0);
205  add(testButton, gridBagConstraints);
206 
207  org.openide.awt.Mnemonics.setLocalizedText(targetLanguageLabel, org.openide.util.NbBundle.getMessage(BingTranslatorSettingsPanel.class, "BingTranslatorSettingsPanel.targetLanguageLabel.text")); // NOI18N
208  gridBagConstraints = new java.awt.GridBagConstraints();
209  gridBagConstraints.gridx = 0;
210  gridBagConstraints.gridy = 2;
211  gridBagConstraints.gridwidth = 3;
212  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
213  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
214  gridBagConstraints.insets = new java.awt.Insets(10, 12, 0, 0);
215  add(targetLanguageLabel, gridBagConstraints);
216 
217  targetLanguageComboBox.setEnabled(false);
218  targetLanguageComboBox.addItemListener(new java.awt.event.ItemListener() {
219  public void itemStateChanged(java.awt.event.ItemEvent evt) {
221  }
222  });
223  gridBagConstraints = new java.awt.GridBagConstraints();
224  gridBagConstraints.gridx = 3;
225  gridBagConstraints.gridy = 2;
226  gridBagConstraints.gridwidth = 4;
227  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
228  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
229  gridBagConstraints.insets = new java.awt.Insets(8, 5, 0, 0);
230  add(targetLanguageComboBox, gridBagConstraints);
231 
232  testUntranslatedTextField.setText(DEFUALT_TEST_STRING);
233  testUntranslatedTextField.setMinimumSize(new java.awt.Dimension(160, 22));
234  testUntranslatedTextField.setPreferredSize(new java.awt.Dimension(160, 22));
235  gridBagConstraints = new java.awt.GridBagConstraints();
236  gridBagConstraints.gridx = 4;
237  gridBagConstraints.gridy = 3;
238  gridBagConstraints.gridwidth = 2;
239  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
240  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
241  gridBagConstraints.insets = new java.awt.Insets(8, 5, 0, 0);
242  add(testUntranslatedTextField, gridBagConstraints);
243 
244  org.openide.awt.Mnemonics.setLocalizedText(untranslatedLabel, org.openide.util.NbBundle.getMessage(BingTranslatorSettingsPanel.class, "BingTranslatorSettingsPanel.untranslatedLabel.text")); // NOI18N
245  gridBagConstraints = new java.awt.GridBagConstraints();
246  gridBagConstraints.gridx = 3;
247  gridBagConstraints.gridy = 3;
248  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
249  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
250  gridBagConstraints.insets = new java.awt.Insets(10, 5, 0, 0);
251  add(untranslatedLabel, gridBagConstraints);
252 
253  org.openide.awt.Mnemonics.setLocalizedText(resultLabel, org.openide.util.NbBundle.getMessage(BingTranslatorSettingsPanel.class, "BingTranslatorSettingsPanel.resultLabel.text")); // NOI18N
254  gridBagConstraints = new java.awt.GridBagConstraints();
255  gridBagConstraints.gridx = 6;
256  gridBagConstraints.gridy = 3;
257  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
258  gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 0);
259  add(resultLabel, gridBagConstraints);
260 
261  org.openide.awt.Mnemonics.setLocalizedText(testResultValueLabel, org.openide.util.NbBundle.getMessage(BingTranslatorSettingsPanel.class, "BingTranslatorSettingsPanel.testResultValueLabel.text")); // NOI18N
262  testResultValueLabel.setMaximumSize(new java.awt.Dimension(600, 22));
263  gridBagConstraints = new java.awt.GridBagConstraints();
264  gridBagConstraints.gridx = 7;
265  gridBagConstraints.gridy = 3;
266  gridBagConstraints.gridwidth = 3;
267  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
268  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
269  gridBagConstraints.insets = new java.awt.Insets(4, 7, 0, 12);
270  add(testResultValueLabel, gridBagConstraints);
271 
272  org.openide.awt.Mnemonics.setLocalizedText(authenticationKeyLabel, org.openide.util.NbBundle.getMessage(BingTranslatorSettingsPanel.class, "BingTranslatorSettingsPanel.authenticationKeyLabel.text")); // NOI18N
273  authenticationKeyLabel.setMaximumSize(new java.awt.Dimension(200, 16));
274  gridBagConstraints = new java.awt.GridBagConstraints();
275  gridBagConstraints.gridx = 0;
276  gridBagConstraints.gridy = 1;
277  gridBagConstraints.gridwidth = 3;
278  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
279  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
280  gridBagConstraints.insets = new java.awt.Insets(16, 12, 0, 0);
281  add(authenticationKeyLabel, gridBagConstraints);
282 
283  instructionsScrollPane.setBorder(javax.swing.BorderFactory.createEtchedBorder());
284  instructionsScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
285  instructionsScrollPane.setPreferredSize(new java.awt.Dimension(168, 80));
286 
287  instructionsTextArea.setEditable(false);
288  instructionsTextArea.setBackground(new java.awt.Color(240, 240, 240));
289  instructionsTextArea.setContentType("text/html"); // NOI18N
290  instructionsTextArea.setText(org.openide.util.NbBundle.getMessage(BingTranslatorSettingsPanel.class, "BingTranslatorSettingsPanel.instructionsTextArea.text_1")); // NOI18N
291  instructionsTextArea.setMaximumSize(new java.awt.Dimension(1000, 200));
292  instructionsTextArea.setPreferredSize(new java.awt.Dimension(164, 78));
294 
295  gridBagConstraints = new java.awt.GridBagConstraints();
296  gridBagConstraints.gridx = 0;
297  gridBagConstraints.gridy = 0;
298  gridBagConstraints.gridwidth = 10;
299  gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
300  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
301  gridBagConstraints.weightx = 1.0;
302  gridBagConstraints.weighty = 1.0;
303  gridBagConstraints.insets = new java.awt.Insets(13, 12, 0, 0);
304  add(instructionsScrollPane, gridBagConstraints);
305  gridBagConstraints = new java.awt.GridBagConstraints();
306  gridBagConstraints.gridx = 10;
307  gridBagConstraints.gridy = 0;
308  gridBagConstraints.gridheight = 5;
309  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
310  gridBagConstraints.weightx = 0.6;
311  add(filler1, gridBagConstraints);
312  }// </editor-fold>//GEN-END:initComponents
313 
314  @Messages({"BingTranslatorSettingsPanel.warning.invalidKey=Invalid translation authentication key"})
315  private void testButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testButtonActionPerformed
316  if (testTranslationSetup()) {
317  warningLabel.setText("");
318  } else {
319  warningLabel.setText(Bundle.BingTranslatorSettingsPanel_warning_invalidKey());
320  }
321  }//GEN-LAST:event_testButtonActionPerformed
322 
323  private void targetLanguageComboBoxSelected(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_targetLanguageComboBoxSelected
324  String selectedCode = ((LanguageWrapper) targetLanguageComboBox.getSelectedItem()).getLanguageCode();
325  if (!StringUtils.isBlank(selectedCode) && !selectedCode.equals(targetLanguageCode)) {
326  targetLanguageCode = selectedCode;
327  testResultValueLabel.setText("");
328  firePropertyChange("SettingChanged", true, false);
329  }
330  }//GEN-LAST:event_targetLanguageComboBoxSelected
331 
332  // Variables declaration - do not modify//GEN-BEGIN:variables
333  private javax.swing.JTextField authenticationKeyField;
334  private javax.swing.JLabel authenticationKeyLabel;
335  private javax.swing.JScrollPane instructionsScrollPane;
336  private javax.swing.JTextPane instructionsTextArea;
337  private javax.swing.JLabel resultLabel;
338  private javax.swing.JComboBox<LanguageWrapper> targetLanguageComboBox;
339  private javax.swing.JLabel targetLanguageLabel;
340  private javax.swing.JButton testButton;
341  private javax.swing.JLabel testResultValueLabel;
342  private javax.swing.JTextField testUntranslatedTextField;
343  private javax.swing.JLabel untranslatedLabel;
344  private javax.swing.JLabel warningLabel;
345  // End of variables declaration//GEN-END:variables
346 
354  private boolean testTranslationSetup() {
355  testResultValueLabel.setText("");
356  MediaType mediaType = MediaType.parse("application/json");
357  JsonArray jsonArray = new JsonArray();
358  JsonObject jsonObject = new JsonObject();
359  jsonObject.addProperty("Text", testUntranslatedTextField.getText());
360  jsonArray.add(jsonObject);
361  String bodyString = jsonArray.toString();
362 
363  RequestBody body = RequestBody.create(mediaType,
364  bodyString);
365  Request request = new Request.Builder()
366  .url(BingTranslator.getTranlatorUrl(targetLanguageCode)).post(body)
367  .addHeader("Ocp-Apim-Subscription-Key", authenticationKeyField.getText())
368  .addHeader("Content-type", "application/json").build();
369  try {
370  Response response = new OkHttpClient().newCall(request).execute();
371  JsonParser parser = new JsonParser();
372  JsonArray responses = parser.parse(response.body().string()).getAsJsonArray();
373  //As far as I know, there's always exactly one item in the array.
374  JsonObject response0 = responses.get(0).getAsJsonObject();
375  JsonArray translations = response0.getAsJsonArray("translations");
376  JsonObject translation0 = translations.get(0).getAsJsonObject();
377  testResultValueLabel.setText(translation0.get("text").getAsString());
378  return true;
379  } catch (IOException | IllegalStateException | ClassCastException | NullPointerException | IndexOutOfBoundsException e) {
380  logger.log(Level.WARNING, "Test of Bing Translator failed due to exception", e);
381  return false;
382  }
383  }
384 
391  String getAuthenticationKey() {
392  return authenticationKeyField.getText();
393  }
394 
400  String getTargetLanguageCode() {
401  return targetLanguageCode;
402  }
403 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2021 Basis Technology. Generated on: Thu Sep 30 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.