Search in sources :

Example 1 with ReadById

use of org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction.ReadById 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 2 with ReadById

use of org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction.ReadById 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 3 with ReadById

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

the class SerializeTests method testInvokeEntity.

@Test
public void testInvokeEntity() throws IOException {
    ResourceWithMetadata entityResource = locator.locateEntityResource(api, "sheep", HttpMethod.GET);
    assertNotNull(entityResource);
    EntityResourceAction.ReadById<?> getter = (ReadById<?>) entityResource.getResource();
    String out = writeResponse(helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api, null, NOT_USED, getter.readById("1234A3", NOT_USED)));
    assertTrue("There must be json output", StringUtils.startsWith(out, "{\"entry\":"));
    EntityResourceAction.Read<?> getAll = (Read<?>) entityResource.getResource();
    CollectionWithPagingInfo<?> resources = getAll.readAll(null);
    out = writeResponse(helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api, null, Params.valueOf("notUsed", null, null), resources));
    assertTrue("There must be json output as List", StringUtils.startsWith(out, "{\"list\":"));
}
Also used : Read(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction.Read) EntityResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction) ReadById(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction.ReadById) WebScriptResponse(org.springframework.extensions.webscripts.WebScriptResponse) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) Test(org.junit.Test)

Aggregations

EntityResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction)3 ReadById (org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction.ReadById)3 ResourceWithMetadata (org.alfresco.rest.framework.core.ResourceWithMetadata)2 UnsupportedResourceOperationException (org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException)2 Read (org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction.Read)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 Api (org.alfresco.rest.framework.Api)1 DeletedResourceException (org.alfresco.rest.framework.core.exceptions.DeletedResourceException)1 BinaryResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.BinaryResourceAction)1 RelationshipResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction)1 RelationshipResourceBinaryAction (org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceBinaryAction)1 BinaryResource (org.alfresco.rest.framework.resource.content.BinaryResource)1 CollectionWithPagingInfo (org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo)1 Goat (org.alfresco.rest.framework.tests.api.mocks.Goat)1 SlimGoat (org.alfresco.rest.framework.tests.api.mocks3.SlimGoat)1 JSONObject (org.json.JSONObject)1 WebScriptResponse (org.springframework.extensions.webscripts.WebScriptResponse)1