Search in sources :

Example 21 with ConstraintViolatedException

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

the class CustomModelsImpl method mergeProperties.

private void mergeProperties(AbstractClassModel existingDetails, AbstractClassModel newDetails, Parameters parameters, boolean isModelActive) {
    validateList(newDetails.getProperties(), "cmm.rest_api.properties_empty_null");
    // Transform existing properties into a map
    Map<String, CustomModelProperty> existingProperties = transformToMap(existingDetails.getProperties(), toNameFunction());
    // Transform new properties into a map
    Map<String, CustomModelProperty> newProperties = transformToMap(newDetails.getProperties(), toNameFunction());
    String propName = parameters.getParameter(PARAM_UPDATE_PROP);
    // (propName == null) => property create request
    if (propName == null) {
        // As this is a create request, check for duplicate properties
        for (String name : newProperties.keySet()) {
            if (existingProperties.containsKey(name)) {
                throw new ConstraintViolatedException(I18NUtil.getMessage("cmm.rest_api.property_create_name_already_in_use", name));
            }
        }
    } else {
        // Update request
        CustomModelProperty existingProp = existingProperties.get(propName);
        if (existingProp == null) {
            throw new EntityNotFoundException(propName);
        }
        CustomModelProperty modifiedProp = newProperties.get(propName);
        if (modifiedProp == null) {
            throw new InvalidArgumentException("cmm.rest_api.property_update_prop_not_found", new Object[] { propName });
        }
        existingProp.setTitle(modifiedProp.getTitle());
        existingProp.setDescription(modifiedProp.getDescription());
        existingProp.setDefaultValue(modifiedProp.getDefaultValue());
        existingProp.setConstraintRefs(modifiedProp.getConstraintRefs());
        existingProp.setConstraints(modifiedProp.getConstraints());
        if (isModelActive) {
            validateActivePropertyUpdate(existingProp, modifiedProp);
        }
        existingProp.setDataType(modifiedProp.getDataType());
        existingProp.setMandatory(modifiedProp.isMandatory());
        existingProp.setMandatoryEnforced(modifiedProp.isMandatoryEnforced());
        existingProp.setMultiValued(modifiedProp.isMultiValued());
    }
    // Override properties
    existingProperties.putAll(newProperties);
    existingDetails.setProperties(new ArrayList<>(existingProperties.values()));
}
Also used : InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) CustomModelProperty(org.alfresco.rest.api.model.CustomModelProperty)

Example 22 with ConstraintViolatedException

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

the class CustomModelsImpl method createCustomModel.

@Override
public CustomModel createCustomModel(M2Model m2Model) {
    // Check the current user is authorised to import the custom model
    validateCurrentUser();
    validateImportedM2Model(m2Model);
    CompiledModel compiledModel = null;
    try {
        compiledModel = customModelService.compileModel(m2Model);
    } catch (CustomModelConstraintException mce) {
        throw new ConstraintViolatedException(mce.getMessage());
    } catch (InvalidCustomModelException iex) {
        throw new InvalidArgumentException(iex.getMessage());
    }
    ModelDefinition modelDefinition = compiledModel.getModelDefinition();
    CustomModel customModel = new CustomModel();
    customModel.setName(modelDefinition.getName().getLocalName());
    customModel.setAuthor(modelDefinition.getAuthor());
    customModel.setDescription(modelDefinition.getDescription(dictionaryService));
    customModel.setStatus(ModelStatus.DRAFT);
    NamespaceDefinition nsd = modelDefinition.getNamespaces().iterator().next();
    customModel.setNamespaceUri(nsd.getUri());
    customModel.setNamespacePrefix(nsd.getPrefix());
    List<CustomType> customTypes = convertToCustomTypes(compiledModel.getTypes(), false);
    List<CustomAspect> customAspects = convertToCustomAspects(compiledModel.getAspects(), false);
    List<ConstraintDefinition> constraintDefinitions = CustomModelDefinitionImpl.removeInlineConstraints(compiledModel);
    List<CustomModelConstraint> customModelConstraints = convertToCustomModelConstraints(constraintDefinitions);
    customModel.setTypes(customTypes);
    customModel.setAspects(customAspects);
    customModel.setConstraints(customModelConstraints);
    return createCustomModelImpl(customModel, false);
}
Also used : CustomType(org.alfresco.rest.api.model.CustomType) InvalidCustomModelException(org.alfresco.service.cmr.dictionary.CustomModelException.InvalidCustomModelException) CustomAspect(org.alfresco.rest.api.model.CustomAspect) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) CustomModelConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.CustomModelConstraintException) CustomModel(org.alfresco.rest.api.model.CustomModel) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) CompiledModel(org.alfresco.repo.dictionary.CompiledModel) ModelDefinition(org.alfresco.service.cmr.dictionary.ModelDefinition) CustomModelDefinition(org.alfresco.service.cmr.dictionary.CustomModelDefinition) NamespaceDefinition(org.alfresco.service.cmr.dictionary.NamespaceDefinition) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint)

Example 23 with ConstraintViolatedException

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

the class CustomModelsImpl method deleteCustomType.

@Override
public void deleteCustomType(String modelName, String typeName) {
    // Check the current user is authorised to delete the custom model's type
    validateCurrentUser();
    if (typeName == null) {
        throw new InvalidArgumentException(TYPE_NAME_NULL_ERR);
    }
    ModelDetails existingModelDetails = new ModelDetails(getCustomModelImpl(modelName));
    if (existingModelDetails.isActive()) {
        throw new ConstraintViolatedException("cmm.rest_api.type_cannot_delete");
    }
    Map<String, CustomType> allTypes = transformToMap(existingModelDetails.getTypes(), toNameFunction());
    CustomType typeToBeDeleted = allTypes.get(typeName);
    if (typeToBeDeleted == null) {
        throw new EntityNotFoundException(typeName);
    }
    // Validate type's dependency
    validateTypeAspectDelete(allTypes.values(), typeToBeDeleted.getPrefixedName());
    // Remove the validated type
    allTypes.remove(typeName);
    existingModelDetails.setTypes(new ArrayList<>(allTypes.values()));
    updateModel(existingModelDetails, "cmm.rest_api.type_delete_failure");
}
Also used : CustomType(org.alfresco.rest.api.model.CustomType) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)

Example 24 with ConstraintViolatedException

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

the class NodesImpl method upload.

@Override
public Node upload(String parentFolderNodeId, FormData formData, Parameters parameters) {
    if (formData == null || !formData.getIsMultiPart()) {
        throw new InvalidArgumentException("The request content-type is not multipart: " + parentFolderNodeId);
    }
    NodeRef parentNodeRef = validateOrLookupNode(parentFolderNodeId, null);
    if (!nodeMatches(parentNodeRef, Collections.singleton(ContentModel.TYPE_FOLDER), null, false)) {
        throw new InvalidArgumentException("NodeId of folder is expected: " + parentNodeRef.getId());
    }
    String fileName = null;
    Content content = null;
    boolean autoRename = false;
    QName nodeTypeQName = ContentModel.TYPE_CONTENT;
    // If a fileName clashes for a versionable file
    boolean overwrite = false;
    Boolean versionMajor = null;
    String versionComment = null;
    String relativePath = null;
    String renditionNames = null;
    Map<String, Object> qnameStrProps = new HashMap<>();
    Map<QName, Serializable> properties = null;
    for (FormData.FormField field : formData.getFields()) {
        switch(field.getName().toLowerCase()) {
            case "name":
                String str = getStringOrNull(field.getValue());
                if ((str != null) && (!str.isEmpty())) {
                    fileName = str;
                }
                break;
            case "filedata":
                if (field.getIsFile()) {
                    fileName = (fileName != null ? fileName : field.getFilename());
                    content = field.getContent();
                }
                break;
            case "autorename":
                autoRename = Boolean.valueOf(field.getValue());
                break;
            case "nodetype":
                nodeTypeQName = createQName(getStringOrNull(field.getValue()));
                if (!isSubClass(nodeTypeQName, ContentModel.TYPE_CONTENT)) {
                    throw new InvalidArgumentException("Can only upload type of cm:content: " + nodeTypeQName);
                }
                break;
            case "overwrite":
                overwrite = Boolean.valueOf(field.getValue());
                break;
            case "majorversion":
                versionMajor = Boolean.valueOf(field.getValue());
                break;
            case "comment":
                versionComment = getStringOrNull(field.getValue());
                break;
            case "relativepath":
                relativePath = getStringOrNull(field.getValue());
                break;
            case "renditions":
                renditionNames = getStringOrNull(field.getValue());
                break;
            default:
                {
                    final String propName = field.getName();
                    if (propName.indexOf(QName.NAMESPACE_PREFIX) > -1) {
                        qnameStrProps.put(propName, field.getValue());
                    }
                }
        }
    }
    // result in a success message, but the files do not appear.
    if (formData.getFields().length == 0) {
        throw new ConstraintViolatedException("No disk space available");
    }
    // destination, or site + container or updateNodeRef
    if ((fileName == null) || fileName.isEmpty() || (content == null)) {
        throw new InvalidArgumentException("Required parameters are missing");
    }
    if (autoRename && overwrite) {
        throw new InvalidArgumentException("Both 'overwrite' and 'autoRename' should not be true when uploading a file");
    }
    // if requested, make (get or create) path
    parentNodeRef = getOrCreatePath(parentNodeRef, relativePath);
    final QName assocTypeQName = ContentModel.ASSOC_CONTAINS;
    final Set<String> renditions = getRequestedRenditions(renditionNames);
    try {
        // Map the given properties, if any.
        if (qnameStrProps.size() > 0) {
            properties = mapToNodeProperties(qnameStrProps);
        }
        /*
             * Existing file handling
             */
        NodeRef existingFile = nodeService.getChildByName(parentNodeRef, assocTypeQName, fileName);
        if (existingFile != null) {
            // File already exists, decide what to do
            if (autoRename) {
                // attempt to find a unique name
                fileName = findUniqueName(parentNodeRef, fileName);
            // drop-through !
            } else if (overwrite && nodeService.hasAspect(existingFile, ContentModel.ASPECT_VERSIONABLE)) {
                // overwrite existing (versionable) file
                BasicContentInfo contentInfo = new ContentInfoImpl(content.getMimetype(), content.getEncoding(), -1, null);
                return updateExistingFile(parentNodeRef, existingFile, fileName, contentInfo, content.getInputStream(), parameters, versionMajor, versionComment);
            } else {
                // name clash (and no autoRename or overwrite)
                throw new ConstraintViolatedException(fileName + " already exists.");
            }
        }
        // Note: pending REPO-159, we currently auto-enable versioning on new upload (but not when creating empty file)
        if (versionMajor == null) {
            versionMajor = true;
        }
        // Create a new file.
        NodeRef nodeRef = createNewFile(parentNodeRef, fileName, nodeTypeQName, content, properties, assocTypeQName, parameters, versionMajor, versionComment);
        // Create the response
        final Node fileNode = getFolderOrDocumentFullInfo(nodeRef, parentNodeRef, nodeTypeQName, parameters);
        // RA-1052
        try {
            List<ThumbnailDefinition> thumbnailDefs = getThumbnailDefs(renditions);
            requestRenditions(thumbnailDefs, fileNode);
        } catch (Exception ex) {
            // Note: The log level is not 'error' as it could easily fill out the log file, especially in the Cloud.
            if (logger.isDebugEnabled()) {
                // Don't throw the exception as we don't want the the upload to fail, just log it.
                logger.debug("Asynchronous request to create a rendition upon upload failed: " + ex.getMessage());
            }
        }
        return fileNode;
    // Do not clean formData temp files to allow for retries.
    // Temp files will be deleted later when GC call DiskFileItem#finalize() method or by temp file cleaner.
    } catch (AccessDeniedException ade) {
        throw new PermissionDeniedException(ade.getMessage());
    }
/*
         * NOTE: Do not clean formData temp files to allow for retries. It's
         * possible for a temp file to remain if max retry attempts are
         * made, but this is rare, so leave to usual temp file cleanup.
         */
}
Also used : FormData(org.springframework.extensions.webscripts.servlet.FormData) Serializable(java.io.Serializable) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) Node(org.alfresco.rest.api.model.Node) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) FileExistsException(org.alfresco.service.cmr.model.FileExistsException) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) DuplicateChildNodeNameException(org.alfresco.service.cmr.repository.DuplicateChildNodeNameException) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException) IntegrityException(org.alfresco.repo.node.integrity.IntegrityException) IOException(java.io.IOException) RequestEntityTooLargeException(org.alfresco.rest.framework.core.exceptions.RequestEntityTooLargeException) DisabledServiceException(org.alfresco.rest.framework.core.exceptions.DisabledServiceException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) ContentQuotaException(org.alfresco.service.cmr.usage.ContentQuotaException) UnsupportedMediaTypeException(org.alfresco.rest.framework.core.exceptions.UnsupportedMediaTypeException) AssociationExistsException(org.alfresco.service.cmr.repository.AssociationExistsException) InsufficientStorageException(org.alfresco.rest.framework.core.exceptions.InsufficientStorageException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) NodeLockedException(org.alfresco.service.cmr.lock.NodeLockedException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) ContentLimitViolationException(org.alfresco.repo.content.ContentLimitViolationException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ThumbnailDefinition(org.alfresco.repo.thumbnail.ThumbnailDefinition) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) Content(org.springframework.extensions.surf.util.Content) ContentInfoImpl(org.alfresco.rest.framework.resource.content.ContentInfoImpl) BasicContentInfo(org.alfresco.rest.framework.resource.content.BasicContentInfo) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) FilterPropBoolean(org.alfresco.repo.node.getchildren.FilterPropBoolean)

Example 25 with ConstraintViolatedException

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

the class NodesImpl method makeFolders.

private NodeRef makeFolders(NodeRef parentNodeRef, List<String> pathElements) {
    NodeRef currentParentRef = parentNodeRef;
    // just loop and create if necessary
    for (final String element : pathElements) {
        final NodeRef contextNodeRef = currentParentRef;
        // does it exist?
        // Navigation should not check permissions
        NodeRef nodeRef = AuthenticationUtil.runAs(new RunAsWork<NodeRef>() {

            @Override
            public NodeRef doWork() throws Exception {
                return nodeService.getChildByName(contextNodeRef, ContentModel.ASSOC_CONTAINS, element);
            }
        }, AuthenticationUtil.getSystemUserName());
        if (nodeRef == null) {
            try {
                // Checks for create permissions as the fileFolderService is a public service.
                FileInfo createdFileInfo = fileFolderService.create(currentParentRef, element, ContentModel.TYPE_FOLDER);
                currentParentRef = createdFileInfo.getNodeRef();
            } catch (AccessDeniedException ade) {
                throw new PermissionDeniedException(ade.getMessage());
            } catch (FileExistsException fex) {
                // Assume concurrency failure, so retry
                throw new ConcurrencyFailureException(fex.getMessage());
            }
        } else if (!isSubClass(nodeRef, ContentModel.TYPE_FOLDER, false)) {
            String parentName = (String) nodeService.getProperty(contextNodeRef, ContentModel.PROP_NAME);
            throw new ConstraintViolatedException("Name [" + element + "] already exists in the target parent: " + parentName);
        } else {
            // it exists
            currentParentRef = nodeRef;
        }
    }
    return currentParentRef;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) FileInfo(org.alfresco.service.cmr.model.FileInfo) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) FileExistsException(org.alfresco.service.cmr.model.FileExistsException) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) DuplicateChildNodeNameException(org.alfresco.service.cmr.repository.DuplicateChildNodeNameException) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException) IntegrityException(org.alfresco.repo.node.integrity.IntegrityException) IOException(java.io.IOException) RequestEntityTooLargeException(org.alfresco.rest.framework.core.exceptions.RequestEntityTooLargeException) DisabledServiceException(org.alfresco.rest.framework.core.exceptions.DisabledServiceException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) ContentQuotaException(org.alfresco.service.cmr.usage.ContentQuotaException) UnsupportedMediaTypeException(org.alfresco.rest.framework.core.exceptions.UnsupportedMediaTypeException) AssociationExistsException(org.alfresco.service.cmr.repository.AssociationExistsException) InsufficientStorageException(org.alfresco.rest.framework.core.exceptions.InsufficientStorageException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) NodeLockedException(org.alfresco.service.cmr.lock.NodeLockedException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) ContentLimitViolationException(org.alfresco.repo.content.ContentLimitViolationException) FileExistsException(org.alfresco.service.cmr.model.FileExistsException)

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