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;
}
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);
}
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();
}
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);
}
}
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;
}
Aggregations