@Test public void testTranslateFile() throws FileNotFoundException { Locale fromLocale = new Locale(LocaleCode.EN, "English"); Locale toLocale = new Locale(LocaleCode.DE, "German"); String url = "testing"; BackendID backendID = BackendID.DEV; DocumentContent translatedDocContent = new DocumentContent(new ArrayList<>(), url, toLocale.getLocaleCode().getId(), backendID.getId(), new ArrayList<>()); Response attrResponse = Mockito.mock(Response.class); when(attrResponse.getStatus()).thenReturn(Response.Status.OK.getStatusCode()); when(attrResponse.getEntity()).thenReturn("Translated by testing"); when(localeDAO.getByLocaleCode(fromLocale.getLocaleCode())) .thenReturn(fromLocale); when(localeDAO.getByLocaleCode(toLocale.getLocaleCode())) .thenReturn(fromLocale); when(documentContentTranslatorService .translateDocument(any(), any(), any())) .thenReturn(translatedDocContent); when(backendResource.getStringAttribution(backendID.getId())).thenReturn(attrResponse); documentResource.uriInfo = new ResteasyUriInfo("http: File sourceFile = new File("src/test/resources/test.pot"); InputStream inputStream = new FileInputStream(sourceFile); String filename = "test.pot"; TranslateDocumentForm form = new TranslateDocumentForm(); form.setFileName(filename); form.setFileStream(inputStream); form.setType("text/plain"); Response response = documentResource.translateFile(form, fromLocale.getLocaleCode(), toLocale.getLocaleCode()); String expectedFilename = "test_" + toLocale.getLocaleCode().getId() + ".po"; assertThat(response.getStatus()).isEqualTo(Response.Status.OK.getStatusCode()); assertThat(response.getHeaders()).containsKey("content-disposition"); assertThat(response.getHeaderString("content-disposition")).contains(expectedFilename); DocumentResourceImpl.FilterStreamingOutput output = (DocumentResourceImpl.FilterStreamingOutput) response.getEntity(); assertThat(output).isNotNull(); assertThat(output.fromLocaleCode).isEqualTo(fromLocale.getLocaleCode()); assertThat(output.toLocaleCode).isEqualTo(toLocale.getLocaleCode()); assertThat(output.messages).isNotEmpty(); assertThat(output.translatedDocContent.getUrl()).isEqualTo(url); assertThat(output.translatedDocContent.getLocaleCode()) .isEqualTo(toLocale.getLocaleCode().getId()); }
@Override public Response translateFile(TranslateDocumentForm form, LocaleCode fromLocaleCode, LocaleCode toLocaleCode) { if (fromLocaleCode == null || toLocaleCode == null) { APIResponse apiResponse = new APIResponse(Response.Status.BAD_REQUEST, "Null query param: fromLocaleCode or toLocaleCode"); return Response.status(apiResponse.getStatus()) .entity(apiResponse).build(); } if (StringUtils.isBlank(form.getFileName())) { APIResponse apiResponse = new APIResponse(Response.Status.BAD_REQUEST, "Null form param: fileName"); return Response.status(apiResponse.getStatus()) .entity(apiResponse).build(); } try { String fileExt = FilenameUtils.getExtension(form.getFileName()); TranslationFileAdapter adapter = FILTERS.get(fileExt.toUpperCase()); String url = getURL(uriInfo.getRequestUri().toString(), form.getFileName()); Pair<DocumentContent, Map<String, Message>> contents = adapter .parseSourceDocument(form.getFileStream(), url, fromLocaleCode); Response response = this.translate(contents.getLeft(), toLocaleCode); if (response.getStatus() != Response.Status.OK.getStatusCode()) { return response; } DocumentContent newDocContent = (DocumentContent) response.getEntity(); Response attr = backendResourceImpl .getStringAttribution(newDocContent.getBackendId()); String attribution = attr != null && attr.getStatus() == Response.Status.OK.getStatusCode() ? ATTRIBUTION_KEY + ":" + attr.getEntity().toString() : ""; FilterStreamingOutput stream = new FilterStreamingOutput(adapter, newDocContent, contents.getRight(), fromLocaleCode, toLocaleCode, attribution); String docName = getTransFilename(adapter.getTranslationFileExtension(), form.getFileName(), toLocaleCode.getId()); return Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM) .header("content-disposition", "attachment; filename=\"" + docName + "\"") .header("Access-Control-Expose-Headers", "content-disposition") .build(); } catch (Exception e) { APIResponse apiResponse = new APIResponse(Response.Status.BAD_REQUEST, e, "Unable to parse document"); LOG.error(apiResponse.getDetails(), e); return Response.status(apiResponse.getStatus()) .entity(apiResponse).build(); } }
DocumentResourceImpl implements DocumentResource { @Override public Response translateFile(TranslateDocumentForm form, LocaleCode fromLocaleCode, LocaleCode toLocaleCode) { if (fromLocaleCode == null || toLocaleCode == null) { APIResponse apiResponse = new APIResponse(Response.Status.BAD_REQUEST, "Null query param: fromLocaleCode or toLocaleCode"); return Response.status(apiResponse.getStatus()) .entity(apiResponse).build(); } if (StringUtils.isBlank(form.getFileName())) { APIResponse apiResponse = new APIResponse(Response.Status.BAD_REQUEST, "Null form param: fileName"); return Response.status(apiResponse.getStatus()) .entity(apiResponse).build(); } try { String fileExt = FilenameUtils.getExtension(form.getFileName()); TranslationFileAdapter adapter = FILTERS.get(fileExt.toUpperCase()); String url = getURL(uriInfo.getRequestUri().toString(), form.getFileName()); Pair<DocumentContent, Map<String, Message>> contents = adapter .parseSourceDocument(form.getFileStream(), url, fromLocaleCode); Response response = this.translate(contents.getLeft(), toLocaleCode); if (response.getStatus() != Response.Status.OK.getStatusCode()) { return response; } DocumentContent newDocContent = (DocumentContent) response.getEntity(); Response attr = backendResourceImpl .getStringAttribution(newDocContent.getBackendId()); String attribution = attr != null && attr.getStatus() == Response.Status.OK.getStatusCode() ? ATTRIBUTION_KEY + ":" + attr.getEntity().toString() : ""; FilterStreamingOutput stream = new FilterStreamingOutput(adapter, newDocContent, contents.getRight(), fromLocaleCode, toLocaleCode, attribution); String docName = getTransFilename(adapter.getTranslationFileExtension(), form.getFileName(), toLocaleCode.getId()); return Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM) .header("content-disposition", "attachment; filename=\"" + docName + "\"") .header("Access-Control-Expose-Headers", "content-disposition") .build(); } catch (Exception e) { APIResponse apiResponse = new APIResponse(Response.Status.BAD_REQUEST, e, "Unable to parse document"); LOG.error(apiResponse.getDetails(), e); return Response.status(apiResponse.getStatus()) .entity(apiResponse).build(); } } }
DocumentResourceImpl implements DocumentResource { @Override public Response translateFile(TranslateDocumentForm form, LocaleCode fromLocaleCode, LocaleCode toLocaleCode) { if (fromLocaleCode == null || toLocaleCode == null) { APIResponse apiResponse = new APIResponse(Response.Status.BAD_REQUEST, "Null query param: fromLocaleCode or toLocaleCode"); return Response.status(apiResponse.getStatus()) .entity(apiResponse).build(); } if (StringUtils.isBlank(form.getFileName())) { APIResponse apiResponse = new APIResponse(Response.Status.BAD_REQUEST, "Null form param: fileName"); return Response.status(apiResponse.getStatus()) .entity(apiResponse).build(); } try { String fileExt = FilenameUtils.getExtension(form.getFileName()); TranslationFileAdapter adapter = FILTERS.get(fileExt.toUpperCase()); String url = getURL(uriInfo.getRequestUri().toString(), form.getFileName()); Pair<DocumentContent, Map<String, Message>> contents = adapter .parseSourceDocument(form.getFileStream(), url, fromLocaleCode); Response response = this.translate(contents.getLeft(), toLocaleCode); if (response.getStatus() != Response.Status.OK.getStatusCode()) { return response; } DocumentContent newDocContent = (DocumentContent) response.getEntity(); Response attr = backendResourceImpl .getStringAttribution(newDocContent.getBackendId()); String attribution = attr != null && attr.getStatus() == Response.Status.OK.getStatusCode() ? ATTRIBUTION_KEY + ":" + attr.getEntity().toString() : ""; FilterStreamingOutput stream = new FilterStreamingOutput(adapter, newDocContent, contents.getRight(), fromLocaleCode, toLocaleCode, attribution); String docName = getTransFilename(adapter.getTranslationFileExtension(), form.getFileName(), toLocaleCode.getId()); return Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM) .header("content-disposition", "attachment; filename=\"" + docName + "\"") .header("Access-Control-Expose-Headers", "content-disposition") .build(); } catch (Exception e) { APIResponse apiResponse = new APIResponse(Response.Status.BAD_REQUEST, e, "Unable to parse document"); LOG.error(apiResponse.getDetails(), e); return Response.status(apiResponse.getStatus()) .entity(apiResponse).build(); } } @SuppressWarnings("unused")  DocumentResourceImpl(); @Inject  DocumentResourceImpl(
        DocumentContentTranslatorService documentContentTranslatorService,
        LocaleDAO localeDAO,
        DocumentService documentService,
        BackendResource backendResourceImpl,
        @DevMode boolean isDevMode,
        @DefaultProvider BackendID defaultProvider,
        @BackEndProviders Set<BackendID> availableProviders); }
DocumentResourceImpl implements DocumentResource { @Override public Response translateFile(TranslateDocumentForm form, LocaleCode fromLocaleCode, LocaleCode toLocaleCode) { if (fromLocaleCode == null || toLocaleCode == null) { APIResponse apiResponse = new APIResponse(Response.Status.BAD_REQUEST, "Null query param: fromLocaleCode or toLocaleCode"); return Response.status(apiResponse.getStatus()) .entity(apiResponse).build(); } if (StringUtils.isBlank(form.getFileName())) { APIResponse apiResponse = new APIResponse(Response.Status.BAD_REQUEST, "Null form param: fileName"); return Response.status(apiResponse.getStatus()) .entity(apiResponse).build(); } try { String fileExt = FilenameUtils.getExtension(form.getFileName()); TranslationFileAdapter adapter = FILTERS.get(fileExt.toUpperCase()); String url = getURL(uriInfo.getRequestUri().toString(), form.getFileName()); Pair<DocumentContent, Map<String, Message>> contents = adapter .parseSourceDocument(form.getFileStream(), url, fromLocaleCode); Response response = this.translate(contents.getLeft(), toLocaleCode); if (response.getStatus() != Response.Status.OK.getStatusCode()) { return response; } DocumentContent newDocContent = (DocumentContent) response.getEntity(); Response attr = backendResourceImpl .getStringAttribution(newDocContent.getBackendId()); String attribution = attr != null && attr.getStatus() == Response.Status.OK.getStatusCode() ? ATTRIBUTION_KEY + ":" + attr.getEntity().toString() : ""; FilterStreamingOutput stream = new FilterStreamingOutput(adapter, newDocContent, contents.getRight(), fromLocaleCode, toLocaleCode, attribution); String docName = getTransFilename(adapter.getTranslationFileExtension(), form.getFileName(), toLocaleCode.getId()); return Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM) .header("content-disposition", "attachment; filename=\"" + docName + "\"") .header("Access-Control-Expose-Headers", "content-disposition") .build(); } catch (Exception e) { APIResponse apiResponse = new APIResponse(Response.Status.BAD_REQUEST, e, "Unable to parse document"); LOG.error(apiResponse.getDetails(), e); return Response.status(apiResponse.getStatus()) .entity(apiResponse).build(); } } @SuppressWarnings("unused")  DocumentResourceImpl(); @Inject  DocumentResourceImpl(
        DocumentContentTranslatorService documentContentTranslatorService,
        LocaleDAO localeDAO,
        DocumentService documentService,
        BackendResource backendResourceImpl,
        @DevMode boolean isDevMode,
        @DefaultProvider BackendID defaultProvider,
        @BackEndProviders Set<BackendID> availableProviders); @Override Response getStatistics(@QueryParam("url") String url,
            @QueryParam("fromLocaleCode") LocaleCode fromLocaleCode,
            @QueryParam("toLocaleCode") LocaleCode toLocaleCode,
            @QueryParam("dateRange") String dateRangeParam); @Override Response translate(DocumentContent docContent,
        @QueryParam("toLocaleCode") LocaleCode toLocaleCode); @Override Response translateFile(TranslateDocumentForm form,
        LocaleCode fromLocaleCode, LocaleCode toLocaleCode); }
DocumentResourceImpl implements DocumentResource { @Override public Response translateFile(TranslateDocumentForm form, LocaleCode fromLocaleCode, LocaleCode toLocaleCode) { if (fromLocaleCode == null || toLocaleCode == null) { APIResponse apiResponse = new APIResponse(Response.Status.BAD_REQUEST, "Null query param: fromLocaleCode or toLocaleCode"); return Response.status(apiResponse.getStatus()) .entity(apiResponse).build(); } if (StringUtils.isBlank(form.getFileName())) { APIResponse apiResponse = new APIResponse(Response.Status.BAD_REQUEST, "Null form param: fileName"); return Response.status(apiResponse.getStatus()) .entity(apiResponse).build(); } try { String fileExt = FilenameUtils.getExtension(form.getFileName()); TranslationFileAdapter adapter = FILTERS.get(fileExt.toUpperCase()); String url = getURL(uriInfo.getRequestUri().toString(), form.getFileName()); Pair<DocumentContent, Map<String, Message>> contents = adapter .parseSourceDocument(form.getFileStream(), url, fromLocaleCode); Response response = this.translate(contents.getLeft(), toLocaleCode); if (response.getStatus() != Response.Status.OK.getStatusCode()) { return response; } DocumentContent newDocContent = (DocumentContent) response.getEntity(); Response attr = backendResourceImpl .getStringAttribution(newDocContent.getBackendId()); String attribution = attr != null && attr.getStatus() == Response.Status.OK.getStatusCode() ? ATTRIBUTION_KEY + ":" + attr.getEntity().toString() : ""; FilterStreamingOutput stream = new FilterStreamingOutput(adapter, newDocContent, contents.getRight(), fromLocaleCode, toLocaleCode, attribution); String docName = getTransFilename(adapter.getTranslationFileExtension(), form.getFileName(), toLocaleCode.getId()); return Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM) .header("content-disposition", "attachment; filename=\"" + docName + "\"") .header("Access-Control-Expose-Headers", "content-disposition") .build(); } catch (Exception e) { APIResponse apiResponse = new APIResponse(Response.Status.BAD_REQUEST, e, "Unable to parse document"); LOG.error(apiResponse.getDetails(), e); return Response.status(apiResponse.getStatus()) .entity(apiResponse).build(); } } @SuppressWarnings("unused")  DocumentResourceImpl(); @Inject  DocumentResourceImpl(
        DocumentContentTranslatorService documentContentTranslatorService,
        LocaleDAO localeDAO,
        DocumentService documentService,
        BackendResource backendResourceImpl,
        @DevMode boolean isDevMode,
        @DefaultProvider BackendID defaultProvider,
        @BackEndProviders Set<BackendID> availableProviders); @Override Response getStatistics(@QueryParam("url") String url,
            @QueryParam("fromLocaleCode") LocaleCode fromLocaleCode,
            @QueryParam("toLocaleCode") LocaleCode toLocaleCode,
            @QueryParam("dateRange") String dateRangeParam); @Override Response translate(DocumentContent docContent,
        @QueryParam("toLocaleCode") LocaleCode toLocaleCode); @Override Response translateFile(TranslateDocumentForm form,
        LocaleCode fromLocaleCode, LocaleCode toLocaleCode);  }