Search in sources :

Example 1 with BaseExportProcessor

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);
}
Also used : InvalidParameterException(java.security.InvalidParameterException) IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) FileNotFoundException(java.io.FileNotFoundException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) StreamingOutput(javax.ws.rs.core.StreamingOutput) IllegalSelectorException(java.nio.channels.IllegalSelectorException) BaseExportProcessor(org.pentaho.platform.plugin.services.importexport.BaseExportProcessor)

Example 2 with BaseExportProcessor

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());
}
Also used : IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) ExportHandler(org.pentaho.platform.plugin.services.importexport.ExportHandler) StreamingOutput(javax.ws.rs.core.StreamingOutput) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Matchers.anyString(org.mockito.Matchers.anyString) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) File(java.io.File) BaseExportProcessor(org.pentaho.platform.plugin.services.importexport.BaseExportProcessor)

Aggregations

StreamingOutput (javax.ws.rs.core.StreamingOutput)2 IAuthorizationPolicy (org.pentaho.platform.api.engine.IAuthorizationPolicy)2 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)2 BaseExportProcessor (org.pentaho.platform.plugin.services.importexport.BaseExportProcessor)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IllegalSelectorException (java.nio.channels.IllegalSelectorException)1 InvalidParameterException (java.security.InvalidParameterException)1 Matchers.anyString (org.mockito.Matchers.anyString)1 ExportHandler (org.pentaho.platform.plugin.services.importexport.ExportHandler)1