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) {
59 firePropertyChange(
"SettingChanged",
true,
false);
63 public void removeUpdate(DocumentEvent e) {
65 firePropertyChange(
"SettingChanged",
true,
false);
69 public void changedUpdate(DocumentEvent e) {
71 firePropertyChange(
"SettingChanged",
true,
false);
77 targetLanguageCode = code;
83 @Messages({
"BingTranslatorSettingsPanel.warning.targetLanguageFailure=Unable to get list of target languages or parse the result that was received"})
85 Request get_request =
new Request.Builder()
86 .url(GET_TARGET_LANGUAGES_URL).build();
88 Response response =
new OkHttpClient().newCall(get_request).execute();
89 JsonParser parser =
new JsonParser();
90 String responseBody = response.body().string();
91 JsonElement elementBody = parser.parse(responseBody);
92 JsonObject asObject = elementBody.getAsJsonObject();
93 JsonElement translationElement = asObject.get(
"translation");
94 JsonObject responses = translationElement.getAsJsonObject();
95 responses.entrySet().forEach((entry) -> {
96 targetLanguageComboBox.addItem(
new LanguageWrapper(entry.getKey(), entry.getValue().getAsJsonObject().get(
"name").getAsString()));
99 }
catch (IOException | IllegalStateException | ClassCastException | NullPointerException | IndexOutOfBoundsException ex) {
100 logger.log(Level.SEVERE, Bundle.BingTranslatorSettingsPanel_warning_targetLanguageFailure(), ex);
101 warningLabel.setText(Bundle.BingTranslatorSettingsPanel_warning_targetLanguageFailure());
127 @SuppressWarnings(
"unchecked")
146 warningLabel.setForeground(
new java.awt.Color(255, 0, 0));
150 testButton.addActionListener(
new java.awt.event.ActionListener() {
151 public void actionPerformed(java.awt.event.ActionEvent evt) {
160 public void itemStateChanged(java.awt.event.ItemEvent evt) {
176 instructionsScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
187 javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(
this);
188 this.setLayout(layout);
189 layout.setHorizontalGroup(
190 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
191 .addGroup(layout.createSequentialGroup()
193 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
195 .addGroup(layout.createSequentialGroup()
196 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
197 .addGroup(layout.createSequentialGroup()
198 .addComponent(
authenticationKeyLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
199 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
200 .addComponent(
authenticationKeyField, javax.swing.GroupLayout.PREFERRED_SIZE, 486, javax.swing.GroupLayout.PREFERRED_SIZE))
201 .addComponent(
warningLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 551, javax.swing.GroupLayout.PREFERRED_SIZE)
202 .addGroup(layout.createSequentialGroup()
203 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
205 .addComponent(
testButton, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
207 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
208 .addGroup(layout.createSequentialGroup()
209 .addComponent(
untranslatedLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
210 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
211 .addComponent(
testUntranslatedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
212 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
214 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
215 .addComponent(
testResultValueLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
216 .addGroup(layout.createSequentialGroup()
218 .addGap(276, 276, 276)))))
219 .addGap(0, 0, Short.MAX_VALUE)))
222 layout.setVerticalGroup(
223 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
224 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
226 .addComponent(
instructionsScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
227 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
228 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
229 .addComponent(
authenticationKeyField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
231 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
232 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
234 .addComponent(
targetLanguageComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
235 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
236 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
238 .addComponent(
testUntranslatedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
242 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
243 .addComponent(
warningLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
244 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
248 @Messages({
"BingTranslatorSettingsPanel.warning.invalidKey=Invalid translation authentication key"})
253 warningLabel.setText(Bundle.BingTranslatorSettingsPanel_warning_invalidKey());
259 if (!StringUtils.isBlank(selectedCode) && !selectedCode.equals(targetLanguageCode)) {
260 targetLanguageCode = selectedCode;
262 firePropertyChange(
"SettingChanged",
true,
false);
290 MediaType mediaType = MediaType.parse(
"application/json");
291 JsonArray jsonArray =
new JsonArray();
292 JsonObject jsonObject =
new JsonObject();
294 jsonArray.add(jsonObject);
295 String bodyString = jsonArray.toString();
297 RequestBody body = RequestBody.create(mediaType,
299 Request request =
new Request.Builder()
300 .url(
BingTranslator.getTranlatorUrl(targetLanguageCode)).post(body)
302 .addHeader(
"Content-type",
"application/json").build();
304 Response response =
new OkHttpClient().newCall(request).execute();
305 JsonParser parser =
new JsonParser();
306 JsonArray responses = parser.parse(response.body().string()).getAsJsonArray();
308 JsonObject response0 = responses.get(0).getAsJsonObject();
309 JsonArray translations = response0.getAsJsonArray(
"translations");
310 JsonObject translation0 = translations.get(0).getAsJsonObject();
313 }
catch (IOException | IllegalStateException | ClassCastException | NullPointerException | IndexOutOfBoundsException e) {
314 logger.log(Level.WARNING,
"Test of Bing Translator failed due to exception", e);
325 String getAuthenticationKey() {
334 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