Search in sources :

Example 36 with RepositoryFileDto

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

the class FileServiceTest method testDoGetLocalProperties.

@Test
public void testDoGetLocalProperties() throws Exception {
    String pathId = "path:to:file:file1.ext";
    String fileId = "file1";
    String locale = "";
    doReturn("/path/to/file/file1.ext").when(fileService).idToPath(pathId);
    Set<String> propertiesList = new HashSet<String>();
    propertiesList.add("prop1");
    propertiesList.add("prop2");
    RepositoryFileDto repositoryFileDto = mock(RepositoryFileDto.class);
    doReturn(fileId).when(repositoryFileDto).getId();
    doReturn(repositoryFileDto).when(fileService.defaultUnifiedRepositoryWebService).getFile(anyString());
    Properties properties = mock(Properties.class);
    doReturn("value1").when(properties).getProperty("prop1");
    doReturn("value2").when(properties).getProperty("prop2");
    doReturn(false).when(properties).isEmpty();
    doReturn(propertiesList).when(properties).stringPropertyNames();
    PropertiesWrapper propertiesWrapper = mock(PropertiesWrapper.class);
    when(propertiesWrapper.getProperties()).thenReturn(properties);
    doReturn(propertiesWrapper).when(fileService.defaultUnifiedRepositoryWebService).getLocalePropertiesForFileById(anyString(), anyString());
    List<StringKeyStringValueDto> keyValueList = fileService.doGetLocaleProperties(pathId, locale);
    verify(fileService.defaultUnifiedRepositoryWebService).getFile("/path/to/file/file1.ext");
    verify(properties).getProperty("prop1");
    verify(properties).getProperty("prop2");
    verify(properties).isEmpty();
    verify(properties).stringPropertyNames();
    verify(fileService.defaultUnifiedRepositoryWebService).getLocalePropertiesForFileById(anyString(), anyString());
    assertEquals(2, keyValueList.size());
    assertEquals("prop1", keyValueList.get(1).getKey());
    assertEquals("prop2", keyValueList.get(0).getKey());
    assertEquals("value1", keyValueList.get(1).getValue());
    assertEquals("value2", keyValueList.get(0).getValue());
}
Also used : RepositoryFileDto(org.pentaho.platform.repository2.unified.webservices.RepositoryFileDto) StringKeyStringValueDto(org.pentaho.platform.repository2.unified.webservices.StringKeyStringValueDto) Matchers.anyString(org.mockito.Matchers.anyString) Properties(java.util.Properties) PropertiesWrapper(org.pentaho.platform.repository2.unified.webservices.PropertiesWrapper) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 37 with RepositoryFileDto

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

the class FileServiceTest method testDoGetChildren.

@Test
public void testDoGetChildren() {
    RepositoryFileDto mockRepositoryFileDto = mock(RepositoryFileDto.class);
    Collator mockCollator = mock(Collator.class);
    List<RepositoryFileDto> mockRepositoryFileDtos = new ArrayList<RepositoryFileDto>();
    mockRepositoryFileDtos.add(mockRepositoryFileDto);
    RepositoryRequest mockRepositoryRequest = mock(RepositoryRequest.class);
    doReturn(true).when(fileService).isPathValid(anyString());
    doReturn(mockRepositoryFileDto).when(fileService.defaultUnifiedRepositoryWebService).getFile(anyString());
    doReturn(mockCollator).when(fileService).getCollator(anyInt());
    doReturn(mockRepositoryRequest).when(fileService).getRepositoryRequest((RepositoryFileDto) anyObject(), anyBoolean(), anyString(), anyBoolean());
    doReturn(mockRepositoryFileDtos).when(fileService.defaultUnifiedRepositoryWebService).getChildrenFromRequest(mockRepositoryRequest);
    doReturn(true).when(fileService).isShowingTitle(mockRepositoryRequest);
    List<RepositoryFileDto> repositoryFileDtos = fileService.doGetChildren("mock:path:fileName", null, true, true);
    verify(fileService, times(1)).isPathValid(anyString());
    verify(fileService.defaultUnifiedRepositoryWebService, times(1)).getFile(anyString());
    verify(fileService, times(1)).getCollator(anyInt());
    verify(fileService, times(1)).getRepositoryRequest((RepositoryFileDto) anyObject(), anyBoolean(), anyString(), anyBoolean());
    verify(fileService.defaultUnifiedRepositoryWebService, times(1)).getChildrenFromRequest(mockRepositoryRequest);
    verify(fileService, times(1)).isShowingTitle(mockRepositoryRequest);
    assertEquals(mockRepositoryFileDtos, repositoryFileDtos);
    assertEquals(1, repositoryFileDtos.size());
    assertEquals(mockRepositoryFileDto, repositoryFileDtos.get(0));
}
Also used : RepositoryFileDto(org.pentaho.platform.repository2.unified.webservices.RepositoryFileDto) ArrayList(java.util.ArrayList) RepositoryRequest(org.pentaho.platform.api.repository2.unified.RepositoryRequest) Collator(java.text.Collator) Test(org.junit.Test)

Example 38 with RepositoryFileDto

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

the class FileServiceTest method testDoMoveFiles.

@Test
public void testDoMoveFiles() throws Exception {
    String destPathId = "/test";
    String[] params = { "file1", "file2" };
    RepositoryFileDto repositoryFileDto = mock(RepositoryFileDto.class);
    doReturn(destPathId).when(repositoryFileDto).getPath();
    doReturn(repositoryFileDto).when(fileService.defaultUnifiedRepositoryWebService).getFile(destPathId);
    fileService.doMoveFiles(destPathId, StringUtils.join(params, ","));
    verify(fileService.getRepoWs(), times(1)).moveFile(params[0], destPathId, null);
    verify(fileService.getRepoWs(), times(1)).moveFile(params[1], destPathId, null);
}
Also used : RepositoryFileDto(org.pentaho.platform.repository2.unified.webservices.RepositoryFileDto) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 39 with RepositoryFileDto

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

the class FileService method getSourceFileIdsThatNotConflictWithFolderFiles.

/**
 * Conflict occurs if one of source files has the same
 * name with any of folder files.
 *
 * @param params
 *            String with file ids, separated by comma
 * @param pathToFolder
 *            path to folder
 *
 * @return String
 *            with file ids of not conflict files, separated by comma
 */
protected String getSourceFileIdsThatNotConflictWithFolderFiles(String pathToFolder, String params) {
    String[] sourceFileIds = FileUtils.convertCommaSeparatedStringToArray(params);
    List<String> nonConflictFileIdsList = new ArrayList<>();
    List<RepositoryFileDto> homeFolderFiles = doGetChildren(pathToFolder, null, true, true);
    for (String sourceFileId : sourceFileIds) {
        boolean isConflict = false;
        RepositoryFile fileToRestore = getRepository().getFileById(sourceFileId);
        if (fileToRestore == null) {
            logger.error("Could not get file with id: " + sourceFileId);
            continue;
        }
        for (RepositoryFileDto fileInHomeFolder : homeFolderFiles) {
            if (fileToRestore.getName().equals(fileInHomeFolder.getName())) {
                isConflict = true;
                break;
            }
        }
        if (!isConflict) {
            nonConflictFileIdsList.add(sourceFileId);
        }
    }
    return getCommaSeparatedFileIds(nonConflictFileIdsList);
}
Also used : RepositoryFileDto(org.pentaho.platform.repository2.unified.webservices.RepositoryFileDto) ArrayList(java.util.ArrayList) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile)

Example 40 with RepositoryFileDto

use of org.pentaho.platform.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.repository2.unified.webservices.RepositoryFileDto) StringKeyStringValueDto(org.pentaho.platform.repository2.unified.webservices.StringKeyStringValueDto) IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) Serializable(java.io.Serializable) RepositoryFileAclAceDto(org.pentaho.platform.repository2.unified.webservices.RepositoryFileAclAceDto) RepositoryFileAclDto(org.pentaho.platform.repository2.unified.webservices.RepositoryFileAclDto) GeneralSecurityException(java.security.GeneralSecurityException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)

Aggregations

RepositoryFileDto (org.pentaho.platform.repository2.unified.webservices.RepositoryFileDto)66 Test (org.junit.Test)42 Matchers.anyString (org.mockito.Matchers.anyString)26 FileNotFoundException (java.io.FileNotFoundException)23 ArrayList (java.util.ArrayList)20 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)18 Serializable (java.io.Serializable)15 GeneralSecurityException (java.security.GeneralSecurityException)11 StringKeyStringValueDto (org.pentaho.platform.repository2.unified.webservices.StringKeyStringValueDto)9 IllegalSelectorException (java.nio.channels.IllegalSelectorException)8 InvalidParameterException (java.security.InvalidParameterException)8 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)8 UnifiedRepositoryAccessDeniedException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryAccessDeniedException)8 IOException (java.io.IOException)7 IRepositoryFileData (org.pentaho.platform.api.repository2.unified.IRepositoryFileData)6 RepositoryFileAcl (org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)6 RepositoryFileAclDto (org.pentaho.platform.repository2.unified.webservices.RepositoryFileAclDto)6 Properties (java.util.Properties)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 RepositoryFileAclAceDto (org.pentaho.platform.repository2.unified.webservices.RepositoryFileAclAceDto)4