Search in sources :

Example 1 with ResourceOperation

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

the class ParamsExtractorTests method testPostExtractor.

@SuppressWarnings("unchecked")
@Test
public void testPostExtractor() throws IOException {
    // Put together the stubs
    ResourceWebScriptPost extractor = new ResourceWebScriptPost();
    extractor.setAssistant(assistant);
    Map<String, String> templateVars = new HashMap<String, String>();
    Content content = mock(Content.class);
    when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
    WebScriptRequest request = mock(WebScriptRequest.class);
    when(request.getServiceMatch()).thenReturn(new Match(null, templateVars, null));
    when(request.getContent()).thenReturn(content);
    Params params = extractor.extractParams(mockEntity(), request);
    assertNotNull(params);
    assertNotNull(params.getFilter());
    assertTrue("Default filter is BeanPropertiesFilter.AllProperties", BeanPropertiesFilter.AllProperties.class.equals(params.getFilter().getClass()));
    Object passed = params.getPassedIn();
    assertNotNull(passed);
    assertTrue(List.class.isAssignableFrom(passed.getClass()));
    List<Object> passedObjs = (List<Object>) passed;
    assertTrue(passedObjs.size() == 1);
    assertTrue("A Farmer was passed in.", Farmer.class.equals(passedObjs.get(0).getClass()));
    // No entity id for POST
    templateVars.put(ResourceLocator.ENTITY_ID, "1234");
    try {
        params = extractor.extractParams(mockEntity(), request);
        fail("Should not get here. No entity id for POST");
    } catch (UnsupportedResourceOperationException uoe) {
        // Must throw this exception
        assertNotNull(uoe);
    }
    // reset the reader
    when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
    params = extractor.extractParams(mockRelationship(), request);
    assertNotNull(params);
    assertEquals("1234", params.getEntityId());
    passed = params.getPassedIn();
    assertNotNull(passed);
    passedObjs = (List<Object>) passed;
    assertTrue(passedObjs.size() == 1);
    assertTrue("A Farmer was passed in.", Farmer.class.equals(passedObjs.get(0).getClass()));
    try {
        // reset the reader
        when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
        templateVars.put(ResourceLocator.RELATIONSHIP_ID, "45678");
        params = extractor.extractParams(mockRelationship(), request);
        fail("Should not get here.");
    } catch (UnsupportedResourceOperationException iae) {
        // Must throw this exception
        assertNotNull("POSTING to a relationship collection by id is not correct.", iae);
    }
    templateVars.clear();
    // reset the reader
    when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
    templateVars.put(ResourceLocator.ENTITY_ID, "1234");
    templateVars.put(ResourceLocator.RELATIONSHIP_ID, "codfish");
    try {
        // POST does not support addressed parameters.
        params = extractor.extractParams(mockEntity(), request);
        fail("Should not get here.");
    } catch (UnsupportedResourceOperationException uoe) {
        // Must throw this exception
        assertNotNull(uoe);
    }
    testExtractOperationParams(templateVars, request, extractor);
    templateVars.clear();
    Method aMethod = ResourceInspector.findMethod(EntityResourceAction.Create.class, GrassEntityResource.class);
    ResourceOperation op = ResourceInspector.inspectOperation(GrassEntityResource.class, aMethod, HttpMethod.POST);
    List<ResourceMetadata> metainfo = ResourceInspector.inspect(GrassEntityResource.class);
    assertNotNull(op);
    assertTrue("Create method should have two params", op.getParameters().size() == 2);
    ResourceParameter singleParam = op.getParameters().get(0);
    assertTrue(ResourceParameter.KIND.HTTP_BODY_OBJECT.equals(singleParam.getParamType()));
    assertFalse("Create grass does not support multiple grass creations", singleParam.isAllowMultiple());
    assertFalse(singleParam.isRequired());
    // Test context when the request body is null and 'required' webApiParam is false
    when(request.getHeader("content-length")).thenReturn("0");
    params = extractor.extractParams(metainfo.get(0), request);
    assertNotNull(params);
    // Test context when the request body is provided and 'required' property is false
    when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.GRASS_JSON));
    params = extractor.extractParams(metainfo.get(0), request);
    assertNotNull(params);
}
Also used : WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) EntityResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction) HashMap(java.util.HashMap) UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) Params(org.alfresco.rest.framework.resource.parameters.Params) Method(java.lang.reflect.Method) HttpMethod(org.springframework.http.HttpMethod) ResourceMetadata(org.alfresco.rest.framework.core.ResourceMetadata) Match(org.springframework.extensions.webscripts.Match) ResourceWebScriptPost(org.alfresco.rest.framework.webscripts.ResourceWebScriptPost) ResourceParameter(org.alfresco.rest.framework.core.ResourceParameter) Content(org.springframework.extensions.surf.util.Content) StringReader(java.io.StringReader) List(java.util.List) Farmer(org.alfresco.rest.framework.tests.api.mocks.Farmer) ResourceOperation(org.alfresco.rest.framework.core.ResourceOperation) Test(org.junit.Test)

Example 2 with ResourceOperation

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

the class InspectorTests method testInspectOperation.

@Test
public void testInspectOperation() {
    Method aMethod = ResourceInspector.findMethod(EntityResourceAction.Read.class, SheepEntityResource.class);
    ResourceOperation op = ResourceInspector.inspectOperation(SheepEntityResource.class, aMethod, HttpMethod.GET);
    assertNotNull(op);
    assertTrue(HttpMethod.GET.equals(op.getHttpMethod()));
    assertTrue("Gets all the Sheep".equals(op.getTitle()));
    assertEquals("Sheep ReadALL should return ACCEPTED", Status.STATUS_ACCEPTED, op.getSuccessStatus());
    assertTrue("".equals(op.getDescription()));
    assertNotNull(op.getParameters());
    assertTrue(op.getParameters().size() == 7);
    for (ResourceParameter aParam : op.getParameters()) {
        assertNotNull(aParam.getName());
        assertNotNull(aParam.getDescription());
        assertNotNull(aParam.getTitle());
        switch(aParam.getParamType()) {
            case QUERY_STRING:
                if (("requiredParam".equals(aParam.getName()))) {
                    assertTrue(aParam.isRequired());
                }
                break;
            case URL_PATH:
                assertTrue(("siteId".equals(aParam.getName())));
                assertFalse(aParam.isRequired());
                break;
            case HTTP_BODY_OBJECT:
                assertTrue(("body".equals(aParam.getName())));
                assertFalse(aParam.isRequired());
                break;
            case HTTP_HEADER:
                assertTrue(("who".equals(aParam.getName())));
                assertFalse(aParam.isRequired());
        }
    }
    // No @WebApiDescription or param (in future WebApiDescription will be mandatory)
    aMethod = ResourceInspector.findMethod(EntityResourceAction.ReadById.class, SheepEntityResource.class);
    op = ResourceInspector.inspectOperation(SheepEntityResource.class, aMethod, HttpMethod.GET);
    assertNotNull(op);
    assertTrue(op.getTitle().startsWith("Missing @WebApiDescription annotation"));
    aMethod = ResourceInspector.findMethod(EntityResourceAction.ReadById.class, GrassEntityResource.class);
    op = ResourceInspector.inspectOperation(GrassEntityResource.class, aMethod, HttpMethod.GET);
    assertNotNull(op);
    assertTrue(op.getTitle().startsWith("Gets grass by id"));
    assertTrue("readById method should have 1 url param", op.getParameters().size() == 1);
    ResourceParameter singleParam = op.getParameters().get(0);
    assertTrue(ResourceParameter.KIND.URL_PATH.equals(singleParam.getParamType()));
    assertFalse("URL paths can never suport multiple params, its always FALSE", singleParam.isAllowMultiple());
    assertNotNull(singleParam.getDescription());
    assertNotNull(singleParam.getTitle());
    assertNotNull(singleParam.getName());
    assertTrue(singleParam.isRequired());
    aMethod = ResourceInspector.findMethod(BinaryResourceAction.Read.class, FlockEntityResource.class);
    op = ResourceInspector.inspectOperation(FlockEntityResource.class, aMethod, HttpMethod.GET);
    assertNotNull(op);
    assertTrue(op.getTitle().startsWith("Reads a photo as a Stream"));
    aMethod = ResourceInspector.findMethod(BinaryResourceAction.Delete.class, FlockEntityResource.class);
    op = ResourceInspector.inspectOperation(FlockEntityResource.class, aMethod, HttpMethod.DELETE);
    assertNotNull(op);
    assertTrue(op.getTitle().startsWith("Deletes a photo"));
    aMethod = ResourceInspector.findMethod(BinaryResourceAction.Update.class, FlockEntityResource.class);
    op = ResourceInspector.inspectOperation(FlockEntityResource.class, aMethod, HttpMethod.PUT);
    assertNotNull(op);
    assertTrue(op.getTitle().startsWith("Updates a photo"));
    aMethod = ResourceInspector.findMethod(BinaryResourceAction.Read.class, SheepBaaaahResource.class);
    op = ResourceInspector.inspectOperation(SheepBaaaahResource.class, aMethod, HttpMethod.GET);
    assertNotNull(op);
    assertTrue(op.getTitle().startsWith("Reads a photo"));
    aMethod = ResourceInspector.findMethod(BinaryResourceAction.Delete.class, SheepBaaaahResource.class);
    op = ResourceInspector.inspectOperation(SheepBaaaahResource.class, aMethod, HttpMethod.DELETE);
    assertNotNull(op);
    assertTrue(op.getTitle().startsWith("Deletes a photo"));
    aMethod = ResourceInspector.findMethod(BinaryResourceAction.Update.class, SheepBaaaahResource.class);
    op = ResourceInspector.inspectOperation(SheepBaaaahResource.class, aMethod, HttpMethod.PUT);
    assertNotNull(op);
    assertTrue(op.getTitle().startsWith("Updates a photo"));
}
Also used : ResourceParameter(org.alfresco.rest.framework.core.ResourceParameter) EntityResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction) GrassEntityResource(org.alfresco.rest.framework.tests.api.mocks.GrassEntityResource) Method(java.lang.reflect.Method) HttpMethod(org.springframework.http.HttpMethod) SheepEntityResource(org.alfresco.rest.framework.tests.api.mocks.SheepEntityResource) SheepBaaaahResource(org.alfresco.rest.framework.tests.api.mocks.SheepBaaaahResource) ResourceOperation(org.alfresco.rest.framework.core.ResourceOperation) FlockEntityResource(org.alfresco.rest.framework.tests.api.mocks3.FlockEntityResource) Test(org.junit.Test)

Example 3 with ResourceOperation

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

the class AbstractResourceWebScript method execute.

public Object execute(final ResourceWithMetadata resource, final Params params, final WebScriptResponse res, boolean isReadOnly) {
    final String entityCollectionName = ResourceInspector.findEntityCollectionNameName(resource.getMetaData());
    final ResourceOperation operation = resource.getMetaData().getOperation(getHttpMethod());
    final WithResponse callBack = new WithResponse(operation.getSuccessStatus(), DEFAULT_JSON_CONTENT, CACHE_NEVER);
    Object toReturn = transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Object>() {

        @Override
        public Object execute() throws Throwable {
            Object result = executeAction(resource, params, callBack);
            if (result instanceof BinaryResource) {
                // don't postprocess it.
                return result;
            }
            return helper.processAdditionsToTheResponse(res, resource.getMetaData().getApi(), entityCollectionName, params, result);
        }
    }, isReadOnly, true);
    setResponse(res, callBack);
    return toReturn;
}
Also used : FileBinaryResource(org.alfresco.rest.framework.resource.content.FileBinaryResource) NodeBinaryResource(org.alfresco.rest.framework.resource.content.NodeBinaryResource) BinaryResource(org.alfresco.rest.framework.resource.content.BinaryResource) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) ResourceOperation(org.alfresco.rest.framework.core.ResourceOperation)

Example 4 with ResourceOperation

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

the class ResourceWebScriptDelete method execute.

@Override
public Void execute(final ResourceWithMetadata resource, final Params params, final WebScriptResponse res, boolean isReadOnly) {
    final ResourceOperation operation = resource.getMetaData().getOperation(HttpMethod.DELETE);
    final WithResponse callBack = new WithResponse(operation.getSuccessStatus(), DEFAULT_JSON_CONTENT, CACHE_NEVER);
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // ignore return result
            executeAction(resource, params, callBack);
            return null;
        }
    }, false, true);
    setResponse(res, callBack);
    return null;
}
Also used : ResourceOperation(org.alfresco.rest.framework.core.ResourceOperation)

Example 5 with ResourceOperation

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

the class ResourceWebScriptPost method extractParams.

@Override
public Params extractParams(ResourceMetadata resourceMeta, WebScriptRequest req) {
    final RecognizedParams params = getRecognizedParams(req);
    final String entityId = req.getServiceMatch().getTemplateVars().get(ResourceLocator.ENTITY_ID);
    final String relationshipId = req.getServiceMatch().getTemplateVars().get(ResourceLocator.RELATIONSHIP_ID);
    final ResourceOperation operation = resourceMeta.getOperation(HttpMethod.POST);
    switch(resourceMeta.getType()) {
        case ENTITY:
            if (StringUtils.isNotBlank(entityId)) {
                throw new UnsupportedResourceOperationException("POST is executed against the collection URL");
            } else {
                Object postedObj = processRequest(resourceMeta, operation, req);
                return Params.valueOf(null, params, postedObj, req);
            }
        case RELATIONSHIP:
            if (StringUtils.isNotBlank(relationshipId)) {
                throw new UnsupportedResourceOperationException("POST is executed against the collection URL");
            } else {
                Object postedRel = processRequest(resourceMeta, operation, req);
                return Params.valueOf(entityId, params, postedRel, req);
            }
        case OPERATION:
            final String operationName = req.getServiceMatch().getTemplateVars().get(ResourceLocator.RELATIONSHIP_RESOURCE);
            final String propertyName = req.getServiceMatch().getTemplateVars().get(ResourceLocator.PROPERTY);
            if (StringUtils.isNotBlank(entityId) && StringUtils.isNotBlank(operationName)) {
                Object postedObj = processRequest(resourceMeta, operation, req);
                if (StringUtils.isNotBlank(propertyName)) {
                    return Params.valueOf(entityId, relationshipId, params, postedObj, req);
                } else {
                    return Params.valueOf(entityId, params, postedObj, req);
                }
            }
        // Fall through to unsupported.
        default:
            throw new UnsupportedResourceOperationException("POST not supported for Actions");
    }
}
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)

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