Search in sources :

Example 1 with BeanPropertiesFilter

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

the class SerializeTests method testExpandRelations.

@Test
public void testExpandRelations() throws IOException {
    assertNotNull(helper);
    Map<String, BeanPropertiesFilter> rFilter = getRelationFilter("blacksheep,baaahh");
    ExecutionResult res = (ExecutionResult) helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api, "sheep", ParamsExtender.valueOf(rFilter, "1"), new Farmer("180"));
    assertNotNull(res);
    String out = writeResponse(res);
    assertTrue(Farmer.class.equals(res.getRoot().getClass()));
    assertTrue("There must be json output", StringUtils.isNotBlank(out));
    Paging pageRequest = Paging.valueOf(1, 2);
    Object resultCollection = helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api, "sheep", ParamsExtender.valueOf(rFilter, "1"), CollectionWithPagingInfo.asPaged(pageRequest, Arrays.asList(new Farmer("180"), new Farmer("190"), new Farmer("280"))));
    assertNotNull(resultCollection);
    out = writeResponse(resultCollection);
    assertTrue("There must be json output", StringUtils.isNotBlank(out));
}
Also used : Paging(org.alfresco.rest.framework.resource.parameters.Paging) WebScriptResponse(org.springframework.extensions.webscripts.WebScriptResponse) ExecutionResult(org.alfresco.rest.framework.jacksonextensions.ExecutionResult) JSONObject(org.json.JSONObject) Farmer(org.alfresco.rest.framework.tests.api.mocks.Farmer) BeanPropertiesFilter(org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter) Test(org.junit.Test)

Example 2 with BeanPropertiesFilter

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

the class SerializeTests method testFilter.

// note: exposed as "properties" query param
@Test
public void testFilter() throws IOException, JSONException {
    assertNotNull(helper);
    BeanPropertiesFilter theFilter = getFilter("age");
    Object res = new ExecutionResult(new Sheep("bob"), theFilter);
    String out = writeResponse(res);
    assertTrue("Filter must only return the age.", StringUtils.contains(out, "{\"age\":3}"));
    theFilter = getFilter("age,name");
    res = new ExecutionResult(new Sheep("bob"), theFilter);
    out = writeResponse(res);
    JSONObject jsonRsp = new JSONObject(new JSONTokener(out));
    assertEquals(1, jsonRsp.length());
    JSONObject entry = jsonRsp.getJSONObject("entry");
    assertEquals(2, entry.length());
    assertEquals("The name should be 'Dolly'", "Dolly", entry.getString("name"));
    assertTrue("The age should be 3", entry.getInt("age") == 3);
    // unit test filter with "include" taking precendence over "fields" filter
    List<String> theInclude = getIncludeClause("name");
    theFilter = getFilter("age", theInclude);
    res = new ExecutionResult(new Sheep("bob"), theFilter);
    out = writeResponse(res);
    jsonRsp = new JSONObject(new JSONTokener(out));
    assertEquals(1, jsonRsp.length());
    entry = jsonRsp.getJSONObject("entry");
    assertEquals(2, entry.length());
    assertEquals("The name should be 'Dolly'", "Dolly", entry.getString("name"));
    assertTrue("The age should be 3", entry.getInt("age") == 3);
    Api v3 = Api.valueOf(api.getName(), api.getScope().toString(), "3");
    Map<String, BeanPropertiesFilter> relFiler = getRelationFilter("herd");
    res = helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), v3, "goat", ParamsExtender.valueOf(relFiler, "notUsed"), new SlimGoat());
    out = writeResponse(res);
    jsonRsp = new JSONObject(new JSONTokener(out));
    entry = jsonRsp.getJSONObject("relations").getJSONObject("herd").getJSONObject("list").getJSONArray("entries").getJSONObject(0).getJSONObject("entry");
    assertEquals("The name should be 'bigun'", "bigun", entry.getString("name"));
    assertTrue("The quantity should be 56", entry.getInt("quantity") == 56);
    relFiler = getRelationFilter("herd(name)");
    res = helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), v3, "goat", ParamsExtender.valueOf(relFiler, "notUsed"), new SlimGoat());
    out = writeResponse(res);
    assertTrue("Must return only the herd name.", StringUtils.contains(out, "{\"name\":\"bigun\"}"));
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) Sheep(org.alfresco.rest.framework.tests.api.mocks.Sheep) SlimGoat(org.alfresco.rest.framework.tests.api.mocks3.SlimGoat) JSONObject(org.json.JSONObject) ExecutionResult(org.alfresco.rest.framework.jacksonextensions.ExecutionResult) Api(org.alfresco.rest.framework.Api) BeanPropertiesFilter(org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter) Test(org.junit.Test)

Example 3 with BeanPropertiesFilter

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

the class RecognizedParamsExtractorTest method getRelationFilterTest.

@Test
public void getRelationFilterTest() {
    Map<String, BeanPropertiesFilter> theFilter = getRelationFilter(null);
    assertNotNull(theFilter);
    assertTrue("Null passed in so nothing to filter.", theFilter.isEmpty());
    theFilter = getRelationFilter("bob");
    assertNotNull(theFilter);
    assertTrue("Must be a single relationship", theFilter.size() == 1);
    assertTrue("Must be a single relationship called bob", theFilter.containsKey("bob"));
    BeanPropertiesFilter aFilter = theFilter.get("bob");
    assertTrue("No bean properties specified so need a BeanPropertiesFilter.ALLOW_ALL class", BeanPropertiesFilter.AllProperties.class.equals(aFilter.getClass()));
    theFilter = getRelationFilter("bob,hope");
    assertNotNull(theFilter);
    assertTrue("Must be a two relationships", theFilter.size() == 2);
    assertTrue("Must have hope.", theFilter.containsKey("hope"));
    aFilter = theFilter.get("hope");
    assertTrue("No bean properties specified so need a BeanPropertiesFilter.ALLOW_ALL class", BeanPropertiesFilter.AllProperties.class.equals(aFilter.getClass()));
    theFilter = getRelationFilter("bob(name),hope");
    assertNotNull(theFilter);
    assertTrue("Must be a two relationships", theFilter.size() == 2);
    assertTrue("Must have bob.", theFilter.containsKey("bob"));
    aFilter = theFilter.get("bob");
    assertTrue("Bean properties specified so must be an BeanPropertiesFilter class", BeanPropertiesFilter.class.equals(aFilter.getClass()));
    theFilter = getRelationFilter("bob,hope(age,name)");
    assertNotNull(theFilter);
    assertTrue("Must be a two relationships", theFilter.size() == 2);
    aFilter = theFilter.get("bob");
    assertTrue("No bean properties specified so need a BeanPropertiesFilter.ALLOW_ALL class", BeanPropertiesFilter.AllProperties.class.equals(aFilter.getClass()));
    aFilter = theFilter.get("hope");
    assertTrue("Bean properties specified so must be an BeanPropertiesFilter class", BeanPropertiesFilter.class.equals(aFilter.getClass()));
    theFilter = getRelationFilter("bob(name,age),nohope,hope(height,width)");
    assertNotNull(theFilter);
    assertTrue("Must be a three relationships", theFilter.size() == 3);
    aFilter = theFilter.get("bob");
    assertTrue("Bean properties specified so must be an BeanPropertiesFilter class", BeanPropertiesFilter.class.equals(aFilter.getClass()));
    aFilter = theFilter.get("nohope");
    assertTrue("No bean properties specified so need a ReturnAllBeanProperties class", BeanPropertiesFilter.AllProperties.class.equals(aFilter.getClass()));
    aFilter = theFilter.get("hope");
    assertTrue("Bean properties specified so must be an BeanPropertiesFilter class", BeanPropertiesFilter.class.equals(aFilter.getClass()));
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) BeanPropertiesFilter(org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter) Test(org.junit.Test)

Example 4 with BeanPropertiesFilter

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

the class RecognizedParamsExtractor method getFilter.

/**
 * Takes the web request and looks for a "fields" parameter (otherwise deprecated "properties" parameter).
 * Parses the parameter and produces a list of bean properties to use as a filter A
 * SimpleBeanPropertyFilter it returned that uses the bean properties. If no
 * filter param is set then a default BeanFilter is returned that will never
 * filter fields (ie. Returns all bean properties).
 * If selectList is provided then it will take precedence (ie. be included) over the fields/properties filter
 * for top-level entries (bean properties).
 * For example, this will return entries from both select & properties, eg.
 * select=abc,def&properties=id,name,ghi
 * Note: it should be noted that API-generic "fields" clause does not currently work for sub-entries.
 * Hence, even if the API-specific "select" clause allows selection of a sub-entries this cannot be used
 * with "fields" filtering. For example, an API-specific method may implement and return "abc/blah", eg.
 * select=abc/blah
 * However the following will not return "abc/blah" if used with fields filtering, eg.
 * select=abc/blah&fields=id,name,ghi
 * If fields filtering is desired then it would require "abc" to be selected and returned as a whole, eg.
 * select=abc&fields=id,name,ghi
 *
 * @param filterParams
 * @param selectList
 * @return
 */
default BeanPropertiesFilter getFilter(String filterParams, List<String> selectList) {
    if (filterParams != null) {
        StringTokenizer st = new StringTokenizer(filterParams, ",");
        Set<String> filteredProperties = new HashSet<String>(st.countTokens());
        while (st.hasMoreTokens()) {
            filteredProperties.add(st.nextToken().trim());
        }
        // if supplied, the select takes precedence over the filter (fields/properties) for top-level bean properties
        if (selectList != null) {
            for (String select : selectList) {
                String[] split = select.split("/");
                filteredProperties.add(split[0]);
            }
        }
        rpeLogger().debug("Filtering using the following properties: " + filteredProperties);
        BeanPropertiesFilter filter = new BeanPropertiesFilter(filteredProperties);
        return filter;
    }
    return BeanPropertiesFilter.ALLOW_ALL;
}
Also used : StringTokenizer(java.util.StringTokenizer) BeanPropertiesFilter(org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter) HashSet(java.util.HashSet)

Example 5 with BeanPropertiesFilter

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

the class ResourceWebScriptHelper method executeResource.

/**
 * Executes a single related Resource.  The results are added to list of embedded results used by
 * the ExecutionResult object.
 *
 * @param api Api
 * @param params Params
 * @param uniqueEntityId String
 * @param resourceKey String
 * @param resource ResourceWithMetadata
 * @return Object
 */
private Object executeResource(final Api api, Params params, final String uniqueEntityId, final String resourceKey, final ResourceWithMetadata resource) {
    try {
        BeanPropertiesFilter paramFilter = null;
        final Object[] resultOfExecution = new Object[1];
        Map<String, BeanPropertiesFilter> filters = params.getRelationsFilter();
        if (filters != null) {
            paramFilter = filters.get(resourceKey);
        }
        final Params executionParams = Params.valueOf(paramFilter, uniqueEntityId, params.getRequest());
        final WithResponse callBack = new WithResponse(Status.STATUS_OK, ResponseWriter.DEFAULT_JSON_CONTENT, ResponseWriter.CACHE_NEVER);
        // Read only because this only occurs for GET requests
        Object result = executor.executeAction(resource, executionParams, callBack);
        return processAdditionsToTheResponse(null, api, null, executionParams, result);
    } catch (NotFoundException e) {
        // ignore, cannot access the object so don't embed it
        if (logger.isDebugEnabled()) {
            logger.debug("Ignored error, cannot access the object so can't embed it ", e);
        }
    } catch (PermissionDeniedException e) {
        // ignore, cannot access the object so don't embed it
        if (logger.isDebugEnabled()) {
            logger.debug("Ignored error, cannot access the object so can't embed it ", e);
        }
    } catch (Throwable throwable) {
        logger.warn("Failed to execute a RelatedResource for " + resourceKey + " " + throwable.getMessage());
    }
    // default
    return null;
}
Also used : Params(org.alfresco.rest.framework.resource.parameters.Params) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) BeanPropertiesFilter(org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter)

Aggregations

BeanPropertiesFilter (org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter)10 Paging (org.alfresco.rest.framework.resource.parameters.Paging)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 FilterPropString (org.alfresco.repo.node.getchildren.FilterPropString)2 ExecutionResult (org.alfresco.rest.framework.jacksonextensions.ExecutionResult)2 Params (org.alfresco.rest.framework.resource.parameters.Params)2 SortColumn (org.alfresco.rest.framework.resource.parameters.SortColumn)2 SiteInfo (org.alfresco.service.cmr.site.SiteInfo)2 JSONObject (org.json.JSONObject)2 AbstractList (java.util.AbstractList)1 HashSet (java.util.HashSet)1 StringTokenizer (java.util.StringTokenizer)1 PagingRequest (org.alfresco.query.PagingRequest)1 FilterProp (org.alfresco.repo.node.getchildren.FilterProp)1 FilterPropBoolean (org.alfresco.repo.node.getchildren.FilterPropBoolean)1 Api (org.alfresco.rest.framework.Api)1 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)1 NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)1 PermissionDeniedException (org.alfresco.rest.framework.core.exceptions.PermissionDeniedException)1