Search in sources :

Example 56 with InvalidArgumentException

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

the class CustomModelsImpl method resolveToUriAndPrefix.

/**
 * Gets the namespace URI and prefix from the parent's name, provided that the
 * given name is of a valid format. The valid format consist of a
 * <i>namespace prefix</i>, a <i>colon</i> and a <i>name</i>. <b>E.g. sys:localized</b>
 *
 * @param parentName the parent name
 * @return a pair of namespace URI and prefix object
 */
protected Pair<String, String> resolveToUriAndPrefix(String parentName) {
    QName qName = prefixedStringToQname(parentName);
    Collection<String> prefixes = namespaceService.getPrefixes(qName.getNamespaceURI());
    if (prefixes.size() == 0) {
        throw new InvalidArgumentException("cmm.rest_api.prefix_not_registered", new Object[] { qName.getNamespaceURI() });
    }
    String prefix = prefixes.iterator().next();
    return new Pair<String, String>(qName.getNamespaceURI(), prefix);
}
Also used : InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) QName(org.alfresco.service.namespace.QName) Pair(org.alfresco.util.Pair)

Example 57 with InvalidArgumentException

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

the class CommentsImpl method updateComment.

public Comment updateComment(String nodeId, Comment comment) {
    try {
        NodeRef nodeRef = nodes.validateNode(nodeId);
        String commentNodeId = comment.getId();
        NodeRef commentNodeRef = nodes.validateNode(commentNodeId);
        String title = comment.getTitle();
        String content = comment.getContent();
        if (content == null) {
            throw new InvalidArgumentException();
        }
        commentService.updateComment(commentNodeRef, title, content);
        return toComment(nodeRef, commentNodeRef, INCLUDE_FULL_PERSON);
    } 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)

Example 58 with InvalidArgumentException

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

the class CommentsImpl method getComments.

public CollectionWithPagingInfo<Comment> getComments(String nodeId, Paging paging, List<String> include) {
    final NodeRef nodeRef = nodes.validateNode(nodeId);
    /* MNT-10536 : fix */
    final Set<QName> contentAndFolders = new HashSet<>(Arrays.asList(ContentModel.TYPE_FOLDER, ContentModel.TYPE_CONTENT));
    if (!nodes.nodeMatches(nodeRef, contentAndFolders, null)) {
        throw new InvalidArgumentException("NodeId of folder or content is expected");
    }
    PagingRequest pagingRequest = Util.getPagingRequest(paging);
    final PagingResults<NodeRef> pagingResults = commentService.listComments(nodeRef, pagingRequest);
    final List<NodeRef> page = pagingResults.getPage();
    List<Comment> comments = new AbstractList<>() {

        @Override
        public Comment get(int index) {
            return toComment(nodeRef, page.get(index), include);
        }

        @Override
        public int size() {
            return page.size();
        }
    };
    return CollectionWithPagingInfo.asPaged(paging, comments, pagingResults.hasMoreItems(), pagingResults.getTotalResultCount().getFirst());
}
Also used : AbstractList(java.util.AbstractList) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Comment(org.alfresco.rest.api.model.Comment) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) QName(org.alfresco.service.namespace.QName) PagingRequest(org.alfresco.query.PagingRequest) HashSet(java.util.HashSet)

Example 59 with InvalidArgumentException

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

the class RecognizedParamsExtractor method getSort.

/**
 * Takes the Sort parameter as a String and parses it into a List of SortColumn objects.
 * The format is a comma seperated list of "columnName sortDirection",
 * e.g. "name DESC, age ASC".  It is not case sensitive and the sort direction is optional
 * It default to sort ASCENDING.
 *
 * @param sortParams - String passed in on the request
 * @return - the sort columns or an empty list if the params were invalid.
 */
default List<SortColumn> getSort(String sortParams) {
    if (sortParams != null) {
        StringTokenizer st = new StringTokenizer(sortParams, ",");
        List<SortColumn> sortedColumns = new ArrayList<SortColumn>(st.countTokens());
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            StringTokenizer columnDesc = new StringTokenizer(token, " ");
            if (columnDesc.countTokens() <= 2) {
                String columnName = columnDesc.nextToken();
                String sortOrder = SortColumn.ASCENDING;
                if (columnDesc.hasMoreTokens()) {
                    String sortDef = columnDesc.nextToken().toUpperCase();
                    if (SortColumn.ASCENDING.equals(sortDef) || SortColumn.DESCENDING.equals(sortDef)) {
                        sortOrder = sortDef;
                    } else {
                        rpeLogger().debug("Invalid sort order direction (" + sortDef + ").  Valid values are " + SortColumn.ASCENDING + " or " + SortColumn.DESCENDING + ".");
                        throw new InvalidArgumentException("Unknown sort order direction '" + sortDef + "', expected asc or desc");
                    }
                }
                sortedColumns.add(new SortColumn(columnName, SortColumn.ASCENDING.equals(sortOrder)));
            } else {
                rpeLogger().debug("Invalid sort order definition (" + token + ")");
                throw new InvalidArgumentException("Unknown sort order definition '" + token + "', expected 'field1,field2' or 'field1 asc,field2 desc' or similar");
            }
        // filteredProperties.add();
        }
        // BeanPropertiesFilter filter = new BeanPropertiesFilter(filteredProperties);
        return sortedColumns;
    }
    return Collections.emptyList();
}
Also used : StringTokenizer(java.util.StringTokenizer) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) ArrayList(java.util.ArrayList) SortColumn(org.alfresco.rest.framework.resource.parameters.SortColumn)

Example 60 with InvalidArgumentException

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

the class ResourceWebScriptPost method extractObjFromJson.

/**
 * If the @WebApiParam has been used and set allowMultiple to false then this will get a single entry.  It
 * should error if an array is passed in.
 * @param resourceMeta ResourceMetadata
 * @param req WebScriptRequest
 * @return Either an object
 */
private Object extractObjFromJson(ResourceMetadata resourceMeta, ResourceOperation operation, WebScriptRequest req) {
    if (operation == null) {
        return null;
    }
    Class<?> objType = resourceMeta.getObjectType(operation);
    boolean isTypeOperation = resourceMeta.getType().equals(ResourceMetadata.RESOURCE_TYPE.OPERATION);
    List<ResourceParameter> params = operation.getParameters();
    if (!params.isEmpty()) {
        for (ResourceParameter resourceParameter : params) {
            // POST to collection may or may not support List as json body, Operations don't support a List as json body
            boolean notMultiple = ((!resourceParameter.isAllowMultiple()) || isTypeOperation);
            if (ResourceParameter.KIND.HTTP_BODY_OBJECT.equals(resourceParameter.getParamType()) && notMultiple) {
                // Only allow 1 value.
                try {
                    Object jsonContent = null;
                    if (objType != null) {
                        // check if the body is optional and is not provided
                        if (!resourceParameter.isRequired() && Integer.valueOf(req.getHeader("content-length")) <= 0) {
                            // in some cases the body is optional and the json doesn't need to be extracted
                            return null;
                        } else {
                            jsonContent = extractJsonContent(req, assistant.getJsonHelper(), objType);
                        }
                    }
                    if (isTypeOperation) {
                        return jsonContent;
                    } else {
                        return Arrays.asList(jsonContent);
                    }
                } catch (InvalidArgumentException iae) {
                    if (iae.getMessage().contains("START_ARRAY") && iae.getMessage().contains("line: 1, column: 1")) {
                        throw new UnsupportedResourceOperationException("Only 1 entity is supported in the HTTP request body");
                    } else {
                        throw iae;
                    }
                }
            }
        }
    }
    if (objType == null) {
        return null;
    }
    if (isTypeOperation) {
        // Operations don't support a List as json body
        return extractJsonContent(req, assistant.getJsonHelper(), objType);
    } else {
        return extractJsonContentAsList(req, assistant.getJsonHelper(), objType);
    }
}
Also used : ResourceParameter(org.alfresco.rest.framework.core.ResourceParameter) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException)

Aggregations

InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)132 NodeRef (org.alfresco.service.cmr.repository.NodeRef)39 QName (org.alfresco.service.namespace.QName)37 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)31 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)24 ArrayList (java.util.ArrayList)22 Test (org.junit.Test)20 PermissionDeniedException (org.alfresco.rest.framework.core.exceptions.PermissionDeniedException)17 Serializable (java.io.Serializable)12 SortColumn (org.alfresco.rest.framework.resource.parameters.SortColumn)12 SearchParameters (org.alfresco.service.cmr.search.SearchParameters)11 Paging (org.alfresco.rest.framework.resource.parameters.Paging)10 ApiException (org.alfresco.rest.framework.core.exceptions.ApiException)9 NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)9 SiteInfo (org.alfresco.service.cmr.site.SiteInfo)9 Pair (org.alfresco.util.Pair)9 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 Params (org.alfresco.rest.framework.resource.parameters.Params)8 RelationshipResourceNotFoundException (org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException)7