Search in sources :

Example 1 with NotFoundException

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

the class GroupsImpl method deleteGroupMembership.

public void deleteGroupMembership(String groupId, String groupMemberId) {
    validateGroupId(groupId, false);
    // Not allowed to modify a GROUP_EVERYONE member.
    if (PermissionService.ALL_AUTHORITIES.equals(groupId)) {
        throw new ConstraintViolatedException(ERR_MSG_MODIFY_FIXED_AUTHORITY);
    }
    validateGroupMemberId(groupMemberId);
    // Verify if groupMemberId is member of groupId
    AuthorityType authorityType = AuthorityType.getAuthorityType(groupMemberId);
    Set<String> parents = authorityService.getContainingAuthorities(AuthorityType.GROUP, groupMemberId, true);
    if (!parents.contains(groupId)) {
        throw new NotFoundException(groupMemberId + " is not member of " + groupId);
    }
    authorityService.removeAuthority(groupId, groupMemberId);
}
Also used : AuthorityType(org.alfresco.service.cmr.security.AuthorityType) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)

Example 2 with NotFoundException

use of org.alfresco.rest.framework.core.exceptions.NotFoundException in project records-management by Alfresco.

the class FilePlanComponentsApiUtils method lookupAndValidateRelativePath.

/**
 * Helper method that creates a relative path if it doesn't already exist and if relative path is not read only.
 * If relative path is read only an exception will be thrown if the provided relative path does not exist.
 * The relative path will be build with nodes of the type specified in nodesType
 * If the relative path already exists the method validates if the last element is of type nodesType
 * The method does not validate the type of parentNodeRef
 *
 * @param parentNodeRef  the first node of the path
 * @param relativePath  a string representing the relative path in the format "Folder1/Folder2/Folder3"
 * @param readOnlyRelativePath the flag that indicates if the relativePath should be created if doesn't exist or not
 * @param nodesType  the type of all the containers in the path
 * @return  the last element of the relative path
 */
public NodeRef lookupAndValidateRelativePath(final NodeRef parentNodeRef, String relativePath, boolean readOnlyRelativePath, QName nodesType) {
    mandatory("parentNodeRef", parentNodeRef);
    mandatory("nodesType", nodesType);
    if (StringUtils.isBlank(relativePath)) {
        return parentNodeRef;
    }
    List<String> pathElements = getPathElements(relativePath);
    if (pathElements.isEmpty()) {
        return parentNodeRef;
    }
    /*
         * Get the latest existing path element
         */
    NodeRef lastNodeRef = parentNodeRef;
    int i = 0;
    for (; i < pathElements.size(); i++) {
        final String pathElement = pathElements.get(i);
        final NodeRef contextParentNodeRef = lastNodeRef;
        // Navigation should not check permissions
        NodeRef child = authenticationUtil.runAsSystem(new RunAsWork<NodeRef>() {

            @Override
            public NodeRef doWork() throws Exception {
                return nodeService.getChildByName(contextParentNodeRef, ContentModel.ASSOC_CONTAINS, pathElement);
            }
        });
        if (child == null) {
            break;
        }
        lastNodeRef = child;
    }
    if (i == pathElements.size()) {
        QName nodeType = nodeService.getType(lastNodeRef);
        if (!nodeType.equals(nodesType)) {
            throw new InvalidArgumentException("The given id:'" + parentNodeRef.getId() + "' and the relative path '" + relativePath + "' reach a node type invalid for this endpoint." + " Expected nodeType is:" + nodesType.toString() + ". Actual nodeType is:" + nodeType);
        }
        return lastNodeRef;
    } else {
        if (!readOnlyRelativePath) {
            pathElements = pathElements.subList(i, pathElements.size());
        } else {
            throw new NotFoundException("The entity with relativePath: " + relativePath + " was not found.");
        }
    }
    /*
         * Starting from the latest existing element create the rest of the elements
         */
    if (nodesType.equals(RecordsManagementModel.TYPE_UNFILED_RECORD_FOLDER)) {
        for (String pathElement : pathElements) {
            lastNodeRef = fileFolderService.create(lastNodeRef, pathElement, RecordsManagementModel.TYPE_UNFILED_RECORD_FOLDER).getNodeRef();
        }
    } else if (nodesType.equals(RecordsManagementModel.TYPE_RECORD_CATEGORY)) {
        for (String pathElement : pathElements) {
            lastNodeRef = filePlanService.createRecordCategory(lastNodeRef, pathElement);
        }
    } else {
        // Throw internal error as this method should not be called for other types
        throw new InternalServerErrorException("Creating relative path of type '" + nodesType + "' not suported for this endpoint");
    }
    return lastNodeRef;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) QName(org.alfresco.service.namespace.QName) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) InternalServerErrorException(org.springframework.social.InternalServerErrorException) ContentQuotaException(org.alfresco.service.cmr.usage.ContentQuotaException) InvalidTypeException(org.alfresco.repo.model.filefolder.FileFolderServiceImpl.InvalidTypeException) InsufficientStorageException(org.alfresco.rest.framework.core.exceptions.InsufficientStorageException) DuplicateChildNodeNameException(org.alfresco.service.cmr.repository.DuplicateChildNodeNameException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) DuplicateAttributeException(org.alfresco.service.cmr.attributes.DuplicateAttributeException) NodeLockedException(org.alfresco.service.cmr.lock.NodeLockedException) InternalServerErrorException(org.springframework.social.InternalServerErrorException) IntegrityException(org.alfresco.repo.node.integrity.IntegrityException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) IOException(java.io.IOException) ContentLimitViolationException(org.alfresco.repo.content.ContentLimitViolationException) RequestEntityTooLargeException(org.alfresco.rest.framework.core.exceptions.RequestEntityTooLargeException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)

Example 3 with NotFoundException

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

the class RenditionsImpl method createRenditions.

@Override
public void createRenditions(NodeRef nodeRef, String versionLabelId, List<Rendition> renditions, Parameters parameters) throws NotFoundException, ConstraintViolatedException {
    if (renditions.isEmpty()) {
        return;
    }
    if (!renditionService2.isEnabled()) {
        throw new DisabledServiceException("Rendition generation has been disabled.");
    }
    final NodeRef sourceNodeRef = validateNode(nodeRef.getStoreRef(), nodeRef.getId(), versionLabelId, parameters);
    RenditionDefinitionRegistry2 renditionDefinitionRegistry2 = renditionService2.getRenditionDefinitionRegistry2();
    // So that POST /nodes/{nodeId}/renditions can specify rendition names as a comma separated list just like
    // POST /nodes/{nodId}/children can specify a comma separated list, the following code checks to see if the
    // supplied Rendition names are actually comma separated lists. The following example shows it is possible to
    // use both approaches.
    // [
    // { "id": "doclib" },
    // { "id": "avatar,avatar32" }
    // ]
    Set<String> renditionNames = new HashSet<>();
    for (Rendition rendition : renditions) {
        String name = getName(rendition);
        Set<String> requestedRenditions = NodesImpl.getRequestedRenditions(name);
        if (requestedRenditions == null) {
            renditionNames.add(null);
        } else {
            renditionNames.addAll(requestedRenditions);
        }
    }
    StringJoiner renditionNamesAlreadyExist = new StringJoiner(",");
    StringJoiner renditionNamesNotRegistered = new StringJoiner(",");
    List<String> renditionNamesToCreate = new ArrayList<>();
    for (String renditionName : renditionNames) {
        if (renditionName == null) {
            // 400
            throw new IllegalArgumentException(("Null rendition name supplied"));
        }
        RenditionDefinition2 renditionDefinition = renditionDefinitionRegistry2.getRenditionDefinition(renditionName);
        if (renditionDefinition == null) {
            renditionNamesNotRegistered.add(renditionName);
        }
        final NodeRef renditionNodeRef = getRenditionByName(sourceNodeRef, renditionName, parameters);
        if (renditionNodeRef == null) {
            renditionNamesToCreate.add(renditionName);
        } else {
            renditionNamesAlreadyExist.add(renditionName);
        }
    }
    if (renditionNamesNotRegistered.length() != 0) {
        // 404
        throw new NotFoundException("Renditions not registered: " + renditionNamesNotRegistered);
    }
    if (renditionNamesToCreate.size() == 0) {
        // 409
        throw new ConstraintViolatedException("All renditions requested already exist: " + renditionNamesAlreadyExist);
    }
    for (String renditionName : renditionNamesToCreate) {
        try {
            renditionService2.render(sourceNodeRef, renditionName);
        } catch (UnsupportedOperationException e) {
            // 400
            throw new IllegalArgumentException((e.getMessage()));
        } catch (IllegalStateException e) {
            // 409
            throw new StaleEntityException(e.getMessage());
        }
    }
}
Also used : DisabledServiceException(org.alfresco.rest.framework.core.exceptions.DisabledServiceException) Rendition(org.alfresco.rest.api.model.Rendition) ArrayList(java.util.ArrayList) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) RenditionDefinitionRegistry2(org.alfresco.repo.rendition2.RenditionDefinitionRegistry2) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) RenditionDefinition2(org.alfresco.repo.rendition2.RenditionDefinition2) StaleEntityException(org.alfresco.rest.framework.core.exceptions.StaleEntityException) StringJoiner(java.util.StringJoiner) HashSet(java.util.HashSet)

Example 4 with NotFoundException

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

the class RenditionsImpl method getRendition.

@Override
public Rendition getRendition(NodeRef nodeRef, String versionLabelId, String renditionId, Parameters parameters) {
    final NodeRef validatedNodeRef = validateNode(nodeRef.getStoreRef(), nodeRef.getId(), versionLabelId, parameters);
    NodeRef renditionNodeRef = getRenditionByName(validatedNodeRef, renditionId, parameters);
    boolean includeNotCreated = true;
    String status = getStatus(parameters);
    if (status != null) {
        includeNotCreated = !RenditionStatus.CREATED.equals(RenditionStatus.valueOf(status));
    }
    // if there is no rendition, then try to find the available/registered rendition (yet to be created).
    if (renditionNodeRef == null && includeNotCreated) {
        ContentData contentData = getContentData(validatedNodeRef, true);
        String sourceMimetype = contentData.getMimetype();
        long size = contentData.getSize();
        RenditionDefinitionRegistry2 renditionDefinitionRegistry2 = renditionService2.getRenditionDefinitionRegistry2();
        RenditionDefinition2 renditionDefinition = renditionDefinitionRegistry2.getRenditionDefinition(renditionId);
        if (renditionDefinition == null) {
            throw new NotFoundException(renditionId + " is not registered.");
        } else {
            Set<String> renditionNames = renditionDefinitionRegistry2.getRenditionNamesFrom(sourceMimetype, size);
            boolean found = false;
            for (String renditionName : renditionNames) {
                // Check the registered renditionId is applicable for the node's mimeType
                if (renditionId.equals(renditionName)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                throw new NotFoundException(renditionId + " is not applicable for the node's mimeType " + sourceMimetype);
            }
        }
        return toApiRendition(renditionId);
    }
    if (renditionNodeRef == null) {
        throw new NotFoundException("The rendition with id: " + renditionId + " was not found.");
    }
    return toApiRendition(renditionNodeRef);
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentData(org.alfresco.service.cmr.repository.ContentData) RenditionDefinition2(org.alfresco.repo.rendition2.RenditionDefinition2) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) RenditionDefinitionRegistry2(org.alfresco.repo.rendition2.RenditionDefinitionRegistry2)

Example 5 with NotFoundException

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

the class RenditionsImpl method getContentImpl.

private BinaryResource getContentImpl(NodeRef nodeRef, String renditionId, Parameters parameters) {
    NodeRef renditionNodeRef = getRenditionByName(nodeRef, renditionId, parameters);
    // By default set attachment header (with rendition Id) unless attachment=false
    boolean attach = true;
    String attachment = parameters.getParameter(PARAM_ATTACHMENT);
    if (attachment != null) {
        attach = Boolean.valueOf(attachment);
    }
    final String attachFileName = (attach ? renditionId : null);
    if (renditionNodeRef == null) {
        boolean isPlaceholder = Boolean.valueOf(parameters.getParameter(PARAM_PLACEHOLDER));
        if (!isPlaceholder) {
            throw new NotFoundException("Thumbnail was not found for [" + renditionId + ']');
        }
        String sourceNodeMimeType = null;
        try {
            sourceNodeMimeType = (nodeRef != null ? getMimeType(nodeRef) : null);
        } catch (InvalidArgumentException e) {
        // No content for node, e.g. ASSOC_AVATAR rather than ASSOC_PREFERENCE_IMAGE
        }
        // resource based on the content's mimeType and rendition id
        String phPath = scriptThumbnailService.getMimeAwarePlaceHolderResourcePath(renditionId, sourceNodeMimeType);
        if (phPath == null) {
            // 404 since no thumbnail was found
            throw new NotFoundException("Thumbnail was not found and no placeholder resource available for [" + renditionId + ']');
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("Retrieving content from resource path [" + phPath + ']');
            }
            // get extension of resource
            String ext = "";
            int extIndex = phPath.lastIndexOf('.');
            if (extIndex != -1) {
                ext = phPath.substring(extIndex);
            }
            try {
                final String resourcePath = "classpath:" + phPath;
                InputStream inputStream = resourceLoader.getResource(resourcePath).getInputStream();
                // create temporary file
                File file = TempFileProvider.createTempFile(inputStream, "RenditionsApi-", ext);
                return new FileBinaryResource(file, attachFileName);
            } catch (Exception ex) {
                if (logger.isErrorEnabled()) {
                    logger.error("Couldn't load the placeholder." + ex.getMessage());
                }
                throw new ApiException("Couldn't load the placeholder.");
            }
        }
    }
    Map<QName, Serializable> nodeProps = nodeService.getProperties(renditionNodeRef);
    ContentData contentData = (ContentData) nodeProps.get(ContentModel.PROP_CONTENT);
    Date modified = (Date) nodeProps.get(ContentModel.PROP_MODIFIED);
    org.alfresco.rest.framework.resource.content.ContentInfo contentInfo = null;
    if (contentData != null) {
        contentInfo = new ContentInfoImpl(contentData.getMimetype(), contentData.getEncoding(), contentData.getSize(), contentData.getLocale());
    }
    // add cache settings
    CacheDirective cacheDirective = new CacheDirective.Builder().setNeverCache(false).setMustRevalidate(false).setLastModified(modified).setETag(modified != null ? Long.toString(modified.getTime()) : null).setMaxAge(// one year (in seconds)
    Long.valueOf(31536000)).build();
    return new NodeBinaryResource(renditionNodeRef, ContentModel.PROP_CONTENT, contentInfo, attachFileName, cacheDirective);
}
Also used : Serializable(java.io.Serializable) InputStream(java.io.InputStream) QName(org.alfresco.service.namespace.QName) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) FileBinaryResource(org.alfresco.rest.framework.resource.content.FileBinaryResource) VersionDoesNotExistException(org.alfresco.service.cmr.version.VersionDoesNotExistException) StaleEntityException(org.alfresco.rest.framework.core.exceptions.StaleEntityException) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException) DisabledServiceException(org.alfresco.rest.framework.core.exceptions.DisabledServiceException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) Date(java.util.Date) NodeBinaryResource(org.alfresco.rest.framework.resource.content.NodeBinaryResource) NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) ContentData(org.alfresco.service.cmr.repository.ContentData) CacheDirective(org.alfresco.rest.framework.resource.content.CacheDirective) ContentInfoImpl(org.alfresco.rest.framework.resource.content.ContentInfoImpl) File(java.io.File) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException)

Aggregations

NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)24 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)11 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)9 NodeRef (org.alfresco.service.cmr.repository.NodeRef)9 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)8 DisabledServiceException (org.alfresco.rest.framework.core.exceptions.DisabledServiceException)6 ResourceWithMetadata (org.alfresco.rest.framework.core.ResourceWithMetadata)4 ApiException (org.alfresco.rest.framework.core.exceptions.ApiException)4 StaleEntityException (org.alfresco.rest.framework.core.exceptions.StaleEntityException)4 ContentData (org.alfresco.service.cmr.repository.ContentData)4 IntegrityException (org.alfresco.repo.node.integrity.IntegrityException)3 PermissionDeniedException (org.alfresco.rest.framework.core.exceptions.PermissionDeniedException)3 QName (org.alfresco.service.namespace.QName)3 Test (org.junit.Test)3 File (java.io.File)2 InputStream (java.io.InputStream)2 Serializable (java.io.Serializable)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 RenditionDefinition2 (org.alfresco.repo.rendition2.RenditionDefinition2)2