String iterationSlug, String locale, String fileType, String docId) {
GlobalDocumentId id =
new GlobalDocumentId(projectSlug, iterationSlug, docId);
// TODO scan (again) for virus
final Response response;
HDocument document =
this.documentDAO.getByProjectIterationAndDocId(projectSlug,
iterationSlug, docId);
if (document == null) {
response = Response.status(Status.NOT_FOUND).build();
} else if ("po".equals(fileType)
|| FILE_TYPE_OFFLINE_PO.equals(fileType)) {
// Note: could return 404 or Unsupported media type for "po" in
// non-po projects,
// and suggest to use offlinepo
final Set<String> extensions = new HashSet<String>();
extensions.add("gettext");
extensions.add("comment");
// Perform translation of Hibernate DTOs to JAXB DTOs
TranslationsResource transRes =
(TranslationsResource) this.translatedDocResourceService
.getTranslations(docId, new LocaleId(locale),
extensions, true, null).getEntity();
Resource res = this.resourceUtils.buildResource(document);
StreamingOutput output =
new POStreamingOutput(res, transRes,
FILE_TYPE_OFFLINE_PO.equals(fileType));
response =
Response.ok()
.header("Content-Disposition",
"attachment; filename=\""
+ document.getName() + ".po\"")
.type(MediaType.TEXT_PLAIN).entity(output).build();
} else if (FILETYPE_TRANSLATED_APPROVED.equals(fileType)
|| FILETYPE_TRANSLATED_APPROVED_AND_FUZZY.equals(fileType)) {
if (!filePersistService.hasPersistedDocument(id)) {
return Response.status(Status.NOT_FOUND).build();
}
final Set<String> extensions = Collections.<String> emptySet();
TranslationsResource transRes =
(TranslationsResource) this.translatedDocResourceService
.getTranslations(docId, new LocaleId(locale),
extensions, true, null).getEntity();
// Filter to only provide translated targets. "Preview" downloads
// include fuzzy.
// New list is used as transRes list appears not to be a modifiable
// implementation.
Map<String, TextFlowTarget> translations =
new HashMap<String, TextFlowTarget>();
boolean useFuzzy =
FILETYPE_TRANSLATED_APPROVED_AND_FUZZY.equals(fileType);
for (TextFlowTarget target : transRes.getTextFlowTargets()) {
// TODO rhbz953734 - translatedDocResourceService will map
// review content state to old state. For now this is
// acceptable. Once we have new REST options, we should review
// this
if (target.getState() == ContentState.Approved
|| (useFuzzy && target.getState() == ContentState.NeedReview)) {
translations.put(target.getResId(), target);
}
}
HDocument hDocument =
documentDAO.getByProjectIterationAndDocId(projectSlug,
iterationSlug, docId);
InputStream inputStream;
try {
inputStream =
filePersistService
.getRawDocumentContentAsStream(hDocument
.getRawDocument());
} catch (RawDocumentContentAccessException e) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e)
.build();
}
File tempFile =
translationFileServiceImpl.persistToTempFile(inputStream);
String name = projectSlug + ":" + iterationSlug + ":" + docId;
// TODO damason: this file is not transmitted, but used to generate
// a file later
// the generated file should be scanned instead
virusScanner.scan(tempFile, name);
URI uri = tempFile.toURI();
FileFormatAdapter adapter =
translationFileServiceImpl.getAdapterFor(hDocument
.getRawDocument().getType());
String rawParamString =
hDocument.getRawDocument().getAdapterParameters();
Optional<String> params =
Optional.<String> fromNullable(Strings
.emptyToNull(rawParamString));
StreamingOutput output =
new FormatAdapterStreamingOutput(uri, translations, locale,