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