Search in sources :

Example 6 with IRepositoryFileData

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;
        }
    });
}
Also used : IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) JcrCallback(org.springframework.extensions.jcr.JcrCallback) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) Session(javax.jcr.Session)

Example 7 with IRepositoryFileData

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);
        }
    });
}
Also used : IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) DataNode(org.pentaho.platform.api.repository2.unified.data.node.DataNode) NodeRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.node.NodeRepositoryFileData) DataNode(org.pentaho.platform.api.repository2.unified.data.node.DataNode) Node(javax.jcr.Node) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) JcrCallback(org.springframework.extensions.jcr.JcrCallback) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) Session(javax.jcr.Session)

Example 8 with IRepositoryFileData

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());
}
Also used : RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)

Example 9 with IRepositoryFileData

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);
}
Also used : Version(javax.jcr.version.Version) Node(javax.jcr.Node) VersionManager(javax.jcr.version.VersionManager) IRepositoryVersionManager(org.pentaho.platform.api.repository2.unified.IRepositoryVersionManager)

Example 10 with IRepositoryFileData

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;
}
Also used : IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) IStreamListener(org.pentaho.platform.api.repository2.unified.IStreamListener) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Converter(org.pentaho.platform.api.repository2.unified.Converter) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)23 IRepositoryFileData (org.pentaho.platform.api.repository2.unified.IRepositoryFileData)16 RepositoryFileAcl (org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)9 Test (org.junit.Test)6 NodeRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.node.NodeRepositoryFileData)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 File (java.io.File)5 SimpleRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData)5 FileNotFoundException (java.io.FileNotFoundException)4 Serializable (java.io.Serializable)4 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 Converter (org.pentaho.platform.api.repository2.unified.Converter)4 FileOutputStream (java.io.FileOutputStream)3 IOException (java.io.IOException)3 List (java.util.List)3 Node (javax.jcr.Node)3 Session (javax.jcr.Session)3 Matchers.anyString (org.mockito.Matchers.anyString)3 IUnifiedRepository (org.pentaho.platform.api.repository2.unified.IUnifiedRepository)3