use of org.pentaho.platform.plugin.services.importexport.BaseExportProcessor in project pentaho-platform by pentaho.
the class FileService method doGetFileOrDirAsDownload.
public DownloadFileWrapper doGetFileOrDirAsDownload(String userAgent, String pathId, String strWithManifest) throws Throwable {
// change file id to path
String path = idToPath(pathId);
validateAccess(path);
IAuthorizationPolicy policy = getPolicy();
String originalFileName, encodedFileName = null;
// if no path is sent, return bad request
if (StringUtils.isEmpty(pathId)) {
throw new InvalidParameterException(pathId);
}
// check if path is valid
if (!isPathValid(path)) {
throw new IllegalSelectorException();
}
// check if entity exists in repo
RepositoryFile repositoryFile = getRepository().getFile(path);
if (repositoryFile == null) {
// file does not exist or is not readable but we can't tell at this point
throw new FileNotFoundException(path);
}
// send zip with manifest by default
boolean withManifest = "false".equals(strWithManifest) ? false : true;
boolean requiresZip = repositoryFile.isFolder() || withManifest;
BaseExportProcessor exportProcessor = getDownloadExportProcessor(path, requiresZip, withManifest);
// $NON-NLS-1$//$NON-NLS-2$
originalFileName = requiresZip ? repositoryFile.getName() + ".zip" : repositoryFile.getName();
encodedFileName = makeEncodedFileName(originalFileName);
String quotedFileName = makeQuotedFileName(originalFileName);
// add export handlers for each expected file type
exportProcessor.addExportHandler(getDownloadExportHandler());
// copy streaming output
StreamingOutput streamingOutput = getDownloadStream(repositoryFile, exportProcessor);
final String attachment = makeAttachment(userAgent, encodedFileName, quotedFileName);
return new DownloadFileWrapper(streamingOutput, attachment, encodedFileName);
}
use of org.pentaho.platform.plugin.services.importexport.BaseExportProcessor in project pentaho-platform by pentaho.
the class FileServiceTest method assertDoGetFileOrDirAsDownload.
/**
* @param fileName
* @param withManifest
* @param expectedEncodedFileName
* @param expectedFileName
* @throws Throwable
*/
public void assertDoGetFileOrDirAsDownload(final String fileName, final String withManifest, final String expectedEncodedFileName, final String expectedFileName) throws Throwable {
IAuthorizationPolicy mockAuthPolicy = mock(IAuthorizationPolicy.class);
when(mockAuthPolicy.isAllowed(anyString())).thenReturn(true);
BaseExportProcessor mockExportProcessor = mock(BaseExportProcessor.class);
File mockExportFile = mock(File.class);
ExportHandler mockExportHandler = mock(ExportHandler.class);
StreamingOutput mockStream = mock(StreamingOutput.class);
RepositoryFile mockRepoFile = mock(RepositoryFile.class);
doReturn(fileName).when(mockRepoFile).getName();
doReturn(mockExportFile).when(mockExportProcessor).performExport(mockRepoFile);
doReturn(mockRepoFile).when(fileService.repository).getFile(anyString());
doReturn(mockAuthPolicy).when(fileService).getPolicy();
doReturn(mockExportProcessor).when(fileService).getDownloadExportProcessor(anyString(), anyBoolean(), anyBoolean());
doReturn(mockExportHandler).when(fileService).getDownloadExportHandler();
doReturn(mockStream).when(fileService).getDownloadStream(mockRepoFile, mockExportProcessor);
FileService.DownloadFileWrapper wrapper = fileService.doGetFileOrDirAsDownload("", "mock:path:" + fileName, withManifest);
verify(fileService.repository, times(1)).getFile(anyString());
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(mockAuthPolicy, times(3)).isAllowed(captor.capture());
assertTrue(captor.getAllValues().contains(RepositoryReadAction.NAME));
assertTrue(captor.getAllValues().contains(RepositoryCreateAction.NAME));
assertEquals(mockStream, wrapper.getOutputStream());
assertEquals(expectedEncodedFileName, wrapper.getEncodedFileName());
assertEquals("attachment; filename=\"" + expectedFileName + "\"", wrapper.getAttachment());
}
Aggregations