This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
SourceDocumentUpload.java
348 lines (311 loc) · 13.8 KB
/
SourceDocumentUpload.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* Copyright 2013, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.file;
import static org.zanata.file.DocumentUploadUtil.getInputStream;
import static org.zanata.file.DocumentUploadUtil.isSinglePart;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Collections;
import javax.annotation.Nonnull;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import lombok.extern.slf4j.Slf4j;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.zanata.common.DocumentType;
import org.zanata.common.EntityStatus;
import org.zanata.common.LocaleId;
import org.zanata.common.ProjectType;
import org.zanata.dao.DocumentDAO;
import org.zanata.dao.DocumentUploadDAO;
import org.zanata.dao.ProjectIterationDAO;
import org.zanata.exception.ChunkUploadException;
import org.zanata.exception.VirusDetectedException;
import org.zanata.exception.ZanataServiceException;
import org.zanata.model.HDocument;
import org.zanata.model.HDocumentUpload;
import org.zanata.model.HLocale;
import org.zanata.model.HProjectIteration;
import org.zanata.model.HRawDocument;
import org.zanata.rest.DocumentFileUploadForm;
import org.zanata.rest.StringSet;
import org.zanata.rest.dto.ChunkUploadResponse;
import org.zanata.rest.dto.extensions.ExtensionType;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.service.VirusScanner;
import org.zanata.security.ZanataIdentity;
import org.zanata.service.DocumentService;
import org.zanata.service.TranslationFileService;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
//TODO damason: add thorough unit testing
@Slf4j
@Name("sourceDocumentUploader")
public class SourceDocumentUpload {
private static final HLocale NULL_LOCALE = null;
@In(create = true, value = "documentUploadUtil")
private DocumentUploadUtil util;
@In("filePersistService")
private FilePersistService filePersistService;
@In
private ZanataIdentity identity;
@In
private ProjectIterationDAO projectIterationDAO;
@In
private TranslationFileService translationFileServiceImpl;
@In
private VirusScanner virusScanner;
@In
private DocumentDAO documentDAO;
@In
private DocumentUploadDAO documentUploadDAO;
@In
private DocumentService documentServiceImpl;
public Response tryUploadSourceFileWithoutHash(GlobalDocumentId id,
DocumentFileUploadForm uploadForm) {
try {
failIfSourceUploadNotValid(id, uploadForm);
} catch (ChunkUploadException e) {
return Response.status(e.getStatusCode())
.entity(new ChunkUploadResponse(e.getMessage())).build();
}
return tryValidatedUploadSourceFile(id, uploadForm);
}
public Response tryUploadSourceFile(GlobalDocumentId id,
DocumentFileUploadForm uploadForm) {
try {
failIfSourceUploadNotValid(id, uploadForm);
util.failIfHashNotPresent(uploadForm);
} catch (ChunkUploadException e) {
return Response.status(e.getStatusCode())
.entity(new ChunkUploadResponse(e.getMessage())).build();
}
return tryValidatedUploadSourceFile(id, uploadForm);
}
public Response tryValidatedUploadSourceFile(GlobalDocumentId id,
DocumentFileUploadForm uploadForm) {
try {
Optional<File> tempFile;
int totalChunks;
if (!uploadForm.getLast()) {
HDocumentUpload upload =
util.saveUploadPart(id, NULL_LOCALE, uploadForm);
totalChunks = upload.getParts().size();
return Response
.status(Status.ACCEPTED)
.entity(new ChunkUploadResponse(upload.getId(),
totalChunks, true,
"Chunk accepted, awaiting remaining chunks."))
.build();
}
if (isSinglePart(uploadForm)) {
totalChunks = 1;
tempFile = Optional.<File> absent();
} else {
HDocumentUpload previousParts =
documentUploadDAO.findById(uploadForm.getUploadId());
totalChunks = previousParts.getParts().size();
totalChunks++; // add final part
tempFile =
Optional.of(util
.combineToTempFileAndDeleteUploadRecord(
previousParts,
uploadForm));
}
HProjectIteration version =
projectIterationDAO.getBySlug(id.getProjectSlug(), id.getVersionSlug());
if(version.getProjectType() == ProjectType.File) {
if (!tempFile.isPresent()) {
tempFile = Optional.of(util
.persistTempFileFromUpload(uploadForm));
}
processAdapterFile(tempFile.get(), id, uploadForm);
} else if (DocumentType.getByName(uploadForm.getFileType()) == DocumentType.GETTEXT_PORTABLE_OBJECT) {
InputStream potStream = getInputStream(tempFile, uploadForm);
parsePotFile(potStream, id, uploadForm);
} else {
throw new ZanataServiceException("Unsupported source file: "
+ tempFile.get().getName());
}
if (tempFile.isPresent()) {
tempFile.get().delete();
}
return sourceUploadSuccessResponse(util.isNewDocument(id),
totalChunks);
} catch (ChunkUploadException e) {
return Response.status(e.getStatusCode())
.entity(new ChunkUploadResponse(e.getMessage())).build();
} catch (FileNotFoundException e) {
log.error("failed to create input stream from temp file", e);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e)
.build();
}
}
private void failIfSourceUploadNotValid(GlobalDocumentId id,
DocumentFileUploadForm uploadForm) throws ChunkUploadException {
util.failIfUploadNotValid(id, uploadForm);
failIfSourceUploadNotAllowed(id);
failIfFileTypeNotValid(uploadForm);
}
private void failIfSourceUploadNotAllowed(GlobalDocumentId id)
throws ChunkUploadException {
if (!isDocumentUploadAllowed(id)) {
throw new ChunkUploadException(Status.FORBIDDEN,
"You do not have permission to upload source documents to project-version \""
+ id.getProjectSlug() + ":" + id.getVersionSlug()
+ "\".");
}
}
private boolean isDocumentUploadAllowed(GlobalDocumentId id) {
HProjectIteration projectIteration =
projectIterationDAO.getBySlug(id.getProjectSlug(),
id.getVersionSlug());
return projectIteration.getStatus() == EntityStatus.ACTIVE
&& projectIteration.getProject().getStatus() == EntityStatus.ACTIVE
&& identity != null
&& identity.hasPermission("import-template", projectIteration);
}
private void failIfFileTypeNotValid(DocumentFileUploadForm uploadForm)
throws ChunkUploadException {
DocumentType type = DocumentType.getByName(uploadForm.getFileType());
if (!isSourceDocumentType(type)) {
throw new ChunkUploadException(Status.BAD_REQUEST, "The type \""
+ uploadForm.getFileType()
+ "\" specified in form parameter 'type' "
+ "is not valid for a source file on this server.");
}
}
private boolean isSourceDocumentType(DocumentType type) {
return isPotType(type) || isAdapterType(type);
}
private boolean isPotType(DocumentType type) {
return type == DocumentType.GETTEXT_PORTABLE_OBJECT;
}
private boolean isAdapterType(DocumentType type) {
return translationFileServiceImpl.hasAdapterFor(type);
}
private static Response sourceUploadSuccessResponse(boolean isNewDocument,
int acceptedChunks) {
Response response;
ChunkUploadResponse uploadResponse = new ChunkUploadResponse();
uploadResponse.setAcceptedChunks(acceptedChunks);
uploadResponse.setExpectingMore(false);
if (isNewDocument) {
uploadResponse
.setSuccessMessage("Upload of new source document successful.");
response =
Response.status(Status.CREATED).entity(uploadResponse)
.build();
} else {
uploadResponse
.setSuccessMessage("Upload of new version of source document successful.");
response =
Response.status(Status.OK).entity(uploadResponse).build();
}
return response;
}
private void processAdapterFile(@Nonnull File tempFile,
GlobalDocumentId id, DocumentFileUploadForm uploadForm) {
String name =
id.getProjectSlug() + ":" + id.getVersionSlug() + ":"
+ id.getDocId();
try {
virusScanner.scan(tempFile, name);
} catch (VirusDetectedException e) {
log.warn("File failed virus scan: {}", e.getMessage());
throw new ChunkUploadException(Status.BAD_REQUEST,
"Uploaded file did not pass virus scan");
}
HDocument document;
Optional<String> params;
params =
Optional.fromNullable(Strings.emptyToNull(uploadForm
.getAdapterParams()));
if (!params.isPresent()) {
params =
documentDAO.getAdapterParams(id.getProjectSlug(),
id.getVersionSlug(), id.getDocId());
}
try {
Optional<String> docType =
Optional.fromNullable(uploadForm.getFileType());
Resource doc =
translationFileServiceImpl.parseUpdatedAdapterDocumentFile(
tempFile.toURI(), id.getDocId(),
uploadForm.getFileType(), params, docType);
doc.setLang(LocaleId.EN_US);
// TODO Copy Trans values
document =
documentServiceImpl.saveDocument(id.getProjectSlug(),
id.getVersionSlug(), doc,
Collections.<String> emptySet(), false);
} catch (SecurityException e) {
throw new ChunkUploadException(Status.INTERNAL_SERVER_ERROR,
e.getMessage(), e);
} catch (ZanataServiceException e) {
throw new ChunkUploadException(Status.INTERNAL_SERVER_ERROR,
e.getMessage(), e);
}
String contentHash = uploadForm.getHash();
DocumentType documentType =
DocumentType.getByName(uploadForm.getFileType());
persistRawDocument(document, tempFile, contentHash, documentType,
params);
translationFileServiceImpl.removeTempFile(tempFile);
}
private void persistRawDocument(HDocument document, File rawFile,
String contentHash, DocumentType documentType,
Optional<String> params) {
HRawDocument rawDocument = new HRawDocument();
rawDocument.setDocument(document);
rawDocument.setContentHash(contentHash);
rawDocument.setType(documentType);
rawDocument.setUploadedBy(identity.getCredentials().getUsername());
filePersistService.persistRawDocumentContentFromFile(rawDocument,
rawFile);
if (params.isPresent()) {
rawDocument.setAdapterParameters(params.get());
}
documentDAO.addRawDocument(document, rawDocument);
documentDAO.flush();
}
private void parsePotFile(InputStream potStream, GlobalDocumentId id,
DocumentFileUploadForm uploadForm) {
// real upload filename not available, but the service only cares about
// the suffix.
String uploadFileName = "." + uploadForm.getFileType();
Resource doc = translationFileServiceImpl.parseUpdatedPotFile(potStream,
id.getDocId(), uploadFileName,
useOfflinePo(id));
doc.setLang(LocaleId.EN_US);
// TODO Copy Trans values
documentServiceImpl.saveDocument(id.getProjectSlug(),
id.getVersionSlug(), doc,
new StringSet(ExtensionType.GetText.toString()), false);
}
private boolean useOfflinePo(GlobalDocumentId id) {
return !util.isNewDocument(id)
&& !translationFileServiceImpl
.isPoDocument(id.getProjectSlug(), id.getVersionSlug(),
id.getDocId());
}
}