Search in sources :

Example 1 with ResourceAction

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

the class PublicApiDeclarativeRegistry method findWebScript.

/* (non-Javadoc)
     * @see org.alfresco.web.scripts.Registry#findWebScript(java.lang.String, java.lang.String)
     */
public Match findWebScript(String method, String uri) {
    Match match;
    HttpMethod httpMethod = HttpMethod.valueOf(method);
    if (HttpMethod.GET.equals(httpMethod)) {
        if (uri.equals(PublicApiTenantWebScriptServletRequest.NETWORKS_PATH)) {
            Map<String, String> templateVars = new HashMap<>();
            templateVars.put("apiScope", "public");
            templateVars.put("apiVersion", "1");
            templateVars.put("apiName", "networks");
            match = new Match("", templateVars, "", getNetworksWebScript);
        } else if (uri.equals(PublicApiTenantWebScriptServletRequest.NETWORK_PATH)) {
            Map<String, String> templateVars = new HashMap<>();
            templateVars.put("apiScope", "public");
            templateVars.put("apiVersion", "1");
            templateVars.put("apiName", "network");
            match = new Match("", templateVars, "", getNetworkWebScript);
        } else {
            match = super.findWebScript(method, uri);
            if (match == null) {
                return null;
            }
            Map<String, String> templateVars = match.getTemplateVars();
            ResourceWithMetadata rwm = getResourceWithMetadataOrNull(templateVars, httpMethod);
            if (rwm != null) {
                Class<? extends ResourceAction> resAction = null;
                String entityId = templateVars.get(ResourceLocator.ENTITY_ID);
                String relationshipId = templateVars.get(ResourceLocator.RELATIONSHIP_ID);
                switch(rwm.getMetaData().getType()) {
                    case ENTITY:
                        if (StringUtils.isNotBlank(entityId)) {
                            if (EntityResourceAction.ReadById.class.isAssignableFrom(rwm.getResource().getClass())) {
                                resAction = EntityResourceAction.ReadById.class;
                            }
                        } else {
                            if (EntityResourceAction.Read.class.isAssignableFrom(rwm.getResource().getClass())) {
                                resAction = EntityResourceAction.Read.class;
                            }
                        }
                        break;
                    case PROPERTY:
                        if (StringUtils.isNotBlank(entityId)) {
                            if (BinaryResourceAction.Read.class.isAssignableFrom(rwm.getResource().getClass())) {
                                resAction = BinaryResourceAction.Read.class;
                            } else if (RelationshipResourceBinaryAction.Read.class.isAssignableFrom(rwm.getResource().getClass())) {
                                resAction = RelationshipResourceBinaryAction.Read.class;
                            }
                        }
                        break;
                    case RELATIONSHIP:
                        if (StringUtils.isNotBlank(relationshipId)) {
                            if (RelationshipResourceAction.ReadById.class.isAssignableFrom(rwm.getResource().getClass())) {
                                resAction = RelationshipResourceAction.ReadById.class;
                            }
                        } else {
                            if (RelationshipResourceAction.Read.class.isAssignableFrom(rwm.getResource().getClass())) {
                                resAction = RelationshipResourceAction.Read.class;
                            }
                        }
                        break;
                    default:
                        break;
                }
                final boolean noAuth = (resAction != null && rwm.getMetaData().isNoAuth(resAction));
                if (noAuth) {
                    // override match with noAuth
                    match = overrideMatch(match);
                }
            }
        }
    } else if (HttpMethod.POST.equals(httpMethod)) {
        match = super.findWebScript(method, uri);
        if (match != null) {
            ResourceWithMetadata rwm = getResourceWithMetadataOrNull(match.getTemplateVars(), httpMethod);
            if (rwm != null) {
                Class<? extends ResourceAction> resAction = null;
                Boolean noAuth = null;
                switch(rwm.getMetaData().getType()) {
                    case ENTITY:
                        if (EntityResourceAction.Create.class.isAssignableFrom(rwm.getResource().getClass())) {
                            resAction = EntityResourceAction.Create.class;
                        } else if (EntityResourceAction.CreateWithResponse.class.isAssignableFrom(rwm.getResource().getClass())) {
                            resAction = EntityResourceAction.CreateWithResponse.class;
                        }
                        break;
                    case RELATIONSHIP:
                        if (RelationshipResourceAction.Create.class.isAssignableFrom(rwm.getResource().getClass())) {
                            resAction = RelationshipResourceAction.Create.class;
                        } else if (RelationshipResourceAction.CreateWithResponse.class.isAssignableFrom(rwm.getResource().getClass())) {
                            resAction = RelationshipResourceAction.CreateWithResponse.class;
                        }
                        break;
                    case OPERATION:
                        noAuth = rwm.getMetaData().isNoAuth(null);
                        break;
                    default:
                        break;
                }
                if (noAuth == null) {
                    noAuth = (resAction != null && rwm.getMetaData().isNoAuth(resAction));
                }
                if (noAuth) {
                    // override match with noAuth
                    match = overrideMatch(match);
                }
            }
        }
    } else {
        match = super.findWebScript(method, uri);
    }
    if (match == null) {
        throw new NotFoundException(NotFoundException.DEFAULT_MESSAGE_ID, new String[] { uri });
    }
    return match;
}
Also used : EntityResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction) HashMap(java.util.HashMap) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) RelationshipResourceBinaryAction(org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceBinaryAction) RelationshipResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction) HashMap(java.util.HashMap) Map(java.util.Map) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) HttpMethod(org.springframework.http.HttpMethod) RelationshipResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction) EntityResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction) ResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.ResourceAction) BinaryResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.BinaryResourceAction)

Example 2 with ResourceAction

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

the class ResourceInspector method inspectRelationship.

/**
 * Inspects the relationship resource and returns meta data about it
 *
 * @param annot RelationshipResource
 * @param resource Class<?>
 */
private static List<ResourceMetadata> inspectRelationship(RelationshipResource annot, Class<?> resource) {
    Map<String, Object> annotAttribs = AnnotationUtils.getAnnotationAttributes(annot);
    String urlPath = String.valueOf(annotAttribs.get("name"));
    String entityPath = findEntityNameByAnnotationAttributes(annotAttribs);
    String relationshipKey = ResourceDictionary.resourceKey(entityPath, urlPath);
    Api api = inspectApi(resource);
    List<ResourceMetadata> metainfo = new ArrayList<ResourceMetadata>();
    MetaHelper helper = new MetaHelper(resource);
    findOperation(RelationshipResourceAction.Create.class, HttpMethod.POST, helper);
    findOperation(RelationshipResourceAction.Read.class, HttpMethod.GET, helper);
    findOperation(RelationshipResourceAction.ReadById.class, HttpMethod.GET, helper);
    findOperation(RelationshipResourceAction.Update.class, HttpMethod.PUT, helper);
    findOperation(RelationshipResourceAction.Delete.class, HttpMethod.DELETE, helper);
    findOperation(RelationshipResourceAction.DeleteSet.class, HttpMethod.DELETE, helper);
    findOperation(RelationshipResourceAction.CreateWithResponse.class, HttpMethod.POST, helper);
    findOperation(RelationshipResourceAction.ReadWithResponse.class, HttpMethod.GET, helper);
    findOperation(RelationshipResourceAction.ReadByIdWithResponse.class, HttpMethod.GET, helper);
    findOperation(RelationshipResourceAction.UpdateWithResponse.class, HttpMethod.PUT, helper);
    findOperation(RelationshipResourceAction.DeleteWithResponse.class, HttpMethod.DELETE, helper);
    findOperation(RelationshipResourceAction.DeleteSetWithResponse.class, HttpMethod.DELETE, helper);
    findOperation(MultiPartRelationshipResourceAction.Create.class, HttpMethod.POST, helper);
    boolean noAuth = resource.isAnnotationPresent(WebApiNoAuth.class);
    if (noAuth) {
        throw new IllegalArgumentException("@WebApiNoAuth should not be on all (relationship resource class) - only on methods: " + urlPath);
    }
    Set<Class<? extends ResourceAction>> apiNoAuth = helper.apiNoAuth;
    if (resource.isAnnotationPresent(WebApiDeleted.class)) {
        metainfo.add(new ResourceMetadata(relationshipKey, RESOURCE_TYPE.RELATIONSHIP, null, inspectApi(resource), ALL_RELATIONSHIP_RESOURCE_INTERFACES, apiNoAuth, entityPath));
    } else {
        metainfo.add(new ResourceMetadata(relationshipKey, RESOURCE_TYPE.RELATIONSHIP, helper.operations, inspectApi(resource), helper.apiDeleted, apiNoAuth, entityPath));
    }
    inspectAddressedProperties(api, resource, relationshipKey, metainfo);
    inspectOperations(api, resource, relationshipKey, metainfo);
    return metainfo;
}
Also used : MultiPartRelationshipResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.MultiPartRelationshipResourceAction) ArrayList(java.util.ArrayList) RelationshipResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction) MultiPartRelationshipResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.MultiPartRelationshipResourceAction) Api(org.alfresco.rest.framework.Api) WebApi(org.alfresco.rest.framework.WebApi) RelationshipResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction) EntityResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction) MultiPartResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.MultiPartResourceAction) MultiPartRelationshipResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.MultiPartRelationshipResourceAction) ResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.ResourceAction) BinaryResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.BinaryResourceAction)

Example 3 with ResourceAction

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

the class ResourceInspector method inspectEntity.

/**
 * Inspects the entity resource and returns meta data about it
 *
 * @param annot EntityResource
 * @param resource Class<?>
 */
private static List<ResourceMetadata> inspectEntity(EntityResource annot, Class<?> resource) {
    String urlPath = findEntityName(annot);
    logger.debug("Found EntityResource: " + urlPath);
    List<ResourceMetadata> metainfo = new ArrayList<ResourceMetadata>();
    Api api = inspectApi(resource);
    MetaHelper helper = new MetaHelper(resource);
    findOperation(EntityResourceAction.Create.class, HttpMethod.POST, helper);
    findOperation(EntityResourceAction.Read.class, HttpMethod.GET, helper);
    findOperation(EntityResourceAction.ReadById.class, HttpMethod.GET, helper);
    findOperation(EntityResourceAction.Update.class, HttpMethod.PUT, helper);
    findOperation(EntityResourceAction.Delete.class, HttpMethod.DELETE, helper);
    findOperation(EntityResourceAction.DeleteSet.class, HttpMethod.DELETE, helper);
    findOperation(EntityResourceAction.CreateWithResponse.class, HttpMethod.POST, helper);
    findOperation(EntityResourceAction.ReadWithResponse.class, HttpMethod.GET, helper);
    findOperation(EntityResourceAction.ReadByIdWithResponse.class, HttpMethod.GET, helper);
    findOperation(EntityResourceAction.UpdateWithResponse.class, HttpMethod.PUT, helper);
    findOperation(EntityResourceAction.DeleteWithResponse.class, HttpMethod.DELETE, helper);
    findOperation(EntityResourceAction.DeleteSetWithResponse.class, HttpMethod.DELETE, helper);
    findOperation(MultiPartResourceAction.Create.class, HttpMethod.POST, helper);
    boolean noAuth = resource.isAnnotationPresent(WebApiNoAuth.class);
    if (noAuth) {
        throw new IllegalArgumentException("@WebApiNoAuth should not be on all (entity resource class) - only on individual methods: " + urlPath);
    }
    Set<Class<? extends ResourceAction>> apiNoAuth = helper.apiNoAuth;
    if (resource.isAnnotationPresent(WebApiDeleted.class)) {
        metainfo.add(new ResourceMetadata(ResourceDictionary.resourceKey(urlPath, null), RESOURCE_TYPE.ENTITY, null, api, ALL_ENTITY_RESOURCE_INTERFACES, apiNoAuth, null));
    } else {
        if (!helper.apiDeleted.isEmpty() || !helper.operations.isEmpty()) {
            metainfo.add(new ResourceMetadata(ResourceDictionary.resourceKey(urlPath, null), RESOURCE_TYPE.ENTITY, helper.operations, api, helper.apiDeleted, apiNoAuth, null));
        }
    }
    inspectAddressedProperties(api, resource, urlPath, metainfo);
    inspectOperations(api, resource, urlPath, metainfo);
    return metainfo;
}
Also used : EntityResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction) ArrayList(java.util.ArrayList) MultiPartResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.MultiPartResourceAction) Api(org.alfresco.rest.framework.Api) WebApi(org.alfresco.rest.framework.WebApi) RelationshipResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction) EntityResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction) MultiPartResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.MultiPartResourceAction) MultiPartRelationshipResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.MultiPartRelationshipResourceAction) ResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.ResourceAction) BinaryResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.BinaryResourceAction)

Aggregations

BinaryResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.BinaryResourceAction)3 EntityResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction)3 RelationshipResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction)3 ResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.ResourceAction)3 ArrayList (java.util.ArrayList)2 Api (org.alfresco.rest.framework.Api)2 WebApi (org.alfresco.rest.framework.WebApi)2 MultiPartRelationshipResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.MultiPartRelationshipResourceAction)2 MultiPartResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.MultiPartResourceAction)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ResourceWithMetadata (org.alfresco.rest.framework.core.ResourceWithMetadata)1 NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)1 RelationshipResourceBinaryAction (org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceBinaryAction)1 HttpMethod (org.springframework.http.HttpMethod)1