use of org.pentaho.platform.api.repository2.unified.IRepositoryFileData in project pentaho-platform by pentaho.
the class JcrRepositoryFileDao method getData.
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public <T extends IRepositoryFileData> T getData(final Serializable fileId, final Serializable versionId, final Class<T> contentClass) {
Assert.notNull(fileId);
return (T) jcrTemplate.execute(new JcrCallback() {
@Override
public Object doInJcr(final Session session) throws RepositoryException, IOException {
PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(session);
IRepositoryFileData data = JcrRepositoryFileUtils.getContent(session, pentahoJcrConstants, fileId, versionId, findTransformerForRead(JcrRepositoryFileUtils.getFileContentType(session, pentahoJcrConstants, fileId, versionId), contentClass));
if (fileId != null) {
RepositoryFile file = internalGetFileById(fileId, false, null);
if (file != null) {
RepositoryFileAcl acl = aclDao.getAcl(fileId);
// Invoke accessVoterManager to see if we have access to perform this operation
if (!accessVoterManager.hasAccess(file, RepositoryFilePermission.READ, acl, PentahoSessionHolder.getSession())) {
return null;
}
}
}
return data;
}
});
}
use of org.pentaho.platform.api.repository2.unified.IRepositoryFileData in project pentaho-platform by pentaho.
the class JcrRepositoryFileDao method internalCreateFile.
private RepositoryFile internalCreateFile(final Serializable parentFolderId, final RepositoryFile file, final IRepositoryFileData content, final RepositoryFileAcl acl, final String versionMessage) {
if (isKioskEnabled()) {
// $NON-NLS-1$
throw new RuntimeException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED"));
}
/*
* PPP-3049: Changed the Assert.notNull(content) to code that creates a file with a single blank when the assert
* WOULD have been triggered.
*/
Assert.notNull(file);
Assert.isTrue(!file.isFolder());
// Get repository file info and acl info of parent
if (parentFolderId != null) {
RepositoryFile parentRepositoryFile = getFileById(parentFolderId);
if (parentRepositoryFile != null) {
RepositoryFileAcl parentAcl = aclDao.getAcl(parentRepositoryFile.getId());
// Invoke accessVoterManager to see if we have access to perform this operation
if (!accessVoterManager.hasAccess(parentRepositoryFile, RepositoryFilePermission.WRITE, parentAcl, PentahoSessionHolder.getSession())) {
return null;
}
}
}
// Assert.notNull(content);
DataNode emptyDataNode = new DataNode(file.getName());
// $NON-NLS-1$ //$NON-NLS-2$
emptyDataNode.setProperty(" ", "content");
final IRepositoryFileData emptyContent = new NodeRepositoryFileData(emptyDataNode);
return (RepositoryFile) jcrTemplate.execute(new JcrCallback() {
@Override
public Object doInJcr(final Session session) throws RepositoryException, IOException {
PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(session);
JcrRepositoryFileUtils.checkoutNearestVersionableFileIfNecessary(session, pentahoJcrConstants, parentFolderId);
Node fileNode = JcrRepositoryFileUtils.createFileNode(session, pentahoJcrConstants, parentFolderId, file, content == null ? emptyContent : content, findTransformerForWrite(content == null ? emptyContent.getClass() : content.getClass()));
// create a tmp file with correct path for default acl creation purposes.
String path = JcrRepositoryFileUtils.getAbsolutePath(session, pentahoJcrConstants, fileNode);
RepositoryFile tmpFile = new RepositoryFile.Builder(file).path(path).build();
// we must create the acl during checkout
aclDao.createAcl(fileNode.getIdentifier(), acl == null ? defaultAclHandler.createDefaultAcl(tmpFile) : acl);
session.save();
if (file.isVersioned()) {
JcrRepositoryFileUtils.checkinNearestVersionableNodeIfNecessary(session, pentahoJcrConstants, fileNode, versionMessage, file.getCreatedDate(), false);
}
JcrRepositoryFileUtils.checkinNearestVersionableFileIfNecessary(session, pentahoJcrConstants, parentFolderId, Messages.getInstance().getString("JcrRepositoryFileDao.USER_0002_VER_COMMENT_ADD_FILE", file.getName(), // $NON-NLS-1$ //$NON-NLS-2$
(parentFolderId == null ? "root" : parentFolderId.toString())));
return JcrRepositoryFileUtils.nodeToFile(session, pentahoJcrConstants, pathConversionHelper, lockHelper, fileNode);
}
});
}
use of org.pentaho.platform.api.repository2.unified.IRepositoryFileData in project pentaho-platform by pentaho.
the class JcrRepositoryFileDao method internalUpdateFile.
private RepositoryFile internalUpdateFile(final Session session, final PentahoJcrConstants pentahoJcrConstants, final RepositoryFile file, final IRepositoryFileData content, final String versionMessage) throws RepositoryException {
if (isKioskEnabled()) {
// $NON-NLS-1$
throw new RuntimeException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED"));
}
Assert.notNull(file);
Assert.isTrue(!file.isFolder());
Assert.notNull(content);
// Get repository file info and acl info of parent
RepositoryFileAcl acl = aclDao.getAcl(file.getId());
// Invoke accessVoterManager to see if we have access to perform this operation
if (!accessVoterManager.hasAccess(file, RepositoryFilePermission.WRITE, acl, PentahoSessionHolder.getSession())) {
return null;
}
lockHelper.addLockTokenToSessionIfNecessary(session, pentahoJcrConstants, file.getId());
JcrRepositoryFileUtils.checkoutNearestVersionableFileIfNecessary(session, pentahoJcrConstants, file.getId());
JcrRepositoryFileUtils.updateFileNode(session, pentahoJcrConstants, file, content, findTransformerForWrite(content.getClass()));
session.save();
JcrRepositoryFileUtils.checkinNearestVersionableFileIfNecessary(session, pentahoJcrConstants, file.getId(), versionMessage, file.getCreatedDate() != null ? file.getCreatedDate() : new java.util.Date(), true);
lockHelper.removeLockTokenFromSessionIfNecessary(session, pentahoJcrConstants, file.getId());
return JcrRepositoryFileUtils.nodeIdToFile(session, pentahoJcrConstants, pathConversionHelper, lockHelper, file.getId());
}
use of org.pentaho.platform.api.repository2.unified.IRepositoryFileData in project pentaho-platform by pentaho.
the class JcrRepositoryFileUtils method getContent.
public static IRepositoryFileData getContent(final Session session, final PentahoJcrConstants pentahoJcrConstants, final Serializable fileId, final Serializable versionId, final ITransformer<IRepositoryFileData> transformer) throws RepositoryException {
Node fileNode = session.getNodeByIdentifier(fileId.toString());
if (isVersioned(session, pentahoJcrConstants, fileNode)) {
VersionManager vMgr = session.getWorkspace().getVersionManager();
Version version = null;
if (versionId != null) {
version = vMgr.getVersionHistory(fileNode.getPath()).getVersion(versionId.toString());
} else {
version = vMgr.getBaseVersion(fileNode.getPath());
}
fileNode = getNodeAtVersion(pentahoJcrConstants, version);
}
Assert.isTrue(!isPentahoFolder(pentahoJcrConstants, fileNode));
return transformer.fromContentNode(session, pentahoJcrConstants, fileNode);
}
use of org.pentaho.platform.api.repository2.unified.IRepositoryFileData in project pentaho-platform by pentaho.
the class RepositoryFileOutputStream method flush.
@Override
public void flush() throws IOException {
if (closed) {
return;
}
super.flush();
ByteArrayInputStream bis = new ByteArrayInputStream(toByteArray());
// make an effort to determine the correct mime type, default to application/octet-stream
String extension = RepositoryFilenameUtils.getExtension(path);
// $NON-NLS-1$
String mimeType = "application/octet-stream";
if (extension != null) {
// $NON-NLS-1$
String tempMimeType = MimeHelper.getMimeTypeFromExtension("." + extension);
if (tempMimeType != null) {
mimeType = tempMimeType;
}
}
if (charsetName == null) {
charsetName = MimeHelper.getDefaultCharset(mimeType);
}
// FIXME: not a good idea that we assume we are dealing with text. Best if this is somehow moved to the
// RepositoryFileWriter
// but I couldn't figure out a clean way to do that. For now, charsetName is passed in here and we use it if
// available.
final IRepositoryFileData payload;
Converter converter;
if (TRANS_EXT.equalsIgnoreCase(extension)) {
converter = PentahoSystem.get(Converter.class, null, Collections.singletonMap("name", "PDITransformationStreamConverter"));
mimeType = "application/vnd.pentaho.transformation";
} else if (JOB_EXT.equalsIgnoreCase(extension)) {
converter = PentahoSystem.get(Converter.class, null, Collections.singletonMap("name", "PDIJobStreamConverter"));
mimeType = "application/vnd.pentaho.job";
} else {
converter = null;
}
payload = convert(converter, bis, mimeType);
if (!flushed) {
RepositoryFile file = repository.getFile(path);
RepositoryFile parentFolder = getParent(path);
String baseFileName = RepositoryFilenameUtils.getBaseName(path);
if (file == null) {
if (autoCreateDirStructure) {
ArrayList<String> foldersToCreate = new ArrayList<>();
String parentPath = RepositoryFilenameUtils.getFullPathNoEndSeparator(path);
// Make sure the parent path isn't the root
while ((parentPath != null) && (parentPath.length() > 0 && !path.equals(parentPath)) && (repository.getFile(parentPath) == null)) {
foldersToCreate.add(RepositoryFilenameUtils.getName(parentPath));
parentPath = RepositoryFilenameUtils.getFullPathNoEndSeparator(parentPath);
}
Collections.reverse(foldersToCreate);
parentFolder = ((parentPath != null) && (parentPath.length() > 0)) ? repository.getFile(parentPath) : repository.getFile("/");
if (!parentFolder.isFolder()) {
throw new FileNotFoundException();
}
for (String folderName : foldersToCreate) {
parentFolder = repository.createFolder(parentFolder.getId(), new RepositoryFile.Builder(folderName).folder(true).build(), null);
}
} else {
if (parentFolder == null) {
throw new FileNotFoundException();
}
}
file = buildRepositoryFile(RepositoryFilenameUtils.getName(path), extension, baseFileName);
repository.createFile(parentFolder.getId(), file, payload, // $NON-NLS-1$
"commit from " + RepositoryFileOutputStream.class.getName());
for (IStreamListener listener : listeners) {
listener.fileCreated(path);
}
} else if (file.isFolder()) {
throw new FileNotFoundException(MessageFormat.format("Repository file {0} is a directory", file.getPath()));
} else {
if (autoCreateUniqueFileName) {
int nameCount = 1;
String newFileName = null;
String newBaseFileName = null;
List<RepositoryFile> children = repository.getChildren(parentFolder.getId());
boolean hasFile = true;
while (hasFile) {
hasFile = false;
nameCount++;
newBaseFileName = baseFileName + "(" + nameCount + ")";
if ((extension != null) && (extension.length() > 0)) {
// $NON-NLS-1$ //$NON-NLS-2$
newFileName = newBaseFileName + "." + extension;
} else {
// $NON-NLS-1$ //$NON-NLS-2$
newFileName = newBaseFileName;
}
for (RepositoryFile child : children) {
if (child.getPath().equals(parentFolder.getPath() + "/" + newFileName)) {
hasFile = true;
break;
}
}
}
file = buildRepositoryFile(newFileName, extension, newBaseFileName);
// $NON-NLS-1$
file = repository.createFile(parentFolder.getId(), file, payload, "New File");
path = file.getPath();
for (IStreamListener listener : listeners) {
listener.fileCreated(path);
}
} else {
// $NON-NLS-1$
repository.updateFile(file, payload, "New File");
path = file.getPath();
for (IStreamListener listener : listeners) {
listener.fileCreated(path);
}
}
}
} else {
RepositoryFile file = repository.getFile(path);
// $NON-NLS-1$
repository.updateFile(file, payload, "New File");
}
flushed = true;
}
Aggregations