Search in sources :

Example 11 with UserInfo

use of org.alfresco.rest.api.model.UserInfo in project alfresco-remote-api by Alfresco.

the class AbstractNodeRelation method listNodeChildAssocs.

protected CollectionWithPagingInfo<Node> listNodeChildAssocs(List<ChildAssociationRef> childAssocRefs, Parameters parameters, Boolean isPrimary, boolean returnChild) {
    Map<QName, String> qnameMap = new HashMap<>(3);
    Map<String, UserInfo> mapUserInfo = new HashMap<>(10);
    List<String> includeParam = parameters.getInclude();
    List<Node> collection = new ArrayList<Node>(childAssocRefs.size());
    for (ChildAssociationRef childAssocRef : childAssocRefs) {
        if (isPrimary == null || (isPrimary == childAssocRef.isPrimary())) {
            // minimal info by default (unless "include"d otherwise)
            NodeRef nodeRef = (returnChild ? childAssocRef.getChildRef() : childAssocRef.getParentRef());
            Node node = nodes.getFolderOrDocument(nodeRef, null, null, includeParam, mapUserInfo);
            QName assocTypeQName = childAssocRef.getTypeQName();
            if (!EXCLUDED_NS.contains(assocTypeQName.getNamespaceURI())) {
                String assocType = qnameMap.get(assocTypeQName);
                if (assocType == null) {
                    assocType = assocTypeQName.toPrefixString(namespaceService);
                    qnameMap.put(assocTypeQName, assocType);
                }
                node.setAssociation(new AssocChild(assocType, childAssocRef.isPrimary()));
                collection.add(node);
            }
        }
    }
    return listPage(collection, parameters.getPaging());
}
Also used : HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) Node(org.alfresco.rest.api.model.Node) ArrayList(java.util.ArrayList) AssocChild(org.alfresco.rest.api.model.AssocChild) UserInfo(org.alfresco.rest.api.model.UserInfo) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef)

Example 12 with UserInfo

use of org.alfresco.rest.api.model.UserInfo in project alfresco-remote-api by Alfresco.

the class AbstractNodeRelation method listNodePeerAssocs.

protected CollectionWithPagingInfo<Node> listNodePeerAssocs(List<AssociationRef> assocRefs, Parameters parameters, boolean returnTarget) {
    Map<QName, String> qnameMap = new HashMap<>(3);
    Map<String, UserInfo> mapUserInfo = new HashMap<>(10);
    List<String> includeParam = parameters.getInclude();
    List<Node> collection = new ArrayList<Node>(assocRefs.size());
    for (AssociationRef assocRef : assocRefs) {
        // minimal info by default (unless "include"d otherwise)
        NodeRef nodeRef = (returnTarget ? assocRef.getTargetRef() : assocRef.getSourceRef());
        Node node = nodes.getFolderOrDocument(nodeRef, null, null, includeParam, mapUserInfo);
        QName assocTypeQName = assocRef.getTypeQName();
        if (!EXCLUDED_NS.contains(assocTypeQName.getNamespaceURI())) {
            String assocType = qnameMap.get(assocTypeQName);
            if (assocType == null) {
                assocType = assocTypeQName.toPrefixString(namespaceService);
                qnameMap.put(assocTypeQName, assocType);
            }
            node.setAssociation(new Assoc(assocType));
            collection.add(node);
        }
    }
    return listPage(collection, parameters.getPaging());
}
Also used : HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) Node(org.alfresco.rest.api.model.Node) ArrayList(java.util.ArrayList) UserInfo(org.alfresco.rest.api.model.UserInfo) AssociationRef(org.alfresco.service.cmr.repository.AssociationRef) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Assoc(org.alfresco.rest.api.model.Assoc)

Example 13 with UserInfo

use of org.alfresco.rest.api.model.UserInfo in project alfresco-remote-api by Alfresco.

the class AuditImpl method getQueryResultAuditEntries.

/**
 * @param auditAppId
 * @param propertyWalker
 * @param includeParams
 * @param maxItem
 * @param forward
 * @return
 */
public List<AuditEntry> getQueryResultAuditEntries(AuditService.AuditApplication auditApplication, AuditEntryQueryWalker propertyWalker, List<String> includeParam, int maxItem, Boolean forward) {
    final List<AuditEntry> results = new ArrayList<>();
    final String auditAppId = auditApplication.getKey().substring(1);
    String auditApplicationName = auditApplication.getName();
    // Execute the query
    AuditQueryParameters params = new AuditQueryParameters();
    // used to orderBY by field createdAt
    params.setForward(forward);
    params.setApplicationName(auditApplicationName);
    params.setUser(propertyWalker.getCreatedByUser());
    Long fromId = propertyWalker.getFromId();
    Long toId = propertyWalker.getToId();
    validateWhereBetween(auditAppId, fromId, toId);
    Long fromTime = propertyWalker.getFromTime();
    Long toTime = propertyWalker.getToTime();
    validateWhereBetween(auditAppId, fromTime, toTime);
    params.setFromTime(fromTime);
    params.setToTime(toTime);
    params.setFromId(fromId);
    params.setToId(toId);
    if (propertyWalker.getValuesKey() != null && propertyWalker.getValuesValue() != null) {
        params.addSearchKey(propertyWalker.getValuesKey(), propertyWalker.getValuesValue());
    }
    final Map<String, UserInfo> mapUserInfo = new HashMap<>(10);
    // create the callback for auditQuery method
    final AuditQueryCallback callback = new AuditQueryCallback() {

        public boolean valuesRequired() {
            return ((includeParam != null) && (includeParam.contains(PARAM_INCLUDE_VALUES)));
        }

        public boolean handleAuditEntryError(Long entryId, String errorMsg, Throwable error) {
            throw new AlfrescoRuntimeException("Failed to retrieve audit data.", error);
        }

        public boolean handleAuditEntry(Long entryId, String applicationName, String userName, long time, Map<String, Serializable> values) {
            UserInfo userInfo = Node.lookupUserInfo(userName, mapUserInfo, personService);
            AuditEntry auditEntry = new AuditEntry(entryId, auditAppId, userInfo, new Date(time), values);
            results.add(auditEntry);
            return true;
        }
    };
    auditService.auditQuery(callback, params, maxItem);
    return results;
}
Also used : HashMap(java.util.HashMap) AuditQueryParameters(org.alfresco.service.cmr.audit.AuditQueryParameters) ArrayList(java.util.ArrayList) UserInfo(org.alfresco.rest.api.model.UserInfo) Date(java.util.Date) AuditEntry(org.alfresco.rest.api.model.AuditEntry) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) AuditQueryCallback(org.alfresco.service.cmr.audit.AuditService.AuditQueryCallback) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with UserInfo

use of org.alfresco.rest.api.model.UserInfo in project alfresco-remote-api by Alfresco.

the class AuditImpl method getQueryResultAuditEntriesByNodeRef.

private List<AuditEntry> getQueryResultAuditEntriesByNodeRef(NodeRef nodeRef, AuditEntriesByNodeIdQueryWalker propertyWalker, List<String> includeParam, boolean forward, int limit) {
    final List<AuditEntry> results = new ArrayList<>();
    String auditAppId = "alfresco-access";
    String auditApplicationName = AuthenticationUtil.runAs(new RunAsWork<String>() {

        public String doWork() throws Exception {
            return findAuditAppByIdOr404(auditAppId).getName();
        }
    }, AuthenticationUtil.getSystemUserName());
    // create the callback for auditQuery method
    final AuditQueryCallback callback = new AuditQueryCallback() {

        public boolean valuesRequired() {
            return ((includeParam != null) && (includeParam.contains(PARAM_INCLUDE_VALUES)));
        }

        public boolean handleAuditEntryError(Long entryId, String errorMsg, Throwable error) {
            throw new AlfrescoRuntimeException("Failed to retrieve audit data.", error);
        }

        public boolean handleAuditEntry(Long entryId, String applicationName, String userName, long time, Map<String, Serializable> values) {
            UserInfo userInfo = Node.lookupUserInfo(userName, new HashMap<>(0), personService);
            AuditEntry auditEntry = new AuditEntry(entryId, auditAppId, userInfo, new Date(time), values);
            results.add(auditEntry);
            return true;
        }
    };
    // resolve the path of the node - note: this will also check read permission for current user
    final String nodePath = ISO9075.decode(nodeService.getPath(nodeRef).toPrefixString(namespaceService));
    Long fromTime = propertyWalker.getFromTime();
    Long toTime = propertyWalker.getToTime();
    validateWhereBetween(nodeRef.getId(), fromTime, toTime);
    AuthenticationUtil.runAs(new RunAsWork<Object>() {

        public Object doWork() throws Exception {
            // QueryParameters
            AuditQueryParameters pathParams = new AuditQueryParameters();
            // used to orderBY by field createdAt
            pathParams.setForward(forward);
            pathParams.setUser(propertyWalker.getCreatedByUser());
            pathParams.setFromTime(fromTime);
            pathParams.setToTime(toTime);
            pathParams.setApplicationName(auditApplicationName);
            pathParams.addSearchKey("/" + auditAppId + "/transaction/path", nodePath);
            auditService.auditQuery(callback, pathParams, limit);
            AuditQueryParameters copyFromPathParams = new AuditQueryParameters();
            // used to orderBY by field createdAt
            copyFromPathParams.setForward(forward);
            copyFromPathParams.setUser(propertyWalker.getCreatedByUser());
            copyFromPathParams.setFromTime(fromTime);
            copyFromPathParams.setToTime(toTime);
            copyFromPathParams.setApplicationName(auditApplicationName);
            copyFromPathParams.addSearchKey("/" + auditAppId + "/transaction/copy/from/path", nodePath);
            auditService.auditQuery(callback, copyFromPathParams, limit);
            AuditQueryParameters moveFromPathParams = new AuditQueryParameters();
            // used to orderBY by field createdAt
            moveFromPathParams.setForward(forward);
            moveFromPathParams.setUser(propertyWalker.getCreatedByUser());
            moveFromPathParams.setFromTime(fromTime);
            moveFromPathParams.setToTime(toTime);
            moveFromPathParams.setApplicationName(auditApplicationName);
            moveFromPathParams.addSearchKey("/" + auditAppId + "/transaction/move/from/path", nodePath);
            auditService.auditQuery(callback, moveFromPathParams, limit);
            return null;
        }
    }, AuthenticationUtil.getSystemUserName());
    return results;
}
Also used : AuditQueryParameters(org.alfresco.service.cmr.audit.AuditQueryParameters) ArrayList(java.util.ArrayList) UserInfo(org.alfresco.rest.api.model.UserInfo) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) DisabledServiceException(org.alfresco.rest.framework.core.exceptions.DisabledServiceException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) Date(java.util.Date) AuditEntry(org.alfresco.rest.api.model.AuditEntry) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) AuditQueryCallback(org.alfresco.service.cmr.audit.AuditService.AuditQueryCallback) HashMap(java.util.HashMap) Map(java.util.Map)

Example 15 with UserInfo

use of org.alfresco.rest.api.model.UserInfo in project alfresco-remote-api by Alfresco.

the class ResultMapperTests method setupTests.

@BeforeClass
public static void setupTests() throws Exception {
    Map<String, UserInfo> mapUserInfo = new HashMap<>();
    mapUserInfo.put(AuthenticationUtil.getSystemUserName(), new UserInfo(AuthenticationUtil.getSystemUserName(), "sys", "sys"));
    Map<QName, Serializable> nodeProps = new HashMap<>();
    NodesImpl nodes = mock(NodesImpl.class);
    ServiceRegistry sr = mock(ServiceRegistry.class);
    DeletedNodes deletedNodes = mock(DeletedNodes.class);
    nodes.setServiceRegistry(sr);
    VersionService versionService = mock(VersionService.class);
    VersionHistory versionHistory = mock(VersionHistory.class);
    Map<String, Serializable> versionProperties = new HashMap<>();
    versionProperties.put(Version.PROP_DESCRIPTION, "ver desc");
    versionProperties.put(Version2Model.PROP_VERSION_TYPE, "v type");
    when(versionHistory.getVersion(anyString())).thenAnswer(invocation -> {
        return new VersionImpl(versionProperties, new NodeRef(StoreMapper.STORE_REF_VERSION2_SPACESSTORE, GUID.generate()));
    });
    NodeService nodeService = mock(NodeService.class);
    when(versionService.getVersionHistory(any(NodeRef.class))).thenAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        NodeRef aNode = (NodeRef) args[0];
        return versionHistory;
    });
    when(nodeService.getProperties(any(NodeRef.class))).thenAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        NodeRef aNode = (NodeRef) args[0];
        if (StoreMapper.STORE_REF_VERSION2_SPACESSTORE.equals(aNode.getStoreRef())) {
            nodeProps.put(Version2Model.PROP_QNAME_FROZEN_NODE_REF, new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, FROZEN_ID + aNode.getId()));
            nodeProps.put(Version2Model.PROP_QNAME_VERSION_LABEL, FROZEN_VER);
        }
        return nodeProps;
    });
    when(sr.getVersionService()).thenReturn(versionService);
    when(sr.getNodeService()).thenReturn(nodeService);
    when(nodes.validateOrLookupNode(nullable(String.class), nullable(String.class))).thenAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String aNode = (String) args[0];
        if (aNode.endsWith("" + VERSIONED_ID)) {
            throw new EntityNotFoundException("" + VERSIONED_ID);
        } else {
            return new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, aNode);
        }
    });
    // // NodeRef nodeRef = nodes.validateOrLookupNode(nodeId, null);
    when(nodes.getFolderOrDocument(any(NodeRef.class), nullable(NodeRef.class), nullable(QName.class), nullable(List.class), nullable(Map.class))).thenAnswer(new Answer<Node>() {

        @Override
        public Node answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            NodeRef aNode = (NodeRef) args[0];
            if (StoreRef.STORE_REF_ARCHIVE_SPACESSTORE.equals(aNode.getStoreRef())) {
                // Return NULL if its from the archive store.
                return null;
            }
            return new Node(aNode, (NodeRef) args[1], nodeProps, mapUserInfo, sr);
        }
    });
    when(deletedNodes.getDeletedNode(nullable(String.class), nullable(Parameters.class), anyBoolean(), nullable(Map.class))).thenAnswer(new Answer<Node>() {

        @Override
        public Node answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            String nodeId = (String) args[0];
            if (FROZEN_ID.equals(nodeId))
                throw new EntityNotFoundException(nodeId);
            NodeRef aNode = new NodeRef(StoreRef.STORE_REF_ARCHIVE_SPACESSTORE, nodeId);
            return new Node(aNode, new NodeRef(StoreRef.STORE_REF_ARCHIVE_SPACESSTORE, "unknown"), nodeProps, mapUserInfo, sr);
        }
    });
    PersonPropertyLookup propertyLookups = mock(PersonPropertyLookup.class);
    when(propertyLookups.supports()).thenReturn(Stream.of("creator", "modifier").collect(Collectors.toSet()));
    when(propertyLookups.lookup(notNull(String.class))).thenAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String value = (String) args[0];
        if ("mjackson".equals(value))
            return "Michael Jackson";
        return null;
    });
    PropertyLookupRegistry propertyLookupRegistry = new PropertyLookupRegistry();
    propertyLookupRegistry.setLookups(Arrays.asList(propertyLookups));
    mapper = new ResultMapper();
    mapper.setNodes(nodes);
    mapper.setStoreMapper(new StoreMapper());
    mapper.setPropertyLookup(propertyLookupRegistry);
    mapper.setDeletedNodes(deletedNodes);
    mapper.setServiceRegistry(sr);
    NodeVersionsRelation nodeVersionsRelation = new NodeVersionsRelation();
    nodeVersionsRelation.setNodes(nodes);
    nodeVersionsRelation.setServiceRegistry(sr);
    nodeVersionsRelation.afterPropertiesSet();
    mapper.setNodeVersions(nodeVersionsRelation);
    helper = new SerializerTestHelper();
    searchMapper.setStoreMapper(new StoreMapper());
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) Node(org.alfresco.rest.api.model.Node) StoreMapper(org.alfresco.rest.api.search.impl.StoreMapper) VersionService(org.alfresco.service.cmr.version.VersionService) UserInfo(org.alfresco.rest.api.model.UserInfo) Mockito.anyString(org.mockito.Mockito.anyString) VersionImpl(org.alfresco.repo.version.common.VersionImpl) NodeRef(org.alfresco.service.cmr.repository.NodeRef) List(java.util.List) TupleList(org.alfresco.rest.api.search.model.TupleList) ArrayList(java.util.ArrayList) DeletedNodes(org.alfresco.rest.api.DeletedNodes) Parameters(org.alfresco.rest.framework.resource.parameters.Parameters) GeneralHighlightParameters(org.alfresco.service.cmr.search.GeneralHighlightParameters) FieldHighlightParameters(org.alfresco.service.cmr.search.FieldHighlightParameters) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) QName(org.alfresco.service.namespace.QName) PersonPropertyLookup(org.alfresco.rest.api.lookups.PersonPropertyLookup) NodeService(org.alfresco.service.cmr.repository.NodeService) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) VersionHistory(org.alfresco.service.cmr.version.VersionHistory) NodesImpl(org.alfresco.rest.api.impl.NodesImpl) PropertyLookupRegistry(org.alfresco.rest.api.lookups.PropertyLookupRegistry) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResultMapper(org.alfresco.rest.api.search.impl.ResultMapper) JSONObject(org.json.JSONObject) ServiceRegistry(org.alfresco.service.ServiceRegistry) NodeVersionsRelation(org.alfresco.rest.api.nodes.NodeVersionsRelation) Map(java.util.Map) HashMap(java.util.HashMap) BeforeClass(org.junit.BeforeClass)

Aggregations

UserInfo (org.alfresco.rest.api.model.UserInfo)26 HashMap (java.util.HashMap)22 NodeRef (org.alfresco.service.cmr.repository.NodeRef)18 AbstractList (java.util.AbstractList)13 WebApiDescription (org.alfresco.rest.framework.WebApiDescription)13 FileInfo (org.alfresco.service.cmr.model.FileInfo)13 QName (org.alfresco.service.namespace.QName)12 ArrayList (java.util.ArrayList)9 List (java.util.List)7 Node (org.alfresco.rest.api.model.Node)7 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)6 LinkedList (java.util.LinkedList)5 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)5 Map (java.util.Map)4 FilterProp (org.alfresco.repo.node.getchildren.FilterProp)4 Date (java.util.Date)3 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)3 AuditEntry (org.alfresco.rest.api.model.AuditEntry)3 AuditQueryParameters (org.alfresco.service.cmr.audit.AuditQueryParameters)3 AuditQueryCallback (org.alfresco.service.cmr.audit.AuditService.AuditQueryCallback)3