Merged HEAD (5.2) to 5.2.N (5.2.1)

126413 jkaabimofrad: Merged FILE-FOLDER-API (5.2.0) to HEAD (5.2)
      121614 jvonka: REST fwk - allow select clause properties to take precedence (ie. be included) when using properties 'filter'
      - eg. select=abc,def&properties=id,name, ghi ... should return all five (not just id,name,ghi)
      RA-739


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@126759 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Ancuta Morarasu
2016-05-11 11:14:00 +00:00
parent c8b6320632
commit d446539d39
2 changed files with 71 additions and 4 deletions

View File

@@ -116,6 +116,44 @@ public class ResourceWebScriptHelper
* ReturnAllBeanProperties class * ReturnAllBeanProperties class
*/ */
public static BeanPropertiesFilter getFilter(String filterParams) 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<String> selectList)
{ {
if (filterParams != null) if (filterParams != null)
{ {
@@ -125,6 +163,17 @@ public class ResourceWebScriptHelper
{ {
filteredProperties.add(st.nextToken()); 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); logger.debug("Filtering using the following properties: " + filteredProperties);
BeanPropertiesFilter filter = new BeanPropertiesFilter(filteredProperties); BeanPropertiesFilter filter = new BeanPropertiesFilter(filteredProperties);
return filter; return filter;
@@ -456,6 +505,7 @@ public class ResourceWebScriptHelper
//Simple property or Collection that can't be embedded so just return it. //Simple property or Collection that can't be embedded so just return it.
return objectToWrap; return objectToWrap;
} }
final ExecutionResult execRes = new ExecutionResult(objectToWrap, params.getFilter()); final ExecutionResult execRes = new ExecutionResult(objectToWrap, params.getFilter());
Map<String,Pair<String,Method>> embeddded = ResourceInspector.findEmbeddedResources(objectToWrap.getClass()); Map<String,Pair<String,Method>> embeddded = ResourceInspector.findEmbeddedResources(objectToWrap.getClass());
@@ -640,13 +690,15 @@ public class ResourceWebScriptHelper
*/ */
public static RecognizedParams getRecognizedParams(WebScriptRequest req) public static RecognizedParams getRecognizedParams(WebScriptRequest req)
{ {
Paging paging = findPaging(req); Paging paging = findPaging(req);
List<SortColumn> sorting = getSort(req.getParameter(ResourceWebScriptHelper.PARAM_ORDERBY)); List<SortColumn> sorting = getSort(req.getParameter(ResourceWebScriptHelper.PARAM_ORDERBY));
Map<String, BeanPropertiesFilter> relationFilter = getRelationFilter(req.getParameter(ResourceWebScriptHelper.PARAM_RELATIONS)); Map<String, BeanPropertiesFilter> relationFilter = getRelationFilter(req.getParameter(ResourceWebScriptHelper.PARAM_RELATIONS));
BeanPropertiesFilter filter = getFilter(req.getParameter(ResourceWebScriptHelper.PARAM_FILTER_PROPS));
Query whereQuery = getWhereClause(req.getParameter(ResourceWebScriptHelper.PARAM_WHERE)); Query whereQuery = getWhereClause(req.getParameter(ResourceWebScriptHelper.PARAM_WHERE));
Map<String, String[]> requestParams = getRequestParameters(req); Map<String, String[]> requestParams = getRequestParameters(req);
List<String> theSelect = getSelectClause(req.getParameter(ResourceWebScriptHelper.PARAM_SELECT)); List<String> 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); return new RecognizedParams(requestParams, paging, filter, relationFilter, theSelect, whereQuery, sorting);
} }

View File

@@ -42,6 +42,7 @@ import java.io.PrintWriter;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@@ -459,7 +460,8 @@ public class SerializeTests
//this is correct //this is correct
} }
} }
// note: exposed as "properties" query param
@Test @Test
public void testFilter() throws IOException, JSONException public void testFilter() throws IOException, JSONException
{ {
@@ -473,7 +475,21 @@ public class SerializeTests
res = new ExecutionResult(new Sheep("bob"),theFilter); res = new ExecutionResult(new Sheep("bob"),theFilter);
out = writeResponse(res); out = writeResponse(res);
JSONObject jsonRsp = new JSONObject(new JSONTokener(out)); JSONObject jsonRsp = new JSONObject(new JSONTokener(out));
assertEquals(1, jsonRsp.length());
JSONObject entry = jsonRsp.getJSONObject("entry"); 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<String> 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")); assertEquals("The name should be 'Dolly'", "Dolly", entry.getString("name"));
assertTrue("The age should be 3", entry.getInt("age") == 3); 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()); res = helper.postProcessResponse(v3,"goat",ParamsExtender.valueOf(relFiler, "notUsed"),new SlimGoat());
out = writeResponse(res); out = writeResponse(res);
assertTrue("Must return only the herd name.", StringUtils.contains(out, "{\"name\":\"bigun\"}")); assertTrue("Must return only the herd name.", StringUtils.contains(out, "{\"name\":\"bigun\"}"));
} }
@Test @Test