diff --git a/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptHelper.java b/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptHelper.java index 9c4f593ec1..855e785960 100644 --- a/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptHelper.java +++ b/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptHelper.java @@ -116,6 +116,44 @@ public class ResourceWebScriptHelper * ReturnAllBeanProperties class */ public static BeanPropertiesFilter getFilter(String filterParams) + { + return getFilter(filterParams, null); + } + + /** + * Takes the web request and looks for a "filter" parameter Parses the + * parameter and produces a list of bean properties to use as a filter A + * SimpleBeanPropertyFilter it returned that uses the properties If no + * filter param is set then a default BeanFilter is returned that will never + * filter properties (ie. Returns all bean properties). + * + * If selectList is provided then it will take precedence (ie. be included) over the 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 "properties" 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 "properties" 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 properties filtering, eg. + * + * select=abc/blah&properties=id,name,ghi + * + * If properties filtering is desired then it would require "abc" to be selected and returned as a whole, eg. + * + * select=abc&properties=id,name,ghi + * + * @param filterParams + * @param selectList + * @return + */ + public static BeanPropertiesFilter getFilter(String filterParams, List selectList) { if (filterParams != null) { @@ -125,6 +163,17 @@ public class ResourceWebScriptHelper { filteredProperties.add(st.nextToken()); } + + // if supplied, the select takes precedence over properties (filter) for top-level bean properties + if (selectList != null) + { + for (String select : selectList) + { + String[] split = select.split("/"); + filteredProperties.add(split[0]); + } + } + logger.debug("Filtering using the following properties: " + filteredProperties); BeanPropertiesFilter filter = new BeanPropertiesFilter(filteredProperties); return filter; @@ -456,6 +505,7 @@ public class ResourceWebScriptHelper //Simple property or Collection that can't be embedded so just return it. return objectToWrap; } + final ExecutionResult execRes = new ExecutionResult(objectToWrap, params.getFilter()); Map> embeddded = ResourceInspector.findEmbeddedResources(objectToWrap.getClass()); @@ -640,13 +690,15 @@ public class ResourceWebScriptHelper */ public static RecognizedParams getRecognizedParams(WebScriptRequest req) { - Paging paging = findPaging(req); + Paging paging = findPaging(req); List sorting = getSort(req.getParameter(ResourceWebScriptHelper.PARAM_ORDERBY)); Map relationFilter = getRelationFilter(req.getParameter(ResourceWebScriptHelper.PARAM_RELATIONS)); - BeanPropertiesFilter filter = getFilter(req.getParameter(ResourceWebScriptHelper.PARAM_FILTER_PROPS)); Query whereQuery = getWhereClause(req.getParameter(ResourceWebScriptHelper.PARAM_WHERE)); Map requestParams = getRequestParameters(req); + List theSelect = getSelectClause(req.getParameter(ResourceWebScriptHelper.PARAM_SELECT)); + BeanPropertiesFilter filter = getFilter(req.getParameter(ResourceWebScriptHelper.PARAM_FILTER_PROPS), theSelect); + return new RecognizedParams(requestParams, paging, filter, relationFilter, theSelect, whereQuery, sorting); } diff --git a/source/test-java/org/alfresco/rest/framework/tests/core/SerializeTests.java b/source/test-java/org/alfresco/rest/framework/tests/core/SerializeTests.java index 87b1b2e623..747044484c 100644 --- a/source/test-java/org/alfresco/rest/framework/tests/core/SerializeTests.java +++ b/source/test-java/org/alfresco/rest/framework/tests/core/SerializeTests.java @@ -42,6 +42,7 @@ import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; @@ -459,7 +460,8 @@ public class SerializeTests //this is correct } } - + + // note: exposed as "properties" query param @Test public void testFilter() throws IOException, JSONException { @@ -473,7 +475,21 @@ public class SerializeTests 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 select taking precendence + List theSelect = ResourceWebScriptHelper.getSelectClause("name"); + theFilter = ResourceWebScriptHelper.getFilter("age", theSelect); + 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); @@ -494,7 +510,6 @@ public class SerializeTests res = helper.postProcessResponse(v3,"goat",ParamsExtender.valueOf(relFiler, "notUsed"),new SlimGoat()); out = writeResponse(res); assertTrue("Must return only the herd name.", StringUtils.contains(out, "{\"name\":\"bigun\"}")); - } @Test