Search in sources :

Example 1 with UnifiedRepositoryException

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

the class PentahoMetadataDomainRepository method loadProperties.

protected Properties loadProperties(final RepositoryFile bundle) {
    try {
        Properties properties = null;
        final SimpleRepositoryFileData bundleData = repository.getDataForRead(bundle.getId(), SimpleRepositoryFileData.class);
        if (bundleData != null) {
            properties = new Properties();
            properties.load(bundleData.getStream());
        } else {
            if (logger.isWarnEnabled()) {
                logger.warn("Could not load properties from repository file: " + bundle.getName());
            }
        }
        return properties;
    } catch (IOException e) {
        throw new UnifiedRepositoryException(messages.getErrorString("PentahoMetadataDomainRepository.ERROR_0008_ERROR_IN_REPOSITORY", e.getLocalizedMessage()), e);
    }
}
Also used : SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) IOException(java.io.IOException) Properties(java.util.Properties)

Example 2 with UnifiedRepositoryException

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

the class PentahoMetadataDomainRepository method getDomain.

/**
 * retrieve a domain from the repo. This does lazy loading of the repo, so it calls reloadDomains() if not already
 * loaded.
 *
 * @param domainId domain to get from the repository
 * @return domain object
 */
@Override
public Domain getDomain(final String domainId) {
    if (logger.isDebugEnabled()) {
        logger.debug("getDomain(" + domainId + ")");
    }
    if (StringUtils.isEmpty(domainId)) {
        throw new IllegalArgumentException(messages.getErrorString("PentahoMetadataDomainRepository.ERROR_0004_DOMAIN_ID_INVALID", domainId));
    }
    Domain domain = null;
    try {
        // Load the domain file
        final RepositoryFile file = getMetadataRepositoryFile(domainId);
        if (file != null) {
            if (hasAccessFor(file)) {
                SimpleRepositoryFileData data = repository.getDataForRead(file.getId(), SimpleRepositoryFileData.class);
                if (data != null) {
                    InputStream is = data.getStream();
                    try {
                        domain = xmiParser.parseXmi(is);
                    } finally {
                        IOUtils.closeQuietly(is);
                    }
                    domain.setId(domainId);
                    logger.debug("loaded domain");
                    // Load any I18N bundles
                    loadLocaleStrings(domainId, domain);
                    logger.debug("loaded I18N bundles");
                } else {
                    throw new UnifiedRepositoryException(messages.getErrorString("PentahoMetadataDomainRepository.ERROR_0005_ERROR_RETRIEVING_DOMAIN", domainId, "data not found"));
                }
            } else {
                throw new PentahoAccessControlException(messages.getErrorString("PentahoMetadataDomainRepository.ERROR_0005_ERROR_RETRIEVING_DOMAIN", domainId, "access denied"));
            }
        }
    } catch (Exception e) {
        if (!(e instanceof UnifiedRepositoryException || e instanceof PentahoAccessControlException)) {
            throw new UnifiedRepositoryException(messages.getErrorString("PentahoMetadataDomainRepository.ERROR_0005_ERROR_RETRIEVING_DOMAIN", domainId, e.getLocalizedMessage()), e);
        }
    }
    // Return
    return domain;
}
Also used : SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) ByteArrayInputStream(java.io.ByteArrayInputStream) RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream) InputStream(java.io.InputStream) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Domain(org.pentaho.metadata.model.Domain) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) IOException(java.io.IOException)

Example 3 with UnifiedRepositoryException

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

the class SchedulerResourceTest method testBlockoutWillFireError.

@Test
public void testBlockoutWillFireError() throws Exception {
    JobScheduleRequest mockJobScheduleRequest = mock(JobScheduleRequest.class);
    UnifiedRepositoryException mockUnifiedRepositoryException = mock(UnifiedRepositoryException.class);
    SchedulerException mockSchedulerException = mock(SchedulerException.class);
    Response mockUnifiedRepositoryExceptionResponse = mock(Response.class);
    doReturn(mockUnifiedRepositoryExceptionResponse).when(schedulerResource).buildServerErrorResponse(mockUnifiedRepositoryException);
    Response mockSchedulerExceptionResponse = mock(Response.class);
    doReturn(mockSchedulerExceptionResponse).when(schedulerResource).buildServerErrorResponse(mockSchedulerException);
    // Test 1
    doThrow(mockUnifiedRepositoryException).when(schedulerResource).convertScheduleRequestToJobTrigger(mockJobScheduleRequest);
    Response testResponse = schedulerResource.blockoutWillFire(mockJobScheduleRequest);
    assertEquals(mockUnifiedRepositoryExceptionResponse, testResponse);
    // Test 2
    doThrow(mockSchedulerException).when(schedulerResource).convertScheduleRequestToJobTrigger(mockJobScheduleRequest);
    testResponse = schedulerResource.blockoutWillFire(mockJobScheduleRequest);
    assertEquals(mockSchedulerExceptionResponse, testResponse);
    verify(schedulerResource, times(1)).buildServerErrorResponse(mockUnifiedRepositoryException);
    verify(schedulerResource, times(1)).buildServerErrorResponse(mockSchedulerException);
    verify(schedulerResource, times(2)).convertScheduleRequestToJobTrigger(mockJobScheduleRequest);
}
Also used : Response(javax.ws.rs.core.Response) SchedulerException(org.pentaho.platform.api.scheduler2.SchedulerException) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) Test(org.junit.Test)

Example 4 with UnifiedRepositoryException

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

the class JcrRepositoryFileDao method internalCopyOrMove.

private void internalCopyOrMove(final Serializable fileId, final String destRelPath, final String versionMessage, final boolean copy) {
    if (isKioskEnabled()) {
        // $NON-NLS-1$
        throw new RuntimeException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED"));
    }
    Assert.notNull(fileId);
    jcrTemplate.execute(new JcrCallback() {

        @Override
        public Object doInJcr(final Session session) throws RepositoryException, IOException {
            // if we're moving the file,
            // check that user has permissions to remove the file from it's current location
            RepositoryFile file = getFileById(fileId);
            if (!copy) {
                RepositoryFileAcl acl = aclDao.getAcl(fileId);
                if (!accessVoterManager.hasAccess(file, RepositoryFilePermission.WRITE, acl, PentahoSessionHolder.getSession())) {
                    throw new AccessDeniedException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED_DELETE", fileId));
                }
            }
            // check that user has permissions to write to the destination folder
            RepositoryFile destFolder = getFile(destRelPath);
            if (destFolder != null) {
                RepositoryFileAcl destFolderAcl = aclDao.getAcl(destFolder.getId());
                if (!accessVoterManager.hasAccess(destFolder, RepositoryFilePermission.WRITE, destFolderAcl, PentahoSessionHolder.getSession())) {
                    throw new AccessDeniedException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED_CREATE", destFolder.getId()));
                }
            }
            PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(session);
            String destAbsPath = pathConversionHelper.relToAbs(destRelPath);
            String cleanDestAbsPath = destAbsPath;
            if (cleanDestAbsPath.endsWith(RepositoryFile.SEPARATOR)) {
                cleanDestAbsPath.substring(0, cleanDestAbsPath.length() - 1);
            }
            Node srcFileNode = session.getNodeByIdentifier(fileId.toString());
            Serializable srcParentFolderId = JcrRepositoryFileUtils.getParentId(session, fileId);
            boolean appendFileName = false;
            boolean destExists = true;
            Node destFileNode = null;
            Node destParentFolderNode = null;
            try {
                destFileNode = (Node) session.getItem(JcrStringHelper.pathEncode(cleanDestAbsPath));
            } catch (PathNotFoundException e) {
                destExists = false;
            }
            if (destExists) {
                // make sure it's a file or folder
                Assert.isTrue(JcrRepositoryFileUtils.isSupportedNodeType(pentahoJcrConstants, destFileNode));
                // existing item; make sure src is not a folder if dest is a file
                Assert.isTrue(!(JcrRepositoryFileUtils.isPentahoFolder(pentahoJcrConstants, srcFileNode) && JcrRepositoryFileUtils.isPentahoFile(pentahoJcrConstants, destFileNode)), Messages.getInstance().getString(// $NON-NLS-1$
                "JcrRepositoryFileDao.ERROR_0002_CANNOT_OVERWRITE_FILE_WITH_FOLDER"));
                if (JcrRepositoryFileUtils.isPentahoFolder(pentahoJcrConstants, destFileNode)) {
                    // existing item; caller is not renaming file, only moving it
                    appendFileName = true;
                    destParentFolderNode = destFileNode;
                } else {
                    // get parent of existing dest item
                    int lastSlashIndex = cleanDestAbsPath.lastIndexOf(RepositoryFile.SEPARATOR);
                    Assert.isTrue(lastSlashIndex > 1, Messages.getInstance().getString(// $NON-NLS-1$
                    "JcrRepositoryFileDao.ERROR_0003_ILLEGAL_DEST_PATH"));
                    String absPathToDestParentFolder = cleanDestAbsPath.substring(0, lastSlashIndex);
                    destParentFolderNode = (Node) session.getItem(JcrStringHelper.pathEncode(absPathToDestParentFolder));
                }
            } else {
                // destination doesn't exist; go up one level to a folder that does exist
                int lastSlashIndex = cleanDestAbsPath.lastIndexOf(RepositoryFile.SEPARATOR);
                Assert.isTrue(lastSlashIndex > 1, Messages.getInstance().getString(// $NON-NLS-1$
                "JcrRepositoryFileDao.ERROR_0003_ILLEGAL_DEST_PATH"));
                String absPathToDestParentFolder = cleanDestAbsPath.substring(0, lastSlashIndex);
                // JcrRepositoryFileUtils.checkName( cleanDestAbsPath.substring( lastSlashIndex + 1 ) );
                try {
                    destParentFolderNode = (Node) session.getItem(JcrStringHelper.pathEncode(absPathToDestParentFolder));
                } catch (PathNotFoundException e1) {
                    Assert.isTrue(false, Messages.getInstance().getString(// $NON-NLS-1$
                    "JcrRepositoryFileDao.ERROR_0004_PARENT_MUST_EXIST"));
                }
                Assert.isTrue(JcrRepositoryFileUtils.isPentahoFolder(pentahoJcrConstants, destParentFolderNode), Messages.getInstance().getString(// $NON-NLS-1$
                "JcrRepositoryFileDao.ERROR_0005_PARENT_MUST_BE_FOLDER"));
            }
            if (!copy) {
                JcrRepositoryFileUtils.checkoutNearestVersionableFileIfNecessary(session, pentahoJcrConstants, srcParentFolderId);
            }
            JcrRepositoryFileUtils.checkoutNearestVersionableNodeIfNecessary(session, pentahoJcrConstants, destParentFolderNode);
            String finalEncodedSrcAbsPath = srcFileNode.getPath();
            String finalEncodedDestAbsPath = null;
            if (appendFileName) {
                final String fileName = srcFileNode.getName();
                if (JcrStringHelper.isEncoded(fileName)) {
                    finalEncodedDestAbsPath = JcrStringHelper.pathEncode(cleanDestAbsPath) + RepositoryFile.SEPARATOR + fileName;
                } else {
                    finalEncodedDestAbsPath = JcrStringHelper.pathEncode(cleanDestAbsPath + RepositoryFile.SEPARATOR + fileName);
                }
            } else {
                finalEncodedDestAbsPath = JcrStringHelper.pathEncode(cleanDestAbsPath);
            }
            try {
                if (copy) {
                    session.getWorkspace().copy(finalEncodedSrcAbsPath, finalEncodedDestAbsPath);
                } else {
                    session.getWorkspace().move(finalEncodedSrcAbsPath, finalEncodedDestAbsPath);
                }
            } catch (ItemExistsException iae) {
                throw new UnifiedRepositoryException((file.isFolder() ? "Folder " : "File ") + "with path [" + cleanDestAbsPath + "] already exists in the repository");
            }
            JcrRepositoryFileUtils.checkinNearestVersionableNodeIfNecessary(session, pentahoJcrConstants, destParentFolderNode, versionMessage);
            // if it's a move within the same folder, then the next checkin is unnecessary
            if (!copy && !destParentFolderNode.getIdentifier().equals(srcParentFolderId.toString())) {
                JcrRepositoryFileUtils.checkinNearestVersionableFileIfNecessary(session, pentahoJcrConstants, srcParentFolderId, versionMessage);
            }
            session.save();
            return null;
        }
    });
}
Also used : AccessDeniedException(javax.jcr.AccessDeniedException) Serializable(java.io.Serializable) DataNode(org.pentaho.platform.api.repository2.unified.data.node.DataNode) Node(javax.jcr.Node) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) JcrCallback(org.springframework.extensions.jcr.JcrCallback) ItemExistsException(javax.jcr.ItemExistsException) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) Session(javax.jcr.Session)

Example 5 with UnifiedRepositoryException

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

the class FileSystemRepositoryFileDao method createFolder.

public RepositoryFile createFolder(Serializable parentFolderId, RepositoryFile file, RepositoryFileAcl acl, String versionMessage) {
    try {
        String folderNameWithPath = parentFolderId + "/" + file.getName();
        File newFolder = new File(folderNameWithPath);
        newFolder.mkdir();
        final RepositoryFile repositoryFolder = internalGetFile(newFolder);
        return repositoryFolder;
    } catch (Throwable th) {
        throw new UnifiedRepositoryException();
    }
}
Also used : UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) File(java.io.File) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile)

Aggregations

UnifiedRepositoryException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException)31 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)26 Test (org.junit.Test)12 ITenant (org.pentaho.platform.api.mt.ITenant)9 IOException (java.io.IOException)7 DatasourceMgmtServiceException (org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException)7 NodeRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.node.NodeRepositoryFileData)7 File (java.io.File)6 FileNotFoundException (java.io.FileNotFoundException)6 SimpleRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Matchers.anyString (org.mockito.Matchers.anyString)5 RepositoryFileAcl (org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)5 RepositoryFileSid (org.pentaho.platform.api.repository2.unified.RepositoryFileSid)4 FileOutputStream (java.io.FileOutputStream)3 Serializable (java.io.Serializable)3 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2