Search in sources :

Example 6 with ResourceWithMetadata

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

the class SerializeTests method testInvokeRelation.

@Test
public void testInvokeRelation() throws IOException {
    ResourceWithMetadata relationResource = locator.locateRelationResource(api, "sheep", "baaahh", HttpMethod.GET);
    assertNotNull(relationResource);
    RelationshipResourceAction.Read<?> getter = (RelationshipResourceAction.Read<?>) relationResource.getResource();
    String out = writeResponse(helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api, null, Params.valueOf("notUsed", null, null), getter.readAll("1234A3", Params.valueOf("notUsed", null, null))));
    assertTrue("There must be json output", StringUtils.isNotBlank(out));
}
Also used : Read(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction.Read) RelationshipResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) Test(org.junit.Test)

Example 7 with ResourceWithMetadata

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

the class SerializeTests method testInvokeVersions.

@Test
public void testInvokeVersions() throws IOException {
    final Map<String, Object> respons = new HashMap<String, Object>();
    ResourceWithMetadata entityResource = locator.locateEntityResource(api, "goat", HttpMethod.GET);
    assertNotNull(entityResource);
    EntityResourceAction.ReadById<?> getter = (ReadById<?>) entityResource.getResource();
    Object readById = getter.readById("1234A3", NOT_USED);
    assertTrue("Version 1 must be a goat.", Goat.class.equals(readById.getClass()));
    String out = writeResponse(readById);
    assertNotNull(out);
    Api v3 = Api.valueOf(api.getName(), api.getScope().toString(), "3");
    entityResource = locator.locateEntityResource(v3, "goat", HttpMethod.GET);
    assertNotNull(entityResource);
    getter = (ReadById<?>) entityResource.getResource();
    Object readByIdForNewVersion = getter.readById("1234A3", NOT_USED);
    assertTrue("Version 3 must be a slim goat.", SlimGoat.class.equals(readByIdForNewVersion.getClass()));
    respons.put("v3Goat", readByIdForNewVersion);
    out = writeResponse(readByIdForNewVersion);
    entityResource = locator.locateEntityResource(api, "grass", HttpMethod.GET);
    // ok for version 1
    assertNotNull(entityResource);
    try {
        entityResource = locator.locateEntityResource(v3, "grass", HttpMethod.GET);
        fail("Should throw an UnsupportedResourceOperationException");
    } catch (UnsupportedResourceOperationException error) {
    // this is correct
    }
}
Also used : EntityResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction) ReadById(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction.ReadById) HashMap(java.util.HashMap) UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) SlimGoat(org.alfresco.rest.framework.tests.api.mocks3.SlimGoat) JSONObject(org.json.JSONObject) Goat(org.alfresco.rest.framework.tests.api.mocks.Goat) SlimGoat(org.alfresco.rest.framework.tests.api.mocks3.SlimGoat) Api(org.alfresco.rest.framework.Api) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) Test(org.junit.Test)

Example 8 with ResourceWithMetadata

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

the class ExecutionTests method testInvokePut.

@Test
public void testInvokePut() throws IOException {
    ResourceWithMetadata entityResource = locator.locateEntityResource(api, "sheep", HttpMethod.PUT);
    AbstractResourceWebScript executor = getExecutor("executorOfPut");
    final Sheep aSheep = new Sheep("xyz");
    Object result = executor.execute(entityResource, Params.valueOf("654", null, NULL_PARAMS, aSheep, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
    assertNotNull(result);
    assertEquals(aSheep, ((ExecutionResult) result).getRoot());
    final Goat goat = new Goat("xyz");
    ResourceWithMetadata cowResource = locator.locateEntityResource(api, "cow", HttpMethod.PUT);
    result = executor.execute(cowResource, Params.valueOf("654", null, NULL_PARAMS, goat, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
    assertNotNull(result);
    assertEquals(goat, ((ExecutionResult) result).getRoot());
    ResourceWithMetadata resource = locator.locateRelationResource(api, "sheep", "blacksheep", HttpMethod.PUT);
    result = executor.execute(resource, Params.valueOf("654", null, NULL_PARAMS, aSheep, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
    assertNotNull(result);
    assertEquals(aSheep, ((ExecutionResult) result).getRoot());
    ResourceWithMetadata baaPhoto = locator.locateRelationResource(api, "sheep/{entityId}/baaahh", "photo", HttpMethod.PUT);
    result = executor.execute(baaPhoto, Params.valueOf("4", "56", mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
    assertNull(result);
    ResourceWithMetadata flockEntityResource = locator.locateRelationResource(api3, "flock", "photo", HttpMethod.PUT);
    result = executor.execute(flockEntityResource, Params.valueOf("654", null, NULL_PARAMS, goat, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
    assertNull(result);
    ResourceWithMetadata calf = locator.locateRelationResource(api, "cow", "calf", HttpMethod.PUT);
    result = executor.execute(calf, Params.valueOf("654", null, NULL_PARAMS, goat, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
    assertNotNull(result);
    assertEquals(goat, ((ExecutionResult) result).getRoot());
    cowResource = locator.locateRelationResource(api, "cow", "photo", HttpMethod.PUT);
    result = executor.execute(cowResource, Params.valueOf("654", null, NULL_PARAMS, goat, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
    assertNull(result);
    calf = locator.locateRelationResource(api, "cow/{entityId}/calf", "photo", HttpMethod.PUT);
    result = executor.execute(calf, Params.valueOf("4", "56", mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
    assertNull(result);
}
Also used : WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) AbstractResourceWebScript(org.alfresco.rest.framework.webscripts.AbstractResourceWebScript) WebScriptResponse(org.springframework.extensions.webscripts.WebScriptResponse) Sheep(org.alfresco.rest.framework.tests.api.mocks.Sheep) Goat(org.alfresco.rest.framework.tests.api.mocks.Goat) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) Test(org.junit.Test)

Example 9 with ResourceWithMetadata

use of org.alfresco.rest.framework.core.ResourceWithMetadata 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 10 with ResourceWithMetadata

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

the class WebScriptOptionsMetaData method execute.

@Override
public void execute(final Api api, WebScriptRequest req, WebScriptResponse res) throws IOException {
    final Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
    ResourceDictionary resourceDic = lookupDictionary.getDictionary();
    Map<String, ResourceWithMetadata> apiResources = resourceDic.getAllResources().get(api);
    if (apiResources == null) {
        throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_INVALID_API);
    }
    String collectionName = templateVars.get(ResourceLocator.COLLECTION_RESOURCE);
    String resourceName = templateVars.get(ResourceLocator.RELATIONSHIP_RESOURCE);
    String resourceKey = ResourceDictionary.resourceKey(collectionName, resourceName);
    if (logger.isDebugEnabled()) {
        logger.debug("Locating resource :" + resourceKey);
    }
    ResourceWithMetadata resource = apiResources.get(resourceKey);
    if (resource == null) {
        // Get entity resource and check if we are referencing a property on it.
        resourceKey = ResourceDictionary.propertyResourceKey(collectionName, resourceName);
        resource = apiResources.get(resourceKey);
    }
    ResourceMetaDataWriter writer = chooseWriter(req);
    writer.writeMetaData(res.getOutputStream(), resource, apiResources);
}
Also used : InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) ResourceDictionary(org.alfresco.rest.framework.core.ResourceDictionary) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) ResourceMetaDataWriter(org.alfresco.rest.framework.metadata.ResourceMetaDataWriter)

Aggregations

ResourceWithMetadata (org.alfresco.rest.framework.core.ResourceWithMetadata)27 Test (org.junit.Test)21 HashMap (java.util.HashMap)10 Api (org.alfresco.rest.framework.Api)7 WebScriptResponse (org.springframework.extensions.webscripts.WebScriptResponse)7 AbstractResourceWebScript (org.alfresco.rest.framework.webscripts.AbstractResourceWebScript)5 NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)4 UnsupportedResourceOperationException (org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException)4 Read (org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction.Read)4 RelationshipResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction)4 WebScriptRequest (org.springframework.extensions.webscripts.WebScriptRequest)4 ResourceDictionary (org.alfresco.rest.framework.core.ResourceDictionary)3 EntityResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 ExecutionResult (org.alfresco.rest.framework.jacksonextensions.ExecutionResult)2 BinaryResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.BinaryResourceAction)2 ReadById (org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction.ReadById)2 RelationshipResourceBinaryAction (org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceBinaryAction)2