Autopsy 4.22.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
GoogleTranslator.java
Go to the documentation of this file.
1/*
2 * Autopsy
3 *
4 * Copyright 2019 Basis Technology Corp.
5 * Contact: carrier <at> sleuthkit <dot> org
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19package org.sleuthkit.autopsy.texttranslation.translators;
20
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;
43
48@ServiceProvider(service = TextTranslator.class)
49public final class GoogleTranslator implements TextTranslator {
50
51 private static final Logger logger = Logger.getLogger(GoogleTranslator.class.getName());
52 //See translate method for justification of this limit.
53 private static final int MAX_PAYLOAD_SIZE = 5000;
56 private Translate googleTranslate;
57
62 // Instantiates a client
64 }
65
71 private static boolean googleIsReachable() {
72 String host = "www.google.com";
73 InetAddress address;
74 try {
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);
79 return false;
80 } catch (IOException ex) {
81 logger.log(Level.WARNING, "Unable to reach google.com due IOException", ex);
82 return false;
83 }
84 }
85
86 @Override
87 public String translate(String string) throws TranslationException {
88 if (!googleIsReachable()) {
89 throw new TranslationException("Failure translating using GoogleTranslator: Cannot connect to Google");
90 }
91
92 if (googleTranslate != null) {
93 try {
94 // Translates some text into English, without specifying the source language.
95 String substring = string.trim();
96
97 // We can't currently set parameters, so we are using the default behavior of
98 // assuming the input is HTML. We need to replace newlines with <br> for Google to preserve them
99 substring = substring.replaceAll("(\r\n|\n)", "<br />");
100
101 // The API complains if the "Payload" is over 204800 bytes. Google references that
102 //their service is optimized for 2K code points and recommends keeping the requests that size.
103 //There is a hard limit of 30K code points per request. There is also a time-based quota that
104 //we are not enforcing, which may lead to 403 errors. We are currently configured for a max of 5K
105 //in each request, for two reasons. 1) To be more in line with Google's recommendation. 2) To
106 //minimize accidental exceedence of time based quotas.
107 if (substring.length() > MAX_PAYLOAD_SIZE) {
108 substring = substring.substring(0, MAX_PAYLOAD_SIZE);
109 }
110 Translation translation
111 = googleTranslate.translate(substring);
112 String translatedString = translation.getTranslatedText();
113
114 // put back the newlines
115 translatedString = translatedString.replaceAll("<br />", "\n");
116
117 // With our current settings, Google Translate outputs HTML
118 // so we need to undo the escape characters.
119 translatedString = EscapeUtil.unEscapeHtml(translatedString);
120 return translatedString;
121 } catch (Throwable ex) {
122 //Catching throwables because some of this Google Translate code throws throwables
123 throw new TranslationException("Failure translating using GoogleTranslator", ex);
124 }
125 } else {
126 throw new TranslationException("Google Translator has not been configured, credentials need to be specified");
127 }
128 }
129
130 @Messages({"GoogleTranslator.name.text=Google Translate"})
131 @Override
132 public String getName() {
133 return Bundle.GoogleTranslator_name_text();
134 }
135
136 @Override
138 public JPanel getSettingsPanel() {
139 if(settingsPanel == null) {
140 settingsPanel = new GoogleTranslatorSettingsPanel(settings.getCredentialPath(), settings.getTargetLanguageCode());
141 }
142 return settingsPanel;
143 }
144
149 private void loadTranslator() {
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");
154 } else {
155 try {
156 credentialStream = new FileInputStream(settings.getCredentialPath());
157 } catch (FileNotFoundException ex) {
158 logger.log(Level.WARNING, "JSON file for GoogleTranslator credentials not found", ex);
159 }
160 }
161 if (credentialStream != null) {
162 try {
163 creds = ServiceAccountCredentials.fromStream(credentialStream);
164 } catch (IOException ex) {
165 logger.log(Level.WARNING, "Error converting JSON file to Credentials object for GoogleTranslator", ex);
166 }
167 }
168 if (creds == null) {
169 logger.log(Level.WARNING, "Credentials were not successfully made, no translations will be available from the GoogleTranslator");
170 googleTranslate = null;
171 } else {
172 TranslateOptions.Builder builder = TranslateOptions.newBuilder();
173 builder.setCredentials(creds);
174 builder.setTargetLanguage(settings.getTargetLanguageCode());
175 googleTranslate = builder.build().getService();
176 }
177 }
178
179 @Override
181 settings.setTargetLanguageCode(settingsPanel.getTargetLanguageCode());
182 settings.setCredentialPath(settingsPanel.getCredentialsPath());
183 settings.saveSettings();
185 }
186
187 @Override
188 public int getMaxTextChars() {
189 return MAX_PAYLOAD_SIZE;
190 }
191}
static String unEscapeHtml(String toUnescape)
synchronized static Logger getLogger(String name)
Definition Logger.java:124

Copyright © 2012-2024 Sleuth Kit Labs. Generated on:
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.