Search in sources :

Example 26 with SimpleRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.

the class FileService method doGetFileAsInline.

/**
 * Retrieves the file from the repository as inline. This is mainly used for css or and dependent files for the html
 * document
 *
 * @param pathId (colon separated path for the repository file)
 * @return RepositoryFileToStreamWrapper
 * @throws FileNotFoundException
 */
// have to accept anything for browsers to work
public RepositoryFileToStreamWrapper doGetFileAsInline(String pathId) throws FileNotFoundException {
    String path = null;
    RepositoryFile repositoryFile = null;
    // Check if the path is actually and ID
    path = idToPath(pathId);
    if (isPath(path)) {
        if (!isPathValid(path)) {
            throw new IllegalArgumentException();
        }
        repositoryFile = getRepository().getFile(path);
    } else {
        // Yes path provided is an ID
        repositoryFile = getRepository().getFileById(pathId);
    }
    if (repositoryFile == null) {
        // file does not exist or is not readable but we can't tell at this point
        throw new FileNotFoundException();
    }
    // check whitelist acceptance of file (based on extension)
    if (!getWhitelist().accept(repositoryFile.getName())) {
        // if whitelist check fails, we can still inline if you have PublishAction, otherwise we're FORBIDDEN
        if (!getPolicy().isAllowed(PublishAction.NAME)) {
            throw new IllegalArgumentException();
        }
    }
    try {
        SimpleRepositoryFileData fileData = getRepository().getDataForRead(repositoryFile.getId(), SimpleRepositoryFileData.class);
        final InputStream is = fileData.getInputStream();
        // copy streaming output
        StreamingOutput streamingOutput = getStreamingOutput(is);
        RepositoryFileToStreamWrapper wrapper = new RepositoryFileToStreamWrapper();
        wrapper.setOutputStream(streamingOutput);
        wrapper.setRepositoryFile(repositoryFile);
        return wrapper;
    } catch (Exception e) {
        logger.error(Messages.getInstance().getString("FileResource.EXPORT_FAILED", repositoryFile.getName() + " " + e.getMessage()), e);
        throw new InternalError();
    }
}
Also used : SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) StreamingOutput(javax.ws.rs.core.StreamingOutput) GeneralSecurityException(java.security.GeneralSecurityException) InvalidParameterException(java.security.InvalidParameterException) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) UnifiedRepositoryAccessDeniedException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryAccessDeniedException) FileNotFoundException(java.io.FileNotFoundException) PlatformImportException(org.pentaho.platform.plugin.services.importer.PlatformImportException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ExportException(org.pentaho.platform.plugin.services.importexport.ExportException) IllegalSelectorException(java.nio.channels.IllegalSelectorException) IOException(java.io.IOException)

Example 27 with SimpleRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.

the class FileServiceTest method testDoGetFileAsInline.

@Test
public void testDoGetFileAsInline() throws FileNotFoundException {
    /*
     * TEST 1
     */
    doReturn(true).when(fileService).isPath(anyString());
    doReturn(true).when(fileService).isPathValid(anyString());
    RepositoryDownloadWhitelist mockWhiteList = mock(RepositoryDownloadWhitelist.class);
    doReturn(mockWhiteList).when(fileService).getWhitelist();
    doReturn(true).when(mockWhiteList).accept(anyString());
    RepositoryFile mockRepoFile = mock(RepositoryFile.class);
    doReturn(mockRepoFile).when(fileService.repository).getFile(anyString());
    SimpleRepositoryFileData mockData = mock(SimpleRepositoryFileData.class);
    doReturn(mockData).when(fileService.repository).getDataForRead(any(Serializable.class), any(Class.class));
    InputStream mockInputStream = mock(InputStream.class);
    doReturn(mockInputStream).when(mockData).getInputStream();
    StreamingOutput mockStreamingOutput = mock(StreamingOutput.class);
    doReturn(mockStreamingOutput).when(fileService).getStreamingOutput(mockInputStream);
    FileService.RepositoryFileToStreamWrapper wrapper = fileService.doGetFileAsInline("test");
    verify(fileService.repository, times(1)).getFile(anyString());
    verify(mockWhiteList, times(1)).accept(anyString());
    verify(fileService, times(2)).getRepository();
    verify(fileService.repository, times(1)).getDataForRead(any(Serializable.class), any(Class.class));
    verify(mockData, times(1)).getInputStream();
    assertEquals(mockRepoFile, wrapper.getRepositoryFile());
    assertEquals(mockStreamingOutput, wrapper.getOutputStream());
    /*
     * TEST 2
     */
    doReturn(false).when(fileService).isPath(anyString());
    doReturn(mockRepoFile).when(fileService.repository).getFileById(anyString());
    wrapper = fileService.doGetFileAsInline("test");
    verify(fileService.repository, times(1)).getFileById(anyString());
    verify(fileService, times(4)).getRepository();
    assertEquals(mockRepoFile, wrapper.getRepositoryFile());
    assertEquals(mockStreamingOutput, wrapper.getOutputStream());
}
Also used : Serializable(java.io.Serializable) RepositoryDownloadWhitelist(org.pentaho.platform.repository.RepositoryDownloadWhitelist) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream) InputStream(java.io.InputStream) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) StreamingOutput(javax.ws.rs.core.StreamingOutput) Test(org.junit.Test)

Example 28 with SimpleRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.

the class PentahoMetadataDomainRepositoryTest method testLoadAnnotationsXml.

@Test
public void testLoadAnnotationsXml() throws Exception {
    String domainId = "test.xmi";
    String domainFileId = "00000000";
    String annotationsId = domainFileId + IModelAnnotationsAwareMetadataDomainRepositoryImporter.ANNOTATIONS_FILE_ID_POSTFIX;
    String annotationsXml = "<annotations/>";
    Log logger = mock(Log.class);
    doReturn(logger).when(domainRepositorySpy).getLogger();
    assertNull(domainRepositorySpy.loadAnnotationsXml(null));
    assertNull(domainRepositorySpy.loadAnnotationsXml(""));
    IUnifiedRepository repository = mock(IUnifiedRepository.class);
    doReturn(repository).when(domainRepositorySpy).getRepository();
    // Success
    RepositoryFile domainFile = mock(RepositoryFile.class);
    doReturn(domainFile).when(domainRepositorySpy).getMetadataRepositoryFile(domainId);
    doReturn(domainFileId).when(domainFile).getId();
    RepositoryFile annotationsFile = mock(RepositoryFile.class);
    doReturn(annotationsFile).when(repository).getFile(File.separator + "etc" + File.separator + "metadata/" + annotationsId);
    doReturn(annotationsId).when(annotationsFile).getId();
    SimpleRepositoryFileData data = mock(SimpleRepositoryFileData.class);
    doReturn(data).when(repository).getDataForRead(annotationsId, SimpleRepositoryFileData.class);
    doReturn(IOUtils.toInputStream(annotationsXml)).when(data).getInputStream();
    assertEquals(annotationsXml, domainRepositorySpy.loadAnnotationsXml(domainId));
    // Error
    doThrow(new RuntimeException()).when(data).getInputStream();
    domainRepositorySpy.loadAnnotationsXml(domainId);
    verify(logger, times(1)).warn("Unable to load annotations xml file for domain: test.xmi");
}
Also used : Log(org.apache.commons.logging.Log) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Matchers.anyString(org.mockito.Matchers.anyString) IUnifiedRepository(org.pentaho.platform.api.repository2.unified.IUnifiedRepository) Test(org.junit.Test)

Example 29 with SimpleRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.

the class ExporterTest method testExportFile.

public void testExportFile() throws Exception {
    Serializable mockSerializable = mock(Serializable.class);
    when(repositoryFile.getId()).thenReturn(mockSerializable);
    SimpleRepositoryFileData mockRepoFileData = mock(SimpleRepositoryFileData.class);
    when(unifiedRepository.getDataForRead(mockSerializable, SimpleRepositoryFileData.class)).thenReturn(mockRepoFileData);
    InputStream mockInputStream = new InputStream() {

        @Override
        public int read() throws IOException {
            // EOF
            return -1;
        }
    };
    when(mockRepoFileData.getStream()).thenReturn(mockInputStream);
    String name = "name.txt";
    when(repositoryFile.getName()).thenReturn(name);
    File mockFile = mock(File.class);
    when(mockFile.exists()).thenReturn(true);
    when(mockFile.isDirectory()).thenReturn(true);
    when(mockFile.getAbsolutePath()).thenReturn(System.getProperty("java.io.tmpdir"));
    exporter.exportFile(repositoryFile, mockFile);
    verify(repositoryFile, times(1)).getId();
    verify(unifiedRepository, times(1)).getDataForRead(mockSerializable, SimpleRepositoryFileData.class);
    verify(mockRepoFileData, times(1)).getStream();
    verify(repositoryFile, times(1)).getName();
    verify(mockFile, times(1)).exists();
    verify(mockFile, times(1)).isDirectory();
    verify(mockFile, times(1)).getAbsolutePath();
}
Also used : Serializable(java.io.Serializable) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) InputStream(java.io.InputStream) File(java.io.File) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile)

Example 30 with SimpleRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.

the class DefaultUnifiedRepositoryAuthorizationIT method testDeleteUsersFolder.

@Test
public void testDeleteUsersFolder() throws Exception {
    loginAsSysTenantAdmin();
    ITenant tenantAcme = tenantManager.createTenant(systemTenant, TENANT_ID_ACME, tenantAdminRoleName, tenantAuthenticatedRoleName, ANONYMOUS_ROLE_NAME);
    userRoleDao.createUser(tenantAcme, USERNAME_ADMIN, PASSWORD, "", new String[] { tenantAdminRoleName });
    login(USERNAME_ADMIN, tenantAcme, new String[] { tenantAdminRoleName, tenantAuthenticatedRoleName });
    userRoleDao.createUser(tenantAcme, USERNAME_SUZY, PASSWORD, "", null);
    login(USERNAME_SUZY, tenantAcme, new String[] { tenantAuthenticatedRoleName });
    final String parentFolderPath = ClientRepositoryPaths.getUserHomeFolderPath(PentahoSessionHolder.getSession().getName());
    RepositoryFile parentFolder = repo.getFile(parentFolderPath);
    final String dataString = "Hello World!";
    final String encoding = "UTF-8";
    byte[] data = dataString.getBytes(encoding);
    ByteArrayInputStream dataStream = new ByteArrayInputStream(data);
    final String mimeType = "text/plain";
    final String fileName = "helloworld.xaction";
    final SimpleRepositoryFileData content = new SimpleRepositoryFileData(dataStream, encoding, mimeType);
    RepositoryFile newFile = repo.createFile(parentFolder.getId(), new RepositoryFile.Builder(fileName).build(), content, null);
    final String filePath = parentFolderPath + RepositoryFile.SEPARATOR + fileName;
    login(USERNAME_ADMIN, tenantAcme, new String[] { tenantAdminRoleName, tenantAuthenticatedRoleName });
    try {
        repo.deleteFile(repo.getFile(parentFolderPath).getId(), null);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Matchers.anyString(org.mockito.Matchers.anyString) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) UnifiedRepositoryAccessDeniedException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryAccessDeniedException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) Test(org.junit.Test)

Aggregations

SimpleRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData)58 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)43 ByteArrayInputStream (java.io.ByteArrayInputStream)31 Test (org.junit.Test)25 Matchers.anyString (org.mockito.Matchers.anyString)18 ITenant (org.pentaho.platform.api.mt.ITenant)17 InputStream (java.io.InputStream)16 IOException (java.io.IOException)14 UnifiedRepositoryException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException)12 Serializable (java.io.Serializable)10 File (java.io.File)9 IUnifiedRepository (org.pentaho.platform.api.repository2.unified.IUnifiedRepository)9 FileNotFoundException (java.io.FileNotFoundException)5 DomainStorageException (org.pentaho.metadata.repository.DomainStorageException)5 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)5 VersionSummary (org.pentaho.platform.api.repository2.unified.VersionSummary)5 FileOutputStream (java.io.FileOutputStream)4 DomainAlreadyExistsException (org.pentaho.metadata.repository.DomainAlreadyExistsException)4 DomainIdNullException (org.pentaho.metadata.repository.DomainIdNullException)4 RepositoryRequest (org.pentaho.platform.api.repository2.unified.RepositoryRequest)4