Search in sources :

Example 11 with UnsupportedResourceOperationException

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

the class TagsImpl method addTags.

public List<Tag> addTags(String nodeId, final List<Tag> tags) {
    NodeRef nodeRef = nodes.validateNode(nodeId);
    if (!typeConstraint.matches(nodeRef)) {
        throw new UnsupportedResourceOperationException("Cannot tag this node");
    }
    List<String> tagValues = new AbstractList<String>() {

        @Override
        public String get(int arg0) {
            String tag = tags.get(arg0).getTag();
            return tag;
        }

        @Override
        public int size() {
            return tags.size();
        }
    };
    try {
        List<Pair<String, NodeRef>> tagNodeRefs = taggingService.addTags(nodeRef, tagValues);
        List<Tag> ret = new ArrayList<Tag>(tags.size());
        for (Pair<String, NodeRef> pair : tagNodeRefs) {
            ret.add(new Tag(pair.getSecond(), pair.getFirst()));
        }
        return ret;
    } catch (IllegalArgumentException e) {
        throw new InvalidArgumentException(e.getMessage());
    }
}
Also used : AbstractList(java.util.AbstractList) UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) ArrayList(java.util.ArrayList) NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) Tag(org.alfresco.rest.api.model.Tag) Pair(org.alfresco.util.Pair)

Example 12 with UnsupportedResourceOperationException

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

the class ResourceWebScriptGet method executeAction.

/**
 * Executes the action on the resource
 * @param resource ResourceWithMetadata
 * @param params parameters to use
 * @return anObject the result of the execute
 */
@Override
public Object executeAction(ResourceWithMetadata resource, Params params, WithResponse withResponse) throws Throwable {
    switch(resource.getMetaData().getType()) {
        case ENTITY:
            if (StringUtils.isBlank(params.getEntityId())) {
                // Get the collection
                if (EntityResourceAction.Read.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(EntityResourceAction.Read.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    EntityResourceAction.Read<?> getter = (Read<?>) resource.getResource();
                    CollectionWithPagingInfo<?> resources = getter.readAll(params);
                    return resources;
                } else if (EntityResourceAction.ReadWithResponse.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(EntityResourceAction.ReadWithResponse.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    EntityResourceAction.ReadWithResponse<?> getter = (EntityResourceAction.ReadWithResponse<?>) resource.getResource();
                    CollectionWithPagingInfo<?> resources = getter.readAll(params, withResponse);
                    return resources;
                } else if (EntityResourceAction.DeleteSetWithResponse.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(EntityResourceAction.DeleteSetWithResponse.class)) {
                        throw new DeletedResourceException("(DELETE) " + resource.getMetaData().getUniqueId());
                    }
                    EntityResourceAction.DeleteSetWithResponse relationDeleter = (EntityResourceAction.DeleteSetWithResponse) resource.getResource();
                    relationDeleter.deleteSet(params, withResponse);
                    // Don't pass anything to the callback - its just successful
                    return null;
                } else if (EntityResourceAction.DeleteSet.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(EntityResourceAction.Delete.class)) {
                        throw new DeletedResourceException("(DELETE) " + resource.getMetaData().getUniqueId());
                    }
                    EntityResourceAction.DeleteSet relationDeleter = (EntityResourceAction.DeleteSet) resource.getResource();
                    relationDeleter.deleteSet(params);
                    // Don't pass anything to the callback - its just successful
                    return null;
                } else {
                    throw new UnsupportedResourceOperationException();
                }
            } else {
                if (EntityResourceAction.ReadById.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(EntityResourceAction.ReadById.class)) {
                        throw new DeletedResourceException("(GET by id) " + resource.getMetaData().getUniqueId());
                    }
                    EntityResourceAction.ReadById<?> entityGetter = (ReadById<?>) resource.getResource();
                    Object result = entityGetter.readById(params.getEntityId(), params);
                    return result;
                } else if (EntityResourceAction.ReadByIdWithResponse.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(EntityResourceAction.ReadByIdWithResponse.class)) {
                        throw new DeletedResourceException("(GET by id) " + resource.getMetaData().getUniqueId());
                    }
                    EntityResourceAction.ReadByIdWithResponse<?> entityGetter = (EntityResourceAction.ReadByIdWithResponse<?>) resource.getResource();
                    Object result = entityGetter.readById(params.getEntityId(), params, withResponse);
                    return result;
                } else {
                    throw new UnsupportedResourceOperationException();
                }
            }
        case RELATIONSHIP:
            if (StringUtils.isNotBlank(params.getRelationshipId())) {
                if (RelationshipResourceAction.ReadById.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(RelationshipResourceAction.ReadById.class)) {
                        throw new DeletedResourceException("(GET by id) " + resource.getMetaData().getUniqueId());
                    }
                    RelationshipResourceAction.ReadById<?> relationGetter = (RelationshipResourceAction.ReadById<?>) resource.getResource();
                    Object result = relationGetter.readById(params.getEntityId(), params.getRelationshipId(), params);
                    return result;
                } else if (RelationshipResourceAction.ReadByIdWithResponse.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(RelationshipResourceAction.ReadByIdWithResponse.class)) {
                        throw new DeletedResourceException("(GET by id) " + resource.getMetaData().getUniqueId());
                    }
                    RelationshipResourceAction.ReadByIdWithResponse<?> relationGetter = (RelationshipResourceAction.ReadByIdWithResponse<?>) resource.getResource();
                    Object result = relationGetter.readById(params.getEntityId(), params.getRelationshipId(), params, withResponse);
                    return result;
                } else {
                    throw new UnsupportedResourceOperationException();
                }
            } else {
                if (RelationshipResourceAction.Read.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(RelationshipResourceAction.Read.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    RelationshipResourceAction.Read<?> relationGetter = (RelationshipResourceAction.Read<?>) resource.getResource();
                    CollectionWithPagingInfo<?> relations = relationGetter.readAll(params.getEntityId(), params);
                    return relations;
                } else {
                    if (resource.getMetaData().isDeleted(RelationshipResourceAction.ReadWithResponse.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    RelationshipResourceAction.ReadWithResponse<?> relationGetter = (RelationshipResourceAction.ReadWithResponse<?>) resource.getResource();
                    CollectionWithPagingInfo<?> relations = relationGetter.readAll(params.getEntityId(), params, withResponse);
                    return relations;
                }
            }
        case PROPERTY:
            if (StringUtils.isNotBlank(params.getEntityId())) {
                if (BinaryResourceAction.Read.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(BinaryResourceAction.Read.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    BinaryResourceAction.Read getter = (BinaryResourceAction.Read) resource.getResource();
                    BinaryResource prop = getter.readProperty(params.getEntityId(), params);
                    return prop;
                }
                if (BinaryResourceAction.ReadWithResponse.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(BinaryResourceAction.ReadWithResponse.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    BinaryResourceAction.ReadWithResponse getter = (BinaryResourceAction.ReadWithResponse) resource.getResource();
                    BinaryResource prop = getter.readProperty(params.getEntityId(), params, withResponse);
                    return prop;
                }
                if (RelationshipResourceBinaryAction.Read.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(RelationshipResourceBinaryAction.Read.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    RelationshipResourceBinaryAction.Read getter = (RelationshipResourceBinaryAction.Read) resource.getResource();
                    BinaryResource prop = getter.readProperty(params.getEntityId(), params.getRelationshipId(), params);
                    return prop;
                }
                if (RelationshipResourceBinaryAction.ReadWithResponse.class.isAssignableFrom(resource.getResource().getClass())) {
                    if (resource.getMetaData().isDeleted(RelationshipResourceBinaryAction.ReadWithResponse.class)) {
                        throw new DeletedResourceException("(GET) " + resource.getMetaData().getUniqueId());
                    }
                    RelationshipResourceBinaryAction.ReadWithResponse getter = (RelationshipResourceBinaryAction.ReadWithResponse) resource.getResource();
                    BinaryResource prop = getter.readProperty(params.getEntityId(), params.getRelationshipId(), params, withResponse);
                    return prop;
                }
            } else {
                throw new UnsupportedResourceOperationException();
            }
        default:
            throw new UnsupportedResourceOperationException("GET not supported for Actions");
    }
}
Also used : CollectionWithPagingInfo(org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo) DeletedResourceException(org.alfresco.rest.framework.core.exceptions.DeletedResourceException) Read(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction.Read) EntityResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction) UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) BinaryResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.BinaryResourceAction) RelationshipResourceBinaryAction(org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceBinaryAction) RelationshipResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction) ReadById(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction.ReadById) BinaryResource(org.alfresco.rest.framework.resource.content.BinaryResource)

Example 13 with UnsupportedResourceOperationException

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

the class ResourceWebScriptPost method extractParams.

@Override
public Params extractParams(ResourceMetadata resourceMeta, WebScriptRequest req) {
    final RecognizedParams params = getRecognizedParams(req);
    final String entityId = req.getServiceMatch().getTemplateVars().get(ResourceLocator.ENTITY_ID);
    final String relationshipId = req.getServiceMatch().getTemplateVars().get(ResourceLocator.RELATIONSHIP_ID);
    final ResourceOperation operation = resourceMeta.getOperation(HttpMethod.POST);
    switch(resourceMeta.getType()) {
        case ENTITY:
            if (StringUtils.isNotBlank(entityId)) {
                throw new UnsupportedResourceOperationException("POST is executed against the collection URL");
            } else {
                Object postedObj = processRequest(resourceMeta, operation, req);
                return Params.valueOf(null, params, postedObj, req);
            }
        case RELATIONSHIP:
            if (StringUtils.isNotBlank(relationshipId)) {
                throw new UnsupportedResourceOperationException("POST is executed against the collection URL");
            } else {
                Object postedRel = processRequest(resourceMeta, operation, req);
                return Params.valueOf(entityId, params, postedRel, req);
            }
        case OPERATION:
            final String operationName = req.getServiceMatch().getTemplateVars().get(ResourceLocator.RELATIONSHIP_RESOURCE);
            final String propertyName = req.getServiceMatch().getTemplateVars().get(ResourceLocator.PROPERTY);
            if (StringUtils.isNotBlank(entityId) && StringUtils.isNotBlank(operationName)) {
                Object postedObj = processRequest(resourceMeta, operation, req);
                if (StringUtils.isNotBlank(propertyName)) {
                    return Params.valueOf(entityId, relationshipId, params, postedObj, req);
                } else {
                    return Params.valueOf(entityId, params, postedObj, req);
                }
            }
        // Fall through to unsupported.
        default:
            throw new UnsupportedResourceOperationException("POST not supported for Actions");
    }
}
Also used : RecognizedParams(org.alfresco.rest.framework.resource.parameters.Params.RecognizedParams) UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) ResourceOperation(org.alfresco.rest.framework.core.ResourceOperation)

Example 14 with UnsupportedResourceOperationException

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

the class ResourceWebScriptPut method extractParams.

@Override
public Params extractParams(ResourceMetadata resourceMeta, WebScriptRequest req) {
    final String relationshipId = req.getServiceMatch().getTemplateVars().get(ResourceLocator.RELATIONSHIP_ID);
    final String entityId = req.getServiceMatch().getTemplateVars().get(ResourceLocator.ENTITY_ID);
    final RecognizedParams params = getRecognizedParams(req);
    final ResourceOperation operation = resourceMeta.getOperation(HttpMethod.PUT);
    switch(resourceMeta.getType()) {
        case ENTITY:
            if (StringUtils.isBlank(entityId)) {
                throw new UnsupportedResourceOperationException("PUT is executed against the instance URL");
            } else {
                Object putEnt = extractJsonContent(req, assistant.getJsonHelper(), resourceMeta.getObjectType(operation));
                return Params.valueOf(entityId, params, putEnt, req);
            }
        case RELATIONSHIP:
            if (StringUtils.isBlank(relationshipId)) {
                throw new UnsupportedResourceOperationException("PUT is executed against the instance URL");
            } else {
                Object putRel = extractJsonContent(req, assistant.getJsonHelper(), resourceMeta.getObjectType(operation));
                ResourceWebScriptHelper.setUniqueId(putRel, relationshipId);
                return Params.valueOf(entityId, params, putRel, req);
            }
        case PROPERTY:
            final String resourceName = req.getServiceMatch().getTemplateVars().get(ResourceLocator.RELATIONSHIP_RESOURCE);
            final String propertyName = req.getServiceMatch().getTemplateVars().get(ResourceLocator.PROPERTY);
            if (StringUtils.isNotBlank(entityId) && StringUtils.isNotBlank(resourceName)) {
                if (StringUtils.isNotBlank(propertyName)) {
                    return Params.valueOf(entityId, relationshipId, null, getStream(req), propertyName, params, getContentInfo(req), req);
                } else {
                    return Params.valueOf(entityId, null, null, getStream(req), resourceName, params, getContentInfo(req), req);
                }
            }
        // Fall through to unsupported.
        default:
            throw new UnsupportedResourceOperationException("PUT not supported for this request.");
    }
}
Also used : RecognizedParams(org.alfresco.rest.framework.resource.parameters.Params.RecognizedParams) UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) ResourceOperation(org.alfresco.rest.framework.core.ResourceOperation)

Example 15 with UnsupportedResourceOperationException

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

the class ExceptionResolverTests method testMatchException.

// 04180006 Authentication failed for Web Script org/alfresco/api/ResourceWebScript.get
@Test
public void testMatchException() {
    ErrorResponse response = assistant.resolveException(new ApiException(null));
    assertNotNull(response);
    // default to INTERNAL_SERVER_ERROR
    assertEquals(500, response.getStatusCode());
    response = assistant.resolveException(new InvalidArgumentException(null));
    // default to STATUS_BAD_REQUEST
    assertEquals(400, response.getStatusCode());
    response = assistant.resolveException(new InvalidQueryException(null));
    // default to STATUS_BAD_REQUEST
    assertEquals(400, response.getStatusCode());
    response = assistant.resolveException(new NotFoundException(null));
    // default to STATUS_NOT_FOUND
    assertEquals(404, response.getStatusCode());
    response = assistant.resolveException(new EntityNotFoundException(null));
    // default to STATUS_NOT_FOUND
    assertEquals(404, response.getStatusCode());
    response = assistant.resolveException(new RelationshipResourceNotFoundException(null, null));
    // default to STATUS_NOT_FOUND
    assertEquals(404, response.getStatusCode());
    response = assistant.resolveException(new PermissionDeniedException(null));
    // default to STATUS_FORBIDDEN
    assertEquals(403, response.getStatusCode());
    response = assistant.resolveException(new UnsupportedResourceOperationException(null));
    // default to STATUS_METHOD_NOT_ALLOWED
    assertEquals(405, response.getStatusCode());
    response = assistant.resolveException(new DeletedResourceException(null));
    // default to STATUS_METHOD_NOT_ALLOWED
    assertEquals(405, response.getStatusCode());
    response = assistant.resolveException(new ConstraintViolatedException(null));
    // default to STATUS_CONFLICT
    assertEquals(409, response.getStatusCode());
    response = assistant.resolveException(new StaleEntityException(null));
    // default to STATUS_CONFLICT
    assertEquals(409, response.getStatusCode());
    // Try a random exception
    response = assistant.resolveException(new FormNotFoundException(null));
    // default to INTERNAL_SERVER_ERROR
    assertEquals(500, response.getStatusCode());
    response = assistant.resolveException(new InsufficientStorageException(null));
    assertEquals(507, response.getStatusCode());
    response = assistant.resolveException(new IntegrityException(null));
    assertEquals(422, response.getStatusCode());
}
Also used : UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) InsufficientStorageException(org.alfresco.rest.framework.core.exceptions.InsufficientStorageException) FormNotFoundException(org.alfresco.repo.forms.FormNotFoundException) RelationshipResourceNotFoundException(org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) IntegrityException(org.alfresco.repo.node.integrity.IntegrityException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) DeletedResourceException(org.alfresco.rest.framework.core.exceptions.DeletedResourceException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) ErrorResponse(org.alfresco.rest.framework.core.exceptions.ErrorResponse) RelationshipResourceNotFoundException(org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) FormNotFoundException(org.alfresco.repo.forms.FormNotFoundException) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) StaleEntityException(org.alfresco.rest.framework.core.exceptions.StaleEntityException) InvalidQueryException(org.alfresco.rest.framework.resource.parameters.where.InvalidQueryException) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException) Test(org.junit.Test)

Aggregations

UnsupportedResourceOperationException (org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException)17 HashMap (java.util.HashMap)8 Test (org.junit.Test)8 ResourceWithMetadata (org.alfresco.rest.framework.core.ResourceWithMetadata)4 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)4 Api (org.alfresco.rest.framework.Api)3 ResourceOperation (org.alfresco.rest.framework.core.ResourceOperation)3 EntityResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction)3 Params (org.alfresco.rest.framework.resource.parameters.Params)3 RecognizedParams (org.alfresco.rest.framework.resource.parameters.Params.RecognizedParams)3 Match (org.springframework.extensions.webscripts.Match)3 WebScriptRequest (org.springframework.extensions.webscripts.WebScriptRequest)3 StringReader (java.io.StringReader)2 AbstractList (java.util.AbstractList)2 ResourceParameter (org.alfresco.rest.framework.core.ResourceParameter)2 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)2 DeletedResourceException (org.alfresco.rest.framework.core.exceptions.DeletedResourceException)2 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)2 NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)2 PermissionDeniedException (org.alfresco.rest.framework.core.exceptions.PermissionDeniedException)2