19package org.sleuthkit.autopsy.texttranslation.translators;
21import com.google.auth.Credentials;
22import com.google.auth.oauth2.ServiceAccountCredentials;
23import com.google.cloud.translate.Translate;
24import com.google.cloud.translate.TranslateOptions;
25import com.google.cloud.translate.Translation;
26import java.io.FileInputStream;
27import java.io.FileNotFoundException;
28import java.io.IOException;
29import java.io.InputStream;
30import java.net.InetAddress;
31import java.net.UnknownHostException;
32import java.util.logging.Level;
33import javax.swing.JPanel;
34import org.sleuthkit.autopsy.coreutils.Logger;
35import org.apache.commons.lang3.StringUtils;
36import org.openide.util.NbBundle.Messages;
37import org.openide.util.lookup.ServiceProvider;
38import org.sleuthkit.autopsy.coreutils.EscapeUtil;
39import org.sleuthkit.autopsy.coreutils.ThreadConfined;
40import org.sleuthkit.autopsy.texttranslation.TextTranslator;
41import org.sleuthkit.autopsy.texttranslation.TranslationConfigException;
42import org.sleuthkit.autopsy.texttranslation.TranslationException;
72 String host =
"www.google.com";
75 address = InetAddress.getByName(host);
76 return address.isReachable(1500);
77 }
catch (UnknownHostException ex) {
78 logger.log(Level.WARNING,
"Unable to reach google.com due to unknown host", ex);
80 }
catch (IOException ex) {
81 logger.log(Level.WARNING,
"Unable to reach google.com due IOException", ex);
89 throw new TranslationException(
"Failure translating using GoogleTranslator: Cannot connect to Google");
95 String substring =
string.trim();
99 substring = substring.replaceAll(
"(\r\n|\n)",
"<br />");
110 Translation translation
112 String translatedString = translation.getTranslatedText();
115 translatedString = translatedString.replaceAll(
"<br />",
"\n");
120 return translatedString;
121 }
catch (Throwable ex) {
126 throw new TranslationException(
"Google Translator has not been configured, credentials need to be specified");
130 @Messages({
"GoogleTranslator.name.text=Google Translate"})
133 return Bundle.GoogleTranslator_name_text();
150 InputStream credentialStream =
null;
151 Credentials creds =
null;
152 if (StringUtils.isBlank(
settings.getCredentialPath())) {
153 logger.log(Level.INFO,
"No credentials file has been provided for Google Translator");
156 credentialStream =
new FileInputStream(
settings.getCredentialPath());
157 }
catch (FileNotFoundException ex) {
158 logger.log(Level.WARNING,
"JSON file for GoogleTranslator credentials not found", ex);
161 if (credentialStream !=
null) {
163 creds = ServiceAccountCredentials.fromStream(credentialStream);
164 }
catch (IOException ex) {
165 logger.log(Level.WARNING,
"Error converting JSON file to Credentials object for GoogleTranslator", ex);
169 logger.log(Level.WARNING,
"Credentials were not successfully made, no translations will be available from the GoogleTranslator");
172 TranslateOptions.Builder builder = TranslateOptions.newBuilder();
173 builder.setCredentials(creds);
174 builder.setTargetLanguage(
settings.getTargetLanguageCode());
static String unEscapeHtml(String toUnescape)
synchronized static Logger getLogger(String name)
static final int MAX_PAYLOAD_SIZE
static final Logger logger
static boolean googleIsReachable()
String translate(String string)
final GoogleTranslatorSettings settings
JPanel getSettingsPanel()
Translate googleTranslate
GoogleTranslatorSettingsPanel settingsPanel