Search in sources :

Example 11 with StringKeyStringValueDto

use of org.pentaho.platform.api.repository2.unified.webservices.StringKeyStringValueDto 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 12 with StringKeyStringValueDto

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

the class UnifiedRepositoryToWebServiceAdapter method getFileMetadata.

@Override
public Map<String, Serializable> getFileMetadata(final Serializable fileId) {
    final List<StringKeyStringValueDto> fileMetadata = repoWebService.getFileMetadata(fileId.toString());
    Assert.notNull(fileMetadata);
    final Map<String, Serializable> repoFileMetadata = new HashMap<String, Serializable>(fileMetadata.size());
    for (StringKeyStringValueDto entry : fileMetadata) {
        repoFileMetadata.put(entry.getKey(), entry.getValue());
    }
    return repoFileMetadata;
}
Also used : StringKeyStringValueDto(org.pentaho.platform.api.repository2.unified.webservices.StringKeyStringValueDto) Serializable(java.io.Serializable) HashMap(java.util.HashMap)

Example 13 with StringKeyStringValueDto

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

the class StringKeyStringValueDtoTest method testInitialization.

@Test
public void testInitialization() {
    final StringKeyStringValueDto empty = new StringKeyStringValueDto();
    assertNull(empty.getKey());
    assertNull(empty.getValue());
    assertTrue(empty.equals(new StringKeyStringValueDto()));
    assertEquals(empty.hashCode(), new StringKeyStringValueDto().hashCode());
    assertFalse(StringUtils.isEmpty(empty.toString()));
    final StringKeyStringValueDto sample1 = new StringKeyStringValueDto();
    sample1.setKey("test key");
    sample1.setValue("test value");
    final StringKeyStringValueDto sample2 = new StringKeyStringValueDto("test key", "test value");
    assertTrue(sample1.equals(sample2));
    assertEquals(sample1.hashCode(), sample2.hashCode());
    sample1.setKey(null);
    sample2.setKey(null);
    assertTrue(sample1.equals(sample2));
    assertEquals(sample1.hashCode(), sample2.hashCode());
    sample1.setKey(sample1.getValue());
    sample1.setValue(null);
    sample2.setKey(sample2.getValue());
    sample2.setValue(null);
    assertTrue(sample1.equals(sample2));
    assertEquals(sample1.hashCode(), sample2.hashCode());
    sample1.setKey(null);
    assertTrue(empty.equals(sample1));
    assertEquals(empty.hashCode(), sample1.hashCode());
    sample1.setValue("test");
    assertFalse(empty.equals(sample1));
    assertFalse(empty.equals(sample2));
    assertFalse(empty.hashCode() == sample1.hashCode());
    assertFalse(empty.hashCode() == sample2.hashCode());
    assertFalse(empty.equals(null));
    assertFalse(empty.equals(sample1.toString()));
}
Also used : StringKeyStringValueDto(org.pentaho.platform.api.repository2.unified.webservices.StringKeyStringValueDto) Test(org.junit.Test)

Example 14 with StringKeyStringValueDto

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

the class DefaultUnifiedRepositoryWebServiceTest method testFileMetadata.

public void testFileMetadata() throws Exception {
    final RepositoryFile testfile = repository.createFile(repository.getFile("/etc").getId(), new RepositoryFile.Builder("testfile").build(), new SimpleRepositoryFileData(new ByteArrayInputStream("test".getBytes()), "UTF-8", "text/plain"), null);
    // CHECKSTYLE IGNORE AvoidNestedBlocks FOR NEXT 3 LINES
    {
        // Make sure the repository is setup correctly
        assertNotNull(testfile);
        assertNotNull(testfile.getId());
        final Map<String, Serializable> fileMetadata = repository.getFileMetadata(testfile.getId());
        assertNotNull(fileMetadata);
        assertEquals(0, fileMetadata.size());
    }
    final List<StringKeyStringValueDto> metadata = new ArrayList<StringKeyStringValueDto>();
    metadata.add(new StringKeyStringValueDto("sample key", "sample value"));
    metadata.add(new StringKeyStringValueDto("complex key?", "\"an even more 'complex' value\"! {and them some}"));
    repositoryWS.setFileMetadata(testfile.getId().toString(), metadata);
    // CHECKSTYLE IGNORE AvoidNestedBlocks FOR NEXT 3 LINES
    {
        // Make sure the repository sees the metadata
        assertNotNull(testfile);
        assertNotNull(testfile.getId());
        final Map<String, Serializable> fileMetadata = repository.getFileMetadata(testfile.getId());
        assertNotNull(fileMetadata);
        assertEquals(2, fileMetadata.size());
    }
    // CHECKSTYLE IGNORE AvoidNestedBlocks FOR NEXT 3 LINES
    {
        // Make sure we can get the same metadata back via the web service
        final List<StringKeyStringValueDto> fileMetadata = repositoryWS.getFileMetadata(testfile.getId().toString());
        assertNotNull(fileMetadata);
        assertEquals(2, fileMetadata.size());
        assertTrue(metadata.get(0).equals(fileMetadata.get(0)) || metadata.get(0).equals(fileMetadata.get(1)));
        assertTrue(metadata.get(1).equals(fileMetadata.get(0)) || metadata.get(1).equals(fileMetadata.get(1)));
    }
}
Also used : StringKeyStringValueDto(org.pentaho.platform.api.repository2.unified.webservices.StringKeyStringValueDto) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 15 with StringKeyStringValueDto

use of org.pentaho.platform.api.repository2.unified.webservices.StringKeyStringValueDto 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)

Aggregations

StringKeyStringValueDto (org.pentaho.platform.api.repository2.unified.webservices.StringKeyStringValueDto)20 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)14 RepositoryFileDto (org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto)12 GeneralSecurityException (java.security.GeneralSecurityException)9 FileNotFoundException (java.io.FileNotFoundException)8 Serializable (java.io.Serializable)8 Properties (java.util.Properties)8 Matchers.anyString (org.mockito.Matchers.anyString)6 StringKeyStringValueDto (org.pentaho.platform.repository2.unified.webservices.StringKeyStringValueDto)6 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)5 RepositoryFileDto (org.pentaho.platform.repository2.unified.webservices.RepositoryFileDto)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 IOException (java.io.IOException)4 IllegalSelectorException (java.nio.channels.IllegalSelectorException)4 InvalidParameterException (java.security.InvalidParameterException)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)4 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)4