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 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;
49 private static final String
GET_TARGET_LANGUAGES_URL =
"https://api.cognitive.microsofttranslator.com/languages?api-version=3.0&scope=translation";
61 public void insertUpdate(DocumentEvent e) {
63 firePropertyChange(
"SettingChanged",
true,
false);
67 public void removeUpdate(DocumentEvent e) {
69 firePropertyChange(
"SettingChanged",
true,
false);
73 public void changedUpdate(DocumentEvent e) {
75 firePropertyChange(
"SettingChanged",
true,
false);
81 targetLanguageCode = code;
85 public void hyperlinkUpdate(HyperlinkEvent e) {
86 if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
88 if(Desktop.isDesktopSupported()) {
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);
104 @Messages({
"BingTranslatorSettingsPanel.warning.targetLanguageFailure=Unable to get list of target languages or parse the result that was received"})
106 Request get_request =
new Request.Builder()
107 .url(GET_TARGET_LANGUAGES_URL).build();
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()));
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());
148 @SuppressWarnings(
"unchecked")
151 java.awt.GridBagConstraints gridBagConstraints;
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));
167 setLayout(
new java.awt.GridBagLayout());
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);
181 warningLabel.setForeground(
new java.awt.Color(255, 0, 0));
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);
194 testButton.addActionListener(
new java.awt.event.ActionListener() {
195 public void actionPerformed(java.awt.event.ActionEvent evt) {
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);
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);
219 public void itemStateChanged(java.awt.event.ItemEvent evt) {
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);
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);
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);
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);
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);
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);
284 instructionsScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
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);
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);
314 @Messages({
"BingTranslatorSettingsPanel.warning.invalidKey=Invalid translation authentication key"})
319 warningLabel.setText(Bundle.BingTranslatorSettingsPanel_warning_invalidKey());
325 if (!StringUtils.isBlank(selectedCode) && !selectedCode.equals(targetLanguageCode)) {
326 targetLanguageCode = selectedCode;
328 firePropertyChange(
"SettingChanged",
true,
false);
356 MediaType mediaType = MediaType.parse(
"application/json");
357 JsonArray jsonArray =
new JsonArray();
358 JsonObject jsonObject =
new JsonObject();
360 jsonArray.add(jsonObject);
361 String bodyString = jsonArray.toString();
363 RequestBody body = RequestBody.create(mediaType,
365 Request request =
new Request.Builder()
366 .url(
BingTranslator.getTranlatorUrl(targetLanguageCode)).post(body)
368 .addHeader(
"Content-type",
"application/json").build();
370 Response response =
new OkHttpClient().newCall(request).execute();
371 JsonParser parser =
new JsonParser();
372 JsonArray responses = parser.parse(response.body().string()).getAsJsonArray();
374 JsonObject response0 = responses.get(0).getAsJsonObject();
375 JsonArray translations = response0.getAsJsonArray(
"translations");
376 JsonObject translation0 = translations.get(0).getAsJsonObject();
379 }
catch (IOException | IllegalStateException | ClassCastException | NullPointerException | IndexOutOfBoundsException e) {
380 logger.log(Level.WARNING,
"Test of Bing Translator failed due to exception", e);
391 String getAuthenticationKey() {
400 String getTargetLanguageCode() {
javax.swing.JTextField testUntranslatedTextField
javax.swing.JLabel testResultValueLabel
javax.swing.JTextPane instructionsTextArea
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.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