Search in sources :

Example 1 with ResourceParameter

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

the class ResourceWebScriptPost method extractObjFromJson.

/**
 * If the @WebApiParam has been used and set allowMultiple to false then this will get a single entry.  It
 * should error if an array is passed in.
 * @param resourceMeta ResourceMetadata
 * @param req WebScriptRequest
 * @return Either an object
 */
private Object extractObjFromJson(ResourceMetadata resourceMeta, ResourceOperation operation, WebScriptRequest req) {
    if (operation == null) {
        return null;
    }
    Class<?> objType = resourceMeta.getObjectType(operation);
    boolean isTypeOperation = resourceMeta.getType().equals(ResourceMetadata.RESOURCE_TYPE.OPERATION);
    List<ResourceParameter> params = operation.getParameters();
    if (!params.isEmpty()) {
        for (ResourceParameter resourceParameter : params) {
            // POST to collection may or may not support List as json body, Operations don't support a List as json body
            boolean notMultiple = ((!resourceParameter.isAllowMultiple()) || isTypeOperation);
            if (ResourceParameter.KIND.HTTP_BODY_OBJECT.equals(resourceParameter.getParamType()) && notMultiple) {
                // Only allow 1 value.
                try {
                    Object jsonContent = null;
                    if (objType != null) {
                        // check if the body is optional and is not provided
                        if (!resourceParameter.isRequired() && Integer.valueOf(req.getHeader("content-length")) <= 0) {
                            // in some cases the body is optional and the json doesn't need to be extracted
                            return null;
                        } else {
                            jsonContent = extractJsonContent(req, assistant.getJsonHelper(), objType);
                        }
                    }
                    if (isTypeOperation) {
                        return jsonContent;
                    } else {
                        return Arrays.asList(jsonContent);
                    }
                } catch (InvalidArgumentException iae) {
                    if (iae.getMessage().contains("START_ARRAY") && iae.getMessage().contains("line: 1, column: 1")) {
                        throw new UnsupportedResourceOperationException("Only 1 entity is supported in the HTTP request body");
                    } else {
                        throw iae;
                    }
                }
            }
        }
    }
    if (objType == null) {
        return null;
    }
    if (isTypeOperation) {
        // Operations don't support a List as json body
        return extractJsonContent(req, assistant.getJsonHelper(), objType);
    } else {
        return extractJsonContentAsList(req, assistant.getJsonHelper(), objType);
    }
}
Also used : ResourceParameter(org.alfresco.rest.framework.core.ResourceParameter) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException)

Example 2 with ResourceParameter

use of org.alfresco.rest.framework.core.ResourceParameter 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);
    extractor.setLocator(locator);
    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 3 with ResourceParameter

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

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

use of org.alfresco.rest.framework.core.ResourceParameter 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)

Aggregations

ResourceParameter (org.alfresco.rest.framework.core.ResourceParameter)6 ResourceOperation (org.alfresco.rest.framework.core.ResourceOperation)5 Test (org.junit.Test)5 Method (java.lang.reflect.Method)3 ResourceMetadata (org.alfresco.rest.framework.core.ResourceMetadata)3 HttpMethod (org.springframework.http.HttpMethod)3 UnsupportedResourceOperationException (org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException)2 EntityResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction)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 HashMap (java.util.HashMap)1 List (java.util.List)1 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)1 BinaryResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.BinaryResourceAction)1 Params (org.alfresco.rest.framework.resource.parameters.Params)1 Farmer (org.alfresco.rest.framework.tests.api.mocks.Farmer)1 GoatEntityResource (org.alfresco.rest.framework.tests.api.mocks.GoatEntityResource)1 GrassEntityResource (org.alfresco.rest.framework.tests.api.mocks.GrassEntityResource)1