5106: Error message when we can't connect to Google.

This commit is contained in:
Brian Kjersten 2019-05-31 17:40:53 -04:00
parent b16b27f23b
commit ab242a405e

View File

@ -28,6 +28,8 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.openide.util.NbBundle.Messages; import org.openide.util.NbBundle.Messages;
@ -57,8 +59,25 @@ public final class GoogleTranslator implements TextTranslator {
loadTranslator(); 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 @Override
public String translate(String string) throws TranslationException { public String translate(String string) throws TranslationException {
if (!googleIsReachable()) {
throw new TranslationException("Failure translating using GoogleTranslator: Cannot connect to Google");
}
if (googleTranslate != null) { if (googleTranslate != null) {
try { try {
// Translates some text into English, without specifying the source language. // Translates some text into English, without specifying the source language.
@ -66,8 +85,8 @@ public final class GoogleTranslator implements TextTranslator {
// HTML files were producing lots of white space at the end // HTML files were producing lots of white space at the end
String substring = string.trim(); String substring = string.trim();
// WE can't currently set parameters, so we are using the default behavior of // 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 <br> for Google to preserve them // assuming the input is HTML. We need to replace newlines with <br> for Google to preserve them
substring = substring.replaceAll("(\r\n|\n)", "<br />"); substring = substring.replaceAll("(\r\n|\n)", "<br />");
// The API complains if the "Payload" is over 204800 bytes. I'm assuming that // The API complains if the "Payload" is over 204800 bytes. I'm assuming that