Search in sources :

Example 56 with RetryingTransactionCallback

use of org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback in project alfresco-remote-api by Alfresco.

the class RemoteFileFolderLoaderTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    this.repositoryHelper = (Repository) getServer().getApplicationContext().getBean("repositoryHelper");
    this.nodeService = (NodeService) getServer().getApplicationContext().getBean("nodeService");
    this.transactionService = (TransactionService) getServer().getApplicationContext().getBean("TransactionService");
    this.fileFolderService = (FileFolderService) getServer().getApplicationContext().getBean("FileFolderService");
    // Get the path of the shared folder home
    final NodeRef companyHomeNodeRef = repositoryHelper.getCompanyHome();
    final NodeRef sharedHomeNodeRef = repositoryHelper.getSharedHome();
    RetryingTransactionCallback<NodeRef> createFolderWork = new RetryingTransactionCallback<NodeRef>() {

        @Override
        public NodeRef execute() throws Throwable {
            List<FileInfo> sharedHomeFileInfos = fileFolderService.getNamePath(companyHomeNodeRef, sharedHomeNodeRef);
            sharedHomePath = "/" + sharedHomeFileInfos.get(0).getName();
            String folderName = UUID.randomUUID().toString();
            // Create a folder
            FileInfo folderInfo = fileFolderService.create(sharedHomeNodeRef, folderName, ContentModel.TYPE_FOLDER);
            loadHomePath = sharedHomePath + "/" + folderName;
            // Done
            return folderInfo.getNodeRef();
        }
    };
    // Will be cleared later
    AuthenticationUtil.pushAuthentication();
    AuthenticationUtil.setAdminUserAsFullyAuthenticatedUser();
    loadHomeNodeRef = transactionService.getRetryingTransactionHelper().doInTransaction(createFolderWork);
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) FileInfo(org.alfresco.service.cmr.model.FileInfo) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)

Example 57 with RetryingTransactionCallback

use of org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback in project alfresco-remote-api by Alfresco.

the class LockMethodTest method testRefreshLock.

@Test
public void testRefreshLock() throws Exception {
    MockHttpServletRequest lockRequest = new MockHttpServletRequest();
    lockRequest.addHeader(WebDAV.HEADER_TIMEOUT, WebDAV.SECOND + 5);
    lockRequest.setRequestURI("/" + TEST_FILE_NAME);
    String content = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><D:lockinfo xmlns:D=\"DAV:\"><D:lockscope xmlns:D=\"DAV:\">" + "<D:exclusive xmlns:D=\"DAV:\"/></D:lockscope><D:locktype xmlns:D=\"DAV:\"><D:write xmlns:D=\"DAV:\"/></D:locktype>" + "<D:owner xmlns:D=\"DAV:\">" + userName + "</D:owner></D:lockinfo>";
    lockRequest.setContent(content.getBytes("UTF-8"));
    lockMethod.setDetails(lockRequest, new MockHttpServletResponse(), davHelper, folderNodeRef);
    lockMethod.parseRequestHeaders();
    lockMethod.parseRequestBody();
    RetryingTransactionCallback<Void> lockExecuteImplCallBack = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            lockMethod.executeImpl();
            return null;
        }
    };
    // lock node for 5 seconds
    this.transactionService.getRetryingTransactionHelper().doInTransaction(lockExecuteImplCallBack);
    RetryingTransactionCallback<LockInfo> getNodeLockInfoCallBack = new RetryingTransactionCallback<LockInfo>() {

        @Override
        public LockInfo execute() throws Throwable {
            return lockMethod.getNodeLockInfo(fileFolderService.getFileInfo(fileNodeRef));
        }
    };
    // get lock info
    LockInfo lockInfo = this.transactionService.getRetryingTransactionHelper().doInTransaction(getNodeLockInfoCallBack);
    assertNotNull(lockInfo);
    // wait for 1 second
    Thread.sleep(1000);
    String lockToken = fileNodeRef.getId() + WebDAV.LOCK_TOKEN_SEPERATOR + this.userName;
    String lockHeaderValue = "(<" + WebDAV.OPAQUE_LOCK_TOKEN + lockToken + ">)";
    lockRequest.addHeader(WebDAV.HEADER_IF, lockHeaderValue);
    lockMethod.parseRequestHeaders();
    // update lock for another 5 seconds
    this.transactionService.getRetryingTransactionHelper().doInTransaction(lockExecuteImplCallBack);
    // get updated lock info
    LockInfo updatedLockInfo = this.transactionService.getRetryingTransactionHelper().doInTransaction(getNodeLockInfoCallBack);
    assertNotNull(updatedLockInfo);
    assertEquals("Lock owner should not change after lock refresh.", lockInfo.getOwner(), updatedLockInfo.getOwner());
    assertEquals("Lock token should not change after lock refresh.", lockInfo.getExclusiveLockToken(), updatedLockInfo.getExclusiveLockToken());
    assertFalse("Expires was not updated.", lockInfo.getExpires().equals(updatedLockInfo.getExpires()));
    assertTrue("Expires was updated incorrectly.", lockInfo.getExpires().before(updatedLockInfo.getExpires()));
    // prepare propfind method
    MockHttpServletRequest propFindRequest = new MockHttpServletRequest();
    propFindRequest.setRequestURI("/" + TEST_FILE_NAME);
    propFindRequest.addHeader(WebDAV.HEADER_DEPTH, "1");
    content = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><D:propfind xmlns:D=\"DAV:\"><D:prop><D:getlastmodified/>" + "<D:getcontentlength/><D:resourcetype/><D:supportedlock/><D:lockdiscovery/></D:prop></D:propfind>";
    propFindRequest.setContent(content.getBytes("UTF-8"));
    MockHttpServletResponse propfindResponse = new MockHttpServletResponse();
    propFindMethod.setDetails(propFindRequest, propfindResponse, davHelper, folderNodeRef);
    propFindMethod.parseRequestHeaders();
    propFindMethod.parseRequestBody();
    RetryingTransactionCallback<Void> propfindExecuteImplCallBack = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            propFindMethod.executeImpl();
            return null;
        }
    };
    // make propfind method call for locked resource
    this.transactionService.getRetryingTransactionHelper().doInTransaction(propfindExecuteImplCallBack);
    String response = propfindResponse.getContentAsString();
    assertFalse("Propfind response should contain lock informarion.", response.indexOf("lockdiscovery") == -1);
    // wait for lock expiration
    Thread.sleep(6000);
    propfindResponse = new MockHttpServletResponse();
    propFindMethod.setDetails(propFindRequest, propfindResponse, davHelper, folderNodeRef);
    // make another propfind call on resource with expired lock
    this.transactionService.getRetryingTransactionHelper().doInTransaction(propfindExecuteImplCallBack);
    response = propfindResponse.getContentAsString();
    // check that lock information is not there for expired lock
    assertTrue("Propfind response should not conatain information about expired lock", response.indexOf("lockdiscovery") == -1);
}
Also used : RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 58 with RetryingTransactionCallback

use of org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback in project alfresco-remote-api by Alfresco.

the class LockMethodTest method setUp.

@Before
public void setUp() throws Exception {
    lockMethod = new LockMethod();
    propFindMethod = new PropFindMethod();
    this.transactionService = (TransactionService) applicationContext.getBean("TransactionService");
    this.davHelper = (WebDAVHelper) applicationContext.getBean("webDAVHelper");
    this.authenticationComponent = (AuthenticationComponent) applicationContext.getBean("authenticationComponent");
    this.nodeService = (NodeService) applicationContext.getBean("NodeService");
    this.authenticationService = (MutableAuthenticationService) applicationContext.getBean("authenticationService");
    this.permissionService = (PermissionService) applicationContext.getBean("permissionService");
    this.contentService = (ContentService) applicationContext.getBean("contentService");
    this.fileFolderService = (FileFolderService) applicationContext.getBean("fileFolderService");
    this.authenticationComponent.setSystemUserAsCurrentUser();
    RetryingTransactionCallback<Void> createTestFileCallback = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            NodeRef rootNodeRef = nodeService.getRootNode(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE);
            // Create and authenticate the user
            userName = "webdavLockTest" + GUID.generate();
            TestWithUserUtils.createUser(userName, PWD, rootNodeRef, nodeService, authenticationService);
            permissionService.setPermission(rootNodeRef, userName, PermissionService.ALL_PERMISSIONS, true);
            TestWithUserUtils.authenticateUser(userName, PWD, rootNodeRef, authenticationService);
            // create test file in test folder
            folderNodeRef = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("test"), ContentModel.TYPE_FOLDER, Collections.<QName, Serializable>singletonMap(ContentModel.PROP_NAME, TEST_FOLDER_NAME)).getChildRef();
            fileNodeRef = nodeService.createNode(folderNodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName("test"), ContentModel.TYPE_CONTENT, Collections.<QName, Serializable>singletonMap(ContentModel.PROP_NAME, TEST_FILE_NAME)).getChildRef();
            ContentWriter contentWriter = contentService.getWriter(fileNodeRef, ContentModel.PROP_CONTENT, true);
            contentWriter.setMimetype("text/plain");
            contentWriter.setEncoding("UTF-8");
            contentWriter.putContent(CONTENT_1);
            return null;
        }
    };
    this.transactionService.getRetryingTransactionHelper().doInTransaction(createTestFileCallback);
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) Before(org.junit.Before)

Example 59 with RetryingTransactionCallback

use of org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback in project alfresco-remote-api by Alfresco.

the class LockMethodTest method testMNT_10873.

@Test
public void testMNT_10873() throws Exception {
    String fileName = TEST_FILE_NAME + GUID.generate();
    final MockHttpServletRequest lockRequest = new MockHttpServletRequest();
    MockHttpServletResponse lockResponse = new MockHttpServletResponse();
    lockRequest.addHeader(WebDAV.HEADER_TIMEOUT, WebDAV.SECOND + 5);
    // set request uri to point to non-existent file
    lockRequest.setRequestURI("/" + fileName);
    String content = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><D:lockinfo xmlns:D=\"DAV:\"><D:lockscope xmlns:D=\"DAV:\">" + "<D:exclusive xmlns:D=\"DAV:\"/></D:lockscope><D:locktype xmlns:D=\"DAV:\"><D:write xmlns:D=\"DAV:\"/></D:locktype>" + "<D:owner xmlns:D=\"DAV:\">" + userName + "</D:owner></D:lockinfo>";
    lockRequest.setContent(content.getBytes("UTF-8"));
    lockMethod.setDetails(lockRequest, lockResponse, davHelper, folderNodeRef);
    lockMethod.parseRequestHeaders();
    lockMethod.parseRequestBody();
    RetryingTransactionCallback<Void> lockExecuteImplCallBack = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            lockMethod.executeImpl();
            return null;
        }
    };
    // lock node for 5 seconds
    this.transactionService.getRetryingTransactionHelper().doInTransaction(lockExecuteImplCallBack);
    assertEquals("Unexpected response status code.", HttpServletResponse.SC_CREATED, lockResponse.getStatus());
    RetryingTransactionCallback<NodeRef> getNodeRefCallback = new RetryingTransactionCallback<NodeRef>() {

        @Override
        public NodeRef execute() throws Throwable {
            return lockMethod.getNodeForPath(folderNodeRef, lockRequest.getRequestURI()).getNodeRef();
        }
    };
    NodeRef nodeRef = this.transactionService.getRetryingTransactionHelper().doInTransaction(getNodeRefCallback);
    assertTrue("NodeRef should exists.", nodeService.exists(nodeRef));
    assertTrue("sys:webdavNoContent aspect should be applied on node.", nodeService.hasAspect(nodeRef, ContentModel.ASPECT_WEBDAV_NO_CONTENT));
    // sleep for 6 seconds to ensure that timer was triggered
    Thread.sleep(6000);
    assertFalse("File should note exist in repo any more.", nodeService.exists(nodeRef));
    assertFalse("File should note exist in trashcan.", nodeService.exists(new NodeRef(StoreRef.STORE_REF_ARCHIVE_SPACESSTORE, nodeRef.getId())));
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 60 with RetryingTransactionCallback

use of org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback in project alfresco-remote-api by Alfresco.

the class WebDAVLockServiceImplTest method setUp.

@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
    davLockService = new WebDAVLockServiceImpl();
    davLockService.setNodeService(nodeService);
    davLockService.setCheckOutCheckInService(cociService);
    davLockService.setCurrentSession(session);
    davLockService.setLockService(lockService);
    // Train the mock LockStore to respond to get() requests for certain noderefs.
    nodeRef1 = new NodeRef("workspace://SpacesStore/f6e3f82a-cfef-445b-9fca-7986a14181cc");
    lockInfo1 = new LockInfoImplTest.LockInfoImplEx();
    lockState1 = LockState.createLock(nodeRef1, LockType.WRITE_LOCK, "user1", null, Lifetime.EPHEMERAL, null);
    Mockito.when(lockService.getLockState(nodeRef1)).thenReturn(lockState1);
    nodeRef2 = new NodeRef("workspace://SpacesStore/a6a4371c-99b9-4618-8cd2-e71d7d96aa87");
    lockInfo2 = new LockInfoImplTest.LockInfoImplEx();
    lockInfo2.setExclusiveLockToken("a-random-token");
    lockInfo2.setDepth("infinity");
    lockInfo2.setScope(WebDAV.XML_EXCLUSIVE);
    lockState2 = LockState.createLock(nodeRef2, LockType.WRITE_LOCK, "user2", new Date(999L), Lifetime.EPHEMERAL, lockInfo2.toJSON());
    Mockito.when(lockService.getLockState(nodeRef2)).thenReturn(lockState2);
    // The mock HttpSession should return the mock session list.
    Mockito.when(session.getAttribute("_webdavLockedResources")).thenReturn(sessionList);
    // Provide a user name for our fictional user.
    authenticationUtil = new AuthenticationUtil();
    authenticationUtil.afterPropertiesSet();
    AuthenticationUtil.setFullyAuthenticatedUser("some_user_name");
    Mockito.when(txHelper.doInTransaction(any(RetryingTransactionCallback.class), anyBoolean())).thenAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            RetryingTransactionCallback<Void> callback = (RetryingTransactionCallback<Void>) args[0];
            callback.execute();
            return null;
        }
    });
    Mockito.when(transactionService.getRetryingTransactionHelper()).thenReturn(txHelper);
    davLockService.setTransactionService(transactionService);
}
Also used : Date(java.util.Date) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Answer(org.mockito.stubbing.Answer) AuthenticationUtil(org.alfresco.repo.security.authentication.AuthenticationUtil) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Before(org.junit.Before)

Aggregations

RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)78 NodeRef (org.alfresco.service.cmr.repository.NodeRef)44 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)20 FileInfo (org.alfresco.service.cmr.model.FileInfo)20 WebApiDescription (org.alfresco.rest.framework.WebApiDescription)16 HashMap (java.util.HashMap)15 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)15 FacesContext (javax.faces.context.FacesContext)12 QName (org.alfresco.service.namespace.QName)11 Serializable (java.io.Serializable)10 List (java.util.List)9 ArrayList (java.util.ArrayList)8 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)8 IOException (java.io.IOException)7 FileNotFoundException (org.alfresco.service.cmr.model.FileNotFoundException)7 Test (org.junit.Test)7 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)7 AuthenticationUtil (org.alfresco.repo.security.authentication.AuthenticationUtil)6 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)6 AbstractList (java.util.AbstractList)5