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 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()));
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());
147 @SuppressWarnings(
"unchecked")
150 java.awt.GridBagConstraints gridBagConstraints;
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));
166 setLayout(
new java.awt.GridBagLayout());
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);
180 warningLabel.setForeground(
new java.awt.Color(255, 0, 0));
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);
193 testButton.addActionListener(
new java.awt.event.ActionListener() {
194 public void actionPerformed(java.awt.event.ActionEvent evt) {
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);
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);
218 public void itemStateChanged(java.awt.event.ItemEvent evt) {
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);
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);
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);
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);
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);
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);
283 instructionsScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
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);
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);
313 @Messages({
"BingTranslatorSettingsPanel.warning.invalidKey=Invalid translation authentication key"})
318 warningLabel.setText(Bundle.BingTranslatorSettingsPanel_warning_invalidKey());
324 if (!StringUtils.isBlank(selectedCode) && !selectedCode.equals(targetLanguageCode)) {
325 targetLanguageCode = selectedCode;
327 firePropertyChange(
"SettingChanged",
true,
false);
355 MediaType mediaType = MediaType.parse(
"application/json");
356 JsonArray jsonArray =
new JsonArray();
357 JsonObject jsonObject =
new JsonObject();
359 jsonArray.add(jsonObject);
360 String bodyString = jsonArray.toString();
362 RequestBody body = RequestBody.create(mediaType,
364 Request request =
new Request.Builder()
365 .url(
BingTranslator.getTranlatorUrl(targetLanguageCode)).post(body)
367 .addHeader(
"Content-type",
"application/json").build();
369 Response response =
new OkHttpClient().newCall(request).execute();
370 JsonArray responses = JsonParser.parseString(response.body().string()).getAsJsonArray();
372 JsonObject response0 = responses.get(0).getAsJsonObject();
373 JsonArray translations = response0.getAsJsonArray(
"translations");
374 JsonObject translation0 = translations.get(0).getAsJsonObject();
377 }
catch (IOException | IllegalStateException | ClassCastException | NullPointerException | IndexOutOfBoundsException e) {
378 logger.log(Level.WARNING,
"Test of Bing Translator failed due to exception", e);
389 String getAuthenticationKey() {
398 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