Search in sources :

Example 6 with RetryingTransactionCallback

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

the class UnlockMethodTest method unlockWorkingCopy.

/**
 * Test MNT-9680: Working copies are open in read-only mode when using Webdav online edit
 *
 * @throws Exception
 */
@Test
public void unlockWorkingCopy() throws Exception {
    setUpPreconditionForCheckedOutTest();
    try {
        String workingCopyName = nodeService.getProperty(fileWorkingCopyNodeRef, ContentModel.PROP_NAME).toString();
        String lockToken = fileWorkingCopyNodeRef.getId() + WebDAV.LOCK_TOKEN_SEPERATOR + this.userName;
        String lockHeaderValue = "<" + WebDAV.OPAQUE_LOCK_TOKEN + lockToken + ">";
        final WebDAVHelper davHelper = (WebDAVHelper) appContext.getBean("webDAVHelper");
        request.addHeader(WebDAV.HEADER_LOCK_TOKEN, lockHeaderValue);
        request.setRequestURI("/" + workingCopyName);
        String content = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "<d:lockinfo xmlns:d=\"DAV:\">" + "<d:lockscope><d:exclusive/></d:lockscope>" + "</d:lockinfo>";
        request.setContent(content.getBytes("UTF-8"));
        lockMethod.setDetails(request, new MockHttpServletResponse(), davHelper, folderNodeRef);
        lockMethod.parseRequestHeaders();
        lockMethod.parseRequestBody();
        RetryingTransactionCallback<Void> lockExecuteImplCallBack = new RetryingTransactionCallback<Void>() {

            @Override
            public Void execute() throws Throwable {
                lockMethod.executeImpl();
                return null;
            }
        };
        this.transactionService.getRetryingTransactionHelper().doInTransaction(lockExecuteImplCallBack);
        unlockMethod.setDetails(request, new MockHttpServletResponse(), davHelper, folderNodeRef);
        unlockMethod.parseRequestHeaders();
        RetryingTransactionCallback<Void> unlockExecuteImplCallBack = new RetryingTransactionCallback<Void>() {

            @Override
            public Void execute() throws Throwable {
                unlockMethod.executeImpl();
                return null;
            }
        };
        this.transactionService.getRetryingTransactionHelper().doInTransaction(unlockExecuteImplCallBack);
        assertNull("lockType property should be deleted on unlock", nodeService.getProperty(fileWorkingCopyNodeRef, ContentModel.PROP_LOCK_TYPE));
        assertNull("lockOwner property should be deleted on unlock", nodeService.getProperty(fileWorkingCopyNodeRef, ContentModel.PROP_LOCK_OWNER));
    } finally {
        // clear context for current user
        this.authenticationComponent.clearCurrentSecurityContext();
        // delete test store as system user
        this.authenticationComponent.setSystemUserAsCurrentUser();
        RetryingTransactionCallback<Void> deleteStoreCallback = new RetryingTransactionCallback<Void>() {

            @Override
            public Void execute() throws Throwable {
                if (nodeService.exists(storeRef)) {
                    nodeService.deleteStore(storeRef);
                }
                return null;
            }
        };
        this.transactionService.getRetryingTransactionHelper().doInTransaction(deleteStoreCallback);
    }
}
Also used : RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 7 with RetryingTransactionCallback

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

the class UnlockMethodTest method unlockCheckedOutNode.

/**
 * Test that it is impossible to unlock a checked out node
 *
 * @throws Exception
 */
@Test
public void unlockCheckedOutNode() throws Exception {
    setUpPreconditionForCheckedOutTest();
    try {
        String lockToken = fileNodeRef.getId() + WebDAV.LOCK_TOKEN_SEPERATOR + this.userName;
        String lockHeaderValue = "<" + WebDAV.OPAQUE_LOCK_TOKEN + lockToken + ">";
        request.addHeader(WebDAV.HEADER_LOCK_TOKEN, lockHeaderValue);
        request.setRequestURI("/" + TEST_FILE_NAME);
        WebDAVHelper davHelper = (WebDAVHelper) appContext.getBean("webDAVHelper");
        unlockMethod.setDetails(request, new MockHttpServletResponse(), davHelper, folderNodeRef);
        unlockMethod.parseRequestHeaders();
        transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

            @Override
            public Void execute() throws Throwable {
                unlockMethod.executeImpl();
                return null;
            }
        });
        fail("Exception should have been thrown, but wasn't.");
    } catch (AlfrescoRuntimeException e) {
        if (e.getCause() instanceof WebDAVServerException) {
            WebDAVServerException ee = (WebDAVServerException) e.getCause();
            assertEquals(HttpServletResponse.SC_PRECONDITION_FAILED, ee.getHttpStatusCode());
        } else {
            fail("Incorrect exception thrown.");
        }
    } catch (WebDAVServerException e) {
        assertEquals(HttpServletResponse.SC_PRECONDITION_FAILED, e.getHttpStatusCode());
    } finally {
        // clear context for current user
        this.authenticationComponent.clearCurrentSecurityContext();
        // delete test store as system user
        this.authenticationComponent.setSystemUserAsCurrentUser();
        RetryingTransactionCallback<Void> deleteStoreCallback = new RetryingTransactionCallback<Void>() {

            @Override
            public Void execute() throws Throwable {
                if (nodeService.exists(storeRef)) {
                    nodeService.deleteStore(storeRef);
                }
                return null;
            }
        };
        this.transactionService.getRetryingTransactionHelper().doInTransaction(deleteStoreCallback);
    }
}
Also used : RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 8 with RetryingTransactionCallback

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

the class UnlockMethodTest method setUpPreconditionForCheckedOutTest.

/**
 * Set up preconditions for unlock a checked out node
 */
protected void setUpPreconditionForCheckedOutTest() throws Exception {
    appContext = ApplicationContextHelper.getApplicationContext(new String[] { "classpath:alfresco/application-context.xml", "classpath:alfresco/web-scripts-application-context.xml", "classpath:alfresco/remote-api-context.xml" });
    // Set the services
    this.cociService = (CheckOutCheckInService) appContext.getBean("checkOutCheckInService");
    this.contentService = (ContentService) appContext.getBean("contentService");
    this.authenticationService = (MutableAuthenticationService) appContext.getBean("authenticationService");
    this.permissionService = (PermissionService) appContext.getBean("permissionService");
    this.transactionService = (TransactionService) appContext.getBean("TransactionService");
    this.nodeService = (NodeService) appContext.getBean("NodeService");
    // Authenticate as system to create initial test data set
    this.authenticationComponent = (AuthenticationComponent) appContext.getBean("authenticationComponent");
    this.authenticationComponent.setSystemUserAsCurrentUser();
    RetryingTransactionCallback<Void> createTestFileCallback = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Create the store and get the root node reference
            storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, TEST_STORE_IDENTIFIER);
            if (!nodeService.exists(storeRef)) {
                storeRef = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, TEST_STORE_IDENTIFIER);
            }
            rootNodeRef = nodeService.getRootNode(storeRef);
            // Create and authenticate the user
            userName = "webdavUnlockTest" + GUID.generate();
            TestWithUserUtils.createUser(userName, PWD, rootNodeRef, nodeService, authenticationService);
            permissionService.setPermission(rootNodeRef, userName, PermissionService.ALL_PERMISSIONS, true);
            TestWithUserUtils.authenticateUser(userName, PWD, rootNodeRef, authenticationService);
            userNodeRef = TestWithUserUtils.getCurrentUser(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, "folder")).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);
            // Check out test file
            fileWorkingCopyNodeRef = cociService.checkout(fileNodeRef);
            assertNotNull(fileWorkingCopyNodeRef);
            assertEquals(userNodeRef, nodeService.getProperty(fileNodeRef, ContentModel.PROP_LOCK_OWNER));
            return null;
        }
    };
    this.transactionService.getRetryingTransactionHelper().doInTransaction(createTestFileCallback);
}
Also used : StoreRef(org.alfresco.service.cmr.repository.StoreRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)

Example 9 with RetryingTransactionCallback

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

the class InviteServiceTest method testInviteeResourcesNotDeletedUponRejectWhenInvitesPending.

public void testInviteeResourcesNotDeletedUponRejectWhenInvitesPending() throws Exception {
    // Test only applies to legacy invite workflow
    this.invitationServiceImpl.setNominatedInvitationWorkflowId(WorkflowModelNominatedInvitation.WORKFLOW_DEFINITION_NAME_ACTIVITI_INVITE);
    // Create invitee person
    final String inviteeEmail = INVITEE_EMAIL_PREFIX + RandomStringUtils.randomAlphanumeric(6) + "@" + INVITEE_EMAIL_DOMAIN;
    AuthenticationUtil.runAs(new RunAsWork<Object>() {

        public Object doWork() throws Exception {
            createPerson(INVITEE_FIRSTNAME, INVITEE_LASTNAME, INVITEE_FIRSTNAME + "_" + INVITEE_LASTNAME, inviteeEmail);
            return null;
        }
    }, AuthenticationUtil.getSystemUserName());
    // inviter invites invitee to site 1
    JSONObject result = startInvite(INVITEE_FIRSTNAME, INVITEE_LASTNAME, inviteeEmail, INVITEE_SITE_ROLE, SITE_SHORT_NAME_INVITE_1, Status.STATUS_CREATED);
    // get hold of properties of started invite
    JSONObject data = result.getJSONObject("data");
    String invite1Id = data.getString("inviteId");
    String invite1Ticket = data.getString("inviteTicket");
    final String inviteeUserName = data.getString("inviteeUserName");
    // inviter invites invitee to site 2
    startInvite(INVITEE_FIRSTNAME, INVITEE_LASTNAME, inviteeEmail, INVITEE_SITE_ROLE, SITE_SHORT_NAME_INVITE_2, Status.STATUS_CREATED);
    rejectInvite(invite1Id, invite1Ticket, Status.STATUS_OK);
    boolean inviteeUserExists = AuthenticationUtil.runAs(new RunAsWork<Boolean>() {

        public Boolean doWork() throws Exception {
            RetryingTransactionHelper tranHelper = transactionService.getRetryingTransactionHelper();
            Boolean result = tranHelper.doInTransaction(new RetryingTransactionCallback<Boolean>() {

                public Boolean execute() throws Throwable {
                    Boolean result = mutableAuthenticationDao.userExists(inviteeUserName);
                    return result;
                }
            });
            return result;
        }
    }, AuthenticationUtil.getSystemUserName());
    // test that the invitee's user account still exists (has not been deleted
    assertEquals(true, inviteeUserExists);
    boolean inviteePersonExists = AuthenticationUtil.runAs(new RunAsWork<Boolean>() {

        public Boolean doWork() throws Exception {
            Boolean result = personService.personExists(inviteeUserName);
            return result;
        }
    }, AuthenticationUtil.getSystemUserName());
    assertEquals(true, inviteePersonExists);
    // Reset back to default
    this.invitationServiceImpl.setNominatedInvitationWorkflowId(WorkflowModelNominatedInvitation.WORKFLOW_DEFINITION_NAME_ACTIVITI_ADD_DIRECT);
}
Also used : JSONObject(org.json.JSONObject) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) JSONObject(org.json.JSONObject)

Example 10 with RetryingTransactionCallback

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

the class InviteServiceTest method testInviteeResourcesDeletedUponRejectWhenNoInvitePending.

public void testInviteeResourcesDeletedUponRejectWhenNoInvitePending() throws Exception {
    // inviter starts invite workflow
    JSONObject result = startInvite(INVITEE_FIRSTNAME, INVITEE_LASTNAME, INVITEE_SITE_ROLE, SITE_SHORT_NAME_INVITE_1, Status.STATUS_CREATED);
    // get hold of properties of started invite
    JSONObject data = result.getJSONObject("data");
    String inviteId = data.getString("inviteId");
    String inviteTicket = data.getString("inviteTicket");
    final String inviteeUserName = data.getString("inviteeUserName");
    rejectInvite(inviteId, inviteTicket, Status.STATUS_OK);
    AuthenticationUtil.runAs(new RunAsWork<Void>() {

        public Void doWork() throws Exception {
            transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

                public Void execute() throws Throwable {
                    assertEquals(false, mutableAuthenticationDao.userExists(inviteeUserName));
                    assertEquals(false, personService.personExists(inviteeUserName));
                    return null;
                }
            });
            return null;
        }
    }, AuthenticationUtil.getSystemUserName());
}
Also used : JSONObject(org.json.JSONObject) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)

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