Search in sources :

Example 61 with RepositoryFileDto

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

the class RepositoryFileAdapter method toFileDto.

public static RepositoryFileDto toFileDto(final RepositoryFile v, Set<String> memberSet, boolean exclude, boolean includeAcls) {
    if (v == null) {
        return null;
    }
    RepositoryFileDto f = new RepositoryFileDto();
    // no longer exists, so it returns null
    try {
        if (include("name", memberSet, exclude)) {
            f.setName(v.getName());
        }
        if (include("path", memberSet, exclude)) {
            f.setPath(v.getPath());
        }
        if (include("hidden", memberSet, exclude)) {
            f.setHidden(v.isHidden());
        }
        if (include("aclNode", memberSet, exclude)) {
            f.setAclNode(v.isAclNode());
        }
        if (include("createDate", memberSet, exclude)) {
            f.setCreatedDate(marshalDate(v.getCreatedDate()));
        }
        if (include("creatorId", memberSet, exclude)) {
            f.setCreatorId(v.getCreatorId());
        }
        if (include("fileSize", memberSet, exclude)) {
            f.setFileSize(v.getFileSize());
        }
        if (include("description", memberSet, exclude)) {
            f.setDescription(v.getDescription());
        }
        if (include("folder", memberSet, exclude)) {
            f.setFolder(v.isFolder());
        }
        // it must be present or the tree rest service call will error
        if (v.getId() != null) {
            f.setId(v.getId().toString());
        }
        if (include("lastModifiedDate", memberSet, exclude)) {
            f.setLastModifiedDate(marshalDate(v.getLastModifiedDate()));
        }
        if (include("locale", memberSet, exclude)) {
            f.setLocale(v.getLocale());
        }
        if (include("originalParentFolderPath", memberSet, exclude)) {
            f.setOriginalParentFolderPath(v.getOriginalParentFolderPath());
        }
        if (include("deletedDate", memberSet, exclude)) {
            f.setDeletedDate(marshalDate(v.getDeletedDate()));
        }
        if (include("lockDate", memberSet, exclude)) {
            f.setLockDate(marshalDate(v.getLockDate()));
        }
        if (include("locked", memberSet, exclude)) {
            f.setLocked(v.isLocked());
        }
        if (include("lockMessage", memberSet, exclude)) {
            f.setLockMessage(v.getLockMessage());
        }
        if (include("lockOwner", memberSet, exclude)) {
            f.setLockOwner(v.getLockOwner());
        }
        if (include("title", memberSet, exclude)) {
            f.setTitle(v.getTitle());
        }
        if (include("versioned", memberSet, exclude)) {
            f.setVersioned(v.isVersioned());
        }
        if (include("versionId", memberSet, exclude)) {
            if (v.getVersionId() != null) {
                f.setVersionId(v.getVersionId().toString());
            }
        }
    } catch (NullPointerException e) {
        getLogger().warn("NullPointerException while reading file attributes, returning null. Probable cause: File does not" + "exist anymore: ");
        return null;
    }
    if (includeAcls) {
        if (v.getId() != null) {
            try {
                String id = v.getId().toString();
                f.setRepositoryFileAclDto(getRepoWs().getAcl(id));
                if (f.getRepositoryFileAclDto().isEntriesInheriting()) {
                    List<RepositoryFileAclAceDto> aces = getRepoWs().getEffectiveAces(id);
                    f.getRepositoryFileAclDto().setAces(aces, f.getRepositoryFileAclDto().isEntriesInheriting());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (include("owner", memberSet, exclude)) {
            Serializable id = v.getId();
            if (id != null) {
                RepositoryFileAclDto acl = getRepoWs().getAcl("" + id);
                if (acl != null) {
                    f.setOwner(acl.getOwner());
                }
            }
        }
    }
    if (include("locales", memberSet, exclude)) {
        if (v.getLocalePropertiesMap() != null) {
            f.setLocalePropertiesMapEntries(new ArrayList<LocaleMapDto>());
            for (Map.Entry<String, Properties> entry : v.getLocalePropertiesMap().entrySet()) {
                LocaleMapDto localeMapDto = new LocaleMapDto();
                List<StringKeyStringValueDto> valuesDto = new ArrayList<StringKeyStringValueDto>();
                Properties properties = entry.getValue();
                if (properties != null) {
                    for (String propertyName : properties.stringPropertyNames()) {
                        valuesDto.add(new StringKeyStringValueDto(propertyName, properties.getProperty(propertyName)));
                    }
                }
                localeMapDto.setLocale(entry.getKey());
                localeMapDto.setProperties(valuesDto);
                f.getLocalePropertiesMapEntries().add(localeMapDto);
            }
        }
    }
    IRepositoryVersionManager repositoryVersionManager;
    try {
        repositoryVersionManager = JcrRepositoryFileUtils.getRepositoryVersionManager();
        // Not found, must be in Spoon
        if (repositoryVersionManager == null) {
            return f;
        }
    } catch (NoClassDefFoundError ex) {
        // support tree and child calls.
        return f;
    }
    if (include("versioningEnabled", memberSet, exclude)) {
        f.setVersioningEnabled(repositoryVersionManager.isVersioningEnabled(v.getPath()));
    }
    if (include("versionCommentEnabled", memberSet, exclude)) {
        f.setVersionCommentEnabled(repositoryVersionManager.isVersionCommentEnabled(v.getPath()));
    }
    return f;
}
Also used : RepositoryFileDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto) StringKeyStringValueDto(org.pentaho.platform.api.repository2.unified.webservices.StringKeyStringValueDto) Serializable(java.io.Serializable) LocaleMapDto(org.pentaho.platform.api.repository2.unified.webservices.LocaleMapDto) RepositoryFileAclAceDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileAclAceDto) RepositoryFileAclDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileAclDto) ArrayList(java.util.ArrayList) Properties(java.util.Properties) IRepositoryVersionManager(org.pentaho.platform.api.repository2.unified.IRepositoryVersionManager) Map(java.util.Map)

Example 62 with RepositoryFileDto

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

the class RepositoryFileUtils method convertFromRepositoryFile.

public static RepositoryFileDto convertFromRepositoryFile(RepositoryFile file) {
    RepositoryFileDto repositoryFile = new RepositoryFileDto();
    repositoryFile.setCreatedDate(RepositoryFileAdapter.marshalDate(file.getCreatedDate()));
    repositoryFile.setDeletedDate(RepositoryFileAdapter.marshalDate(file.getDeletedDate()));
    repositoryFile.setDescription(file.getDescription());
    repositoryFile.setFolder(file.isFolder());
    repositoryFile.setHidden(file.isHidden());
    repositoryFile.setAclNode(false);
    repositoryFile.setId(file.getId());
    repositoryFile.setLastModifiedDate(RepositoryFileAdapter.marshalDate(file.getLastModifiedDate()));
    repositoryFile.setLocale(file.getLocale());
    repositoryFile.setLockDate(RepositoryFileAdapter.marshalDate(file.getLockDate()));
    repositoryFile.setLocked(file.isLocked());
    repositoryFile.setLockMessage(file.getLockMessage());
    repositoryFile.setLockOwner(file.getLockOwner());
    repositoryFile.setName(file.getName());
    repositoryFile.setOriginalParentFolderPath(file.getOriginalParentFolderPath());
    repositoryFile.setOriginalParentFolderPath(file.getOriginalParentFolderPath());
    repositoryFile.setOwner(file.getOwner());
    repositoryFile.setPath(file.getPath());
    repositoryFile.setTitle(file.getTitle());
    repositoryFile.setVersionId(file.getVersionId());
    return repositoryFile;
}
Also used : RepositoryFileDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto)

Example 63 with RepositoryFileDto

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

the class DefaultUnifiedRepositoryWebService method getFile.

public RepositoryFileDto getFile(String path) {
    validateEtcReadAccess(path);
    RepositoryFile file = repo.getFile(path);
    return file != null ? repositoryFileAdapter.marshal(file) : null;
}
Also used : RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile)

Example 64 with RepositoryFileDto

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

the class FileServiceTest method doSetMetadataException.

@Test
public void doSetMetadataException() {
    String pathId = "path:to:file:file1.ext";
    List<StringKeyStringValueDto> stringKeyStringValueDtos = new ArrayList<StringKeyStringValueDto>();
    doReturn("/path/to/file/file1.ext").when(fileService).idToPath(pathId);
    doReturn(false).when(fileService.policy).isAllowed(RepositoryReadAction.NAME);
    doReturn(false).when(fileService.policy).isAllowed(RepositoryCreateAction.NAME);
    doReturn(false).when(fileService.policy).isAllowed(AdministerSecurityAction.NAME);
    RepositoryFileDto file = mock(RepositoryFileDto.class);
    doReturn(file).when(fileService.defaultUnifiedRepositoryWebService).getFile(anyString());
    RepositoryFileAclDto repositoryFileAclDto = mock(RepositoryFileAclDto.class);
    doReturn("sessionName").when(repositoryFileAclDto).getOwner();
    doReturn(repositoryFileAclDto).when(fileService.defaultUnifiedRepositoryWebService).getAcl(anyString());
    IPentahoSession pentahoSession = mock(IPentahoSession.class);
    doReturn(pentahoSession).when(fileService).getSession();
    doReturn("sessionName1").when(pentahoSession).getName();
    try {
        fileService.doSetMetadata(pathId, stringKeyStringValueDtos);
        fail();
    } catch (GeneralSecurityException e) {
    // Should catch the exception
    }
    verify(fileService.defaultUnifiedRepositoryWebService).getFile(anyString());
    verify(fileService.defaultUnifiedRepositoryWebService).getAcl(anyString());
    verify(repositoryFileAclDto).getOwner();
    verify(fileService.policy).isAllowed(anyString());
}
Also used : StringKeyStringValueDto(org.pentaho.platform.repository2.unified.webservices.StringKeyStringValueDto) RepositoryFileDto(org.pentaho.platform.repository2.unified.webservices.RepositoryFileDto) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) RepositoryFileAclDto(org.pentaho.platform.repository2.unified.webservices.RepositoryFileAclDto) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 65 with RepositoryFileDto

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

the class FileServiceTest method testDoGetGeneratedContent.

@Test
public void testDoGetGeneratedContent() {
    String pathId = "test.prpt", userFolder = "public/admin";
    RepositoryFileDto fileDetailsMock = mock(RepositoryFileDto.class);
    RepositoryFile workspaceFolder = mock(RepositoryFile.class);
    doReturn(userFolder).when(workspaceFolder).getId();
    SessionResource sessionResource = mock(SessionResource.class);
    List<RepositoryFile> children = new ArrayList<RepositoryFile>();
    RepositoryFile mockedChild = mock(RepositoryFile.class);
    doReturn(false).when(mockedChild).isFolder();
    children.add(mockedChild);
    Map<String, Serializable> mockedFileMetadata = mock(Map.class);
    doReturn(pathId).when(mockedFileMetadata).get(PentahoJcrConstants.PHO_CONTENTCREATOR);
    when(fileService.repository.getFileMetadata(mockedChild.getId())).thenReturn(mockedFileMetadata);
    doReturn(pathId).when(fileDetailsMock).getId();
    doReturn(userFolder).when(sessionResource).doGetCurrentUserDir();
    doReturn(workspaceFolder).when(fileService.repository).getFile(userFolder);
    doReturn(sessionResource).when(fileService).getSessionResource();
    doReturn(children).when(fileService.repository).getChildren(userFolder);
    RepositoryFileDto mockedRepositoryFileDto = mock(RepositoryFileDto.class);
    doReturn(mockedRepositoryFileDto).when(fileService).toFileDto(mockedChild, null, false);
    try {
        doReturn(fileDetailsMock).when(fileService).doGetProperties(pathId);
        List<RepositoryFileDto> list = fileService.doGetGeneratedContent(pathId);
        assertEquals(list.size(), 1);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        fail();
    } catch (Throwable t) {
        fail();
    }
}
Also used : RepositoryFileDto(org.pentaho.platform.repository2.unified.webservices.RepositoryFileDto) Serializable(java.io.Serializable) SessionResource(org.pentaho.platform.web.http.api.resources.SessionResource) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

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