Search in sources :

Example 1 with RetryingTransactionCallback

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

the class RepoStore method getAllDocumentPaths.

/* (non-Javadoc)
     * @see org.alfresco.web.scripts.Store#getAllDocumentPaths()
     */
public String[] getAllDocumentPaths() {
    return AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<String[]>() {

        public String[] doWork() throws Exception {
            return retryingTransactionHelper.doInTransaction(new RetryingTransactionCallback<String[]>() {

                public String[] execute() throws Exception {
                    int baseDirLength = getBaseDir().length() + 1;
                    List<String> documentPaths;
                    NodeRef repoStoreRootNodeRef = nodeService.getRootNode(repoStore);
                    List<NodeRef> nodeRefs = searchService.selectNodes(repoStoreRootNodeRef, repoPath + "//*[subtypeOf('{http://www.alfresco.org/model/content/1.0}content')]\"", new QueryParameterDefinition[] {}, namespaceService, false, SearchService.LANGUAGE_XPATH);
                    documentPaths = new ArrayList<String>(nodeRefs.size());
                    for (NodeRef nodeRef : nodeRefs) {
                        if (isContentPresent(nodeRef)) {
                            String nodeDir = getPath(nodeRef);
                            documentPaths.add(nodeDir.substring(baseDirLength));
                        }
                    }
                    return documentPaths.toArray(new String[documentPaths.size()]);
                }
            }, true, false);
        }
    }, AuthenticationUtil.getSystemUserName());
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) AuthenticationUtil(org.alfresco.repo.security.authentication.AuthenticationUtil) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with RetryingTransactionCallback

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

the class RepoStore method getScriptDocumentPaths.

/* (non-Javadoc)
     * @see org.alfresco.web.scripts.Store#getScriptDocumentPaths(org.alfresco.web.scripts.WebScript)
     */
public String[] getScriptDocumentPaths(final WebScript script) {
    return AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<String[]>() {

        public String[] doWork() throws Exception {
            return retryingTransactionHelper.doInTransaction(new RetryingTransactionCallback<String[]>() {

                public String[] execute() throws Exception {
                    int baseDirLength = getBaseDir().length() + 1;
                    List<String> documentPaths = null;
                    String scriptPath = script.getDescription().getScriptPath();
                    NodeRef scriptNodeRef = (scriptPath.length() == 0) ? getBaseNodeRef() : findNodeRef(scriptPath);
                    if (scriptNodeRef != null) {
                        org.alfresco.service.cmr.repository.Path repoScriptPath = nodeService.getPath(scriptNodeRef);
                        String id = script.getDescription().getId().substring(scriptPath.length() + (scriptPath.length() > 0 ? 1 : 0));
                        NodeRef repoStoreRootNodeRef = nodeService.getRootNode(repoStore);
                        List<NodeRef> nodeRefs = searchService.selectNodes(repoStoreRootNodeRef, repoScriptPath.toPrefixString(namespaceService) + "//*[like(@cm:name, '" + SearchLanguageConversion.convert(SearchLanguageConversion.DEF_LUCENE, SearchLanguageConversion.DEF_XPATH_LIKE, id + "*") + "', false)]", new QueryParameterDefinition[] {}, namespaceService, false, SearchService.LANGUAGE_XPATH);
                        documentPaths = new ArrayList<String>(nodeRefs.size());
                        for (NodeRef nodeRef : nodeRefs) {
                            if (isContentPresent(nodeRef)) {
                                String name = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
                                if (name.startsWith(id)) {
                                    String nodeDir = getPath(nodeRef);
                                    String documentPath = nodeDir.substring(baseDirLength);
                                    documentPaths.add(documentPath);
                                }
                            }
                        }
                    // String query = "+PATH:\"" + repoScriptPath.toPrefixString(namespaceService) +
                    // "//*\" +QNAME:" + lucenifyNamePattern(id) + "*";
                    // ResultSet resultSet = searchService.query(repoStore, SearchService.LANGUAGE_LUCENE, query);
                    // try
                    // {
                    // documentPaths = new ArrayList<String>(resultSet.length());
                    // List<NodeRef> nodes = resultSet.getNodeRefs();
                    // for (NodeRef nodeRef : nodes)
                    // {
                    // String name = (String)nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
                    // if (name.startsWith(id))
                    // {
                    // String nodeDir = getPath(nodeRef);
                    // String documentPath = nodeDir.substring(baseDirLength);
                    // documentPaths.add(documentPath);
                    // }
                    // }
                    // }
                    // finally
                    // {
                    // resultSet.close();
                    // }
                    }
                    return documentPaths != null ? documentPaths.toArray(new String[documentPaths.size()]) : new String[0];
                }
            }, true, false);
        }
    }, AuthenticationUtil.getSystemUserName());
}
Also used : FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) AuthenticationUtil(org.alfresco.repo.security.authentication.AuthenticationUtil) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)

Example 3 with RetryingTransactionCallback

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

the class RepoStore method getDocument.

/* (non-Javadoc)
     * @see org.alfresco.web.scripts.Store#getDescriptionDocument(java.lang.String)
     */
public InputStream getDocument(final String documentPath) throws IOException {
    return AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<InputStream>() {

        public InputStream doWork() throws Exception {
            return retryingTransactionHelper.doInTransaction(new RetryingTransactionCallback<InputStream>() {

                public InputStream execute() throws Exception {
                    NodeRef nodeRef = findNodeRef(documentPath);
                    if (nodeRef == null) {
                        throw new IOException("Document " + documentPath + " does not exist.");
                    }
                    ContentReader reader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
                    if (reader == null || !reader.exists()) {
                        throw new IOException("Failed to read content at " + documentPath + " (content reader does not exist)");
                    }
                    return reader.getContentInputStream();
                }
            }, true, false);
        }
    }, AuthenticationUtil.getSystemUserName());
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) AuthenticationUtil(org.alfresco.repo.security.authentication.AuthenticationUtil) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) InputStream(java.io.InputStream) ContentReader(org.alfresco.service.cmr.repository.ContentReader) IOException(java.io.IOException) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with RetryingTransactionCallback

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

the class LockMethodTest method testMNT_11990.

@Test
public void testMNT_11990() throws Exception {
    MockHttpServletRequest lockRequest = new MockHttpServletRequest();
    lockRequest.addHeader(WebDAV.HEADER_TIMEOUT, WebDAV.SECOND + 3600);
    lockRequest.addHeader(WebDAV.HEADER_IF, "(<" + WebDAV.makeLockToken(fileNodeRef, userName) + ">)");
    lockRequest.setRequestURI("/" + TEST_FILE_NAME);
    lockMethod.setDetails(lockRequest, new MockHttpServletResponse(), davHelper, folderNodeRef);
    lockMethod.parseRequestHeaders();
    lockMethod.parseRequestBody();
    RetryingTransactionCallback<Void> lockExecuteImplCallBack = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            try {
                lockMethod.executeImpl();
                fail("Lock should not be refreshed for non-locked file.");
            } catch (WebDAVServerException e) {
                assertEquals(e.getHttpStatusCode(), HttpServletResponse.SC_BAD_REQUEST);
            }
            return null;
        }
    };
    // try to refresh lock for non-locked node
    this.transactionService.getRetryingTransactionHelper().doInTransaction(lockExecuteImplCallBack);
}
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 5 with RetryingTransactionCallback

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

the class LockMethodTest method testMNT_12425.

@Test
public void testMNT_12425() throws Exception {
    MockHttpServletRequest lockRequest = new MockHttpServletRequest();
    MockHttpServletResponse lockResponse = new MockHttpServletResponse();
    // refresh lock set to 1 hour
    lockRequest.addHeader(WebDAV.HEADER_TIMEOUT, WebDAV.SECOND + 3600);
    lockRequest.addHeader(WebDAV.HEADER_IF, "(<" + WebDAV.makeLockToken(fileNodeRef, userName) + ">)");
    // specify path to non-existing file
    lockRequest.setRequestURI("/" + TEST_NEW_FOLDER_NAME + "/" + TEST_NEW_FILE_NAME);
    lockMethod.setDetails(lockRequest, lockResponse, davHelper, folderNodeRef);
    lockMethod.parseRequestHeaders();
    RetryingTransactionCallback<Void> lockExecuteImplCallBack = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            try {
                lockMethod.executeImpl();
                fail("Refresh lock for non-exisitent resource should fail.");
            } catch (WebDAVServerException e) {
                assertEquals(HttpServletResponse.SC_FORBIDDEN, e.getHttpStatusCode());
            }
            return null;
        }
    };
    // try to lock non-existent file
    this.transactionService.getRetryingTransactionHelper().doInTransaction(lockExecuteImplCallBack);
}
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)

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