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

130181 gjames: Merged searchapi (5.2.1) to 5.2.N (5.2.1)
      129821 gjames: SEARCH-150: Implementing include in the JSON body


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@130332 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Alan Davis
2016-09-06 22:04:51 +00:00
parent 7e95472e8c
commit e8e0209f8c
4 changed files with 81 additions and 5 deletions

View File

@@ -42,8 +42,18 @@ import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.util.ParameterCheck; import org.alfresco.util.ParameterCheck;
import org.apache.commons.lang.NotImplementedException; import org.apache.commons.lang.NotImplementedException;
import static org.alfresco.rest.api.Nodes.PARAM_INCLUDE_ALLOWABLEOPERATIONS;
import static org.alfresco.rest.api.Nodes.PARAM_INCLUDE_ISLINK;
import static org.alfresco.rest.api.Nodes.PARAM_INCLUDE_PATH;
import static org.alfresco.rest.api.Nodes.PARAM_INCLUDE_ASPECTNAMES;
import static org.alfresco.rest.api.Nodes.PARAM_INCLUDE_PROPERTIES;
import static org.alfresco.service.cmr.search.SearchService.*; import static org.alfresco.service.cmr.search.SearchService.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/** /**
* Maps from a json request and a solr SearchParameters object. * Maps from a json request and a solr SearchParameters object.
* *
@@ -51,6 +61,9 @@ import static org.alfresco.service.cmr.search.SearchService.*;
*/ */
public class SearchMapper public class SearchMapper
{ {
public static final List<String> PERMITTED_INCLUDES
= Arrays.asList(PARAM_INCLUDE_ALLOWABLEOPERATIONS,PARAM_INCLUDE_ASPECTNAMES,
PARAM_INCLUDE_ISLINK, PARAM_INCLUDE_PATH, PARAM_INCLUDE_PROPERTIES);
/** /**
* Turn the params into the Java SearchParameters object * Turn the params into the Java SearchParameters object
@@ -64,6 +77,7 @@ public class SearchMapper
SearchParameters sp = new SearchParameters(); SearchParameters sp = new SearchParameters();
fromQuery(sp, searchQuery.getQuery()); fromQuery(sp, searchQuery.getQuery());
fromPaging(sp, searchQuery.getPaging()); fromPaging(sp, searchQuery.getPaging());
validateInclude(searchQuery.getInclude());
//Hardcode workspace store //Hardcode workspace store
sp.addStore(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE); sp.addStore(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE);
@@ -109,4 +123,20 @@ public class SearchMapper
sp.setSkipCount(paging.getSkipCount()); sp.setSkipCount(paging.getSkipCount());
} }
} }
public void validateInclude(List<String> includes)
{
if (includes != null && !includes.isEmpty())
{
for (String inc:includes)
{
if (!PERMITTED_INCLUDES.contains(inc))
{
throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_MESSAGE_ID,
new Object[] { inc });
}
}
}
}
} }

View File

@@ -30,6 +30,8 @@ import org.alfresco.rest.framework.resource.parameters.Paging;
import org.codehaus.jackson.annotate.JsonCreator; import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonProperty; import org.codehaus.jackson.annotate.JsonProperty;
import java.util.List;
/** /**
* POJO class representing the JSON body for a search request * POJO class representing the JSON body for a search request
* *
@@ -39,15 +41,18 @@ public class SearchQuery
{ {
private final Query query; private final Query query;
private final Paging paging; private final Paging paging;
private final List<String> include;
public static final SearchQuery EMPTY = new SearchQuery(null, null); public static final SearchQuery EMPTY = new SearchQuery(null, null, null);
@JsonCreator @JsonCreator
public SearchQuery(@JsonProperty("query") Query query, public SearchQuery(@JsonProperty("query") Query query,
@JsonProperty("paging") Paging paging) @JsonProperty("paging") Paging paging,
@JsonProperty("include") List<String> include)
{ {
this.query = query; this.query = query;
this.paging = paging; this.paging = paging;
this.include = include;
} }
public Query getQuery() public Query getQuery()
@@ -59,4 +64,9 @@ public class SearchQuery
{ {
return paging; return paging;
} }
public List<String> getInclude()
{
return include;
}
} }

View File

@@ -37,11 +37,12 @@ import org.alfresco.rest.api.search.model.Query;
import org.alfresco.rest.api.search.model.SearchQuery; import org.alfresco.rest.api.search.model.SearchQuery;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException; import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.rest.framework.resource.parameters.Paging; import org.alfresco.rest.framework.resource.parameters.Paging;
import org.alfresco.rest.framework.resource.parameters.Params;
import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.SearchParameters; import org.alfresco.service.cmr.search.SearchParameters;
import org.junit.Test; import org.junit.Test;
import java.util.Arrays;
/** /**
* Tests the SearchMapper class * Tests the SearchMapper class
* *
@@ -136,10 +137,41 @@ public class SearchMapperTests
assertEquals(searchParameters.getSkipCount(),paging.getSkipCount()); assertEquals(searchParameters.getSkipCount(),paging.getSkipCount());
} }
@Test
public void validateInclude() throws Exception
{
try
{
searchMapper.validateInclude(Arrays.asList("sausage"));
fail();
}
catch (InvalidArgumentException iae)
{
//Sausage is illegal
assertNotNull(iae);
}
searchMapper.validateInclude(null);
try
{
searchMapper.validateInclude(Arrays.asList("AspectNames"));
fail();
}
catch (InvalidArgumentException iae)
{
//Case sensitive
assertNotNull(iae);
}
searchMapper.validateInclude(Arrays.asList("properties", "aspectNames"));
}
private SearchQuery minimalQuery() private SearchQuery minimalQuery()
{ {
Query query = new Query("cmis", "foo", ""); Query query = new Query("cmis", "foo", "");
SearchQuery sq = new SearchQuery(query,null); SearchQuery sq = new SearchQuery(query,null, null);
return sq; return sq;
} }
} }

View File

@@ -26,6 +26,7 @@
package org.alfresco.rest.api.search; package org.alfresco.rest.api.search;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import org.alfresco.rest.api.model.Comment; import org.alfresco.rest.api.model.Comment;
@@ -64,7 +65,7 @@ public class SearchQuerySerializerTests implements RequestReader
public void testDeserializeQuery() throws IOException public void testDeserializeQuery() throws IOException
{ {
String json = "{ \"query\": {\"query\": \"g*\",\"userQuery\": \"great\",\"language\": \"bob\"}, " String json = "{ \"query\": {\"query\": \"g*\",\"userQuery\": \"great\",\"language\": \"bob\"}, "
+ "\"paging\": {\"maxItems\": \"99\",\"skipCount\": \"4\"}}"; + "\"paging\": {\"maxItems\": \"99\",\"skipCount\": \"4\"}, \"include\": [\"bob\", \"hope\"]}";
SearchQuery searchQuery = extractFromJson(json); SearchQuery searchQuery = extractFromJson(json);
assertEquals(SearchQuery.class, searchQuery.getClass()); assertEquals(SearchQuery.class, searchQuery.getClass());
assertEquals("bob", searchQuery.getQuery().getLanguage()); assertEquals("bob", searchQuery.getQuery().getLanguage());
@@ -72,6 +73,9 @@ public class SearchQuerySerializerTests implements RequestReader
assertEquals("great", searchQuery.getQuery().getUserQuery()); assertEquals("great", searchQuery.getQuery().getUserQuery());
assertEquals(99, searchQuery.getPaging().getMaxItems()); assertEquals(99, searchQuery.getPaging().getMaxItems());
assertEquals(4, searchQuery.getPaging().getSkipCount()); assertEquals(4, searchQuery.getPaging().getSkipCount());
assertEquals(2, searchQuery.getInclude().size());
assertTrue(searchQuery.getInclude().contains("bob"));
assertTrue(searchQuery.getInclude().contains("hope"));
} }
private SearchQuery extractFromJson(String json) throws IOException private SearchQuery extractFromJson(String json) throws IOException