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();
}
}
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());
}
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");
}
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();
}
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();
}
}
Aggregations