Search in sources :

Example 11 with FileFolderService

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

the class LockMethod method attemptLock.

/**
 * The main lock implementation method.
 *
 * @throws WebDAVServerException
 * @throws Exception
 */
protected void attemptLock() throws WebDAVServerException, Exception {
    FileFolderService fileFolderService = getFileFolderService();
    final String path = getPath();
    NodeRef rootNodeRef = getRootNodeRef();
    // Get the active user
    final String userName = getDAVHelper().getAuthenticationService().getCurrentUserName();
    if (logger.isDebugEnabled()) {
        logger.debug("Locking node: \n" + "   user: " + userName + "\n" + "   path: " + path);
    }
    FileInfo lockNodeInfo = null;
    try {
        // Check if the path exists
        lockNodeInfo = getNodeForPath(getRootNodeRef(), getPath());
    } catch (FileNotFoundException e) {
        if (m_conditions != null) {
            // MNT-12303 fix, check whether this is a refresh lock request
            for (Condition condition : m_conditions) {
                List<String> lockTolensMatch = condition.getLockTokensMatch();
                List<String> etagsMatch = condition.getETagsMatch();
                if (m_request.getContentLength() == -1 && (lockTolensMatch != null && !lockTolensMatch.isEmpty()) || (etagsMatch != null && !etagsMatch.isEmpty())) {
                    // so there is nothing to refresh. Return 403 Forbidden as original SharePoint Server.
                    throw new WebDAVServerException(HttpServletResponse.SC_FORBIDDEN);
                }
            }
        }
        // need to create it
        String[] splitPath = getDAVHelper().splitPath(path);
        // check
        if (splitPath[1].length() == 0) {
            throw new WebDAVServerException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
        FileInfo dirInfo = null;
        List<String> dirPathElements = getDAVHelper().splitAllPaths(splitPath[0]);
        if (dirPathElements.size() == 0) {
            // if there are no path elements we are at the root so get the root node
            dirInfo = fileFolderService.getFileInfo(getRootNodeRef());
        } else {
            // make sure folder structure is present
            dirInfo = FileFolderUtil.makeFolders(fileFolderService, rootNodeRef, dirPathElements, ContentModel.TYPE_FOLDER);
        }
        if (dirInfo == null) {
            throw new WebDAVServerException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
        // create the file
        lockNodeInfo = createNode(dirInfo.getNodeRef(), splitPath[1], ContentModel.TYPE_CONTENT);
        // ALF-10309 fix, mark created node with webdavNoContent aspect, we assume that save operation
        // is performed by client, webdavNoContent aspect normally removed in put method unless there
        // is a cancel before the PUT request takes place
        int lockTimeout = getLockTimeout();
        if (lockTimeout > 0 && !getNodeService().hasAspect(lockNodeInfo.getNodeRef(), ContentModel.ASPECT_WEBDAV_NO_CONTENT)) {
            final NodeRef nodeRef = lockNodeInfo.getNodeRef();
            getNodeService().addAspect(nodeRef, ContentModel.ASPECT_WEBDAV_NO_CONTENT, null);
            // Remove node after the timeout (MS Office 2003 requests 3 minutes) if the PUT or UNLOCK has not taken place
            timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    // run as current user
                    AuthenticationUtil.runAs(new RunAsWork<Void>() {

                        @Override
                        public Void doWork() throws Exception {
                            try {
                                if (getNodeService().hasAspect(nodeRef, ContentModel.ASPECT_WEBDAV_NO_CONTENT)) {
                                    getTransactionService().getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<String>() {

                                        public String execute() throws Throwable {
                                            getNodeService().deleteNode(nodeRef);
                                            if (logger.isDebugEnabled()) {
                                                logger.debug("Timer DELETE " + path);
                                            }
                                            return null;
                                        }
                                    }, false, true);
                                } else if (logger.isDebugEnabled()) {
                                    logger.debug("Timer IGNORE " + path);
                                }
                            } catch (InvalidNodeRefException e) {
                                // Might get this if the node is deleted. If so just ignore.
                                if (logger.isDebugEnabled()) {
                                    logger.debug("Timer DOES NOT EXIST " + path);
                                }
                            }
                            return null;
                        }
                    }, userName);
                }
            }, lockTimeout * 1000);
            if (logger.isDebugEnabled()) {
                logger.debug("Timer START in " + lockTimeout + " seconds " + path);
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Created new node for lock: \n" + "   path: " + path + "\n" + "   node: " + lockNodeInfo);
        }
        m_response.setStatus(HttpServletResponse.SC_CREATED);
    }
    // Check if this is a new lock or a lock refresh
    if (hasLockToken()) {
        lockInfo = checkNode(lockNodeInfo);
        if (!lockInfo.isLocked() && m_request.getContentLength() == -1) {
            // see http://www.ics.uci.edu/~ejw/authoring/protocol/rfc2518.html#rfc.section.7.8
            throw new WebDAVServerException(HttpServletResponse.SC_BAD_REQUEST);
        }
        // If a request body is not defined and "If" header is sent we have createExclusive as false,
        // but we need to check a previous LOCK was an exclusive. I.e. get the property for node. It
        // is already has got in a checkNode method, so we need just get a scope from lockInfo.
        // This particular case was raised as ALF-4008.
        this.createExclusive = WebDAV.XML_EXCLUSIVE.equals(this.lockInfo.getScope());
        // Refresh an existing lock
        refreshLock(lockNodeInfo, userName);
    } else {
        lockInfo = checkNode(lockNodeInfo, true, createExclusive);
        // Create a new lock
        createLock(lockNodeInfo, userName);
    }
    m_response.setHeader(WebDAV.HEADER_LOCK_TOKEN, "<" + WebDAV.makeLockToken(lockNodeInfo.getNodeRef(), userName) + ">");
    m_response.setHeader(WebDAV.HEADER_CONTENT_TYPE, WebDAV.XML_CONTENT_TYPE);
    // We either created a new lock or refreshed an existing lock, send back the lock details
    generateResponse(lockNodeInfo, userName);
}
Also used : FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) FileFolderService(org.alfresco.service.cmr.model.FileFolderService) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) FileInfo(org.alfresco.service.cmr.model.FileInfo) TimerTask(java.util.TimerTask) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) List(java.util.List) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException)

Example 12 with FileFolderService

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

the class MkcolMethod method executeImpl.

/**
 * Execute the request
 *
 * @exception WebDAVServerException
 */
protected void executeImpl() throws WebDAVServerException, Exception {
    FileFolderService fileFolderService = getFileFolderService();
    // see if it exists
    try {
        getDAVHelper().getNodeForPath(getRootNodeRef(), getPath());
        // already exists
        throw new WebDAVServerException(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
    } catch (FileNotFoundException e) {
    // it doesn't exist
    }
    // Trim the last path component and check if the parent path exists
    String parentPath = getPath();
    int lastPos = parentPath.lastIndexOf(WebDAVHelper.PathSeperator);
    NodeRef parentNodeRef = null;
    if (lastPos == 0) {
        // Create new folder at root
        parentPath = WebDAVHelper.PathSeperator;
        parentNodeRef = getRootNodeRef();
    } else if (lastPos != -1) {
        // Trim the last path component
        parentPath = parentPath.substring(0, lastPos + 1);
        try {
            FileInfo parentFileInfo = getDAVHelper().getNodeForPath(getRootNodeRef(), parentPath);
            parentNodeRef = parentFileInfo.getNodeRef();
        } catch (FileNotFoundException e) {
            // parent path is missing
            throw new WebDAVServerException(HttpServletResponse.SC_CONFLICT);
        }
    } else {
        // Looks like a bad path
        throw new WebDAVServerException(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
    }
    // Get the new folder name
    String folderName = getPath().substring(lastPos + 1);
    // Create the new folder node
    FileInfo fileInfo = fileFolderService.create(parentNodeRef, folderName, ContentModel.TYPE_FOLDER);
    // Don't post activity data for hidden folder
    if (!fileInfo.isHidden()) {
        postActivity(fileInfo);
    }
    // Return a success status
    m_response.setStatus(HttpServletResponse.SC_CREATED);
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) FileInfo(org.alfresco.service.cmr.model.FileInfo) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) FileFolderService(org.alfresco.service.cmr.model.FileFolderService)

Example 13 with FileFolderService

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

the class MoveMethodTest method setUp.

@Before
public void setUp() throws Exception {
    req = new MockHttpServletRequest();
    resp = new MockHttpServletResponse();
    rootNode = new NodeRef("workspace://SpacesStore/node1");
    moveMethod = new MoveMethod() {

        @Override
        protected LockInfo checkNode(FileInfo fileInfo, boolean ignoreShared, boolean lockMethod) throws WebDAVServerException {
            return new LockInfoImpl();
        }

        @Override
        protected LockInfo checkNode(FileInfo fileInfo) throws WebDAVServerException {
            return new LockInfoImpl();
        }
    };
    moveMethod.setDetails(req, resp, davHelper, rootNode);
    sourceFileInfo = Mockito.mock(FileInfo.class);
    when(sourceFileInfo.isFolder()).thenReturn(true);
    destPath = "/path/to/dest.doc";
    moveMethod.m_strDestinationPath = destPath;
    sourcePath = "/path/to/source.doc";
    moveMethod.m_strPath = sourcePath;
    when(davHelper.getFileFolderService()).thenReturn(mockFileFolderService);
    List<String> sourcePathSplit = Arrays.asList("path", "to", "source.doc");
    when(davHelper.splitAllPaths(sourcePath)).thenReturn(sourcePathSplit);
    List<String> destPathSplit = Arrays.asList("path", "to", "dest.doc");
    when(davHelper.splitAllPaths(destPath)).thenReturn(destPathSplit);
    when(mockFileFolderService.resolveNamePath(rootNode, sourcePathSplit)).thenReturn(sourceFileInfo);
    FileInfo destFileInfo = Mockito.mock(FileInfo.class);
    when(mockFileFolderService.resolveNamePath(rootNode, destPathSplit)).thenReturn(destFileInfo);
    sourceParentNodeRef = new NodeRef("workspace://SpacesStore/parent");
    destParentNodeRef = new NodeRef("workspace://SpacesStore/parent");
    sourceNodeRef = new NodeRef("workspace://SpacesStore/sourcefile");
    when(davHelper.getLockService()).thenReturn(davLockService);
    searchService = ctx.getBean("SearchService", SearchService.class);
    fileFolderService = ctx.getBean("FileFolderService", FileFolderService.class);
    nodeService = ctx.getBean("NodeService", NodeService.class);
    transactionService = ctx.getBean("transactionService", TransactionService.class);
    contentService = ctx.getBean("contentService", ContentService.class);
    webDAVHelper = ctx.getBean("webDAVHelper", WebDAVHelper.class);
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    repositoryHelper = (Repository) ctx.getBean("repositoryHelper");
    companyHomeNodeRef = repositoryHelper.getCompanyHome();
}
Also used : TransactionService(org.alfresco.service.transaction.TransactionService) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) NodeService(org.alfresco.service.cmr.repository.NodeService) FileFolderService(org.alfresco.service.cmr.model.FileFolderService) ContentService(org.alfresco.service.cmr.repository.ContentService) NodeRef(org.alfresco.service.cmr.repository.NodeRef) FileInfo(org.alfresco.service.cmr.model.FileInfo) SearchService(org.alfresco.service.cmr.search.SearchService) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Before(org.junit.Before)

Example 14 with FileFolderService

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

the class WebDAVonContentUpdateTest 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);
    lockService = ctx.getBean("LockService", LockService.class);
    policyComponent = ctx.getBean("policyComponent", PolicyComponent.class);
    namespaceService = ctx.getBean("namespaceService", NamespaceService.class);
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    repositoryHelper = (Repository) ctx.getBean("repositoryHelper");
    companyHomeNodeRef = repositoryHelper.getCompanyHome();
    InputStream testDataIS = getClass().getClassLoader().getResourceAsStream(TEST_DATA_FILE_NAME);
    InputStream davLockInfoIS = getClass().getClassLoader().getResourceAsStream(DAV_LOCK_INFO_XML);
    testDataFile = IOUtils.toByteArray(testDataIS);
    davLockInfoFile = IOUtils.toByteArray(davLockInfoIS);
    testDataIS.close();
    davLockInfoIS.close();
    txn = transactionService.getUserTransaction();
    txn.begin();
}
Also used : PolicyComponent(org.alfresco.repo.policy.PolicyComponent) TransactionService(org.alfresco.service.transaction.TransactionService) LockService(org.alfresco.service.cmr.lock.LockService) NamespaceService(org.alfresco.service.namespace.NamespaceService) InputStream(java.io.InputStream) SearchService(org.alfresco.service.cmr.search.SearchService) NodeService(org.alfresco.service.cmr.repository.NodeService) FileFolderService(org.alfresco.service.cmr.model.FileFolderService) Before(org.junit.Before)

Example 15 with FileFolderService

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

the class TestEnterpriseAtomPubTCK method setup.

@Before
public void setup() throws Exception {
    JettyComponent jetty = getTestFixture().getJettyComponent();
    final SearchService searchService = (SearchService) jetty.getApplicationContext().getBean("searchService");
    ;
    final NodeService nodeService = (NodeService) jetty.getApplicationContext().getBean("nodeService");
    final FileFolderService fileFolderService = (FileFolderService) jetty.getApplicationContext().getBean("fileFolderService");
    final NamespaceService namespaceService = (NamespaceService) jetty.getApplicationContext().getBean("namespaceService");
    final TransactionService transactionService = (TransactionService) jetty.getApplicationContext().getBean("transactionService");
    final String name = "abc" + System.currentTimeMillis();
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
            Repository repositoryHelper = (Repository) jetty.getApplicationContext().getBean("repositoryHelper");
            NodeRef companyHome = repositoryHelper.getCompanyHome();
            fileFolderService.create(companyHome, name, ContentModel.TYPE_FOLDER).getNodeRef();
            return null;
        }
    }, false, true);
    int port = jetty.getPort();
    Map<String, String> cmisParameters = new HashMap<String, String>();
    cmisParameters.put(TestParameters.DEFAULT_RELATIONSHIP_TYPE, "R:cm:replaces");
    cmisParameters.put(TestParameters.DEFAULT_TEST_FOLDER_PARENT, "/" + name);
    clientContext = new OpenCMISClientContext(BindingType.ATOMPUB, MessageFormat.format(CMIS_URL, "localhost", String.valueOf(port), "alfresco"), "admin", "admin", cmisParameters, jetty.getApplicationContext());
    overrideVersionableAspectProperties(jetty.getApplicationContext());
}
Also used : TransactionService(org.alfresco.service.transaction.TransactionService) HashMap(java.util.HashMap) NodeService(org.alfresco.service.cmr.repository.NodeService) FileFolderService(org.alfresco.service.cmr.model.FileFolderService) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Repository(org.alfresco.repo.model.Repository) NamespaceService(org.alfresco.service.namespace.NamespaceService) SearchService(org.alfresco.service.cmr.search.SearchService) JettyComponent(org.alfresco.repo.web.util.JettyComponent) OpenCMISClientContext(org.alfresco.opencmis.OpenCMISClientContext) Before(org.junit.Before)

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