Search in sources :

Example 46 with RepositoryFileDto

use of org.pentaho.platform.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.repository2.unified.webservices.RepositoryFileDto) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)

Example 47 with RepositoryFileDto

use of org.pentaho.platform.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.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 48 with RepositoryFileDto

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

the class CopyFilesOperation_CopyTest method copyFiles_RenameMode.

@Test
public void copyFiles_RenameMode() {
    CopyFilesOperation operation = new CopyFilesOperation(repo, webService, getIdList(file1, file2), PATH_DEST_DIR, FileService.MODE_RENAME);
    // emulate, file with the same name exists.
    RepositoryFile conflict = mockFile(generateID(), DEFAULT, PATH_DEST_DIR + SEPARATOR + NAME_FILE_1);
    String conflictFilePath = conflict.getPath();
    RepositoryFileDto dtoConflictFile = mock(RepositoryFileDto.class);
    doReturn(dtoConflictFile).when(webService).getFile(eq(conflictFilePath));
    operation.execute();
    verify(repo, times(2)).createFile(eq(destFolder.getId()), any(RepositoryFile.class), any(IRepositoryFileData.class), any(RepositoryFileAcl.class), anyString());
    verify(repo, never()).createFolder(any(RepositoryFile.class), any(RepositoryFile.class), any(RepositoryFileAcl.class), anyString());
}
Also used : RepositoryFileDto(org.pentaho.platform.repository2.unified.webservices.RepositoryFileDto) IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) Test(org.junit.Test)

Example 49 with RepositoryFileDto

use of org.pentaho.platform.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 50 with RepositoryFileDto

use of org.pentaho.platform.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

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