Search in sources :

Example 1 with ResourceDictionary

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

the class WebScriptOptionsMetaData method execute.

@Override
public void execute(final Api api, WebScriptRequest req, WebScriptResponse res) throws IOException {
    final Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
    ResourceDictionary resourceDic = lookupDictionary.getDictionary();
    Map<String, ResourceWithMetadata> apiResources = resourceDic.getAllResources().get(api);
    if (apiResources == null) {
        throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_INVALID_API);
    }
    String collectionName = templateVars.get(ResourceLocator.COLLECTION_RESOURCE);
    String resourceName = templateVars.get(ResourceLocator.RELATIONSHIP_RESOURCE);
    String resourceKey = ResourceDictionary.resourceKey(collectionName, resourceName);
    if (logger.isDebugEnabled()) {
        logger.debug("Locating resource :" + resourceKey);
    }
    ResourceWithMetadata resource = apiResources.get(resourceKey);
    if (resource == null) {
        // Get entity resource and check if we are referencing a property on it.
        resourceKey = ResourceDictionary.propertyResourceKey(collectionName, resourceName);
        resource = apiResources.get(resourceKey);
    }
    ResourceMetaDataWriter writer = chooseWriter(req);
    writer.writeMetaData(res.getOutputStream(), resource, apiResources);
}
Also used : InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) ResourceDictionary(org.alfresco.rest.framework.core.ResourceDictionary) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) ResourceMetaDataWriter(org.alfresco.rest.framework.metadata.ResourceMetaDataWriter)

Example 2 with ResourceDictionary

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

the class InfoWebScriptGet method execute.

@Override
public void execute(final Api api, WebScriptRequest req, WebScriptResponse res) throws IOException {
    ResourceDictionary resourceDic = lookupDictionary.getDictionary();
    final Map<String, ResourceWithMetadata> apiResources = resourceDic.getAllResources().get(api);
    if (apiResources == null) {
        throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_INVALID_API);
    }
    assistant.getJsonHelper().withWriter(res.getOutputStream(), new Writer() {

        @Override
        public void writeContents(JsonGenerator generator, ObjectMapper objectMapper) throws JsonGenerationException, JsonMappingException, IOException {
            List<ExecutionResult> entities = new ArrayList<ExecutionResult>();
            for (ResourceWithMetadata resource : apiResources.values()) {
                entities.add(new ExecutionResult(resource.getMetaData(), null));
            }
            Collections.sort(entities, new Comparator<ExecutionResult>() {

                public int compare(ExecutionResult r1, ExecutionResult r2) {
                    return ((ResourceMetadata) r1.getRoot()).getUniqueId().compareTo(((ResourceMetadata) r2.getRoot()).getUniqueId());
                }
            });
            objectMapper.writeValue(generator, CollectionWithPagingInfo.asPaged(Paging.DEFAULT, entities));
        }
    });
}
Also used : ResourceDictionary(org.alfresco.rest.framework.core.ResourceDictionary) ExecutionResult(org.alfresco.rest.framework.jacksonextensions.ExecutionResult) IOException(java.io.IOException) Comparator(java.util.Comparator) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ArrayList(java.util.ArrayList) List(java.util.List) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) Writer(org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with ResourceDictionary

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

the class WriterTests method testWriters.

@Test
public void testWriters() throws IOException {
    ResourceDictionary resourceDic = locator.getDictionary();
    Map<String, ResourceWithMetadata> apiResources = resourceDic.getAllResources().get(api);
    String writtenOut = testWriter(defaultMetaWriter, apiResources.get("/sheep"), apiResources);
    assertTrue(writtenOut.startsWith("{\"list\":{\"pagination\":{\"count\":5"));
// ResourceMetaDataWriter wadlWriter = new WebScriptOptionsMetaData();
// writtenOut = testWriter(wadlWriter, apiResources.get("/sheep"), apiResources);
// assertTrue(writtenOut.startsWith("{\"list\":{\"pagination\":{\"count\":4"));
}
Also used : ResourceDictionary(org.alfresco.rest.framework.core.ResourceDictionary) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) Test(org.junit.Test)

Aggregations

ResourceDictionary (org.alfresco.rest.framework.core.ResourceDictionary)3 ResourceWithMetadata (org.alfresco.rest.framework.core.ResourceWithMetadata)3 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 ExecutionResult (org.alfresco.rest.framework.jacksonextensions.ExecutionResult)1 Writer (org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer)1 ResourceMetaDataWriter (org.alfresco.rest.framework.metadata.ResourceMetaDataWriter)1 Test (org.junit.Test)1