Search in sources :

Example 1 with FileFolderService

use of org.alfresco.service.cmr.model.FileFolderService in project alfresco-remote-api by Alfresco.

the class AbstractWorkflowRestApiTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    ApplicationContext appContext = getServer().getApplicationContext();
    namespaceService = (NamespaceService) appContext.getBean("NamespaceService");
    workflowService = (WorkflowService) appContext.getBean("WorkflowService");
    MutableAuthenticationService authenticationService = (MutableAuthenticationService) appContext.getBean("AuthenticationService");
    PersonService personService = (PersonService) appContext.getBean("PersonService");
    SearchService searchService = (SearchService) appContext.getBean("SearchService");
    FileFolderService fileFolderService = (FileFolderService) appContext.getBean("FileFolderService");
    nodeService = (NodeService) appContext.getBean("NodeService");
    // for the purposes of the tests make sure workflow engine is enabled/visible.
    WorkflowAdminServiceImpl workflowAdminService = (WorkflowAdminServiceImpl) appContext.getBean("workflowAdminService");
    this.wfTestHelper = new WorkflowTestHelper(workflowAdminService, getEngine(), true);
    AuthorityService authorityService = (AuthorityService) appContext.getBean("AuthorityService");
    personManager = new TestPersonManager(authenticationService, personService, nodeService);
    groupManager = new TestGroupManager(authorityService);
    authenticationComponent = (AuthenticationComponent) appContext.getBean("authenticationComponent");
    dictionaryService = (DictionaryService) appContext.getBean("dictionaryService");
    personManager.createPerson(USER1);
    personManager.createPerson(USER2);
    personManager.createPerson(USER3);
    authenticationComponent.setSystemUserAsCurrentUser();
    groupManager.addUserToGroup(GROUP, USER2);
    packageRef = workflowService.createPackage(null);
    NodeRef companyHome = searchService.selectNodes(nodeService.getRootNode(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE), COMPANY_HOME, null, namespaceService, false).get(0);
    contentNodeRef = fileFolderService.create(companyHome, TEST_CONTENT + System.currentTimeMillis(), ContentModel.TYPE_CONTENT).getNodeRef();
    authenticationComponent.clearCurrentSecurityContext();
}
Also used : TestPersonManager(org.alfresco.repo.security.person.TestPersonManager) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ApplicationContext(org.springframework.context.ApplicationContext) PersonService(org.alfresco.service.cmr.security.PersonService) SearchService(org.alfresco.service.cmr.search.SearchService) AuthorityService(org.alfresco.service.cmr.security.AuthorityService) WorkflowAdminServiceImpl(org.alfresco.repo.workflow.WorkflowAdminServiceImpl) FileFolderService(org.alfresco.service.cmr.model.FileFolderService) MutableAuthenticationService(org.alfresco.service.cmr.security.MutableAuthenticationService) WorkflowTestHelper(org.alfresco.repo.workflow.WorkflowTestHelper) TestGroupManager(org.alfresco.repo.security.person.TestGroupManager)

Example 2 with FileFolderService

use of org.alfresco.service.cmr.model.FileFolderService in project alfresco-remote-api by Alfresco.

the class PutMethodTest method setUp.

@Before
public void setUp() throws Exception {
    searchService = ctx.getBean("SearchService", SearchService.class);
    fileFolderService = ctx.getBean("FileFolderService", FileFolderService.class);
    nodeService = ctx.getBean("NodeService", NodeService.class);
    transactionService = ctx.getBean("transactionService", TransactionService.class);
    webDAVHelper = ctx.getBean("webDAVHelper", WebDAVHelper.class);
    authenticationService = ctx.getBean("authenticationService", MutableAuthenticationService.class);
    personService = ctx.getBean("PersonService", PersonService.class);
    lockService = ctx.getBean("LockService", LockService.class);
    contentService = ctx.getBean("contentService", ContentService.class);
    checkOutCheckInService = ctx.getBean("CheckOutCheckInService", CheckOutCheckInService.class);
    permissionService = ctx.getBean("PermissionService", PermissionService.class);
    namespaceService = ctx.getBean("namespaceService", NamespaceService.class);
    actionService = ctx.getBean("ActionService", ActionService.class);
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    repositoryHelper = (Repository) ctx.getBean("repositoryHelper");
    companyHomeNodeRef = repositoryHelper.getCompanyHome();
    txn = transactionService.getUserTransaction();
    txn.begin();
    createUser(USER1_NAME);
    createUser(USER2_NAME);
    InputStream testDataIS = getClass().getClassLoader().getResourceAsStream(TEST_DATA_FILE_NAME);
    InputStream davLockInfoAdminIS = getClass().getClassLoader().getResourceAsStream(DAV_LOCK_INFO_ADMIN);
    InputStream davLockInfoUser2IS = getClass().getClassLoader().getResourceAsStream(DAV_LOCK_INFO_USER2);
    testDataFile = IOUtils.toByteArray(testDataIS);
    davLockInfoAdminFile = IOUtils.toByteArray(davLockInfoAdminIS);
    davLockInfoUser2File = IOUtils.toByteArray(davLockInfoUser2IS);
    testDataIS.close();
    davLockInfoAdminIS.close();
    davLockInfoUser2IS.close();
    // Create a test file with versionable aspect and content
    Map<QName, Serializable> properties = new HashMap<QName, Serializable>();
    versionableDocName = "doc-" + GUID.generate();
    properties.put(ContentModel.PROP_NAME, versionableDocName);
    versionableDoc = nodeService.createNode(companyHomeNodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName(ContentModel.USER_MODEL_URI, versionableDocName), ContentModel.TYPE_CONTENT, properties).getChildRef();
    contentService.getWriter(versionableDoc, ContentModel.PROP_CONTENT, true).putContent("WebDAVTestContent");
    nodeService.addAspect(versionableDoc, ContentModel.ASPECT_VERSIONABLE, null);
    txn.commit();
    txn = transactionService.getUserTransaction();
    txn.begin();
}
Also used : Serializable(java.io.Serializable) TransactionService(org.alfresco.service.transaction.TransactionService) LockService(org.alfresco.service.cmr.lock.LockService) HashMap(java.util.HashMap) InputStream(java.io.InputStream) QName(org.alfresco.service.namespace.QName) NodeService(org.alfresco.service.cmr.repository.NodeService) PersonService(org.alfresco.service.cmr.security.PersonService) FileFolderService(org.alfresco.service.cmr.model.FileFolderService) ContentService(org.alfresco.service.cmr.repository.ContentService) MutableAuthenticationService(org.alfresco.service.cmr.security.MutableAuthenticationService) PermissionService(org.alfresco.service.cmr.security.PermissionService) CheckOutCheckInService(org.alfresco.service.cmr.coci.CheckOutCheckInService) NamespaceService(org.alfresco.service.namespace.NamespaceService) SearchService(org.alfresco.service.cmr.search.SearchService) ActionService(org.alfresco.service.cmr.action.ActionService) Before(org.junit.Before)

Example 3 with FileFolderService

use of org.alfresco.service.cmr.model.FileFolderService in project alfresco-remote-api by Alfresco.

the class PutMethod method executeImpl.

/**
 * Execute the WebDAV request
 *
 * @exception WebDAVServerException
 */
protected void executeImpl() throws WebDAVServerException, Exception {
    if (logger.isDebugEnabled()) {
        String path = getPath();
        String userName = getDAVHelper().getAuthenticationService().getCurrentUserName();
        logger.debug("Put node: \n" + "     user: " + userName + "\n" + "     path: " + path + "\n" + "noContent: " + noContent);
    }
    FileFolderService fileFolderService = getFileFolderService();
    // Get the status for the request path
    LockInfo nodeLockInfo = null;
    try {
        contentNodeInfo = getNodeForPath(getRootNodeRef(), getPath());
        // make sure that we are not trying to use a folder
        if (contentNodeInfo.isFolder()) {
            throw new WebDAVServerException(HttpServletResponse.SC_BAD_REQUEST);
        }
        nodeLockInfo = checkNode(contentNodeInfo);
        // 'Unhide' nodes hidden by us and behave as though we created them
        NodeRef contentNodeRef = contentNodeInfo.getNodeRef();
        if (fileFolderService.isHidden(contentNodeRef) && !getDAVHelper().isRenameShuffle(getPath())) {
            fileFolderService.setHidden(contentNodeRef, false);
            created = true;
        }
    } catch (FileNotFoundException e) {
        // the file doesn't exist - create it
        String[] paths = getDAVHelper().splitPath(getPath());
        try {
            FileInfo parentNodeInfo = getNodeForPath(getRootNodeRef(), paths[0]);
            // create file
            contentNodeInfo = getDAVHelper().createFile(parentNodeInfo, paths[1]);
            created = true;
        } catch (FileNotFoundException ee) {
            // bad path
            throw new WebDAVServerException(HttpServletResponse.SC_CONFLICT);
        } catch (FileExistsException ee) {
            // ALF-7079 fix, retry: it looks like concurrent access (file not found but file exists)
            throw new ConcurrencyFailureException("Concurrent access was detected.", ee);
        }
    }
    String userName = getDAVHelper().getAuthenticationService().getCurrentUserName();
    LockInfo lockInfo = getDAVLockService().getLockInfo(contentNodeInfo.getNodeRef());
    if (lockInfo != null) {
        if (lockInfo.isLocked() && !lockInfo.getOwner().equals(userName)) {
            if (logger.isDebugEnabled()) {
                String path = getPath();
                String owner = lockInfo.getOwner();
                logger.debug("Node locked: path=[" + path + "], owner=[" + owner + "], current user=[" + userName + "]");
            }
            // Indicate that the resource is locked
            throw new WebDAVServerException(WebDAV.WEBDAV_SC_LOCKED);
        }
    }
    // ALF-16808: We disable the versionable aspect if we are overwriting
    // empty content because it's probably part of a compound operation to
    // create a new single version
    boolean disabledVersioning = false;
    try {
        // Disable versioning if we are overwriting an empty file with content
        NodeRef nodeRef = contentNodeInfo.getNodeRef();
        ContentData contentData = (ContentData) getNodeService().getProperty(nodeRef, ContentModel.PROP_CONTENT);
        if ((contentData == null || contentData.getSize() == 0) && getNodeService().hasAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE)) {
            getDAVHelper().getPolicyBehaviourFilter().disableBehaviour(nodeRef, ContentModel.ASPECT_VERSIONABLE);
            disabledVersioning = true;
        }
        // Access the content
        ContentWriter writer = fileFolderService.getWriter(contentNodeInfo.getNodeRef());
        // set content properties
        writer.guessMimetype(contentNodeInfo.getName());
        writer.guessEncoding();
        // Get the input stream from the request data
        InputStream is = m_request.getInputStream();
        // Write the new data to the content node
        writer.putContent(is);
        // - the node does not have any content (zero length binaries included)
        if (nodeLockInfo != null && nodeLockInfo.isExclusive() && !(ContentData.hasContent(contentData) && contentData.getSize() > 0)) {
            getNodeService().addAspect(contentNodeInfo.getNodeRef(), ContentModel.ASPECT_NO_CONTENT, null);
        }
        // Ask for the document metadata to be extracted
        Action extract = getActionService().createAction(ContentMetadataExtracter.EXECUTOR_NAME);
        if (extract != null) {
            extract.setExecuteAsynchronously(false);
            getActionService().executeAction(extract, contentNodeInfo.getNodeRef());
        }
        // from the original specified in the request, update it.
        if (m_strContentType == null || !m_strContentType.equals(writer.getMimetype())) {
            String oldMimeType = m_strContentType;
            m_strContentType = writer.getMimetype();
            if (logger.isDebugEnabled()) {
                logger.debug("Mimetype originally specified as " + oldMimeType + ", now guessed to be " + m_strContentType);
            }
        }
        // Record the uploaded file's size
        fileSize = writer.getSize();
        // Set the response status, depending if the node existed or not
        m_response.setStatus(created ? HttpServletResponse.SC_CREATED : HttpServletResponse.SC_NO_CONTENT);
    } catch (AccessDeniedException e) {
        throw new WebDAVServerException(HttpServletResponse.SC_FORBIDDEN, e);
    } catch (Throwable e) {
        // we are about to give up
        if (noContent && RetryingTransactionHelper.extractRetryCause(e) == null) {
            // remove the 0 bytes content if save operation failed or was cancelled
            final NodeRef nodeRef = contentNodeInfo.getNodeRef();
            getTransactionService().getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<String>() {

                public String execute() throws Throwable {
                    getNodeService().deleteNode(nodeRef);
                    if (logger.isDebugEnabled()) {
                        logger.debug("Put failed. DELETE  " + getPath());
                    }
                    return null;
                }
            }, false, false);
        }
        throw new WebDAVServerException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
    } finally {
        if (disabledVersioning) {
            getDAVHelper().getPolicyBehaviourFilter().enableBehaviour(contentNodeInfo.getNodeRef(), ContentModel.ASPECT_VERSIONABLE);
        }
    }
    postActivity();
}
Also used : Action(org.alfresco.service.cmr.action.Action) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) InputStream(java.io.InputStream) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) FileFolderService(org.alfresco.service.cmr.model.FileFolderService) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) ContentData(org.alfresco.service.cmr.repository.ContentData) FileInfo(org.alfresco.service.cmr.model.FileInfo) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) FileExistsException(org.alfresco.service.cmr.model.FileExistsException)

Example 4 with FileFolderService

use of org.alfresco.service.cmr.model.FileFolderService in project alfresco-remote-api by Alfresco.

the class WebDAVHelper method getPathFromNode.

/**
 * Return the relative path for the node walking back to the specified root node
 *
 * @param rootNodeRef the root below which the path will be valid
 * @param nodeRef the node's path to get
 * @return Returns string of form <b>/A/B/C</b> where C represents the from node and
 */
public final String getPathFromNode(NodeRef rootNodeRef, NodeRef nodeRef) throws FileNotFoundException {
    // Check if the nodes are valid, or equal
    if (rootNodeRef == null || nodeRef == null)
        throw new IllegalArgumentException("Invalid node(s) in getPathFromNode call");
    // short cut if the path node is the root node
    if (rootNodeRef.equals(nodeRef))
        return "";
    FileFolderService fileFolderService = getFileFolderService();
    // get the path elements
    List<String> pathInfos = fileFolderService.getNameOnlyPath(rootNodeRef, nodeRef);
    // build the path string
    StringBuilder sb = new StringBuilder(pathInfos.size() * 20);
    for (String fileInfo : pathInfos) {
        sb.append(WebDAVHelper.PathSeperatorChar);
        sb.append(fileInfo);
    }
    // done
    if (logger.isDebugEnabled()) {
        logger.debug("Build name path for node: \n" + "   root: " + rootNodeRef + "\n" + "   target: " + nodeRef + "\n" + "   path: " + sb);
    }
    return sb.toString();
}
Also used : FileFolderService(org.alfresco.service.cmr.model.FileFolderService)

Example 5 with FileFolderService

use of org.alfresco.service.cmr.model.FileFolderService in project alfresco-remote-api by Alfresco.

the class WebDAVHelper method getNodeForPath.

/**
 * Get the file info for the given paths
 *
 * @param rootNodeRef       the acting webdav root
 * @param path              the path to search for
 * @return                  Return the file info for the path
 * @throws FileNotFoundException
 *                          if the path doesn't refer to a valid node
 */
public FileInfo getNodeForPath(NodeRef rootNodeRef, String path) throws FileNotFoundException {
    if (rootNodeRef == null) {
        throw new IllegalArgumentException("Root node may not be null");
    } else if (path == null) {
        throw new IllegalArgumentException("Path may not be null");
    }
    FileFolderService fileFolderService = getFileFolderService();
    // Check for the root path
    if (path.length() == 0 || path.equals(PathSeperator)) {
        return fileFolderService.getFileInfo(rootNodeRef);
    }
    // split the paths up
    List<String> splitPath = splitAllPaths(path);
    // find it
    FileInfo fileInfo = m_fileFolderService.resolveNamePath(rootNodeRef, splitPath);
    String fileName = splitPath.get(splitPath.size() - 1);
    if (!fileInfo.getName().equals(fileName)) {
        throw new FileNotFoundException("Requested filename " + fileName + " does not match case of " + fileInfo.getName());
    }
    // done
    if (logger.isDebugEnabled()) {
        logger.debug("Fetched node for path: \n" + "   root: " + rootNodeRef + "\n" + "   path: " + path + "\n" + "   result: " + fileInfo);
    }
    return fileInfo;
}
Also used : FileInfo(org.alfresco.service.cmr.model.FileInfo) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) FileFolderService(org.alfresco.service.cmr.model.FileFolderService)

Aggregations

FileFolderService (org.alfresco.service.cmr.model.FileFolderService)15 NodeRef (org.alfresco.service.cmr.repository.NodeRef)11 FileInfo (org.alfresco.service.cmr.model.FileInfo)9 FileNotFoundException (org.alfresco.service.cmr.model.FileNotFoundException)7 NodeService (org.alfresco.service.cmr.repository.NodeService)7 SearchService (org.alfresco.service.cmr.search.SearchService)5 HashMap (java.util.HashMap)4 InputStream (java.io.InputStream)3 Serializable (java.io.Serializable)3 FileExistsException (org.alfresco.service.cmr.model.FileExistsException)3 QName (org.alfresco.service.namespace.QName)3 TransactionService (org.alfresco.service.transaction.TransactionService)3 Before (org.junit.Before)3 TimerTask (java.util.TimerTask)2 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)2 LockService (org.alfresco.service.cmr.lock.LockService)2 ContentService (org.alfresco.service.cmr.repository.ContentService)2 MutableAuthenticationService (org.alfresco.service.cmr.security.MutableAuthenticationService)2 PermissionService (org.alfresco.service.cmr.security.PermissionService)2 PersonService (org.alfresco.service.cmr.security.PersonService)2