Search in sources :

Example 26 with ConstraintViolatedException

use of org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException in project alfresco-remote-api by Alfresco.

the class NodesImpl method updateNodeImpl.

protected NodeRef updateNodeImpl(String nodeId, Node nodeInfo, Parameters parameters) {
    final NodeRef nodeRef = validateOrLookupNode(nodeId, null);
    QName nodeTypeQName = getNodeType(nodeRef);
    validateCmObject(nodeTypeQName);
    Map<QName, Serializable> props = new HashMap<>(0);
    if (nodeInfo.getProperties() != null) {
        props = mapToNodeProperties(nodeInfo.getProperties());
    }
    String name = nodeInfo.getName();
    if ((name != null) && (!name.isEmpty())) {
        // update node name if needed - note: if the name is different than existing then this is equivalent of a rename (within parent folder)
        props.put(ContentModel.PROP_NAME, name);
    }
    NodePermissions nodePerms = nodeInfo.getPermissions();
    if (nodePerms != null) {
        // Cannot set inherited permissions, only direct (locally set) permissions can be set
        if ((nodePerms.getInherited() != null) && (nodePerms.getInherited().size() > 0)) {
            throw new InvalidArgumentException("Cannot set *inherited* permissions on this node");
        }
        // Check inherit from parent value and if it's changed set the new value
        if (nodePerms.getIsInheritanceEnabled() != null) {
            if (nodePerms.getIsInheritanceEnabled() != permissionService.getInheritParentPermissions(nodeRef)) {
                permissionService.setInheritParentPermissions(nodeRef, nodePerms.getIsInheritanceEnabled());
            }
        }
        // set direct permissions
        if ((nodePerms.getLocallySet() != null)) {
            // list of all directly set permissions
            Set<AccessPermission> directPerms = new HashSet<>(5);
            for (AccessPermission accessPerm : permissionService.getAllSetPermissions(nodeRef)) {
                if (accessPerm.isSetDirectly()) {
                    directPerms.add(accessPerm);
                }
            }
            // check if same permission is sent more than once
            if (hasDuplicatePermissions(nodePerms.getLocallySet())) {
                throw new InvalidArgumentException("Duplicate node permissions, there is more than one permission with the same authority and name!");
            }
            for (NodePermissions.NodePermission nodePerm : nodePerms.getLocallySet()) {
                String permName = nodePerm.getName();
                String authorityId = nodePerm.getAuthorityId();
                AccessStatus accessStatus = AccessStatus.ALLOWED;
                if (nodePerm.getAccessStatus() != null) {
                    accessStatus = AccessStatus.valueOf(nodePerm.getAccessStatus());
                }
                if (authorityId == null || authorityId.isEmpty()) {
                    throw new InvalidArgumentException("Authority Id is expected.");
                }
                if (permName == null || permName.isEmpty()) {
                    throw new InvalidArgumentException("Permission name is expected.");
                }
                if (((!authorityId.equals(PermissionService.ALL_AUTHORITIES) && (!authorityService.authorityExists(authorityId))))) {
                    throw new InvalidArgumentException("Cannot set permissions on this node - unknown authority: " + authorityId);
                }
                AccessPermission existing = null;
                boolean addPerm = true;
                boolean updatePerm = false;
                // If the permission already exists but with different access status it will be updated
                for (AccessPermission accessPerm : directPerms) {
                    if (accessPerm.getAuthority().equals(authorityId) && accessPerm.getPermission().equals(permName)) {
                        existing = accessPerm;
                        addPerm = false;
                        if (accessPerm.getAccessStatus() != accessStatus) {
                            updatePerm = true;
                        }
                        break;
                    }
                }
                if (existing != null) {
                    // ignore existing permissions
                    directPerms.remove(existing);
                }
                if (addPerm || updatePerm) {
                    try {
                        permissionService.setPermission(nodeRef, authorityId, permName, (accessStatus == AccessStatus.ALLOWED));
                    } catch (UnsupportedOperationException e) {
                        throw new InvalidArgumentException("Cannot set permissions on this node - unknown access level: " + permName);
                    }
                }
            }
            // remove any remaining direct perms
            for (AccessPermission accessPerm : directPerms) {
                permissionService.deletePermission(nodeRef, accessPerm.getAuthority(), accessPerm.getPermission());
            }
        }
    }
    String nodeType = nodeInfo.getNodeType();
    if ((nodeType != null) && (!nodeType.isEmpty())) {
        // update node type - ensure that we are performing a specialise (we do not support generalise)
        QName destNodeTypeQName = createQName(nodeType);
        if ((!destNodeTypeQName.equals(nodeTypeQName)) && isSubClass(destNodeTypeQName, nodeTypeQName) && (!isSubClass(destNodeTypeQName, ContentModel.TYPE_SYSTEM_FOLDER))) {
            nodeService.setType(nodeRef, destNodeTypeQName);
        } else {
            throw new InvalidArgumentException("Failed to change (specialise) node type - from " + nodeTypeQName + " to " + destNodeTypeQName);
        }
    }
    NodeRef parentNodeRef = nodeInfo.getParentId();
    if (parentNodeRef != null) {
        NodeRef currentParentNodeRef = getParentNodeRef(nodeRef);
        if (currentParentNodeRef == null) {
            // implies root (Company Home) hence return 403 here
            throw new PermissionDeniedException();
        }
        if (!currentParentNodeRef.equals(parentNodeRef)) {
            // moveOrCopy(nodeRef, parentNodeRef, name, false); // not currently supported - client should use explicit POST /move operation instead
            throw new InvalidArgumentException("Cannot update parentId of " + nodeId + " via PUT /nodes/{nodeId}. Please use explicit POST /nodes/{nodeId}/move operation instead");
        }
    }
    List<String> aspectNames = nodeInfo.getAspectNames();
    updateCustomAspects(nodeRef, aspectNames, EXCLUDED_ASPECTS);
    if (props.size() > 0) {
        validatePropValues(props);
        try {
            // update node properties - note: null will unset the specified property
            nodeService.addProperties(nodeRef, props);
        } catch (DuplicateChildNodeNameException dcne) {
            throw new ConstraintViolatedException(dcne.getMessage());
        }
    }
    return nodeRef;
}
Also used : DuplicateChildNodeNameException(org.alfresco.service.cmr.repository.DuplicateChildNodeNameException) Serializable(java.io.Serializable) NodePermissions(org.alfresco.rest.api.model.NodePermissions) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) AccessPermission(org.alfresco.service.cmr.security.AccessPermission) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) AccessStatus(org.alfresco.service.cmr.security.AccessStatus) NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 27 with ConstraintViolatedException

use of org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException in project alfresco-remote-api by Alfresco.

the class RenditionsImpl method createRendition.

@Override
public void createRendition(NodeRef nodeRef, Rendition rendition, boolean executeAsync, Parameters parameters) {
    // If thumbnail generation has been configured off, then don't bother.
    if (!thumbnailService.getThumbnailsEnabled()) {
        throw new DisabledServiceException("Thumbnail generation has been disabled.");
    }
    final NodeRef sourceNodeRef = validateNode(nodeRef.getStoreRef(), nodeRef.getId());
    final NodeRef renditionNodeRef = getRenditionByName(sourceNodeRef, rendition.getId(), parameters);
    if (renditionNodeRef != null) {
        throw new ConstraintViolatedException(rendition.getId() + " rendition already exists.");
    }
    // Use the thumbnail registry to get the details of the thumbnail
    ThumbnailRegistry registry = thumbnailService.getThumbnailRegistry();
    ThumbnailDefinition thumbnailDefinition = registry.getThumbnailDefinition(rendition.getId());
    if (thumbnailDefinition == null) {
        throw new NotFoundException(rendition.getId() + " is not registered.");
    }
    ContentData contentData = getContentData(sourceNodeRef, true);
    // Check if anything is currently available to generate thumbnails for the specified mimeType
    if (!registry.isThumbnailDefinitionAvailable(contentData.getContentUrl(), contentData.getMimetype(), contentData.getSize(), sourceNodeRef, thumbnailDefinition)) {
        throw new InvalidArgumentException("Unable to create thumbnail '" + thumbnailDefinition.getName() + "' for " + contentData.getMimetype() + " as no transformer is currently available.");
    }
    Action action = ThumbnailHelper.createCreateThumbnailAction(thumbnailDefinition, serviceRegistry);
    // Create thumbnail - or else queue for async creation
    actionService.executeAction(action, sourceNodeRef, true, executeAsync);
}
Also used : DisabledServiceException(org.alfresco.rest.framework.core.exceptions.DisabledServiceException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ThumbnailDefinition(org.alfresco.repo.thumbnail.ThumbnailDefinition) Action(org.alfresco.service.cmr.action.Action) ContentData(org.alfresco.service.cmr.repository.ContentData) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) ThumbnailRegistry(org.alfresco.repo.thumbnail.ThumbnailRegistry) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)

Example 28 with ConstraintViolatedException

use of org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException in project alfresco-remote-api by Alfresco.

the class SitesImpl method addFavouriteSite.

public void addFavouriteSite(String personId, FavouriteSite favouriteSite) {
    personId = people.validatePerson(personId);
    String siteId = favouriteSite.getId();
    SiteInfo siteInfo = validateSite(siteId);
    if (siteInfo == null) {
        // site does not exist
        throw new EntityNotFoundException(siteId);
    }
    // set the site id to the short name (to deal with case sensitivity issues with using the siteId from the url)
    siteId = siteInfo.getShortName();
    StringBuilder prefKey = new StringBuilder(FAVOURITE_SITES_PREFIX);
    prefKey.append(siteId);
    String value = (String) preferenceService.getPreference(personId, prefKey.toString());
    boolean isFavouriteSite = (value == null ? false : value.equalsIgnoreCase("true"));
    if (isFavouriteSite) {
        throw new ConstraintViolatedException("Site " + siteId + " is already a favourite site");
    }
    prefKey = new StringBuilder(FAVOURITE_SITES_PREFIX);
    prefKey.append(siteId);
    Map<String, Serializable> preferences = new HashMap<String, Serializable>(1);
    preferences.put(prefKey.toString(), Boolean.TRUE);
    preferenceService.setPreferences(personId, preferences);
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) Serializable(java.io.Serializable) HashMap(java.util.HashMap) FilterPropString(org.alfresco.repo.node.getchildren.FilterPropString) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)

Example 29 with ConstraintViolatedException

use of org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException in project alfresco-remote-api by Alfresco.

the class TagsImpl method changeTag.

public Tag changeTag(StoreRef storeRef, String tagId, Tag tag) {
    try {
        NodeRef existingTagNodeRef = validateTag(storeRef, tagId);
        String existingTagName = taggingService.getTagName(existingTagNodeRef);
        String newTagName = tag.getTag();
        NodeRef newTagNodeRef = taggingService.changeTag(storeRef, existingTagName, newTagName);
        return new Tag(newTagNodeRef, newTagName);
    } catch (NonExistentTagException e) {
        throw new NotFoundException(e.getMessage());
    } catch (TagExistsException e) {
        throw new ConstraintViolatedException(e.getMessage());
    } catch (TaggingException e) {
        throw new InvalidArgumentException(e.getMessage());
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) TagExistsException(org.alfresco.repo.tagging.TagExistsException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) TaggingException(org.alfresco.repo.tagging.TaggingException) Tag(org.alfresco.rest.api.model.Tag) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) NonExistentTagException(org.alfresco.repo.tagging.NonExistentTagException)

Example 30 with ConstraintViolatedException

use of org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException in project alfresco-remote-api by Alfresco.

the class CommentsImpl method deleteComment.

@Override
public void deleteComment(String nodeId, String commentNodeId) {
    try {
        NodeRef nodeRef = nodes.validateNode(nodeId);
        NodeRef commentNodeRef = nodes.validateNode(commentNodeId);
        if (!nodeRef.equals(commentService.getDiscussableAncestor(commentNodeRef))) {
            throw new InvalidArgumentException("Unexpected " + nodeId + "," + commentNodeId);
        }
        commentService.deleteComment(commentNodeRef);
    } catch (IllegalArgumentException e) {
        throw new ConstraintViolatedException(e.getMessage());
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)

Aggregations

ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)31 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)20 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)17 NodeRef (org.alfresco.service.cmr.repository.NodeRef)14 PermissionDeniedException (org.alfresco.rest.framework.core.exceptions.PermissionDeniedException)12 QName (org.alfresco.service.namespace.QName)10 ApiException (org.alfresco.rest.framework.core.exceptions.ApiException)7 NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)7 DuplicateChildNodeNameException (org.alfresco.service.cmr.repository.DuplicateChildNodeNameException)7 Serializable (java.io.Serializable)6 HashMap (java.util.HashMap)6 IntegrityException (org.alfresco.repo.node.integrity.IntegrityException)4 CustomModelConstraintException (org.alfresco.service.cmr.dictionary.CustomModelException.CustomModelConstraintException)4 InvalidCustomModelException (org.alfresco.service.cmr.dictionary.CustomModelException.InvalidCustomModelException)4 AssociationExistsException (org.alfresco.service.cmr.repository.AssociationExistsException)4 ArrayList (java.util.ArrayList)3 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)3 CustomModel (org.alfresco.rest.api.model.CustomModel)3 DisabledServiceException (org.alfresco.rest.framework.core.exceptions.DisabledServiceException)3 InsufficientStorageException (org.alfresco.rest.framework.core.exceptions.InsufficientStorageException)3