Search in sources :

Example 6 with ResourceOperation

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

the class ResourceWebScriptPut method extractParams.

@Override
public Params extractParams(ResourceMetadata resourceMeta, WebScriptRequest req) {
    final String relationshipId = req.getServiceMatch().getTemplateVars().get(ResourceLocator.RELATIONSHIP_ID);
    final String entityId = req.getServiceMatch().getTemplateVars().get(ResourceLocator.ENTITY_ID);
    final RecognizedParams params = getRecognizedParams(req);
    final ResourceOperation operation = resourceMeta.getOperation(HttpMethod.PUT);
    switch(resourceMeta.getType()) {
        case ENTITY:
            if (StringUtils.isBlank(entityId)) {
                throw new UnsupportedResourceOperationException("PUT is executed against the instance URL");
            } else {
                Object putEnt = extractJsonContent(req, assistant.getJsonHelper(), resourceMeta.getObjectType(operation));
                return Params.valueOf(entityId, params, putEnt, req);
            }
        case RELATIONSHIP:
            if (StringUtils.isBlank(relationshipId)) {
                throw new UnsupportedResourceOperationException("PUT is executed against the instance URL");
            } else {
                Object putRel = extractJsonContent(req, assistant.getJsonHelper(), resourceMeta.getObjectType(operation));
                ResourceWebScriptHelper.setUniqueId(putRel, relationshipId);
                return Params.valueOf(entityId, params, putRel, req);
            }
        case PROPERTY:
            final String resourceName = req.getServiceMatch().getTemplateVars().get(ResourceLocator.RELATIONSHIP_RESOURCE);
            final String propertyName = req.getServiceMatch().getTemplateVars().get(ResourceLocator.PROPERTY);
            if (StringUtils.isNotBlank(entityId) && StringUtils.isNotBlank(resourceName)) {
                if (StringUtils.isNotBlank(propertyName)) {
                    return Params.valueOf(entityId, relationshipId, null, getStream(req), propertyName, params, getContentInfo(req), req);
                } else {
                    return Params.valueOf(entityId, null, null, getStream(req), resourceName, params, getContentInfo(req), req);
                }
            }
        // Fall through to unsupported.
        default:
            throw new UnsupportedResourceOperationException("PUT not supported for this request.");
    }
}
Also used : RecognizedParams(org.alfresco.rest.framework.resource.parameters.Params.RecognizedParams) UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) ResourceOperation(org.alfresco.rest.framework.core.ResourceOperation)

Example 7 with ResourceOperation

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

the class InspectorTests method testInspectBodyParam.

@Test
public void testInspectBodyParam() {
    Method aMethod = ResourceInspector.findMethod(BinaryResourceAction.Update.class, FlockEntityResource.class);
    ResourceOperation op = ResourceInspector.inspectOperation(FlockEntityResource.class, aMethod, HttpMethod.PUT);
    assertNotNull(op);
    List<ResourceParameter> params = op.getParameters();
    assertTrue(params.size() == 2);
    for (ResourceParameter param : params) {
        if (ResourceParameter.KIND.HTTP_BODY_OBJECT.equals(param.getParamType())) {
            assertEquals(Flock.class, param.getDataType());
        }
    }
    aMethod = ResourceInspector.findMethod(RelationshipResourceAction.Create.class, SheepBlackSheepResource.class);
    op = ResourceInspector.inspectOperation(SheepBlackSheepResource.class, aMethod, HttpMethod.POST);
    assertNotNull(op);
    params = op.getParameters();
    assertTrue(params.size() == 2);
    for (ResourceParameter param : params) {
        if (ResourceParameter.KIND.HTTP_BODY_OBJECT.equals(param.getParamType())) {
            assertEquals(Sheep.class, param.getDataType());
        }
    }
    aMethod = ResourceInspector.findMethod(EntityResourceAction.Update.class, SheepEntityResourceWithDeletedMethods.class);
    op = ResourceInspector.inspectOperation(SheepEntityResourceWithDeletedMethods.class, aMethod, HttpMethod.POST);
    assertNotNull(op);
    params = op.getParameters();
    assertNotNull(params);
    for (ResourceParameter param : params) {
        if (ResourceParameter.KIND.HTTP_BODY_OBJECT.equals(param.getParamType())) {
            assertEquals(Sheep.class, param.getDataType());
        }
    }
}
Also used : ResourceParameter(org.alfresco.rest.framework.core.ResourceParameter) SheepEntityResourceWithDeletedMethods(org.alfresco.rest.framework.tests.api.mocks3.SheepEntityResourceWithDeletedMethods) BinaryResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.BinaryResourceAction) SheepBlackSheepResource(org.alfresco.rest.framework.tests.api.mocks.SheepBlackSheepResource) Method(java.lang.reflect.Method) HttpMethod(org.springframework.http.HttpMethod) ResourceOperation(org.alfresco.rest.framework.core.ResourceOperation) Test(org.junit.Test)

Example 8 with ResourceOperation

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

the class InspectorTests method testInspectEntity.

@Test
public void testInspectEntity() {
    List<ResourceMetadata> metainfo = ResourceInspector.inspect(SheepEntityResource.class);
    assertTrue("Must be one ResourceMetadata", metainfo.size() == 1);
    ResourceMetadata metaData = metainfo.get(0);
    assertNotNull(metaData);
    assertNotNull("SheepEntityResource supports GET", metaData.getOperation(HttpMethod.GET));
    assertNotNull("SheepEntityResource supports PUT", metaData.getOperation(HttpMethod.PUT));
    assertNotNull("SheepEntityResource supports DELETE", metaData.getOperation(HttpMethod.DELETE));
    assertNull("SheepEntityResource does not support POST", metaData.getOperation(HttpMethod.POST));
    ResourceOperation op = metaData.getOperation(HttpMethod.GET);
    assertEquals("Sheep ReadALL should return ACCEPTED", Status.STATUS_ACCEPTED, op.getSuccessStatus());
    op = metaData.getOperation(HttpMethod.PUT);
    assertTrue("SheepEntityResource must support Sheep", Sheep.class.equals(metaData.getObjectType(op)));
    metainfo = ResourceInspector.inspect(SheepNoActionEntityResource.class);
    assertTrue("SheepNoActionEntityResource has no actions.", metainfo.isEmpty());
    metainfo = ResourceInspector.inspect(GoatEntityResource.class);
    assertTrue("Must be one ResourceMetadata", metainfo.size() == 1);
    metaData = metainfo.get(0);
    assertNotNull(metaData);
    assertNotNull("GoatEntityResource supports GET", metaData.getOperation(HttpMethod.GET));
    op = metaData.getOperation(HttpMethod.GET);
    List<ResourceParameter> params = op.getParameters();
    assertTrue("readById method should have 1 url param", params.size() == 1);
    metainfo = ResourceInspector.inspect(FlockEntityResource.class);
    assertTrue("Must be one ResourceMetadata", metainfo.size() == 1);
    metaData = metainfo.get(0);
    assertNotNull(metaData);
    assertNotNull("FlockEntityResource supports GET", metaData.getOperation(HttpMethod.GET));
    assertNotNull("FlockEntityResource supports PUT", metaData.getOperation(HttpMethod.PUT));
    assertNotNull("FlockEntityResource supports DELETE", metaData.getOperation(HttpMethod.DELETE));
    assertNull("FlockEntityResource does not support POST", metaData.getOperation(HttpMethod.POST));
    metainfo = ResourceInspector.inspect(MultiPartTestEntityResource.class);
    assertTrue("Must be one ResourceMetadata", metainfo.size() == 1);
    metaData = metainfo.get(0);
    assertNotNull(metaData);
    assertNotNull("MultiPartTestEntityResource support POST", metaData.getOperation(HttpMethod.POST));
    assertNull("MultiPartTestEntityResource does not supports GET", metaData.getOperation(HttpMethod.GET));
    assertNull("MultiPartTestEntityResource does not supports PUT", metaData.getOperation(HttpMethod.PUT));
    assertNull("MultiPartTestEntityResource does not supports DELETE", metaData.getOperation(HttpMethod.DELETE));
    op = metaData.getOperation(HttpMethod.POST);
    assertTrue("MultiPartTestEntityResource must support MultiPartTestResponse", MultiPartTestResponse.class.equals(metaData.getObjectType(op)));
    assertEquals("MultiPartTestEntityResource should return ACCEPTED", Status.STATUS_ACCEPTED, op.getSuccessStatus());
}
Also used : SheepNoActionEntityResource(org.alfresco.rest.framework.tests.api.mocks.SheepNoActionEntityResource) ResourceParameter(org.alfresco.rest.framework.core.ResourceParameter) GoatEntityResource(org.alfresco.rest.framework.tests.api.mocks.GoatEntityResource) Sheep(org.alfresco.rest.framework.tests.api.mocks.Sheep) ResourceMetadata(org.alfresco.rest.framework.core.ResourceMetadata) MultiPartTestEntityResource(org.alfresco.rest.framework.tests.api.mocks.MultiPartTestEntityResource) MultiPartTestResponse(org.alfresco.rest.framework.tests.api.mocks.MultiPartTestResponse) ResourceOperation(org.alfresco.rest.framework.core.ResourceOperation) FlockEntityResource(org.alfresco.rest.framework.tests.api.mocks3.FlockEntityResource) Test(org.junit.Test)

Example 9 with ResourceOperation

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

the class InspectorTests method testInspectOperations.

@Test
public void testInspectOperations() throws IllegalAccessException, InstantiationException, Throwable {
    Api api = Api.valueOf("alfrescomock", "private", "1");
    List<ResourceMetadata> metainfo = new ArrayList<ResourceMetadata>();
    GrassEntityResource grassEntityResource = new GrassEntityResource();
    ResourceInspector.inspectOperations(api, GrassEntityResource.class, "-root-", metainfo);
    assertEquals(3, metainfo.size());
    for (ResourceMetadata resourceMetadata : metainfo) {
        assertEquals(ResourceMetadata.RESOURCE_TYPE.OPERATION, resourceMetadata.getType());
        OperationResourceMetaData operationResourceMetaData = (OperationResourceMetaData) resourceMetadata;
        Method actionMethod = operationResourceMetaData.getOperationMethod();
        String result = null;
        final WithResponse wr = new WithResponse(Status.STATUS_OK, ResponseWriter.DEFAULT_JSON_CONTENT, ResponseWriter.CACHE_NEVER);
        switch(resourceMetadata.getUniqueId()) {
            case "/-root-/{id}/grow":
                assertNotNull("GrassEntityResource supports POST", resourceMetadata.getOperation(HttpMethod.POST));
                assertNull("GrassEntityResource does not support DELETE", resourceMetadata.getOperation(HttpMethod.DELETE));
                ResourceOperation op = resourceMetadata.getOperation(HttpMethod.POST);
                assertEquals("grow should return ACCEPTED", Status.STATUS_ACCEPTED, op.getSuccessStatus());
                Class paramType = resourceMetadata.getObjectType(op);
                Object paramObj = paramType.newInstance();
                result = (String) ResourceInspectorUtil.invokeMethod(actionMethod, grassEntityResource, "xyz", paramObj, Params.valueOf("notUsed", null, mock(WebScriptRequest.class)), wr);
                assertEquals("Growing well", result);
                assertFalse(operationResourceMetaData.isNoAuth(null));
                break;
            case "/-root-/{id}/cut":
                assertNotNull("GrassEntityResource supports POST", resourceMetadata.getOperation(HttpMethod.POST));
                assertNull("GrassEntityResource does not support GET", resourceMetadata.getOperation(HttpMethod.GET));
                op = resourceMetadata.getOperation(HttpMethod.POST);
                assertNull(resourceMetadata.getObjectType(op));
                assertEquals("cut should return ACCEPTED", Status.STATUS_NOT_IMPLEMENTED, op.getSuccessStatus());
                result = (String) ResourceInspectorUtil.invokeMethod(actionMethod, grassEntityResource, "xyz", null, Params.valueOf("notUsed", null, mock(WebScriptRequest.class)), wr);
                assertEquals("All done", result);
                assertFalse(operationResourceMetaData.isNoAuth(null));
                break;
            case "/-root-/{id}/cut-noAuth":
                assertNotNull("GrassEntityResource supports POST", resourceMetadata.getOperation(HttpMethod.POST));
                op = resourceMetadata.getOperation(HttpMethod.POST);
                assertNull(resourceMetadata.getObjectType(op));
                assertEquals("cut should return ACCEPTED", Status.STATUS_NOT_IMPLEMENTED, op.getSuccessStatus());
                result = (String) ResourceInspectorUtil.invokeMethod(actionMethod, grassEntityResource, "xyz", null, Params.valueOf("notUsed", null, mock(WebScriptRequest.class)), wr);
                assertEquals("All done without Auth", result);
                assertTrue(operationResourceMetaData.isNoAuth(null));
                break;
            default:
                fail("Invalid action information.");
        }
    }
}
Also used : GrassEntityResource(org.alfresco.rest.framework.tests.api.mocks.GrassEntityResource) WithResponse(org.alfresco.rest.framework.webscripts.WithResponse) OperationResourceMetaData(org.alfresco.rest.framework.core.OperationResourceMetaData) ArrayList(java.util.ArrayList) Api(org.alfresco.rest.framework.Api) Method(java.lang.reflect.Method) HttpMethod(org.springframework.http.HttpMethod) ResourceMetadata(org.alfresco.rest.framework.core.ResourceMetadata) ResourceOperation(org.alfresco.rest.framework.core.ResourceOperation) Test(org.junit.Test)

Example 10 with ResourceOperation

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

the class InspectorTests method testNodesCommentsRelation.

@Test
public void testNodesCommentsRelation() {
    List<ResourceMetadata> metainfo = ResourceInspector.inspect(NodeCommentsRelation.class);
    assertTrue("Must be one ResourceMetadata", metainfo.size() == 1);
    ResourceMetadata metaData = metainfo.get(0);
    assertNotNull(metaData);
    ResourceOperation op = metaData.getOperation(HttpMethod.POST);
    assertTrue("NodeCommentsRelation must support Comment", Comment.class.equals(metaData.getObjectType(op)));
}
Also used : Comment(org.alfresco.rest.api.model.Comment) ResourceMetadata(org.alfresco.rest.framework.core.ResourceMetadata) ResourceOperation(org.alfresco.rest.framework.core.ResourceOperation) Test(org.junit.Test)

Aggregations

ResourceOperation (org.alfresco.rest.framework.core.ResourceOperation)13 ResourceMetadata (org.alfresco.rest.framework.core.ResourceMetadata)7 Test (org.junit.Test)7 HttpMethod (org.springframework.http.HttpMethod)6 ResourceParameter (org.alfresco.rest.framework.core.ResourceParameter)5 Method (java.lang.reflect.Method)4 UnsupportedResourceOperationException (org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException)3 EntityResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction)2 RecognizedParams (org.alfresco.rest.framework.resource.parameters.Params.RecognizedParams)2 GrassEntityResource (org.alfresco.rest.framework.tests.api.mocks.GrassEntityResource)2 MultiPartTestResponse (org.alfresco.rest.framework.tests.api.mocks.MultiPartTestResponse)2 Sheep (org.alfresco.rest.framework.tests.api.mocks.Sheep)2 FlockEntityResource (org.alfresco.rest.framework.tests.api.mocks3.FlockEntityResource)2 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)1 Comment (org.alfresco.rest.api.model.Comment)1 Api (org.alfresco.rest.framework.Api)1