Autopsy 4.22.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 */
19package org.sleuthkit.autopsy.texttranslation.translators;
20
21import com.google.gson.JsonArray;
22import com.google.gson.JsonElement;
23import com.google.gson.JsonParser;
24import com.google.gson.JsonObject;
25import java.io.IOException;
26import java.util.logging.Level;
27import org.sleuthkit.autopsy.coreutils.Logger;
28import javax.swing.event.DocumentEvent;
29import javax.swing.event.DocumentListener;
30import javax.swing.event.HyperlinkEvent;
31import javax.swing.event.HyperlinkListener;
32import org.sleuthkit.autopsy.coreutils.Desktop;
33import java.net.URISyntaxException;
34import okhttp3.MediaType;
35import okhttp3.OkHttpClient;
36import okhttp3.Request;
37import okhttp3.RequestBody;
38import okhttp3.Response;
39import org.apache.commons.lang3.StringUtils;
40import org.openide.util.NbBundle.Messages;
41
45public 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.
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 String responseBody = response.body().string();
111 JsonElement elementBody = JsonParser.parseString(responseBody);
112 JsonObject asObject = elementBody.getAsJsonObject();
113 JsonElement translationElement = asObject.get("translation");
114 JsonObject responses = translationElement.getAsJsonObject();
115 responses.entrySet().forEach((entry) -> {
116 targetLanguageComboBox.addItem(new LanguageWrapper(entry.getKey(), entry.getValue().getAsJsonObject().get("name").getAsString()));
117 });
118 targetLanguageComboBox.setEnabled(true);
119 } catch (IOException | IllegalStateException | ClassCastException | NullPointerException | IndexOutOfBoundsException ex) {
120 logger.log(Level.SEVERE, Bundle.BingTranslatorSettingsPanel_warning_targetLanguageFailure(), ex);
121 warningLabel.setText(Bundle.BingTranslatorSettingsPanel_warning_targetLanguageFailure());
122 targetLanguageComboBox.setEnabled(false);
123 }
124
125 }
126
133 private void selectLanguageByCode(String code) {
134 for (int i = 0; i < targetLanguageComboBox.getModel().getSize(); i++) {
135 if (targetLanguageComboBox.getModel().getElementAt(i).getLanguageCode().equals(code)) {
136 targetLanguageComboBox.setSelectedIndex(i);
137 break;
138 }
139 }
140 }
141
147 @SuppressWarnings("unchecked")
148 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
149 private void initComponents() {
150 java.awt.GridBagConstraints gridBagConstraints;
151
152 authenticationKeyField = new javax.swing.JTextField();
153 warningLabel = new javax.swing.JLabel();
154 testButton = new javax.swing.JButton();
155 targetLanguageLabel = new javax.swing.JLabel();
156 targetLanguageComboBox = new javax.swing.JComboBox<>();
157 testUntranslatedTextField = new javax.swing.JTextField();
158 untranslatedLabel = new javax.swing.JLabel();
159 resultLabel = new javax.swing.JLabel();
160 testResultValueLabel = new javax.swing.JLabel();
161 authenticationKeyLabel = new javax.swing.JLabel();
162 instructionsScrollPane = new javax.swing.JScrollPane();
163 instructionsTextArea = new javax.swing.JTextPane();
164 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));
165
166 setLayout(new java.awt.GridBagLayout());
167
168 authenticationKeyField.setToolTipText(org.openide.util.NbBundle.getMessage(BingTranslatorSettingsPanel.class, "BingTranslatorSettingsPanel.authenticationKeyField.toolTipText")); // NOI18N
169 authenticationKeyField.setMaximumSize(new java.awt.Dimension(800, 22));
170 authenticationKeyField.setPreferredSize(new java.awt.Dimension(163, 22));
171 gridBagConstraints = new java.awt.GridBagConstraints();
172 gridBagConstraints.gridx = 3;
173 gridBagConstraints.gridy = 1;
174 gridBagConstraints.gridwidth = 7;
175 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
176 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
177 gridBagConstraints.insets = new java.awt.Insets(14, 5, 0, 12);
178 add(authenticationKeyField, gridBagConstraints);
179
180 warningLabel.setForeground(new java.awt.Color(255, 0, 0));
181 org.openide.awt.Mnemonics.setLocalizedText(warningLabel, org.openide.util.NbBundle.getMessage(BingTranslatorSettingsPanel.class, "GoogleTranslatorSettingsPanel.warningLabel.text")); // NOI18N
182 gridBagConstraints = new java.awt.GridBagConstraints();
183 gridBagConstraints.gridx = 0;
184 gridBagConstraints.gridy = 4;
185 gridBagConstraints.gridwidth = 10;
186 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
187 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
188 gridBagConstraints.weightx = 1.0;
189 gridBagConstraints.insets = new java.awt.Insets(7, 12, 6, 0);
190 add(warningLabel, gridBagConstraints);
191
192 org.openide.awt.Mnemonics.setLocalizedText(testButton, org.openide.util.NbBundle.getMessage(BingTranslatorSettingsPanel.class, "BingTranslatorSettingsPanel.testButton.text")); // NOI18N
193 testButton.addActionListener(new java.awt.event.ActionListener() {
194 public void actionPerformed(java.awt.event.ActionEvent evt) {
195 testButtonActionPerformed(evt);
196 }
197 });
198 gridBagConstraints = new java.awt.GridBagConstraints();
199 gridBagConstraints.gridx = 0;
200 gridBagConstraints.gridy = 3;
201 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
202 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
203 gridBagConstraints.insets = new java.awt.Insets(6, 12, 0, 0);
204 add(testButton, gridBagConstraints);
205
206 org.openide.awt.Mnemonics.setLocalizedText(targetLanguageLabel, org.openide.util.NbBundle.getMessage(BingTranslatorSettingsPanel.class, "BingTranslatorSettingsPanel.targetLanguageLabel.text")); // NOI18N
207 gridBagConstraints = new java.awt.GridBagConstraints();
208 gridBagConstraints.gridx = 0;
209 gridBagConstraints.gridy = 2;
210 gridBagConstraints.gridwidth = 3;
211 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
212 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
213 gridBagConstraints.insets = new java.awt.Insets(10, 12, 0, 0);
214 add(targetLanguageLabel, gridBagConstraints);
215
216 targetLanguageComboBox.setEnabled(false);
217 targetLanguageComboBox.addItemListener(new java.awt.event.ItemListener() {
218 public void itemStateChanged(java.awt.event.ItemEvent evt) {
219 targetLanguageComboBoxSelected(evt);
220 }
221 });
222 gridBagConstraints = new java.awt.GridBagConstraints();
223 gridBagConstraints.gridx = 3;
224 gridBagConstraints.gridy = 2;
225 gridBagConstraints.gridwidth = 4;
226 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
227 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
228 gridBagConstraints.insets = new java.awt.Insets(8, 5, 0, 0);
229 add(targetLanguageComboBox, gridBagConstraints);
230
231 testUntranslatedTextField.setText(DEFUALT_TEST_STRING);
232 testUntranslatedTextField.setMinimumSize(new java.awt.Dimension(160, 22));
233 testUntranslatedTextField.setPreferredSize(new java.awt.Dimension(160, 22));
234 gridBagConstraints = new java.awt.GridBagConstraints();
235 gridBagConstraints.gridx = 4;
236 gridBagConstraints.gridy = 3;
237 gridBagConstraints.gridwidth = 2;
238 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
239 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
240 gridBagConstraints.insets = new java.awt.Insets(8, 5, 0, 0);
241 add(testUntranslatedTextField, gridBagConstraints);
242
243 org.openide.awt.Mnemonics.setLocalizedText(untranslatedLabel, org.openide.util.NbBundle.getMessage(BingTranslatorSettingsPanel.class, "BingTranslatorSettingsPanel.untranslatedLabel.text")); // NOI18N
244 gridBagConstraints = new java.awt.GridBagConstraints();
245 gridBagConstraints.gridx = 3;
246 gridBagConstraints.gridy = 3;
247 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
248 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
249 gridBagConstraints.insets = new java.awt.Insets(10, 5, 0, 0);
250 add(untranslatedLabel, gridBagConstraints);
251
252 org.openide.awt.Mnemonics.setLocalizedText(resultLabel, org.openide.util.NbBundle.getMessage(BingTranslatorSettingsPanel.class, "BingTranslatorSettingsPanel.resultLabel.text")); // NOI18N
253 gridBagConstraints = new java.awt.GridBagConstraints();
254 gridBagConstraints.gridx = 6;
255 gridBagConstraints.gridy = 3;
256 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
257 gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 0);
258 add(resultLabel, gridBagConstraints);
259
260 org.openide.awt.Mnemonics.setLocalizedText(testResultValueLabel, org.openide.util.NbBundle.getMessage(BingTranslatorSettingsPanel.class, "BingTranslatorSettingsPanel.testResultValueLabel.text")); // NOI18N
261 testResultValueLabel.setMaximumSize(new java.awt.Dimension(600, 22));
262 gridBagConstraints = new java.awt.GridBagConstraints();
263 gridBagConstraints.gridx = 7;
264 gridBagConstraints.gridy = 3;
265 gridBagConstraints.gridwidth = 3;
266 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
267 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
268 gridBagConstraints.insets = new java.awt.Insets(4, 7, 0, 12);
269 add(testResultValueLabel, gridBagConstraints);
270
271 org.openide.awt.Mnemonics.setLocalizedText(authenticationKeyLabel, org.openide.util.NbBundle.getMessage(BingTranslatorSettingsPanel.class, "BingTranslatorSettingsPanel.authenticationKeyLabel.text")); // NOI18N
272 authenticationKeyLabel.setMaximumSize(new java.awt.Dimension(200, 16));
273 gridBagConstraints = new java.awt.GridBagConstraints();
274 gridBagConstraints.gridx = 0;
275 gridBagConstraints.gridy = 1;
276 gridBagConstraints.gridwidth = 3;
277 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
278 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
279 gridBagConstraints.insets = new java.awt.Insets(16, 12, 0, 0);
280 add(authenticationKeyLabel, gridBagConstraints);
281
282 instructionsScrollPane.setBorder(javax.swing.BorderFactory.createEtchedBorder());
283 instructionsScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
284 instructionsScrollPane.setPreferredSize(new java.awt.Dimension(168, 80));
285
286 instructionsTextArea.setEditable(false);
287 instructionsTextArea.setBackground(new java.awt.Color(240, 240, 240));
288 instructionsTextArea.setContentType("text/html"); // NOI18N
289 instructionsTextArea.setText(org.openide.util.NbBundle.getMessage(BingTranslatorSettingsPanel.class, "BingTranslatorSettingsPanel.instructionsTextArea.text_1")); // NOI18N
290 instructionsTextArea.setMaximumSize(new java.awt.Dimension(1000, 200));
291 instructionsTextArea.setPreferredSize(new java.awt.Dimension(164, 78));
292 instructionsScrollPane.setViewportView(instructionsTextArea);
293
294 gridBagConstraints = new java.awt.GridBagConstraints();
295 gridBagConstraints.gridx = 0;
296 gridBagConstraints.gridy = 0;
297 gridBagConstraints.gridwidth = 10;
298 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
299 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
300 gridBagConstraints.weightx = 1.0;
301 gridBagConstraints.weighty = 1.0;
302 gridBagConstraints.insets = new java.awt.Insets(13, 12, 0, 0);
303 add(instructionsScrollPane, gridBagConstraints);
304 gridBagConstraints = new java.awt.GridBagConstraints();
305 gridBagConstraints.gridx = 10;
306 gridBagConstraints.gridy = 0;
307 gridBagConstraints.gridheight = 5;
308 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
309 gridBagConstraints.weightx = 0.6;
310 add(filler1, gridBagConstraints);
311 }// </editor-fold>//GEN-END:initComponents
312
313 @Messages({"BingTranslatorSettingsPanel.warning.invalidKey=Invalid translation authentication key"})
314 private void testButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testButtonActionPerformed
315 if (testTranslationSetup()) {
316 warningLabel.setText("");
317 } else {
318 warningLabel.setText(Bundle.BingTranslatorSettingsPanel_warning_invalidKey());
319 }
320 }//GEN-LAST:event_testButtonActionPerformed
321
322 private void targetLanguageComboBoxSelected(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_targetLanguageComboBoxSelected
323 String selectedCode = ((LanguageWrapper) targetLanguageComboBox.getSelectedItem()).getLanguageCode();
324 if (!StringUtils.isBlank(selectedCode) && !selectedCode.equals(targetLanguageCode)) {
325 targetLanguageCode = selectedCode;
326 testResultValueLabel.setText("");
327 firePropertyChange("SettingChanged", true, false);
328 }
329 }//GEN-LAST:event_targetLanguageComboBoxSelected
330
331 // Variables declaration - do not modify//GEN-BEGIN:variables
332 private javax.swing.JTextField authenticationKeyField;
333 private javax.swing.JLabel authenticationKeyLabel;
334 private javax.swing.JScrollPane instructionsScrollPane;
335 private javax.swing.JTextPane instructionsTextArea;
336 private javax.swing.JLabel resultLabel;
337 private javax.swing.JComboBox<LanguageWrapper> targetLanguageComboBox;
338 private javax.swing.JLabel targetLanguageLabel;
339 private javax.swing.JButton testButton;
340 private javax.swing.JLabel testResultValueLabel;
341 private javax.swing.JTextField testUntranslatedTextField;
342 private javax.swing.JLabel untranslatedLabel;
343 private javax.swing.JLabel warningLabel;
344 // End of variables declaration//GEN-END:variables
345
353 private boolean testTranslationSetup() {
354 testResultValueLabel.setText("");
355 MediaType mediaType = MediaType.parse("application/json");
356 JsonArray jsonArray = new JsonArray();
357 JsonObject jsonObject = new JsonObject();
358 jsonObject.addProperty("Text", testUntranslatedTextField.getText());
359 jsonArray.add(jsonObject);
360 String bodyString = jsonArray.toString();
361
362 RequestBody body = RequestBody.create(mediaType,
363 bodyString);
364 Request request = new Request.Builder()
365 .url(BingTranslator.getTranlatorUrl(targetLanguageCode)).post(body)
366 .addHeader("Ocp-Apim-Subscription-Key", authenticationKeyField.getText())
367 .addHeader("Content-type", "application/json").build();
368 try {
369 Response response = new OkHttpClient().newCall(request).execute();
370 JsonArray responses = JsonParser.parseString(response.body().string()).getAsJsonArray();
371 //As far as I know, there's always exactly one item in the array.
372 JsonObject response0 = responses.get(0).getAsJsonObject();
373 JsonArray translations = response0.getAsJsonArray("translations");
374 JsonObject translation0 = translations.get(0).getAsJsonObject();
375 testResultValueLabel.setText(translation0.get("text").getAsString());
376 return true;
377 } catch (IOException | IllegalStateException | ClassCastException | NullPointerException | IndexOutOfBoundsException e) {
378 logger.log(Level.WARNING, "Test of Bing Translator failed due to exception", e);
379 return false;
380 }
381 }
382
389 String getAuthenticationKey() {
390 return authenticationKeyField.getText();
391 }
392
398 String getTargetLanguageCode() {
399 return targetLanguageCode;
400 }
401}
synchronized static Logger getLogger(String name)
Definition Logger.java:124

Copyright © 2012-2024 Sleuth Kit Labs. Generated on:
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.