Search in sources :

Example 1 with RelationshipResourceAction

use of org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction in project alfresco-remote-api by Alfresco.

the class ResourceInspector method inspectParameters.

/**
 * Inspects the Method to find any @WebApiParameters and @WebApiParam
 * @param resource the class
 * @param aMethod the method
 * @param httpMethod HttpMethod
 * @return a List of parameters
 */
private static List<ResourceParameter> inspectParameters(Class<?> resource, Method aMethod, HttpMethod httpMethod) {
    List<ResourceParameter> params = new ArrayList<ResourceParameter>();
    Annotation annot = AnnotationUtils.findAnnotation(aMethod, WebApiParameters.class);
    if (annot != null) {
        Map<String, Object> annotAttribs = AnnotationUtils.getAnnotationAttributes(annot);
        WebApiParam[] apiParams = (WebApiParam[]) annotAttribs.get("value");
        for (int i = 0; i < apiParams.length; i++) {
            params.add(findResourceParameter(apiParams[i], resource, aMethod));
        }
    } else {
        Annotation paramAnot = AnnotationUtils.findAnnotation(aMethod, WebApiParam.class);
        if (paramAnot != null) {
            params.add(findResourceParameter(paramAnot, resource, aMethod));
        }
    }
    // Setup default parameters
    switch(httpMethod) {
        case POST:
            if (paramsCount(params, ResourceParameter.KIND.URL_PATH) == 0) {
                params.add(ResourceParameter.ENTITY_PARAM);
            }
            if (paramsCount(params, ResourceParameter.KIND.HTTP_BODY_OBJECT) == 0) {
                inspectBodyParamAndReturnType(resource, aMethod, params);
            }
            break;
        case PUT:
            int urlPathForPut = paramsCount(params, ResourceParameter.KIND.URL_PATH);
            if (urlPathForPut == 0) {
                params.add(ResourceParameter.ENTITY_PARAM);
            }
            if (RelationshipResourceAction.Update.class.isAssignableFrom(resource) && urlPathForPut < 2) {
                params.add(ResourceParameter.RELATIONSHIP_PARAM);
            }
            if (paramsCount(params, ResourceParameter.KIND.HTTP_BODY_OBJECT) == 0) {
                inspectBodyParamAndReturnType(resource, aMethod, params);
            }
            break;
        case GET:
            int urlPathForGet = paramsCount(params, ResourceParameter.KIND.URL_PATH);
            if (urlPathForGet == 0 && (EntityResourceAction.ReadById.class.isAssignableFrom(resource) && READ_BY_ID_METHODNAME.equals(aMethod.getName()))) {
                params.add(ResourceParameter.ENTITY_PARAM);
            } else if (RelationshipResourceAction.ReadById.class.isAssignableFrom(resource) || RelationshipResourceAction.Read.class.isAssignableFrom(resource)) {
                // Its a RelationshipResourceAction
                if (urlPathForGet == 0) {
                    params.add(ResourceParameter.ENTITY_PARAM);
                }
                // This method is what we are inspecting not what the class implements.
                if (READ_BY_ID_METHODNAME.equals(aMethod.getName()) && urlPathForGet < 2) {
                    params.add(ResourceParameter.RELATIONSHIP_PARAM);
                }
            }
            if (!READ_BY_ID_METHODNAME.equals(aMethod.getName())) {
                params.add(ResourceParameter.SKIP_PARAM);
                params.add(ResourceParameter.MAX_ITEMS_PARAM);
                params.add(ResourceParameter.PROPS_PARAM);
            }
            break;
        case DELETE:
            int urlPathForDelete = paramsCount(params, ResourceParameter.KIND.URL_PATH);
            if (urlPathForDelete == 0) {
                params.add(ResourceParameter.ENTITY_PARAM);
            }
            // Add relationship param ?
            if (RelationshipResourceAction.Delete.class.isAssignableFrom(resource) && urlPathForDelete < 2) {
                params.add(ResourceParameter.RELATIONSHIP_PARAM);
            }
    }
    return params;
}
Also used : ArrayList(java.util.ArrayList) WebApiParam(org.alfresco.rest.framework.WebApiParam) Annotation(java.lang.annotation.Annotation) RelationshipResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction) MultiPartRelationshipResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.MultiPartRelationshipResourceAction)

Aggregations

Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 WebApiParam (org.alfresco.rest.framework.WebApiParam)1 MultiPartRelationshipResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.MultiPartRelationshipResourceAction)1 RelationshipResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction)1