Search in sources :

Example 11 with Api

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

the class ResourceDictionaryBuilder method processResources.

private static void processResources(ResourceDictionary rd, Map<ApiScopeKey, Map<Integer, List<ResourceWithMetadata>>> entityApiMap, Map<ApiScopeKey, Map<Integer, List<ResourceWithMetadata>>> relationshipApiMap) {
    logger.debug("Will process resources ");
    for (Entry<ApiScopeKey, Map<Integer, List<ResourceWithMetadata>>> apiGroupedByNameAndScopeEntry : entityApiMap.entrySet()) {
        logger.debug("Processing " + apiGroupedByNameAndScopeEntry.getKey());
        Map<Integer, List<ResourceWithMetadata>> apiVersions = apiGroupedByNameAndScopeEntry.getValue();
        List<Integer> versions = new ArrayList<Integer>(apiVersions.keySet());
        logger.debug("Versions " + String.valueOf(versions));
        Collections.sort(versions);
        // Loop through the different versions of the API and add them to the Map of resources By API with Version.
        for (int i = 0; i < versions.size(); i++) {
            Api apiVersion = Api.valueOf(apiGroupedByNameAndScopeEntry.getKey().name, apiGroupedByNameAndScopeEntry.getKey().scope.toString(), Integer.toString(versions.get(i)));
            Map<String, ResourceWithMetadata> resourcesByApiAndVersion = findResources(rd.getAllResources(), apiVersion);
            logger.debug("Working with api " + apiVersion);
            // If there's a previous version, get them first.
            if (i > 0) {
                logger.debug("Has previous so adding all entities from previous version ");
                Api previousVersion = Api.valueOf(apiVersion.getName(), apiVersion.getScope().toString(), Integer.toString(apiVersion.getVersion() - 1));
                logger.debug("Previous version is " + previousVersion);
                Map<String, ResourceWithMetadata> resourcesForPreviousVersion = findResources(rd.getAllResources(), previousVersion);
                resourcesByApiAndVersion.putAll(resourcesForPreviousVersion);
            }
            // Now add this version's resource (overwriting any from the previous version)
            for (ResourceWithMetadata resourceWithMetadata : apiGroupedByNameAndScopeEntry.getValue().get(apiVersion.getVersion())) {
                resourcesByApiAndVersion.put(resourceWithMetadata.getMetaData().getUniqueId(), resourceWithMetadata);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Api(org.alfresco.rest.framework.Api) Map(java.util.Map) HashMap(java.util.HashMap)

Example 12 with Api

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

the class ResourceDictionaryBuilder method parseResources.

/**
 * Sort through the resources grouping them by api name/scope, then version.
 * @param resources Collection<Object>
 * @return Map
 */
private static <T> Map<ApiScopeKey, Map<Integer, List<ResourceWithMetadata>>> parseResources(Collection<Object> resources) {
    Map<ApiScopeKey, Map<Integer, List<ResourceWithMetadata>>> apiMap = new HashMap<ApiScopeKey, Map<Integer, List<ResourceWithMetadata>>>();
    for (Object bean : resources) {
        List<ResourceMetadata> metaData = ResourceInspector.inspect(bean.getClass());
        Api api = ResourceInspector.inspectApi(bean.getClass());
        if (api == null) {
            throw new PlatformRuntimeException("Invalid resource bean defintion.  No @WebApi defined for package: " + bean.getClass().getPackage().getName());
        }
        ApiScopeKey key = new ApiScopeKey(api);
        // Find api scope/name and use a key
        Map<Integer, List<ResourceWithMetadata>> apiVersions = apiMap.get(key);
        if (apiVersions == null) {
            // if doesn't exist then create it.
            apiVersions = new HashMap<Integer, List<ResourceWithMetadata>>();
            apiMap.put(key, apiVersions);
        }
        List<ResourceWithMetadata> resourcesWithMeta = apiVersions.get(api.getVersion());
        if (resourcesWithMeta == null) {
            // if doesn't exist then create it.
            resourcesWithMeta = new ArrayList<ResourceWithMetadata>();
            apiVersions.put(api.getVersion(), resourcesWithMeta);
        }
        // For each meta just add it to the list.
        for (ResourceMetadata resourceMeta : metaData) {
            resourcesWithMeta.add(new ResourceWithMetadata(bean, resourceMeta));
        }
    }
    return apiMap;
}
Also used : PlatformRuntimeException(org.springframework.extensions.surf.exception.PlatformRuntimeException) HashMap(java.util.HashMap) List(java.util.List) ArrayList(java.util.ArrayList) Api(org.alfresco.rest.framework.Api) Map(java.util.Map) HashMap(java.util.HashMap)

Example 13 with Api

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

the class ApiWebScript method execute.

@Override
public void execute(final WebScriptRequest req, final WebScriptResponse res) throws IOException {
    Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
    Api api = assistant.determineApi(templateVars);
    final BufferedRequest bufferedReq = getRequest(req);
    final BufferedResponse bufferedRes = getResponse(res);
    try {
        execute(api, bufferedReq, bufferedRes);
    } finally {
        // Get rid of any temporary files
        if (bufferedReq != null) {
            bufferedReq.close();
        }
    }
    // Ensure a response is always flushed after successful execution
    if (bufferedRes != null) {
        bufferedRes.writeResponse();
    }
}
Also used : BufferedRequest(org.alfresco.repo.web.scripts.BufferedRequest) BufferedResponse(org.alfresco.repo.web.scripts.BufferedResponse) Api(org.alfresco.rest.framework.Api)

Example 14 with Api

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

the class InspectorTests method testInspectAddressedProperties.

@Test
public void testInspectAddressedProperties() {
    Api api = Api.valueOf("alfrescomock", "private", "1");
    List<ResourceMetadata> metainfo = new ArrayList<ResourceMetadata>();
    ResourceInspector.inspectAddressedProperties(api, FlockEntityResource.class, "myroot", metainfo);
    assertTrue(metainfo.size() == 1);
    ResourceMetadata metaData = metainfo.get(0);
    assertEquals("/myroot/{id}/photo", metaData.getUniqueId());
    assertTrue(metaData.getOperations().size() == 3);
    assertNotNull("FlockEntityResource supports GET", metaData.getOperation(HttpMethod.GET));
    assertNotNull("FlockEntityResource supports PUT", metaData.getOperation(HttpMethod.PUT));
    assertNotNull("FlockEntityResource supports DELETE", metaData.getOperation(HttpMethod.DELETE));
    metainfo.clear();
    ResourceInspector.inspectAddressedProperties(api, FlocketEntityResource.class, "myroot", metainfo);
    assertTrue(metainfo.size() == 3);
    for (ResourceMetadata resourceMetadata : metainfo) {
        if ("/myroot/{id}/photo".equals(resourceMetadata.getUniqueId())) {
            assertNotNull("FlocketEntityResource supports GET", resourceMetadata.getOperation(HttpMethod.GET));
            assertNotNull("FlocketEntityResource supports PUT", resourceMetadata.getOperation(HttpMethod.PUT));
            assertNotNull("FlocketEntityResource supports DELETE", resourceMetadata.getOperation(HttpMethod.DELETE));
        } else {
            if ("/myroot/{id}/album".equals(resourceMetadata.getUniqueId())) {
                assertNotNull("FlocketEntityResource supports GET", resourceMetadata.getOperation(HttpMethod.GET));
                assertNotNull("FlocketEntityResource supports PUT", resourceMetadata.getOperation(HttpMethod.PUT));
                assertNull("FlocketEntityResource does not support DELETE", resourceMetadata.getOperation(HttpMethod.DELETE));
            } else {
                if ("/myroot/{id}/madeUpProp".equals(resourceMetadata.getUniqueId())) {
                    assertNotNull("FlocketEntityResource supports GET", resourceMetadata.getOperation(HttpMethod.GET));
                    assertNull("FlocketEntityResource does not supports PUT", resourceMetadata.getOperation(HttpMethod.PUT));
                    assertNull("FlocketEntityResource does not support DELETE", resourceMetadata.getOperation(HttpMethod.DELETE));
                } else {
                    fail("Invalid address property information.");
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Api(org.alfresco.rest.framework.Api) ResourceMetadata(org.alfresco.rest.framework.core.ResourceMetadata) Test(org.junit.Test)

Example 15 with Api

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

Aggregations

Api (org.alfresco.rest.framework.Api)18 Test (org.junit.Test)12 ResourceWithMetadata (org.alfresco.rest.framework.core.ResourceWithMetadata)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 UnsupportedResourceOperationException (org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException)3 EntityResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction)3 JSONObject (org.json.JSONObject)3 List (java.util.List)2 Map (java.util.Map)2 WebApi (org.alfresco.rest.framework.WebApi)2 ResourceMetadata (org.alfresco.rest.framework.core.ResourceMetadata)2 NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)2 BinaryResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.BinaryResourceAction)2 MultiPartRelationshipResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.MultiPartRelationshipResourceAction)2 MultiPartResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.MultiPartResourceAction)2 RelationshipResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction)2 ResourceAction (org.alfresco.rest.framework.resource.actions.interfaces.ResourceAction)2 SlimGoat (org.alfresco.rest.framework.tests.api.mocks3.SlimGoat)2 Method (java.lang.reflect.Method)1