diff --git a/Core/src/org/sleuthkit/autopsy/texttranslation/Bundle.properties b/Core/src/org/sleuthkit/autopsy/texttranslation/Bundle.properties new file mode 100755 index 0000000000000000000000000000000000000000..5fcf80851cecb213007e7afdfc6620e247f46014 --- /dev/null +++ b/Core/src/org/sleuthkit/autopsy/texttranslation/Bundle.properties @@ -0,0 +1,8 @@ +OptionsCategory_Name_Machine_Translation=Machine Translation +OptionsCategory_Keywords_Machine_Translation_Settings=Machine Translation Settings +TranslationContentPanel.ocrLabel.text=OCR: +TranslationOptionsPanelController.moduleErr=Module Error +TranslationOptionsPanelController.moduleErr.msg=A module caused an error listening to TranslationSettingsPanelController updates. See log to determine which module. Some data could be incomplete. +TranslationContentPanel.showLabel.text=Show: +TranslationOptionsPanel.translationServiceLabel.text=Text translator: +TranslationOptionsPanel.translationOptionsDescription.text=Configure a 3rd party text translation service to enable text and file name translation. diff --git a/Core/src/org/sleuthkit/autopsy/texttranslation/Bundle.properties-MERGED b/Core/src/org/sleuthkit/autopsy/texttranslation/Bundle.properties-MERGED new file mode 100755 index 0000000000000000000000000000000000000000..665c07c36f38a71178e045205ae64d61d2c48489 --- /dev/null +++ b/Core/src/org/sleuthkit/autopsy/texttranslation/Bundle.properties-MERGED @@ -0,0 +1,12 @@ +OptionsCategory_Name_Machine_Translation=Machine Translation +OptionsCategory_Keywords_Machine_Translation_Settings=Machine Translation Settings +TranslationContentPanel.ocrLabel.text=OCR: +TranslationOptionsPanel.noTextTranslators.text=No text translators exist, translation is disabled. +TranslationOptionsPanel.noTextTranslatorSelected.text=No text translator selected, translation is disabled. +TranslationOptionsPanel.textTranslatorsUnavailable.text=Unable to get selected text translator, translation is disabled. +TranslationOptionsPanel.translationDisabled.text=Translation disabled +TranslationOptionsPanelController.moduleErr=Module Error +TranslationOptionsPanelController.moduleErr.msg=A module caused an error listening to TranslationSettingsPanelController updates. See log to determine which module. Some data could be incomplete. +TranslationContentPanel.showLabel.text=Show: +TranslationOptionsPanel.translationServiceLabel.text=Text translator: +TranslationOptionsPanel.translationOptionsDescription.text=Configure a 3rd party text translation service to enable text and file name translation. diff --git a/Core/src/org/sleuthkit/autopsy/texttranslation/TextTranslationService.java b/Core/src/org/sleuthkit/autopsy/texttranslation/TextTranslationService.java index b24dca2aacd9d6ce5ab4950732641a8e23b338fb..638bad71c85005a49330d47bf6e2022b92dfb38d 100755 --- a/Core/src/org/sleuthkit/autopsy/texttranslation/TextTranslationService.java +++ b/Core/src/org/sleuthkit/autopsy/texttranslation/TextTranslationService.java @@ -23,6 +23,7 @@ import java.util.Optional; import org.openide.util.Lookup; import org.sleuthkit.autopsy.core.UserPreferences; +import javax.annotation.concurrent.GuardedBy; /** * Performs a lookup for a TextTranslator service provider and if present, will @@ -33,6 +34,7 @@ public final class TextTranslationService { private final static TextTranslationService tts = new TextTranslationService(); private final Collection<? extends TextTranslator> translators; + @GuardedBy("this") private Optional<TextTranslator> selectedTranslator; private TextTranslationService() { @@ -50,7 +52,7 @@ public static TextTranslationService getInstance() { * Update the translator currently in use to match the one saved to the user * preferences */ - public void updateSelectedTranslator() { + synchronized void updateSelectedTranslator() { String translatorName = UserPreferences.getTextTranslatorName(); for (TextTranslator translator : translators) { if (translator.getName().equals(translatorName)) { @@ -75,7 +77,7 @@ public void updateSelectedTranslator() { * when specific translation * implementations fail */ - public String translate(String input) throws NoServiceProviderException, TranslationException { + public synchronized String translate(String input) throws NoServiceProviderException, TranslationException { if (hasProvider()) { return selectedTranslator.get().translate(input); } @@ -92,7 +94,7 @@ public String translate(String input) throws NoServiceProviderException, Transla * * @throws NoServiceProviderException */ - public TextTranslator getTranslatorByName(String translatorName) throws NoServiceProviderException { + TextTranslator getTranslatorByName(String translatorName) throws NoServiceProviderException { for (TextTranslator translator : translators) { if (translator.getName().equals(translatorName)) { return translator; @@ -107,7 +109,7 @@ public TextTranslator getTranslatorByName(String translatorName) throws NoServic * * @return an unmodifiable collection of TextTranslators */ - public Collection<? extends TextTranslator> getTranslators() { + Collection<? extends TextTranslator> getTranslators() { return Collections.unmodifiableCollection(translators); } @@ -117,16 +119,16 @@ public Collection<? extends TextTranslator> getTranslators() { * * @return */ - public boolean hasProvider() { + public synchronized boolean hasProvider() { return selectedTranslator.isPresent(); } - + /** - * Returns the hard limit for translation request sizes. - * - * @return + * Gets the maximum number of characters allowed in a translation request. + * + * @return The maximum character count. */ - public int getMaxPayloadSize() { - return selectedTranslator.get().getMaxPayloadSize(); + public synchronized int getMaxTextChars() { + return selectedTranslator.get().getMaxTextChars(); } } diff --git a/Core/src/org/sleuthkit/autopsy/texttranslation/TextTranslator.java b/Core/src/org/sleuthkit/autopsy/texttranslation/TextTranslator.java index e996e6331e0809204c48f01b2da747ff1a67246e..525e432d1dc6d3dfc0a78c1b46efca0ee772dbe2 100755 --- a/Core/src/org/sleuthkit/autopsy/texttranslation/TextTranslator.java +++ b/Core/src/org/sleuthkit/autopsy/texttranslation/TextTranslator.java @@ -18,7 +18,7 @@ */ package org.sleuthkit.autopsy.texttranslation; -import java.awt.Component; +import javax.swing.JPanel; /** * Interface for creating text translators. Implementing classes will be picked @@ -45,22 +45,24 @@ public interface TextTranslator { String getName(); /** - * Get the component to display on the settings options panel when this + * Get the JPanel to display on the settings options panel when this * TextTranslator is selected * - * @return the component which displays the settings options + * @return the panel which displays the settings options */ - Component getComponent(); + JPanel getSettingsPanel(); /** - * Save the settings as they have been modified in the component. + * Saves the current state of the settings in the settings panel. + * + * @throws TranslationConfigException */ - void saveSettings(); + void saveSettings() throws TranslationConfigException; /** - * Returns the hard limit for translation request sizes. + * Gets the maximum number of characters allowed in a translation request. * - * @return + * @return The maximum character count. */ - int getMaxPayloadSize(); + int getMaxTextChars(); } diff --git a/Core/src/org/sleuthkit/autopsy/texttranslation/TranslationConfigException.java b/Core/src/org/sleuthkit/autopsy/texttranslation/TranslationConfigException.java new file mode 100755 index 0000000000000000000000000000000000000000..cb3df8c268903d62a6a84e4df799f51e45824b8c --- /dev/null +++ b/Core/src/org/sleuthkit/autopsy/texttranslation/TranslationConfigException.java @@ -0,0 +1,48 @@ +/* + * Autopsy Forensic Browser + * + * Copyright 2019 Basis Technology Corp. + * Contact: carrier <at> sleuthkit <dot> org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.sleuthkit.autopsy.texttranslation; + +/** + * Instances of this exception class are thrown when there is an error + * configuring text translation. + */ +public class TranslationConfigException extends Exception { + + private static final long serialVersionUID = 1L; + + /** + * Constructs a new exception with the specified message. + * + * @param message The message. + */ + public TranslationConfigException(String message) { + super(message); + } + + /** + * Constructs a new exception with the specified message and cause. + * + * @param message The message. + * @param cause The cause. + */ + public TranslationConfigException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/Core/src/org/sleuthkit/autopsy/texttranslation/TranslationException.java b/Core/src/org/sleuthkit/autopsy/texttranslation/TranslationException.java index 4d8703e585c4391592dd26ffc2cb9a9cbd0d24f6..01adc6617615f462b606e902250bfb286590698a 100755 --- a/Core/src/org/sleuthkit/autopsy/texttranslation/TranslationException.java +++ b/Core/src/org/sleuthkit/autopsy/texttranslation/TranslationException.java @@ -22,13 +22,8 @@ * Provides a system exception for Text Translation errors */ public class TranslationException extends Exception { - - /** - * Constructs a new exception with null as its message. - */ - public TranslationException() { - super(); - } + + private static final long serialVersionUID = 1L; /** * Constructs a new exception with the specified message. diff --git a/Core/src/org/sleuthkit/autopsy/texttranslation/ui/TranslationOptionsPanel.form b/Core/src/org/sleuthkit/autopsy/texttranslation/TranslationOptionsPanel.form similarity index 91% rename from Core/src/org/sleuthkit/autopsy/texttranslation/ui/TranslationOptionsPanel.form rename to Core/src/org/sleuthkit/autopsy/texttranslation/TranslationOptionsPanel.form index 8705086cfe80e0452142b30b4108cbafef177b0a..52cd0fb65cd56c787bdc285f80e6ff9a1713f8a4 100644 --- a/Core/src/org/sleuthkit/autopsy/texttranslation/ui/TranslationOptionsPanel.form +++ b/Core/src/org/sleuthkit/autopsy/texttranslation/TranslationOptionsPanel.form @@ -61,7 +61,7 @@ <Component class="javax.swing.JLabel" name="translationServiceLabel"> <Properties> <Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor"> - <ResourceString bundle="org/sleuthkit/autopsy/texttranslation/ui/Bundle.properties" key="TranslationOptionsPanel.translationServiceLabel.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, "{key}")"/> + <ResourceString bundle="org/sleuthkit/autopsy/texttranslation/Bundle.properties" key="TranslationOptionsPanel.translationServiceLabel.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, "{key}")"/> </Property> </Properties> </Component> @@ -72,7 +72,7 @@ <Component class="javax.swing.JLabel" name="translationOptionsDescription"> <Properties> <Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor"> - <ResourceString bundle="org/sleuthkit/autopsy/texttranslation/ui/Bundle.properties" key="TranslationOptionsPanel.translationOptionsDescription.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, "{key}")"/> + <ResourceString bundle="org/sleuthkit/autopsy/texttranslation/Bundle.properties" key="TranslationOptionsPanel.translationOptionsDescription.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, "{key}")"/> </Property> </Properties> </Component> diff --git a/Core/src/org/sleuthkit/autopsy/texttranslation/ui/TranslationOptionsPanel.java b/Core/src/org/sleuthkit/autopsy/texttranslation/TranslationOptionsPanel.java similarity index 95% rename from Core/src/org/sleuthkit/autopsy/texttranslation/ui/TranslationOptionsPanel.java rename to Core/src/org/sleuthkit/autopsy/texttranslation/TranslationOptionsPanel.java index b742f51be039f6120cbf1bc5f17765737cf5fdd3..3c9ee7dc7b596fcffe3543bd5e17fa036ce1c3da 100644 --- a/Core/src/org/sleuthkit/autopsy/texttranslation/ui/TranslationOptionsPanel.java +++ b/Core/src/org/sleuthkit/autopsy/texttranslation/TranslationOptionsPanel.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.sleuthkit.autopsy.texttranslation.ui; +package org.sleuthkit.autopsy.texttranslation; import java.awt.BorderLayout; import java.awt.Color; @@ -27,14 +27,12 @@ import javax.swing.JLabel; import org.openide.util.NbBundle.Messages; import org.sleuthkit.autopsy.core.UserPreferences; -import org.sleuthkit.autopsy.texttranslation.NoServiceProviderException; -import org.sleuthkit.autopsy.texttranslation.TextTranslationService; import org.sleuthkit.autopsy.coreutils.Logger; /** * Options panel to display translation options */ -public class TranslationOptionsPanel extends javax.swing.JPanel { +final class TranslationOptionsPanel extends javax.swing.JPanel { private final static Logger logger = Logger.getLogger(TranslationOptionsPanel.class.getName()); private static final long serialVersionUID = 1L; @@ -45,7 +43,7 @@ public class TranslationOptionsPanel extends javax.swing.JPanel { * Creates new form TranslationOptionsPanel */ @Messages({"TranslationOptionsPanel.translationDisabled.text=Translation disabled"}) - public TranslationOptionsPanel(TranslationOptionsPanelController theController) { + TranslationOptionsPanel(TranslationOptionsPanelController theController) { initComponents(); controller = theController; translatorComboBox.addItem(Bundle.TranslationOptionsPanel_translationDisabled_text()); @@ -78,7 +76,7 @@ private void loadSelectedPanelSettings() { translationServicePanel.removeAll(); if (translatorComboBox.getSelectedItem() != null && !translatorComboBox.getSelectedItem().toString().equals(Bundle.TranslationOptionsPanel_translationDisabled_text())) { try { - Component panel = TextTranslationService.getInstance().getTranslatorByName(translatorComboBox.getSelectedItem().toString()).getComponent(); + Component panel = TextTranslationService.getInstance().getTranslatorByName(translatorComboBox.getSelectedItem().toString()).getSettingsPanel(); panel.addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { @@ -126,7 +124,7 @@ void store() { if (currentSelection != null && !currentSelection.equals(Bundle.TranslationOptionsPanel_translationDisabled_text())) { try { TextTranslationService.getInstance().getTranslatorByName(currentSelection).saveSettings(); - } catch (NoServiceProviderException ex) { + } catch (NoServiceProviderException | TranslationConfigException ex) { logger.log(Level.WARNING, "Unable to save settings for TextTranslator named: " + currentSelection, ex); } } @@ -172,7 +170,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { .addGap(10, 10, 10) .addComponent(translatorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) - .addComponent(translationOptionsDescription, javax.swing.GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE)) + .addComponent(translationOptionsDescription, javax.swing.GroupLayout.PREFERRED_SIZE, 462, Short.MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( diff --git a/Core/src/org/sleuthkit/autopsy/texttranslation/ui/TranslationOptionsPanelController.java b/Core/src/org/sleuthkit/autopsy/texttranslation/TranslationOptionsPanelController.java similarity index 98% rename from Core/src/org/sleuthkit/autopsy/texttranslation/ui/TranslationOptionsPanelController.java rename to Core/src/org/sleuthkit/autopsy/texttranslation/TranslationOptionsPanelController.java index 65488292c74f74f0cf7f5e4eda8bec8af0604602..c52559d6654c5846542ebc72117c95767b0e778b 100644 --- a/Core/src/org/sleuthkit/autopsy/texttranslation/ui/TranslationOptionsPanelController.java +++ b/Core/src/org/sleuthkit/autopsy/texttranslation/TranslationOptionsPanelController.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.sleuthkit.autopsy.texttranslation.ui; +package org.sleuthkit.autopsy.texttranslation; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; diff --git a/Core/src/org/sleuthkit/autopsy/texttranslation/translators/BingTranslator.java b/Core/src/org/sleuthkit/autopsy/texttranslation/translators/BingTranslator.java index 3dfd984b0a04a2b58c70b142c2c37852f8be63dd..948aae921787851ecca458901e7e96ba01623de6 100644 --- a/Core/src/org/sleuthkit/autopsy/texttranslation/translators/BingTranslator.java +++ b/Core/src/org/sleuthkit/autopsy/texttranslation/translators/BingTranslator.java @@ -26,11 +26,12 @@ import com.squareup.okhttp.Request; import com.squareup.okhttp.RequestBody; import com.squareup.okhttp.Response; -import java.awt.Component; import java.io.IOException; +import javax.swing.JPanel; import org.openide.util.NbBundle.Messages; import org.openide.util.lookup.ServiceProvider; import org.sleuthkit.autopsy.texttranslation.TextTranslator; +import org.sleuthkit.autopsy.texttranslation.TranslationConfigException; import org.sleuthkit.autopsy.texttranslation.TranslationException; /** @@ -132,12 +133,12 @@ public String getName() { } @Override - public Component getComponent() { + public JPanel getSettingsPanel() { return settingsPanel; } @Override - public void saveSettings() { + public void saveSettings() throws TranslationConfigException { settings.setAuthenticationKey(settingsPanel.getAuthenticationKey()); settings.setTargetLanguageCode(settingsPanel.getTargetLanguageCode()); settings.saveSettings(); @@ -173,7 +174,7 @@ private String parseJSONResponse(String json_text) throws TranslationException { } @Override - public int getMaxPayloadSize() { + public int getMaxTextChars() { return MAX_STRING_LENGTH; } } diff --git a/Core/src/org/sleuthkit/autopsy/texttranslation/translators/GoogleTranslator.java b/Core/src/org/sleuthkit/autopsy/texttranslation/translators/GoogleTranslator.java index 887da71c8dd9c82a457a748317fcbb20efe9689a..ec741902293001def059c0a8b12a206b3540d833 100644 --- a/Core/src/org/sleuthkit/autopsy/texttranslation/translators/GoogleTranslator.java +++ b/Core/src/org/sleuthkit/autopsy/texttranslation/translators/GoogleTranslator.java @@ -23,7 +23,6 @@ import com.google.cloud.translate.Translate; import com.google.cloud.translate.TranslateOptions; import com.google.cloud.translate.Translation; -import java.awt.Component; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; @@ -31,12 +30,14 @@ import java.net.InetAddress; import java.net.UnknownHostException; import java.util.logging.Level; +import javax.swing.JPanel; import org.sleuthkit.autopsy.coreutils.Logger; import org.apache.commons.lang3.StringUtils; import org.openide.util.NbBundle.Messages; import org.openide.util.lookup.ServiceProvider; import org.sleuthkit.autopsy.coreutils.EscapeUtil; import org.sleuthkit.autopsy.texttranslation.TextTranslator; +import org.sleuthkit.autopsy.texttranslation.TranslationConfigException; import org.sleuthkit.autopsy.texttranslation.TranslationException; /** @@ -133,7 +134,7 @@ public String getName() { } @Override - public Component getComponent() { + public JPanel getSettingsPanel() { return settingsPanel; } @@ -172,7 +173,7 @@ private void loadTranslator() { } @Override - public void saveSettings() { + public void saveSettings() throws TranslationConfigException { settings.setTargetLanguageCode(settingsPanel.getTargetLanguageCode()); settings.setCredentialPath(settingsPanel.getCredentialsPath()); settings.saveSettings(); @@ -180,7 +181,7 @@ public void saveSettings() { } @Override - public int getMaxPayloadSize() { + public int getMaxTextChars() { return MAX_PAYLOAD_SIZE; } } diff --git a/Core/src/org/sleuthkit/autopsy/texttranslation/ui/Bundle.properties b/Core/src/org/sleuthkit/autopsy/texttranslation/ui/Bundle.properties index 15c0fccadd97d6c1356a7b26e8da2aa53968feb7..ac480ca0e14b474e286f8688cf29b1bb83e2e41f 100644 --- a/Core/src/org/sleuthkit/autopsy/texttranslation/ui/Bundle.properties +++ b/Core/src/org/sleuthkit/autopsy/texttranslation/ui/Bundle.properties @@ -1,8 +1,6 @@ OptionsCategory_Name_Machine_Translation=Machine Translation OptionsCategory_Keywords_Machine_Translation_Settings=Machine Translation Settings TranslationContentPanel.ocrLabel.text=OCR: -TranslationOptionsPanel.translationServiceLabel.text=Text translator: TranslationOptionsPanelController.moduleErr=Module Error TranslationOptionsPanelController.moduleErr.msg=A module caused an error listening to TranslationSettingsPanelController updates. See log to determine which module. Some data could be incomplete. -TranslationOptionsPanel.translationOptionsDescription.text=Configure a 3rd party text translation service to enable text and file name translation. TranslationContentPanel.showLabel.text=Show: diff --git a/Core/src/org/sleuthkit/autopsy/texttranslation/ui/Bundle.properties-MERGED b/Core/src/org/sleuthkit/autopsy/texttranslation/ui/Bundle.properties-MERGED index eee82c541b9b6eeb03cb14b15ebce6a3c6af83c8..141ca2f7ef84e31f1fe21a3480e2549fe903217c 100644 --- a/Core/src/org/sleuthkit/autopsy/texttranslation/ui/Bundle.properties-MERGED +++ b/Core/src/org/sleuthkit/autopsy/texttranslation/ui/Bundle.properties-MERGED @@ -17,12 +17,6 @@ TranslatedTextViewer.title=Translation TranslatedTextViewer.toolTip=Displays translated file text. TranslationContentPanel.autoDetectOCR=Autodetect language TranslationContentPanel.ocrLabel.text=OCR: -TranslationOptionsPanel.noTextTranslators.text=No text translators exist, translation is disabled. -TranslationOptionsPanel.noTextTranslatorSelected.text=No text translator selected, translation is disabled. -TranslationOptionsPanel.textTranslatorsUnavailable.text=Unable to get selected text translator, translation is disabled. -TranslationOptionsPanel.translationDisabled.text=Translation disabled -TranslationOptionsPanel.translationServiceLabel.text=Text translator: TranslationOptionsPanelController.moduleErr=Module Error TranslationOptionsPanelController.moduleErr.msg=A module caused an error listening to TranslationSettingsPanelController updates. See log to determine which module. Some data could be incomplete. -TranslationOptionsPanel.translationOptionsDescription.text=Configure a 3rd party text translation service to enable text and file name translation. TranslationContentPanel.showLabel.text=Show: diff --git a/Core/src/org/sleuthkit/autopsy/texttranslation/ui/TranslatedTextViewer.java b/Core/src/org/sleuthkit/autopsy/texttranslation/ui/TranslatedTextViewer.java index 562fa98ddd0416278178367c1486469ccb4dc441..8c49c23ed723afec9e6168d4fba9d534172216f5 100644 --- a/Core/src/org/sleuthkit/autopsy/texttranslation/ui/TranslatedTextViewer.java +++ b/Core/src/org/sleuthkit/autopsy/texttranslation/ui/TranslatedTextViewer.java @@ -96,7 +96,7 @@ public void setNode(final Node node) { } } - int payloadMaxInKB = TextTranslationService.getInstance().getMaxPayloadSize() / 1000; + int payloadMaxInKB = TextTranslationService.getInstance().getMaxTextChars() / 1000; panel.setWarningLabelMsg(String.format(Bundle.TranslatedTextViewer_maxPayloadSize(), payloadMaxInKB)); //Force a background task.