Search in sources :

Example 1 with Document

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

the class NodesImpl method getDocument.

/**
 * @deprecated note: currently required for backwards compat' (Favourites API)
 */
@Override
public Document getDocument(NodeRef nodeRef) {
    Type type = getType(nodeRef);
    if ((type != null) && type.equals(Type.DOCUMENT)) {
        Map<QName, Serializable> properties = nodeService.getProperties(nodeRef);
        Document doc = new Document(nodeRef, getParentNodeRef(nodeRef), properties, null, sr);
        doc.setVersionLabel((String) properties.get(ContentModel.PROP_VERSION_LABEL));
        ContentData cd = (ContentData) properties.get(ContentModel.PROP_CONTENT);
        if (cd != null) {
            doc.setSizeInBytes(BigInteger.valueOf(cd.getSize()));
            doc.setMimeType((cd.getMimetype()));
        }
        setCommonProps(doc, nodeRef, properties);
        return doc;
    } else {
        throw new InvalidArgumentException("Node is not a file: " + nodeRef.getId());
    }
}
Also used : VersionType(org.alfresco.service.cmr.version.VersionType) ActivityType(org.alfresco.repo.activities.ActivityType) Serializable(java.io.Serializable) ContentData(org.alfresco.service.cmr.repository.ContentData) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) QName(org.alfresco.service.namespace.QName) Document(org.alfresco.rest.api.model.Document)

Example 2 with Document

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

the class TargetDeserializer method deserialize.

@Override
public Target deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    Target target = null;
    JsonToken curr = jp.getCurrentToken();
    if (curr == JsonToken.START_OBJECT) {
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String fieldname = jp.getCurrentName();
            if (Type.SITE.toString().equals(fieldname.toUpperCase())) {
                jp.nextToken();
                try {
                    JavaType t = SimpleType.construct(Site.class);
                    BeanProperty p = new BeanProperty.Std(new PropertyName(""), t, null, null, null, null);
                    JsonDeserializer<?> siteDeserializer = ctxt.findContextualValueDeserializer(t, p);
                    Site site = (Site) siteDeserializer.deserialize(jp, ctxt);
                    target = new SiteTarget(site);
                } catch (JsonMappingException e) {
                    throw new IllegalArgumentException("Target body is invalid for target type");
                }
            } else if (Type.FILE.toString().equals(fieldname.toUpperCase())) {
                jp.nextToken();
                try {
                    JavaType t = SimpleType.construct(Document.class);
                    BeanProperty p = new BeanProperty.Std(new PropertyName(""), t, null, null, null, null);
                    JsonDeserializer<?> documentDeserializer = ctxt.findContextualValueDeserializer(t, p);
                    Document document = (Document) documentDeserializer.deserialize(jp, ctxt);
                    target = new DocumentTarget(document);
                } catch (JsonMappingException e) {
                    throw new IllegalArgumentException("Target body is invalid for target type");
                }
            } else if (Type.FOLDER.toString().equals(fieldname.toUpperCase())) {
                jp.nextToken();
                try {
                    JavaType t = SimpleType.construct(Folder.class);
                    BeanProperty p = new BeanProperty.Std(new PropertyName(""), t, null, null, null, null);
                    JsonDeserializer<?> folderDeserializer = ctxt.findContextualValueDeserializer(t, p);
                    Folder folder = (Folder) folderDeserializer.deserialize(jp, ctxt);
                    target = new FolderTarget(folder);
                } catch (JsonMappingException e) {
                    throw new IllegalArgumentException("Target body is invalid for target type");
                }
            }
        }
        return target;
    } else {
        throw new IOException("Unable to deserialize favourite: " + curr.asString());
    }
}
Also used : Site(org.alfresco.rest.api.model.Site) PropertyName(com.fasterxml.jackson.databind.PropertyName) DocumentTarget(org.alfresco.rest.api.model.DocumentTarget) FolderTarget(org.alfresco.rest.api.model.FolderTarget) IOException(java.io.IOException) Document(org.alfresco.rest.api.model.Document) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) Folder(org.alfresco.rest.api.model.Folder) BeanProperty(com.fasterxml.jackson.databind.BeanProperty) Target(org.alfresco.rest.api.model.Target) SiteTarget(org.alfresco.rest.api.model.SiteTarget) DocumentTarget(org.alfresco.rest.api.model.DocumentTarget) FolderTarget(org.alfresco.rest.api.model.FolderTarget) JavaType(com.fasterxml.jackson.databind.JavaType) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonToken(com.fasterxml.jackson.core.JsonToken) SiteTarget(org.alfresco.rest.api.model.SiteTarget)

Example 3 with Document

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

the class NodesImpl method getFolderOrDocument.

@Override
public Node getFolderOrDocument(final NodeRef nodeRef, NodeRef parentNodeRef, QName nodeTypeQName, List<String> includeParam, Map<String, UserInfo> mapUserInfo) {
    if (mapUserInfo == null) {
        mapUserInfo = new HashMap<>(2);
    }
    if (includeParam == null) {
        includeParam = Collections.emptyList();
    }
    Node node;
    Map<QName, Serializable> properties = nodeService.getProperties(nodeRef);
    PathInfo pathInfo = null;
    if (includeParam.contains(PARAM_INCLUDE_PATH)) {
        ChildAssociationRef archivedParentAssoc = (ChildAssociationRef) properties.get(ContentModel.PROP_ARCHIVED_ORIGINAL_PARENT_ASSOC);
        pathInfo = lookupPathInfo(nodeRef, archivedParentAssoc);
    }
    if (nodeTypeQName == null) {
        nodeTypeQName = getNodeType(nodeRef);
    }
    if (parentNodeRef == null) {
        parentNodeRef = getParentNodeRef(nodeRef);
    }
    Type type = getType(nodeTypeQName, nodeRef);
    if (type == null) {
        // not direct folder (or file) ...
        // might be sub-type of cm:cmobject (or a cm:link pointing to cm:cmobject or possibly even another cm:link)
        node = new Node(nodeRef, parentNodeRef, properties, mapUserInfo, sr);
        node.setIsFolder(false);
        node.setIsFile(false);
    } else if (type.equals(Type.DOCUMENT)) {
        node = new Document(nodeRef, parentNodeRef, properties, mapUserInfo, sr);
    } else if (type.equals(Type.FOLDER)) {
        node = new Folder(nodeRef, parentNodeRef, properties, mapUserInfo, sr);
    } else {
        throw new RuntimeException("Unexpected - should not reach here: " + type);
    }
    if (includeParam.size() > 0) {
        node.setProperties(mapFromNodeProperties(properties, includeParam, mapUserInfo, EXCLUDED_NS, EXCLUDED_PROPS));
    }
    Set<QName> aspects = null;
    if (includeParam.contains(PARAM_INCLUDE_ASPECTNAMES)) {
        aspects = nodeService.getAspects(nodeRef);
        node.setAspectNames(mapFromNodeAspects(aspects, EXCLUDED_NS, EXCLUDED_ASPECTS));
    }
    if (includeParam.contains(PARAM_INCLUDE_ISLINK)) {
        boolean isLink = isSubClass(nodeTypeQName, ContentModel.TYPE_LINK);
        node.setIsLink(isLink);
    }
    if (includeParam.contains(PARAM_INCLUDE_ISLOCKED)) {
        boolean isLocked = isLocked(nodeRef, aspects);
        node.setIsLocked(isLocked);
    }
    if (includeParam.contains(PARAM_INCLUDE_ISFAVORITE)) {
        boolean isFavorite = isFavorite(nodeRef);
        node.setIsFavorite(isFavorite);
    }
    if (includeParam.contains(PARAM_INCLUDE_ALLOWABLEOPERATIONS)) {
        // note: refactor when requirements change
        Map<String, String> mapPermsToOps = new HashMap<>(3);
        mapPermsToOps.put(PermissionService.DELETE, OP_DELETE);
        mapPermsToOps.put(PermissionService.ADD_CHILDREN, OP_CREATE);
        mapPermsToOps.put(PermissionService.WRITE, OP_UPDATE);
        mapPermsToOps.put(PermissionService.CHANGE_PERMISSIONS, OP_UPDATE_PERMISSIONS);
        List<String> allowableOperations = new ArrayList<>(3);
        for (Entry<String, String> kv : mapPermsToOps.entrySet()) {
            String perm = kv.getKey();
            String op = kv.getValue();
            if (perm.equals(PermissionService.ADD_CHILDREN) && Type.DOCUMENT.equals(type)) {
                // special case: do not return "create" (as an allowable op) for file/content types - note: 'type' can be null
                continue;
            } else if (perm.equals(PermissionService.DELETE) && (isSpecialNode(nodeRef, nodeTypeQName))) {
                // special case: do not return "delete" (as an allowable op) for specific system nodes
                continue;
            } else if (permissionService.hasPermission(nodeRef, perm) == AccessStatus.ALLOWED) {
                allowableOperations.add(op);
            }
        }
        node.setAllowableOperations((allowableOperations.size() > 0) ? allowableOperations : null);
    }
    if (includeParam.contains(PARAM_INCLUDE_PERMISSIONS)) {
        Boolean inherit = permissionService.getInheritParentPermissions(nodeRef);
        List<NodePermissions.NodePermission> inheritedPerms = new ArrayList<>(5);
        List<NodePermissions.NodePermission> setDirectlyPerms = new ArrayList<>(5);
        Set<String> settablePerms = null;
        boolean allowRetrievePermission = true;
        try {
            for (AccessPermission accessPerm : permissionService.getAllSetPermissions(nodeRef)) {
                NodePermissions.NodePermission nodePerm = new NodePermissions.NodePermission(accessPerm.getAuthority(), accessPerm.getPermission(), accessPerm.getAccessStatus().toString());
                if (accessPerm.isSetDirectly()) {
                    setDirectlyPerms.add(nodePerm);
                } else {
                    inheritedPerms.add(nodePerm);
                }
            }
            settablePerms = permissionService.getSettablePermissions(nodeRef);
        } catch (AccessDeniedException ade) {
            // ignore - ie. denied access to retrieve permissions, eg. non-admin on root (Company Home)
            allowRetrievePermission = false;
        }
        // returned only node info that he's allowed to see
        if (allowRetrievePermission) {
            NodePermissions nodePerms = new NodePermissions(inherit, inheritedPerms, setDirectlyPerms, settablePerms);
            node.setPermissions(nodePerms);
        }
    }
    if (includeParam.contains(PARAM_INCLUDE_ASSOCIATION)) {
        // Ugh ... can we optimise this and return the actual assoc directly (via FileFolderService/GetChildrenCQ) ?
        ChildAssociationRef parentAssocRef = nodeService.getPrimaryParent(nodeRef);
        // note: parentAssocRef.parentRef can be null for -root- node !
        if ((parentAssocRef == null) || (parentAssocRef.getParentRef() == null) || (!parentAssocRef.getParentRef().equals(parentNodeRef))) {
            List<ChildAssociationRef> parentAssocRefs = nodeService.getParentAssocs(nodeRef);
            for (ChildAssociationRef pAssocRef : parentAssocRefs) {
                if (pAssocRef.getParentRef().equals(parentNodeRef)) {
                    // for now, assume same parent/child cannot appear more than once (due to unique name)
                    parentAssocRef = pAssocRef;
                    break;
                }
            }
        }
        if (parentAssocRef != null) {
            QName assocTypeQName = parentAssocRef.getTypeQName();
            if ((assocTypeQName != null) && (!EXCLUDED_NS.contains(assocTypeQName.getNamespaceURI()))) {
                AssocChild childAssoc = new AssocChild(assocTypeQName.toPrefixString(namespaceService), parentAssocRef.isPrimary());
                node.setAssociation(childAssoc);
            }
        }
    }
    if (includeParam.contains(PARAM_INCLUDE_DEFINITION)) {
        NodeDefinition nodeDefinition = nodeDefinitionMapper.fromTypeDefinition(getTypeDefinition(nodeRef), dictionaryService);
        node.setDefinition(nodeDefinition);
    }
    node.setNodeType(nodeTypeQName.toPrefixString(namespaceService));
    node.setPath(pathInfo);
    return node;
}
Also used : Serializable(java.io.Serializable) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) NodePermissions(org.alfresco.rest.api.model.NodePermissions) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Node(org.alfresco.rest.api.model.Node) ArrayList(java.util.ArrayList) AssocChild(org.alfresco.rest.api.model.AssocChild) NodeDefinition(org.alfresco.rest.api.model.NodeDefinition) Document(org.alfresco.rest.api.model.Document) Folder(org.alfresco.rest.api.model.Folder) FilterPropBoolean(org.alfresco.repo.node.getchildren.FilterPropBoolean) QName(org.alfresco.service.namespace.QName) AccessPermission(org.alfresco.service.cmr.security.AccessPermission) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) VersionType(org.alfresco.service.cmr.version.VersionType) ActivityType(org.alfresco.repo.activities.ActivityType) PathInfo(org.alfresco.rest.api.model.PathInfo)

Example 4 with Document

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

the class FavouritesImpl method getTarget.

private Target getTarget(PersonFavourite personFavourite, Parameters parameters) {
    Target target = null;
    NodeRef nodeRef = personFavourite.getNodeRef();
    Type type = personFavourite.getType();
    if (type.equals(Type.FILE)) {
        Document document = nodes.getDocument(nodeRef);
        setPathInfo(document, parameters.getInclude());
        target = new DocumentTarget(document);
    } else if (type.equals(Type.FOLDER)) {
        Folder folder = nodes.getFolder(nodeRef);
        setPathInfo(folder, parameters.getInclude());
        target = new FolderTarget(folder);
    } else if (type.equals(Type.SITE)) {
        SiteInfo siteInfo = siteService.getSite(nodeRef);
        String role = sites.getSiteRole(siteInfo.getShortName());
        Site site = new Site(siteInfo, role);
        target = new SiteTarget(site);
    } else {
        throw new AlfrescoRuntimeException("Unexpected favourite target type: " + type);
    }
    return target;
}
Also used : Site(org.alfresco.rest.api.model.Site) NodeRef(org.alfresco.service.cmr.repository.NodeRef) SiteInfo(org.alfresco.service.cmr.site.SiteInfo) SiteTarget(org.alfresco.rest.api.model.SiteTarget) DocumentTarget(org.alfresco.rest.api.model.DocumentTarget) FolderTarget(org.alfresco.rest.api.model.FolderTarget) Target(org.alfresco.rest.api.model.Target) Type(org.alfresco.service.cmr.favourites.FavouritesService.Type) DocumentTarget(org.alfresco.rest.api.model.DocumentTarget) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) SiteTarget(org.alfresco.rest.api.model.SiteTarget) FolderTarget(org.alfresco.rest.api.model.FolderTarget) Document(org.alfresco.rest.api.model.Document) Folder(org.alfresco.rest.api.model.Folder)

Aggregations

Document (org.alfresco.rest.api.model.Document)4 Folder (org.alfresco.rest.api.model.Folder)3 Serializable (java.io.Serializable)2 ActivityType (org.alfresco.repo.activities.ActivityType)2 DocumentTarget (org.alfresco.rest.api.model.DocumentTarget)2 FolderTarget (org.alfresco.rest.api.model.FolderTarget)2 Site (org.alfresco.rest.api.model.Site)2 SiteTarget (org.alfresco.rest.api.model.SiteTarget)2 Target (org.alfresco.rest.api.model.Target)2 VersionType (org.alfresco.service.cmr.version.VersionType)2 QName (org.alfresco.service.namespace.QName)2 JsonToken (com.fasterxml.jackson.core.JsonToken)1 BeanProperty (com.fasterxml.jackson.databind.BeanProperty)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 PropertyName (com.fasterxml.jackson.databind.PropertyName)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1