Search in sources :

Example 1 with InvalidTypeException

use of org.alfresco.service.cmr.dictionary.InvalidTypeException in project alfresco-repository by Alfresco.

the class ContentServiceImpl method getContentData.

private ContentData getContentData(NodeRef nodeRef, QName propertyQName) {
    ContentData contentData = null;
    Serializable propValue = nodeService.getProperty(nodeRef, propertyQName);
    if (propValue instanceof Collection) {
        Collection<Serializable> colPropValue = (Collection<Serializable>) propValue;
        if (colPropValue.size() > 0) {
            propValue = colPropValue.iterator().next();
        }
    }
    if (propValue instanceof ContentData) {
        contentData = (ContentData) propValue;
    }
    if (contentData == null) {
        PropertyDefinition contentPropDef = dictionaryService.getProperty(propertyQName);
        // if no value or a value other content, and a property definition has been provided, ensure that it's CONTENT or ANY
        if (contentPropDef != null && (!(contentPropDef.getDataType().getName().equals(DataTypeDefinition.CONTENT) || contentPropDef.getDataType().getName().equals(DataTypeDefinition.ANY)))) {
            throw new InvalidTypeException("The node property must be of type content: \n" + "   node: " + nodeRef + "\n" + "   property name: " + propertyQName + "\n" + "   property type: " + ((contentPropDef == null) ? "unknown" : contentPropDef.getDataType()), propertyQName);
        }
    }
    return contentData;
}
Also used : Serializable(java.io.Serializable) ContentData(org.alfresco.service.cmr.repository.ContentData) Collection(java.util.Collection) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) InvalidTypeException(org.alfresco.service.cmr.dictionary.InvalidTypeException)

Example 2 with InvalidTypeException

use of org.alfresco.service.cmr.dictionary.InvalidTypeException in project alfresco-repository by Alfresco.

the class NodeServiceTest method testUpdateContentPermissionWithRestrictions.

/**
 * See MNT-20850
 */
@Test
public void testUpdateContentPermissionWithRestrictions() {
    NodeRef workspaceRootNodeRef = nodeService.getRootNode(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE);
    String content = "Some content";
    String userName1 = GUID.generate();
    String userName2 = GUID.generate();
    HashMap<QName, Serializable> properties = new HashMap<>();
    properties.put(ContentModel.PROP_USERNAME, userName1);
    personService.createPerson(properties);
    properties.put(ContentModel.PROP_USERNAME, userName2);
    personService.createPerson(properties);
    Map<QName, Serializable> props = new HashMap<>(3);
    props.put(ContentModel.PROP_NAME, GUID.generate());
    NodeRef folder1 = nodeService.createNode(workspaceRootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName(NAMESPACE, GUID.generate()), ContentModel.TYPE_FOLDER, props).getChildRef();
    props.put(ContentModel.PROP_NAME, GUID.generate());
    NodeRef folder2 = nodeService.createNode(workspaceRootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName(NAMESPACE, GUID.generate()), ContentModel.TYPE_FOLDER, props).getChildRef();
    permissionService.setPermission(folder1, userName1, PermissionService.ALL_PERMISSIONS, true);
    permissionService.setInheritParentPermissions(folder1, false);
    permissionService.setPermission(folder2, userName2, PermissionService.ALL_PERMISSIONS, true);
    permissionService.setInheritParentPermissions(folder2, false);
    ContentData contentProp1 = AuthenticationUtil.runAs(() -> {
        NodeRef nodeRef = createContentNode(folder1);
        // Should be possible to add content via contentService
        addContentToNode(nodeRef);
        try {
            AuthenticationUtil.runAs(() -> {
                contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
                fail("The content of node1 should not be readable by user 2");
                return null;
            }, userName2);
        } catch (Exception e) {
            // expected
            assertTrue("The AccessDeniedException should be thrown.", e instanceof AccessDeniedException);
        }
        return DefaultTypeConverter.INSTANCE.convert(ContentData.class, nodeService.getProperty(nodeRef, ContentModel.PROP_CONTENT));
    }, userName1);
    AuthenticationUtil.runAs(() -> {
        NodeRef nodeRef = nodeService.createNode(folder2, ContentModel.ASSOC_CONTAINS, QName.createQName(GUID.generate()), ContentModel.TYPE_CONTENT).getChildRef();
        try {
            nodeService.setProperty(nodeRef, ContentModel.PROP_CONTENT, contentProp1);
            fail("Should not be possible to call setProperty directly to set content");
        } catch (InvalidTypeException ite) {
        // expected
        }
        try {
            Map<QName, Serializable> testProps = new HashMap<>();
            testProps.put(ContentModel.PROP_CONTENT, contentProp1);
            nodeService.setProperties(nodeRef, testProps);
            fail("Should not be possible to call setProperties directly to set content");
        } catch (InvalidTypeException ite) {
        // expected
        }
        try {
            Map<QName, Serializable> testProps = new HashMap<>();
            testProps.put(ContentModel.PROP_CONTENT, contentProp1);
            nodeService.addProperties(nodeRef, testProps);
            fail("Should not be possible to call addProperties directly to set content");
        } catch (InvalidTypeException ite) {
        // expected
        }
        try {
            Map<QName, Serializable> testProps = new HashMap<>();
            testProps.put(ContentModel.PROP_CONTENT, contentProp1);
            nodeService.addAspect(nodeRef, ContentModel.ASPECT_OWNABLE, testProps);
            fail("Should not be possible to call addAspect directly to set content");
        } catch (InvalidTypeException ite) {
        // expected
        }
        try {
            Map<QName, Serializable> testProps = new HashMap<>();
            testProps.put(ContentModel.PROP_CONTENT, contentProp1);
            createContentNode(folder2, testProps);
            fail("Should not be possible to call createNode directly to set content");
        } catch (InvalidTypeException ite) {
        // expected
        }
        ContentReader contentReader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
        assertNull("The second node should not have any content (all attempts should fail)", contentReader);
        return null;
    }, userName2);
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) ContentData(org.alfresco.service.cmr.repository.ContentData) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) ContentReader(org.alfresco.service.cmr.repository.ContentReader) InvalidTypeException(org.alfresco.service.cmr.dictionary.InvalidTypeException) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) DuplicateChildNodeNameException(org.alfresco.service.cmr.repository.DuplicateChildNodeNameException) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) InvalidTypeException(org.alfresco.service.cmr.dictionary.InvalidTypeException) CannedQueryDAOTest(org.alfresco.repo.domain.query.CannedQueryDAOTest) Test(org.junit.Test)

Example 3 with InvalidTypeException

use of org.alfresco.service.cmr.dictionary.InvalidTypeException in project alfresco-repository by Alfresco.

the class ContentPropertyRestrictionInterceptor method invoke.

@Override
@SuppressWarnings("unchecked")
public Object invoke(MethodInvocation invocation) throws Throwable {
    String methodName = invocation.getMethod().getName();
    Object[] args = invocation.getArguments();
    if (globalContentPropertyRestrictions && !isCallerWhiteListed()) {
        if (methodName.equals("setProperties")) {
            Map<QName, Serializable> properties = args[1] != null ? Collections.unmodifiableMap((Map<QName, Serializable>) args[1]) : Collections.emptyMap();
            NodeRef nodeRef = (NodeRef) args[0];
            if (nodeRef != null) {
                for (QName propQname : properties.keySet()) {
                    if (isContentProperty(propQname, properties.get(propQname)) && isContentNotNullOrEmpty(properties.get(propQname)) && isContentChanged(nodeRef, propQname, properties.get(propQname))) {
                        throw new InvalidTypeException("The node's content can't be updated via NodeService#setProperties directly: \n" + "   node: " + args[0] + "\n" + "   property name: " + propQname.getLocalName(), propQname);
                    }
                }
            }
        } else if (methodName.equals("addProperties")) {
            Map<QName, Serializable> properties = args[1] != null ? Collections.unmodifiableMap((Map<QName, Serializable>) args[1]) : Collections.emptyMap();
            NodeRef nodeRef = (NodeRef) args[0];
            if (nodeRef != null) {
                for (QName propQname : properties.keySet()) {
                    if (isContentProperty(propQname, properties.get(propQname)) && isContentNotNullOrEmpty(properties.get(propQname)) && isContentChanged(nodeRef, propQname, properties.get(propQname))) {
                        throw new InvalidTypeException("The node's content can't be updated via NodeService#addProperties directly: \n" + "   node: " + args[0] + "\n" + "   property name: " + propQname.getLocalName(), propQname);
                    }
                }
            }
        } else if (methodName.equals("createNode") && args.length == 5) {
            Map<QName, Serializable> properties = args[4] != null ? Collections.unmodifiableMap((Map<QName, Serializable>) args[4]) : Collections.emptyMap();
            for (QName propQname : properties.keySet()) {
                if (isContentProperty(propQname, properties.get(propQname)) && isContentNotNullOrEmpty(properties.get(propQname))) {
                    throw new InvalidTypeException("The node's content can't be updated via NodeService#createNode directly: \n" + "   node: " + args[0] + "\n" + "   property name: " + propQname.getLocalName(), propQname);
                }
            }
        } else if (methodName.equals("setProperty")) {
            QName propQname = (QName) args[1];
            Serializable value = (Serializable) args[2];
            NodeRef nodeRef = (NodeRef) args[0];
            if (nodeRef != null) {
                if (isContentProperty(propQname, value) && isContentNotNullOrEmpty(value) && isContentChanged(nodeRef, propQname, value)) {
                    throw new InvalidTypeException("The node's content can't be updated via NodeService#setProperty directly: \n" + "   node: " + args[0] + "\n" + "   property name: " + propQname.getLocalName(), propQname);
                }
            }
        } else if (methodName.equals("addAspect")) {
            Map<QName, Serializable> properties = args[2] != null ? Collections.unmodifiableMap((Map<QName, Serializable>) args[2]) : Collections.emptyMap();
            NodeRef nodeRef = (NodeRef) args[0];
            if (nodeRef != null) {
                for (QName propQname : properties.keySet()) {
                    if (isContentProperty(propQname, properties.get(propQname)) && isContentNotNullOrEmpty(properties.get(propQname)) && isContentChanged(nodeRef, propQname, properties.get(propQname))) {
                        throw new InvalidTypeException("The node's content can't be updated via NodeService#addAspect directly: \n" + "   node: " + args[0] + "\n" + "   property name: " + propQname.getLocalName(), propQname);
                    }
                }
            }
        }
    }
    return invocation.proceed();
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) QName(org.alfresco.service.namespace.QName) Map(java.util.Map) InvalidTypeException(org.alfresco.service.cmr.dictionary.InvalidTypeException)

Example 4 with InvalidTypeException

use of org.alfresco.service.cmr.dictionary.InvalidTypeException in project alfresco-repository by Alfresco.

the class DbNodeServiceImpl method setType.

/**
 * @see org.alfresco.service.cmr.repository.NodeService#setType(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
 */
@Extend(traitAPI = NodeServiceTrait.class, extensionAPI = NodeServiceExtension.class)
public void setType(NodeRef nodeRef, QName typeQName) throws InvalidNodeRefException {
    // The node(s) involved may not be pending deletion
    checkPendingDelete(nodeRef);
    // check the node type
    TypeDefinition nodeTypeDef = dictionaryService.getType(typeQName);
    if (nodeTypeDef == null) {
        throw new InvalidTypeException(typeQName);
    }
    Pair<Long, NodeRef> nodePair = getNodePairNotNull(nodeRef);
    // Invoke policies
    invokeBeforeUpdateNode(nodeRef);
    QName oldType = nodeDAO.getNodeType(nodePair.getFirst());
    invokeBeforeSetType(nodeRef, oldType, typeQName);
    // Set the type
    boolean updatedNode = nodeDAO.updateNode(nodePair.getFirst(), typeQName, null);
    // Add the default aspects and properties required for the given type. Existing values will not be overridden.
    boolean updatedProps = addAspectsAndProperties(nodePair, typeQName, null, null, null, null, false);
    // Invoke policies
    if (updatedNode || updatedProps) {
        // Invoke policies
        invokeOnUpdateNode(nodeRef);
        invokeOnSetType(nodeRef, oldType, typeQName);
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) QName(org.alfresco.service.namespace.QName) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) InvalidTypeException(org.alfresco.service.cmr.dictionary.InvalidTypeException) Extend(org.alfresco.traitextender.Extend)

Example 5 with InvalidTypeException

use of org.alfresco.service.cmr.dictionary.InvalidTypeException in project alfresco-repository by Alfresco.

the class DbNodeServiceImpl method createNode.

/**
 * {@inheritDoc}
 */
@Extend(traitAPI = NodeServiceTrait.class, extensionAPI = NodeServiceExtension.class)
public ChildAssociationRef createNode(NodeRef parentRef, QName assocTypeQName, QName assocQName, QName nodeTypeQName, Map<QName, Serializable> properties) {
    // The node(s) involved may not be pending deletion
    checkPendingDelete(parentRef);
    ParameterCheck.mandatory("parentRef", parentRef);
    ParameterCheck.mandatory("assocTypeQName", assocTypeQName);
    ParameterCheck.mandatory("assocQName", assocQName);
    ParameterCheck.mandatory("nodeTypeQName", nodeTypeQName);
    if (assocQName.getLocalName().length() > QName.MAX_LENGTH) {
        throw new IllegalArgumentException("Localname is too long. Length of " + assocQName.getLocalName().length() + " exceeds the maximum of " + QName.MAX_LENGTH);
    }
    // Get the parent node
    Pair<Long, NodeRef> parentNodePair = getNodePairNotNull(parentRef);
    StoreRef parentStoreRef = parentRef.getStoreRef();
    // null property map is allowed
    if (properties == null) {
        properties = Collections.emptyMap();
    }
    // get an ID for the node
    String newUuid = generateGuid(properties);
    // Invoke policy behaviour
    invokeBeforeCreateNode(parentRef, assocTypeQName, assocQName, nodeTypeQName);
    // check the node type
    TypeDefinition nodeTypeDef = dictionaryService.getType(nodeTypeQName);
    if (nodeTypeDef == null) {
        throw new InvalidTypeException(nodeTypeQName);
    }
    // Ensure child uniqueness
    String newName = extractNameProperty(properties);
    // Get the thread's locale
    Locale locale = I18NUtil.getLocale();
    // create the node instance
    ChildAssocEntity assoc = nodeDAO.newNode(parentNodePair.getFirst(), assocTypeQName, assocQName, parentStoreRef, newUuid, nodeTypeQName, locale, newName, properties);
    ChildAssociationRef childAssocRef = assoc.getRef(qnameDAO);
    Pair<Long, NodeRef> childNodePair = assoc.getChildNode().getNodePair();
    addAspectsAndProperties(childNodePair, nodeTypeQName, null, Collections.<QName>emptySet(), Collections.<QName, Serializable>emptyMap(), Collections.<QName>emptySet(), properties, true, false);
    Map<QName, Serializable> propertiesAfter = nodeDAO.getNodeProperties(childNodePair.getFirst());
    // Propagate timestamps
    propagateTimeStamps(childAssocRef);
    // Invoke policy behaviour
    invokeOnCreateNode(childAssocRef);
    invokeOnCreateChildAssociation(childAssocRef, true);
    Map<QName, Serializable> propertiesBefore = PropertyMap.EMPTY_MAP;
    invokeOnUpdateProperties(childAssocRef.getChildRef(), propertiesBefore, propertiesAfter);
    // Ensure that the parent node has the required aspects
    addAspectsAndPropertiesAssoc(parentNodePair, assocTypeQName, null, null, null, null, false);
    // done
    return childAssocRef;
}
Also used : Locale(java.util.Locale) StoreRef(org.alfresco.service.cmr.repository.StoreRef) Serializable(java.io.Serializable) QName(org.alfresco.service.namespace.QName) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ChildAssocEntity(org.alfresco.repo.domain.node.ChildAssocEntity) InvalidTypeException(org.alfresco.service.cmr.dictionary.InvalidTypeException) Extend(org.alfresco.traitextender.Extend)

Aggregations

InvalidTypeException (org.alfresco.service.cmr.dictionary.InvalidTypeException)6 Serializable (java.io.Serializable)5 NodeRef (org.alfresco.service.cmr.repository.NodeRef)5 QName (org.alfresco.service.namespace.QName)5 ContentData (org.alfresco.service.cmr.repository.ContentData)3 HashMap (java.util.HashMap)2 CannedQueryDAOTest (org.alfresco.repo.domain.query.CannedQueryDAOTest)2 TypeDefinition (org.alfresco.service.cmr.dictionary.TypeDefinition)2 Extend (org.alfresco.traitextender.Extend)2 Test (org.junit.Test)2 Collection (java.util.Collection)1 Locale (java.util.Locale)1 Map (java.util.Map)1 ChildAssocEntity (org.alfresco.repo.domain.node.ChildAssocEntity)1 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)1 PropertyDefinition (org.alfresco.service.cmr.dictionary.PropertyDefinition)1 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)1 ContentReader (org.alfresco.service.cmr.repository.ContentReader)1 DuplicateChildNodeNameException (org.alfresco.service.cmr.repository.DuplicateChildNodeNameException)1 InvalidNodeRefException (org.alfresco.service.cmr.repository.InvalidNodeRefException)1