Skip to content

Commit 7e86b53

Browse files
f4b1uxpalumbamboodavc0n
authored
Add document translation support
Co-authored-by: Daniele Palumbo <daniele.palumbo@translated.net> Co-authored-by: Davide Conti <davide.conti@translated.com>
1 parent bb58278 commit 7e86b53

12 files changed

Lines changed: 487 additions & 0 deletions
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.translated.lara.errors;
2+
3+
public class S3Exception extends Exception {
4+
5+
private final int statusCode;
6+
7+
public S3Exception(int statusCode, String message) {
8+
super(message);
9+
10+
this.statusCode = statusCode;
11+
}
12+
13+
public int getStatusCode() {
14+
return statusCode;
15+
}
16+
}

src/main/java/com/translated/lara/net/LaraClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import com.translated.lara.Version;
88
import com.translated.lara.errors.LaraApiConnectionException;
99
import com.translated.lara.errors.LaraException;
10+
import com.translated.lara.net.json.DocumentStatusTypeAdapter;
1011
import com.translated.lara.net.json.TextResultValueTypeAdapter;
12+
import com.translated.lara.translator.Document;
1113
import com.translated.lara.translator.TextResult;
1214

1315
import javax.crypto.Mac;
@@ -37,6 +39,7 @@ public class LaraClient {
3739

3840
private final Gson gson = new GsonBuilder()
3941
.registerTypeAdapter(TextResult.Value.class, new TextResultValueTypeAdapter())
42+
.registerTypeAdapter(Document.Status.class, new DocumentStatusTypeAdapter())
4043
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
4144
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
4245
.create();
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.translated.lara.net;
2+
3+
import com.translated.lara.errors.S3Exception;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.net.HttpURLConnection;
10+
import java.net.URL;
11+
import java.nio.charset.StandardCharsets;
12+
import java.util.Collections;
13+
import java.util.Map;
14+
15+
public class S3Client {
16+
17+
public void upload(String url, Map<String, Object> s3Params, File file) throws S3Exception {
18+
RequestBody body = new MultipartRequestBody(s3Params, Collections.singletonMap("file", file));
19+
HttpURLConnection connection = null;
20+
21+
try {
22+
// Initialize the connection
23+
connection = (HttpURLConnection) new URL(url).openConnection();
24+
connection.setRequestMethod("POST");
25+
connection.setDoOutput(true);
26+
connection.setUseCaches(false);
27+
connection.setRequestProperty("Content-Type", body.contentType());
28+
29+
// Send the multipart request body
30+
body.send(connection.getOutputStream());
31+
32+
int responseCode = connection.getResponseCode();
33+
34+
if (responseCode != HttpURLConnection.HTTP_NO_CONTENT && responseCode != HttpURLConnection.HTTP_OK) {
35+
String errMsg = readErrorBody(connection);
36+
37+
throw new S3Exception(responseCode, errMsg);
38+
}
39+
} catch (IOException e) {
40+
throw new S3Exception(500, e.getMessage());
41+
} finally {
42+
if (connection != null) {
43+
connection.disconnect();
44+
}
45+
}
46+
}
47+
48+
public InputStream download(String url) throws S3Exception {
49+
try {
50+
return new URL(url).openStream();
51+
} catch (IOException e) {
52+
throw new S3Exception(500, e.getMessage());
53+
}
54+
}
55+
56+
private static String readErrorBody(HttpURLConnection conn) {
57+
InputStream is = conn.getErrorStream();
58+
if (is == null) {
59+
return "";
60+
}
61+
try (InputStream in = is; ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
62+
byte[] buf = new byte[4096];
63+
int n;
64+
while ((n = in.read(buf)) != -1) {
65+
baos.write(buf, 0, n);
66+
}
67+
return baos.toString(StandardCharsets.UTF_8.name()).trim();
68+
} catch (IOException ignored) {
69+
return "";
70+
}
71+
}
72+
73+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.translated.lara.net.json;
2+
3+
import com.google.gson.JsonDeserializationContext;
4+
import com.google.gson.JsonDeserializer;
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonParseException;
7+
import com.translated.lara.translator.Document;
8+
9+
import java.lang.reflect.Type;
10+
11+
public class DocumentStatusTypeAdapter implements JsonDeserializer<Document.Status> {
12+
@Override
13+
public Document.Status deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
14+
if (jsonElement != null && !jsonElement.isJsonNull()) {
15+
return Document.Status.valueOf(jsonElement.getAsString().toUpperCase());
16+
} else {
17+
return null;
18+
}
19+
}
20+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.translated.lara.translator;
2+
3+
public class Document {
4+
5+
public enum Status {
6+
INITIALIZED,
7+
ANALYZING,
8+
PAUSED,
9+
READY,
10+
TRANSLATING,
11+
TRANSLATED,
12+
ERROR
13+
}
14+
15+
private final String id;
16+
private final Status status;
17+
private final int translatedChars;
18+
private final int totalChars;
19+
private final String filename;
20+
private final String source;
21+
private final String target;
22+
private final DocumentOptions options;
23+
private final String errorReason;
24+
private final String createdAt;
25+
private final String updatedAt;
26+
27+
public Document(String id, Status status, int translatedChars, int totalChars, String filename,
28+
String source, String target, DocumentOptions options, String error_reason, String createdAt, String updatedAt) {
29+
//
30+
this.id = id;
31+
this.status = status;
32+
this.translatedChars = translatedChars;
33+
this.totalChars = totalChars;
34+
this.filename = filename;
35+
this.source = source;
36+
this.target = target;
37+
this.options = options;
38+
this.errorReason = error_reason;
39+
this.createdAt = createdAt;
40+
this.updatedAt = updatedAt;
41+
}
42+
43+
public String getId() {
44+
return id;
45+
}
46+
47+
public Status getStatus() {
48+
return status;
49+
}
50+
51+
public int getTranslatedChars() {
52+
return translatedChars;
53+
}
54+
55+
public int getTotalChars() {
56+
return totalChars;
57+
}
58+
59+
public String getFilename() {
60+
return filename;
61+
}
62+
63+
public String getSource() {
64+
return source;
65+
}
66+
67+
public String getTarget() {
68+
return target;
69+
}
70+
71+
public DocumentOptions getOptions() {
72+
return options;
73+
}
74+
75+
public String getErrorReason() {
76+
return errorReason;
77+
}
78+
79+
public String getCreatedAt() {
80+
return createdAt;
81+
}
82+
83+
public String getUpdatedAt() {
84+
return updatedAt;
85+
}
86+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.translated.lara.translator;
2+
3+
import com.translated.lara.net.HttpParams;
4+
5+
public class DocumentDownloadOptions {
6+
7+
private String outputFormat = null;
8+
9+
public String getOutputFormat() {
10+
return outputFormat;
11+
}
12+
13+
public DocumentDownloadOptions setOutputFormat(String outputFormat) {
14+
this.outputFormat = outputFormat;
15+
return this;
16+
}
17+
18+
public HttpParams<Object> toParams() {
19+
HttpParams<Object> params = new HttpParams<>();
20+
params.set("output_format", outputFormat);
21+
22+
return params;
23+
}
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.translated.lara.translator;
2+
3+
import java.util.List;
4+
5+
public class DocumentOptions {
6+
private String[] adaptTo = null;
7+
8+
public String[] getAdaptTo() {
9+
return adaptTo;
10+
}
11+
12+
public DocumentOptions setAdaptTo(List<String> adaptTo) {
13+
this.adaptTo = adaptTo != null ? adaptTo.toArray(new String[0]) : null;
14+
return this;
15+
}
16+
17+
public DocumentOptions setAdaptTo(String... adaptTo) {
18+
this.adaptTo = adaptTo;
19+
return this;
20+
}
21+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.translated.lara.translator;
2+
3+
import com.translated.lara.net.HttpParams;
4+
5+
import java.util.List;
6+
7+
public class DocumentTranslateOptions {
8+
9+
private String[] adaptTo = null;
10+
private String outputFormat = null;
11+
12+
public String[] getAdaptTo() {
13+
return adaptTo;
14+
}
15+
16+
public DocumentTranslateOptions setAdaptTo(List<String> adaptTo) {
17+
this.adaptTo = adaptTo != null ? adaptTo.toArray(new String[0]) : null;
18+
return this;
19+
}
20+
21+
public DocumentTranslateOptions setAdaptTo(String... adaptTo) {
22+
this.adaptTo = adaptTo;
23+
return this;
24+
}
25+
26+
public String getOutputFormat() {
27+
return outputFormat;
28+
}
29+
30+
public DocumentTranslateOptions setOutputFormat(String outputFormat) {
31+
this.outputFormat = outputFormat;
32+
return this;
33+
}
34+
35+
public HttpParams<Object> toParams() {
36+
HttpParams<Object> params = new HttpParams<>();
37+
params.set("adapt_to", adaptTo);
38+
params.set("output_format", outputFormat);
39+
40+
return params;
41+
}
42+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.translated.lara.translator;
2+
3+
import com.translated.lara.net.HttpParams;
4+
5+
import java.util.List;
6+
7+
public class DocumentUploadOptions {
8+
9+
private String[] adaptTo = null;
10+
11+
public String[] getAdaptTo() {
12+
return adaptTo;
13+
}
14+
15+
public DocumentUploadOptions setAdaptTo(List<String> adaptTo) {
16+
this.adaptTo = adaptTo != null ? adaptTo.toArray(new String[0]) : null;
17+
return this;
18+
}
19+
20+
public DocumentUploadOptions setAdaptTo(String... adaptTo) {
21+
this.adaptTo = adaptTo;
22+
return this;
23+
}
24+
25+
public HttpParams<Object> toParams() {
26+
HttpParams<Object> params = new HttpParams<>();
27+
params.set("adapt_to", adaptTo);
28+
29+
return params;
30+
}
31+
}

0 commit comments

Comments
 (0)