Search in sources :

Example 76 with RepositoryFileDto

use of org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto in project pentaho-platform by pentaho.

the class CopyFilesOperation method copyOverrideMode.

private void copyOverrideMode(RepositoryFile file) {
    if (sourceAndDestDirAreSame(file.getPath())) {
        return;
    }
    RepositoryFileAcl acl = getRepository().getAcl(file.getId());
    RepositoryFile destFile = getRepository().getFile(destDir.getPath() + FileUtils.PATH_SEPARATOR + file.getName());
    if (destFile == null) {
        // destFile doesn't exist so we'll create it.
        RepositoryFile duplicateFile = new RepositoryFile.Builder(file.getName()).hidden(file.isHidden()).versioned(file.isVersioned()).build();
        final RepositoryFile repositoryFile = getRepository().createFile(destDir.getId(), duplicateFile, RepositoryFileHelper.getFileData(file), acl, null);
        getRepository().setFileMetadata(repositoryFile.getId(), getRepository().getFileMetadata(file.getId()));
        return;
    }
    RepositoryFileDto destFileDto = toFileDto(destFile, null, false);
    destFileDto.setHidden(file.isHidden());
    destFile = toFile(destFileDto);
    final RepositoryFile repositoryFile = getRepository().updateFile(destFile, RepositoryFileHelper.getFileData(file), null);
    getRepository().updateAcl(acl);
    getRepository().setFileMetadata(repositoryFile.getId(), getRepository().getFileMetadata(file.getId()));
}
Also used : RepositoryFileDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)

Example 77 with RepositoryFileDto

use of org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto in project pentaho-platform by pentaho.

the class CopyFilesOperation method copyRenameMode.

private void copyRenameMode(RepositoryFile repoFile) {
    // First try to see if regular name is available
    String repoFileName = repoFile.getName();
    String copyText = "";
    String rootCopyText = "";
    String nameNoExtension = repoFileName;
    String extension = "";
    int indexOfDot = repoFileName.lastIndexOf('.');
    if (!(indexOfDot == -1)) {
        nameNoExtension = repoFileName.substring(0, indexOfDot);
        extension = repoFileName.substring(indexOfDot);
    }
    RepositoryFileDto testFile = // $NON-NLS-1$
    getRepoWs().getFile(path + FileUtils.PATH_SEPARATOR + nameNoExtension + extension);
    if (testFile != null) {
        // Second try COPY_PREFIX, If the name already ends with a COPY_PREFIX don't append twice
        if (!nameNoExtension.endsWith(Messages.getInstance().getString("FileResource.COPY_PREFIX"))) {
            // $NON-NLS-1$
            copyText = rootCopyText = Messages.getInstance().getString("FileResource.COPY_PREFIX");
            repoFileName = nameNoExtension + copyText + extension;
            testFile = getRepoWs().getFile(path + FileUtils.PATH_SEPARATOR + repoFileName);
        }
    }
    // Third try COPY_PREFIX + DUPLICATE_INDICATOR
    Integer nameCount = 1;
    while (testFile != null) {
        nameCount++;
        copyText = rootCopyText + Messages.getInstance().getString("FileResource.DUPLICATE_INDICATOR", nameCount);
        repoFileName = nameNoExtension + copyText + extension;
        testFile = getRepoWs().getFile(path + FileUtils.PATH_SEPARATOR + repoFileName);
    }
    IRepositoryFileData data = RepositoryFileHelper.getFileData(repoFile);
    RepositoryFileAcl acl = getRepository().getAcl(repoFile.getId());
    RepositoryFile duplicateFile = null;
    final RepositoryFile repositoryFile;
    if (repoFile.isFolder()) {
        // If the title is different than the source file, copy it separately
        if (!repoFile.getName().equals(repoFile.getTitle())) {
            duplicateFile = new RepositoryFile.Builder(repoFileName).title(RepositoryFile.DEFAULT_LOCALE, repoFile.getTitle() + copyText).hidden(repoFile.isHidden()).versioned(repoFile.isVersioned()).folder(true).build();
        } else {
            duplicateFile = new RepositoryFile.Builder(repoFileName).hidden(repoFile.isHidden()).folder(true).build();
        }
        repositoryFile = getRepository().createFolder(destDir.getId(), duplicateFile, acl, null);
        performFolderDeepCopy(repoFile, repositoryFile, DEFAULT_DEEPNESS);
    } else {
        // If the title is different than the source file, copy it separately
        if (!repoFile.getName().equals(repoFile.getTitle())) {
            duplicateFile = new RepositoryFile.Builder(repoFileName).title(RepositoryFile.DEFAULT_LOCALE, repoFile.getTitle() + copyText).hidden(repoFile.isHidden()).versioned(repoFile.isVersioned()).build();
        } else {
            duplicateFile = new RepositoryFile.Builder(repoFileName).hidden(repoFile.isHidden()).build();
        }
        repositoryFile = getRepository().createFile(destDir.getId(), duplicateFile, data, acl, null);
    }
    if (repositoryFile == null) {
        throw new UnifiedRepositoryAccessDeniedException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED_CREATE", destDir.getId()));
    }
    getRepository().setFileMetadata(repositoryFile.getId(), getRepository().getFileMetadata(repoFile.getId()));
}
Also used : RepositoryFileDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto) IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) UnifiedRepositoryAccessDeniedException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryAccessDeniedException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)

Example 78 with RepositoryFileDto

use of org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto in project pentaho-platform by pentaho.

the class FileService method doSetLocaleProperties.

/**
 * Set the list of locale properties for a given locale
 *
 * @param pathId
 * @param locale
 * @param properties
 */
public void doSetLocaleProperties(String pathId, String locale, List<StringKeyStringValueDto> properties) throws Exception {
    RepositoryFileDto file = getRepoWs().getFile(idToPath(pathId));
    Properties fileProperties = new Properties();
    if (properties != null && !properties.isEmpty()) {
        for (StringKeyStringValueDto dto : properties) {
            fileProperties.put(dto.getKey(), dto.getValue());
        }
    }
    getRepoWs().setLocalePropertiesForFileByFileId(file.getId(), locale, fileProperties);
}
Also used : RepositoryFileDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto) StringKeyStringValueDto(org.pentaho.platform.api.repository2.unified.webservices.StringKeyStringValueDto) Properties(java.util.Properties)

Example 79 with RepositoryFileDto

use of org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto in project pentaho-platform by pentaho.

the class FileService method doSetMetadata.

/**
 * Set the metadata on a file
 *
 * @param pathId
 * @param metadata
 * @throws GeneralSecurityException
 */
public void doSetMetadata(String pathId, List<StringKeyStringValueDto> metadata) throws GeneralSecurityException {
    RepositoryFileDto file = getRepoWs().getFile(idToPath(pathId));
    RepositoryFileAclDto fileAcl = getRepoWs().getAcl(file.getId());
    boolean canManage = getSession().getName().equals(fileAcl.getOwner()) || (getPolicy().isAllowed(RepositoryReadAction.NAME) && getPolicy().isAllowed(RepositoryCreateAction.NAME) && getPolicy().isAllowed(AdministerSecurityAction.NAME));
    if (!canManage) {
        if (fileAcl.isEntriesInheriting()) {
            List<RepositoryFileAclAceDto> aces = getRepoWs().getEffectiveAces(file.getId());
            fileAcl.setAces(aces, fileAcl.isEntriesInheriting());
        }
        for (int i = 0; i < fileAcl.getAces().size(); i++) {
            RepositoryFileAclAceDto acl = fileAcl.getAces().get(i);
            if (acl.getRecipient().equals(getSession().getName())) {
                if (acl.getPermissions().contains(RepositoryFilePermission.ACL_MANAGEMENT.ordinal()) || acl.getPermissions().contains(RepositoryFilePermission.ALL.ordinal())) {
                    canManage = true;
                    break;
                }
            }
        }
    }
    if (canManage) {
        Map<String, Serializable> fileMetadata = getRepository().getFileMetadata(file.getId());
        boolean isHidden = RepositoryFile.HIDDEN_BY_DEFAULT;
        boolean isSchedulable = RepositoryFile.SCHEDULABLE_BY_DEFAULT;
        fileMetadata.remove(RepositoryFile.HIDDEN_KEY);
        for (StringKeyStringValueDto nv : metadata) {
            // don't add hidden to the list because it is not actually part of the metadata node
            String key = nv.getKey();
            if (RepositoryFile.HIDDEN_KEY.equalsIgnoreCase(key)) {
                isHidden = BooleanUtils.toBoolean(nv.getValue());
                continue;
            }
            if (RepositoryFile.SCHEDULABLE_KEY.equalsIgnoreCase(key)) {
                isSchedulable = BooleanUtils.toBoolean(nv.getValue());
            }
            fileMetadata.put(key, nv.getValue());
        }
        // now update the rest of the metadata
        if (!file.isFolder()) {
            getRepository().setFileMetadata(file.getId(), fileMetadata);
        }
        // handle hidden flag if it is different
        if (file.isHidden() != isHidden) {
            file.setHidden(isHidden);
            file.setNotSchedulable(!isSchedulable);
            /*
         * Since we cannot simply set the new value, use the RepositoryFileAdapter to create a new instance and then
         * update the original.
         */
            RepositoryFile sourceFile = getRepository().getFileById(file.getId());
            RepositoryFileDto destFileDto = toFileDto(sourceFile, null, false);
            destFileDto.setHidden(isHidden);
            destFileDto.setNotSchedulable(!isSchedulable);
            RepositoryFile destFile = toFile(destFileDto);
            // add the existing acls and file data
            RepositoryFileAcl acl = getRepository().getAcl(sourceFile.getId());
            if (!file.isFolder()) {
                IRepositoryFileData data = RepositoryFileHelper.getFileData(sourceFile);
                getRepository().updateFile(destFile, data, null);
                getRepository().updateAcl(acl);
            } else {
                getRepository().updateFolder(destFile, null);
            }
        }
    } else {
        throw new GeneralSecurityException();
    }
}
Also used : RepositoryFileDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto) StringKeyStringValueDto(org.pentaho.platform.api.repository2.unified.webservices.StringKeyStringValueDto) IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) Serializable(java.io.Serializable) RepositoryFileAclAceDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileAclAceDto) RepositoryFileAclDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileAclDto) GeneralSecurityException(java.security.GeneralSecurityException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)

Example 80 with RepositoryFileDto

use of org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto in project pentaho-platform by pentaho.

the class FileService method doGetMetadata.

/**
 * Get metadata for a file by path id
 *
 * @param pathId
 * @return
 */
public List<StringKeyStringValueDto> doGetMetadata(String pathId) throws FileNotFoundException {
    List<StringKeyStringValueDto> list = null;
    String path = null;
    if (pathId == null || pathId.equals(FileUtils.PATH_SEPARATOR)) {
        path = FileUtils.PATH_SEPARATOR;
    } else {
        if (!pathId.startsWith(FileUtils.PATH_SEPARATOR)) {
            path = idToPath(pathId);
        }
    }
    final RepositoryFileDto file = getRepoWs().getFile(path);
    if (file == null) {
        throw new FileNotFoundException();
    }
    list = getRepoWs().getFileMetadata(file.getId());
    if (list != null) {
        boolean hasSchedulable = false;
        for (StringKeyStringValueDto value : list) {
            if (value.getKey().equals(RepositoryFile.SCHEDULABLE_KEY)) {
                hasSchedulable = true;
                break;
            }
        }
        if (!hasSchedulable) {
            StringKeyStringValueDto schedPerm = new StringKeyStringValueDto(RepositoryFile.SCHEDULABLE_KEY, "true");
            list.add(schedPerm);
        }
        // check file object for hidden value and add it to the list
        list.add(new StringKeyStringValueDto(RepositoryFile.HIDDEN_KEY, String.valueOf(file.isHidden())));
    }
    return list;
}
Also used : StringKeyStringValueDto(org.pentaho.platform.api.repository2.unified.webservices.StringKeyStringValueDto) RepositoryFileDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

Test (org.junit.Test)77 RepositoryFileDto (org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto)77 FileNotFoundException (java.io.FileNotFoundException)35 ArrayList (java.util.ArrayList)34 RepositoryFileDto (org.pentaho.platform.repository2.unified.webservices.RepositoryFileDto)29 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)27 Serializable (java.io.Serializable)26 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)26 Matchers.anyString (org.mockito.Matchers.anyString)25 GeneralSecurityException (java.security.GeneralSecurityException)14 StringKeyStringValueDto (org.pentaho.platform.api.repository2.unified.webservices.StringKeyStringValueDto)11 IllegalSelectorException (java.nio.channels.IllegalSelectorException)9 InvalidParameterException (java.security.InvalidParameterException)9 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)9 Properties (java.util.Properties)8 UnifiedRepositoryAccessDeniedException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryAccessDeniedException)8 DefaultUnifiedRepositoryWebService (org.pentaho.platform.repository2.unified.webservices.DefaultUnifiedRepositoryWebService)8 IOException (java.io.IOException)7 IRepositoryFileData (org.pentaho.platform.api.repository2.unified.IRepositoryFileData)7 RepositoryFileAcl (org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)7