diff --git a/Core/src/org/sleuthkit/autopsy/texttranslation/translators/GoogleTranslator.java b/Core/src/org/sleuthkit/autopsy/texttranslation/translators/GoogleTranslator.java index 38506ef936..318191e713 100644 --- a/Core/src/org/sleuthkit/autopsy/texttranslation/translators/GoogleTranslator.java +++ b/Core/src/org/sleuthkit/autopsy/texttranslation/translators/GoogleTranslator.java @@ -28,6 +28,8 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.logging.Level; import java.util.logging.Logger; import org.openide.util.NbBundle.Messages; @@ -56,9 +58,26 @@ public final class GoogleTranslator implements TextTranslator { settingsPanel = new GoogleTranslatorSettingsPanel(settings.getCredentialPath(), settings.getTargetLanguageCode()); loadTranslator(); } - + + private static boolean googleIsReachable() { + String host = "www.google.com"; + InetAddress address; + try { + address = InetAddress.getByName(host); + return address.isReachable(1500); + }catch (UnknownHostException ex) { + return false; + } catch (IOException ex) { + return false; + } + } + @Override public String translate(String string) throws TranslationException { + if (!googleIsReachable()) { + throw new TranslationException("Failure translating using GoogleTranslator: Cannot connect to Google"); + } + if (googleTranslate != null) { try { // Translates some text into English, without specifying the source language. @@ -66,10 +85,10 @@ public final class GoogleTranslator implements TextTranslator { // HTML files were producing lots of white space at the end String substring = string.trim(); - // WE can't currently set parameters, so we are using the default behavior of - // asuming the input is HTML. We need to replace newlines with
for Google to preserve them + // We can't currently set parameters, so we are using the default behavior of + // assuming the input is HTML. We need to replace newlines with
for Google to preserve them substring = substring.replaceAll("(\r\n|\n)", "
"); - + // The API complains if the "Payload" is over 204800 bytes. I'm assuming that // deals with the full request. At some point, we get different errors about too // much text. Officially, Google says they will googleTranslate only 5k chars, @@ -81,7 +100,7 @@ public final class GoogleTranslator implements TextTranslator { Translation translation = googleTranslate.translate(substring); String translatedString = translation.getTranslatedText(); - + // put back the newlines translatedString = translatedString.replaceAll("
", "\n"); return translatedString; @@ -93,7 +112,7 @@ public final class GoogleTranslator implements TextTranslator { throw new TranslationException("Google Translator has not been configured, credentials need to be specified"); } } - + @Messages({"GoogleTranslator.name.text=Google Translate"}) @Override public String getName() {