diff --git a/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptHelper.java b/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptHelper.java index 6675a1c2a6..0febbbdfb8 100644 --- a/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptHelper.java +++ b/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptHelper.java @@ -1,4 +1,21 @@ - +/* + * Copyright (C) 2005-2016 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ package org.alfresco.rest.framework.webscripts; import java.beans.PropertyDescriptor; @@ -91,6 +108,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) { @@ -100,6 +155,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; @@ -431,6 +497,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()); @@ -615,13 +682,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 15a5b6fc7d..ea7eac3a9f 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 @@ -1,3 +1,21 @@ +/* + * Copyright (C) 2005-2016 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ package org.alfresco.rest.framework.tests.core; import static org.junit.Assert.assertEquals; @@ -17,6 +35,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; @@ -434,7 +453,8 @@ public class SerializeTests //this is correct } } - + + // note: exposed as "properties" query param @Test public void testFilter() throws IOException, JSONException { @@ -448,7 +468,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); @@ -469,7 +503,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