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

129778 gjames: SEARCH-113: Initial implementation of the Search public API


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@130169 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Gethin James
2016-09-06 14:15:02 +00:00
parent f60bdb5914
commit b18f433fca
14 changed files with 971 additions and 11 deletions

View File

@@ -0,0 +1,130 @@
/*-
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* 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 <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.api.search;
import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.model.Node;
import org.alfresco.rest.api.search.impl.ResultMapper;
import org.alfresco.rest.api.search.impl.SearchMapper;
import org.alfresco.rest.api.search.model.SearchQuery;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Params;
import org.alfresco.rest.framework.tools.ApiAssistant;
import org.alfresco.rest.framework.tools.RecognizedParamsExtractor;
import org.alfresco.rest.framework.tools.RequestReader;
import org.alfresco.rest.framework.tools.ResponseWriter;
import org.alfresco.rest.framework.webscripts.ResourceWebScriptHelper;
import org.alfresco.rest.framework.webscripts.WithResponse;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SearchParameters;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.util.ParameterCheck;
import org.alfresco.util.PropertyCheck;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.extensions.webscripts.AbstractWebScript;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
import java.io.IOException;
/**
* An implementation of the {{baseUrl}}/{{networkId}}/public/search/versions/1/search endpoint
*
* @author Gethin James
*/
public class SearchApiWebscript extends AbstractWebScript implements RecognizedParamsExtractor, RequestReader, ResponseWriter,
InitializingBean
{
private ServiceRegistry serviceRegistry;
private SearchService searchService;
private Nodes nodes;
private SearchMapper searchMapper;
private ResultMapper resultMapper;
protected ApiAssistant assistant;
protected ResourceWebScriptHelper helper;
@Override
public void afterPropertiesSet()
{
PropertyCheck.mandatory(this, "serviceRegistry", serviceRegistry);
this.searchService = serviceRegistry.getSearchService();
ParameterCheck.mandatory("assistant", this.assistant);
ParameterCheck.mandatory("nodes", this.nodes);
searchMapper = new SearchMapper();
resultMapper = new ResultMapper(nodes);
}
@Override
public void execute(WebScriptRequest webScriptRequest, WebScriptResponse webScriptResponse) throws IOException
{
try {
//Turn JSON into a Java object respresentation
SearchQuery searchQuery = extractJsonContent(webScriptRequest, assistant.getJsonHelper(), SearchQuery.class);
//Parse the parameter
Params.RecognizedParams recognizedParams = new Params.RecognizedParams(null, null, null, null, null, null, null, null, false);
Params params = Params.valueOf(null, recognizedParams, searchQuery, webScriptRequest);
//Turn the params into the Java SearchParameters object
SearchParameters searchParams = searchMapper.toSearchParameters(params);
//Call searchService
ResultSet results = searchService.query(searchParams);
//Turn solr results into JSON
CollectionWithPagingInfo<Node> resultJson = resultMapper.toCollectionWithPagingInfo(params, results);
//Post-process the request and pass in params, eg. params.getFilter()
Object toRender = helper.processAdditionsToTheResponse(null, null, null, params, resultJson);
//Write response
setResponse(webScriptResponse, DEFAULT_SUCCESS);
renderJsonResponse(webScriptResponse, toRender, assistant.getJsonHelper());
} catch (Exception exception) {
renderException(exception,webScriptResponse,assistant);
}
}
public void setNodes(Nodes nodes) {
this.nodes = nodes;
}
public void setAssistant(ApiAssistant assistant) {
this.assistant = assistant;
}
public void setServiceRegistry(ServiceRegistry serviceRegistry) {
this.serviceRegistry = serviceRegistry;
}
public void setHelper(ResourceWebScriptHelper helper)
{
this.helper = helper;
}
}

View File

@@ -0,0 +1,91 @@
/*-
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* 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 <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.api.search.impl;
import org.alfresco.model.ContentModel;
import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.model.Node;
import org.alfresco.rest.api.model.UserInfo;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Paging;
import org.alfresco.rest.framework.resource.parameters.Params;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.ParameterCheck;
import org.apache.commons.lang.NotImplementedException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static org.alfresco.rest.api.Nodes.PARAM_INCLUDE_ASPECTNAMES;
import static org.alfresco.rest.api.Nodes.PARAM_INCLUDE_PROPERTIES;
/**
* Maps from a Solr ResultSet to a json public api representation.
*
* @author Gethin James
*/
public class ResultMapper
{
private Nodes nodes;
public ResultMapper(Nodes nodes)
{
this.nodes = nodes;
ParameterCheck.mandatory("nodes", this.nodes);
}
/**
*
* @param params
* @param results
* @return
*/
public CollectionWithPagingInfo<Node> toCollectionWithPagingInfo(Params params, ResultSet results)
{
Long totalItems = results.getNumberFound();
List<Node> noderesults = new ArrayList();
results.forEach(row ->
{
Node aNode = nodes.getFolderOrDocument(row.getNodeRef(), null, null, params.getInclude(), null);
//float f = row.getScore();
//Long dbId = (Long) row.getValue(ContentModel.PROP_NODE_DBID);
noderesults.add(aNode);
}
);
Integer total = Integer.valueOf(totalItems.intValue());
return CollectionWithPagingInfo.asPaged(null, noderesults, noderesults.size() < total, total);
}
}

View File

@@ -0,0 +1,102 @@
/*-
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* 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 <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.api.search.impl;
import org.alfresco.rest.api.search.model.Query;
import org.alfresco.rest.api.search.model.SearchQuery;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.rest.framework.resource.content.BasicContentInfo;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Params;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SearchParameters;
import org.alfresco.rest.api.model.Node;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.util.ParameterCheck;
import org.apache.commons.lang.NotImplementedException;
import static org.alfresco.service.cmr.search.SearchService.*;
/**
* Maps from a json request and a solr SearchParameters object.
*
* @author Gethin James
*/
public class SearchMapper
{
/**
* Turn the params into the Java SearchParameters object
* @param params
* @return
*/
public SearchParameters toSearchParameters(Params params)
{
BasicContentInfo info = params.getContentInfo();
SearchQuery searchQuery = (SearchQuery) params.getPassedIn();
ParameterCheck.mandatory("query", searchQuery.getQuery());
SearchParameters sp = new SearchParameters();
fromQuery(sp, searchQuery.getQuery());
//Hardcode workspace store
sp.addStore(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE);
return sp;
}
/**
*
* @param sp
* @param q
*/
public void fromQuery(SearchParameters sp, Query q)
{
ParameterCheck.mandatoryString("query", q.getQuery());
ParameterCheck.mandatoryString("language", q.getLanguage());
switch (q.getLanguage().toLowerCase())
{
case "afts":
sp.setLanguage(LANGUAGE_FTS_ALFRESCO);
break;
case "lucene":
sp.setLanguage(LANGUAGE_LUCENE);
break;
case "cmis":
sp.setLanguage(LANGUAGE_CMIS_ALFRESCO);
break;
default:
throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_MESSAGE_ID,
new Object[] { ": language allowed values: afts,lucene,cmis" });
}
sp.setQuery(q.getQuery());
sp.setSearchTerm(q.getUserQuery());
}
}

View File

@@ -0,0 +1,72 @@
/*-
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* 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 <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.api.search.model;
/**
* POJO class representing the query element of the JSON body
**/
public class Query
{
String language;
String query;
String userQuery;
public Query()
{
}
public String getLanguage()
{
return language;
}
public void setLanguage(String language)
{
this.language = language;
}
public String getQuery()
{
return query;
}
public void setQuery(String query)
{
this.query = query;
}
public String getUserQuery()
{
return userQuery;
}
public void setUserQuery(String userQuery)
{
this.userQuery = userQuery;
}
}

View File

@@ -0,0 +1,51 @@
/*-
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* 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 <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.api.search.model;
/**
* POJO class representing the JSON body for a search request
*
* @author Gethin James
*/
public class SearchQuery
{
Query query;
public SearchQuery()
{
}
public Query getQuery()
{
return query;
}
public void setQuery(Query query)
{
this.query = query;
}
}