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

129777 gjames: Reformatted to Alfresco standards


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@130168 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Gethin James
2016-09-06 14:14:52 +00:00
parent b2a6ad44ef
commit f60bdb5914
3 changed files with 104 additions and 99 deletions

View File

@@ -23,6 +23,7 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L% * #L%
*/ */
package org.alfresco.rest.framework.tools; package org.alfresco.rest.framework.tools;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException; import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
@@ -74,16 +75,18 @@ public interface RecognizedParamsExtractor
public static final String PARAM_SELECT = "select"; public static final String PARAM_SELECT = "select";
public static final String PARAM_INCLUDE = "include"; public static final String PARAM_INCLUDE = "include";
public static final String PARAM_INCLUDE_SOURCE_ENTITY = "includeSource"; public static final String PARAM_INCLUDE_SOURCE_ENTITY = "includeSource";
public static final List<String> KNOWN_PARAMS = Arrays.asList( public static final List<String> KNOWN_PARAMS = Arrays
PARAM_RELATIONS, PARAM_FILTER_PROPERTIES, PARAM_FILTER_FIELDS,PARAM_PAGING_SKIP,PARAM_PAGING_MAX, .asList(PARAM_RELATIONS, PARAM_FILTER_PROPERTIES, PARAM_FILTER_FIELDS, PARAM_PAGING_SKIP, PARAM_PAGING_MAX, PARAM_ORDERBY,
PARAM_ORDERBY, PARAM_WHERE, PARAM_SELECT, PARAM_INCLUDE_SOURCE_ENTITY); PARAM_WHERE, PARAM_SELECT, PARAM_INCLUDE_SOURCE_ENTITY);
default Log rpeLogger() { default Log rpeLogger()
{
return LogFactory.getLog(this.getClass()); return LogFactory.getLog(this.getClass());
} }
/** /**
* Finds the formal set of params that any rest service could potentially have passed in as request params * Finds the formal set of params that any rest service could potentially have passed in as request params
*
* @param req WebScriptRequest * @param req WebScriptRequest
* @return RecognizedParams a POJO containing the params for use with the Params objects * @return RecognizedParams a POJO containing the params for use with the Params objects
*/ */
@@ -112,38 +115,27 @@ public interface RecognizedParamsExtractor
BeanPropertiesFilter filter = getFilter((fields != null ? fields : properties), includedFields); BeanPropertiesFilter filter = getFilter((fields != null ? fields : properties), includedFields);
return new Params.RecognizedParams(requestParams, paging, filter, relationFilter, includedFields, selectFields, whereQuery, sorting, includeSource); return new Params.RecognizedParams(requestParams, paging, filter, relationFilter, includedFields, selectFields, whereQuery, sorting,
includeSource);
} }
/** /**
* Takes the web request and looks for a "fields" parameter (otherwise deprecated "properties" parameter). * Takes the web request and looks for a "fields" parameter (otherwise deprecated "properties" parameter).
*
* Parses the parameter and produces a list of bean properties to use as a filter A * Parses the parameter and produces a list of bean properties to use as a filter A
* SimpleBeanPropertyFilter it returned that uses the bean properties. If no * SimpleBeanPropertyFilter it returned that uses the bean properties. If no
* filter param is set then a default BeanFilter is returned that will never * filter param is set then a default BeanFilter is returned that will never
* filter fields (ie. Returns all bean properties). * filter fields (ie. Returns all bean properties).
*
* If selectList is provided then it will take precedence (ie. be included) over the fields/properties filter * If selectList is provided then it will take precedence (ie. be included) over the fields/properties filter
* for top-level entries (bean properties). * for top-level entries (bean properties).
*
* For example, this will return entries from both select & properties, eg. * For example, this will return entries from both select & properties, eg.
*
* select=abc,def&properties=id,name,ghi * select=abc,def&properties=id,name,ghi
*
* Note: it should be noted that API-generic "fields" clause does not currently work for sub-entries. * Note: it should be noted that API-generic "fields" 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 * Hence, even if the API-specific "select" clause allows selection of a sub-entries this cannot be used
* with "fields" filtering. For example, an API-specific method may implement and return "abc/blah", eg. * with "fields" filtering. For example, an API-specific method may implement and return "abc/blah", eg.
*
* select=abc/blah * select=abc/blah
*
* However the following will not return "abc/blah" if used with fields filtering, eg. * However the following will not return "abc/blah" if used with fields filtering, eg.
*
* select=abc/blah&fields=id,name,ghi * select=abc/blah&fields=id,name,ghi
*
* If fields filtering is desired then it would require "abc" to be selected and returned as a whole, eg. * If fields filtering is desired then it would require "abc" to be selected and returned as a whole, eg.
*
* select=abc&fields=id,name,ghi * select=abc&fields=id,name,ghi
* *
* @param filterParams * @param filterParams
@@ -178,9 +170,9 @@ public interface RecognizedParamsExtractor
return BeanPropertiesFilter.ALLOW_ALL; return BeanPropertiesFilter.ALLOW_ALL;
} }
/** /**
* Takes the "select" parameter and turns it into a List<String> property names * Takes the "select" parameter and turns it into a List<String> property names
*
* @param selectParam String * @param selectParam String
* @return bean property names potentially using JSON Pointer syntax * @return bean property names potentially using JSON Pointer syntax
*/ */
@@ -193,6 +185,7 @@ public interface RecognizedParamsExtractor
/** /**
* Takes the "include" parameter and turns it into a List<String> property names * Takes the "include" parameter and turns it into a List<String> property names
*
* @param includeParam String * @param includeParam String
* @return bean property names potentially using JSON Pointer syntax * @return bean property names potentially using JSON Pointer syntax
*/ */
@@ -204,15 +197,18 @@ public interface RecognizedParamsExtractor
/** /**
* Gets the clause specificed in paramName * Gets the clause specificed in paramName
*
* @param param * @param param
* @param paramName * @param paramName
* @return bean property names potentially using JSON Pointer syntax * @return bean property names potentially using JSON Pointer syntax
*/ */
default List<String> getClause(String param, String paramName) default List<String> getClause(String param, String paramName)
{ {
if (param == null) return Collections.emptyList(); if (param == null)
return Collections.emptyList();
try { try
{
CommonTree selectedPropsTree = WhereCompiler.compileSelectClause(param); CommonTree selectedPropsTree = WhereCompiler.compileSelectClause(param);
if (selectedPropsTree instanceof CommonErrorNode) if (selectedPropsTree instanceof CommonErrorNode)
{ {
@@ -227,7 +223,8 @@ public interface RecognizedParamsExtractor
if (children != null && !children.isEmpty()) if (children != null && !children.isEmpty())
{ {
List<String> properties = new ArrayList<String>(children.size()); List<String> properties = new ArrayList<String>(children.size());
for (Tree child : children) { for (Tree child : children)
{
properties.add(child.getText()); properties.add(child.getText());
} }
return properties; return properties;
@@ -252,14 +249,17 @@ public interface RecognizedParamsExtractor
/** /**
* Takes the "where" parameter and turns it into a Java Object that can be used for querying * Takes the "where" parameter and turns it into a Java Object that can be used for querying
*
* @param whereParam String * @param whereParam String
* @return Query a parsed version of the where clause, represented in Java * @return Query a parsed version of the where clause, represented in Java
*/ */
default Query getWhereClause(String whereParam) throws InvalidQueryException default Query getWhereClause(String whereParam) throws InvalidQueryException
{ {
if (whereParam == null) return QueryImpl.EMPTY; if (whereParam == null)
return QueryImpl.EMPTY;
try { try
{
CommonTree whereTree = WhereCompiler.compileWhereClause(whereParam); CommonTree whereTree = WhereCompiler.compileWhereClause(whereParam);
if (whereTree instanceof CommonErrorNode) if (whereTree instanceof CommonErrorNode)
{ {
@@ -267,9 +267,13 @@ public interface RecognizedParamsExtractor
throw new InvalidQueryException(whereTree); throw new InvalidQueryException(whereTree);
} }
return new QueryImpl(whereTree); return new QueryImpl(whereTree);
} catch (RewriteCardinalityException re) { //Catch any error so it doesn't get thrown up the stack }
catch (RewriteCardinalityException re)
{ //Catch any error so it doesn't get thrown up the stack
rpeLogger().info("Unhandled Error parsing the WHERE clause: " + re); rpeLogger().info("Unhandled Error parsing the WHERE clause: " + re);
} catch (RecognitionException e) { }
catch (RecognitionException e)
{
whereParam += ", " + WhereCompiler.resolveMessage(e); whereParam += ", " + WhereCompiler.resolveMessage(e);
rpeLogger().info("Error parsing the WHERE clause: " + whereParam); rpeLogger().info("Error parsing the WHERE clause: " + whereParam);
} }
@@ -282,6 +286,7 @@ public interface RecognizedParamsExtractor
* The format is a comma seperated list of "columnName sortDirection", * The format is a comma seperated list of "columnName sortDirection",
* e.g. "name DESC, age ASC". It is not case sensitive and the sort direction is optional * e.g. "name DESC, age ASC". It is not case sensitive and the sort direction is optional
* It default to sort ASCENDING. * It default to sort ASCENDING.
*
* @param sortParams - String passed in on the request * @param sortParams - String passed in on the request
* @return - the sort columns or an empty list if the params were invalid. * @return - the sort columns or an empty list if the params were invalid.
*/ */
@@ -308,7 +313,8 @@ public interface RecognizedParamsExtractor
} }
else else
{ {
rpeLogger().debug("Invalid sort order definition ("+sortDef+"). Valid values are "+SortColumn.ASCENDING+" or "+SortColumn.DESCENDING+"."); rpeLogger().debug("Invalid sort order definition (" + sortDef + "). Valid values are " + SortColumn.ASCENDING + " or "
+ SortColumn.DESCENDING + ".");
} }
} }
sortedColumns.add(new SortColumn(columnName, SortColumn.ASCENDING.equals(sortOrder))); sortedColumns.add(new SortColumn(columnName, SortColumn.ASCENDING.equals(sortOrder)));
@@ -356,10 +362,8 @@ public interface RecognizedParamsExtractor
return Paging.valueOf(skipped, max); return Paging.valueOf(skipped, max);
} }
/** /**
* Takes the web request and looks for a "fields" parameter (otherwise deprecated "properties" parameter). * Takes the web request and looks for a "fields" parameter (otherwise deprecated "properties" parameter).
*
* Parses the parameter and produces a list of bean properties to use as a filter A * Parses the parameter and produces a list of bean properties to use as a filter A
* SimpleBeanPropertyFilter it returned that uses the bean properties. If no * SimpleBeanPropertyFilter it returned that uses the bean properties. If no
* filter param is set then a default BeanFilter is returned that will never * filter param is set then a default BeanFilter is returned that will never
@@ -414,7 +418,6 @@ public interface RecognizedParamsExtractor
return Collections.emptyMap(); return Collections.emptyMap();
} }
/** /**
* Finds all request parameters that aren't already know about (eg. not paging or filter params) * Finds all request parameters that aren't already know about (eg. not paging or filter params)
* and returns them for use. * and returns them for use.

View File

@@ -23,6 +23,7 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L% * #L%
*/ */
package org.alfresco.rest.framework.tools; package org.alfresco.rest.framework.tools;
import org.alfresco.rest.framework.core.exceptions.ApiException; import org.alfresco.rest.framework.core.exceptions.ApiException;
@@ -93,10 +94,9 @@ public interface RequestReader
} }
} }
default Log rrLogger() { default Log rrLogger()
{
return LogFactory.getLog(this.getClass()); return LogFactory.getLog(this.getClass());
} }
} }

View File

@@ -23,6 +23,7 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L% * #L%
*/ */
package org.alfresco.rest.framework.tools; package org.alfresco.rest.framework.tools;
import org.alfresco.rest.framework.Api; import org.alfresco.rest.framework.Api;
@@ -90,12 +91,14 @@ public interface ResponseWriter
final WithResponse DEFAULT_SUCCESS = new WithResponse(Status.STATUS_OK, DEFAULT_JSON_CONTENT, CACHE_NEVER); final WithResponse DEFAULT_SUCCESS = new WithResponse(Status.STATUS_OK, DEFAULT_JSON_CONTENT, CACHE_NEVER);
default Log resWriterLogger() { default Log resWriterLogger()
{
return LogFactory.getLog(this.getClass()); return LogFactory.getLog(this.getClass());
} }
/** /**
* Sets the response headers with any information we know about the content * Sets the response headers with any information we know about the content
*
* @param res WebScriptResponse * @param res WebScriptResponse
* @param contentInfo Content Information * @param contentInfo Content Information
*/ */
@@ -134,7 +137,6 @@ public interface ResponseWriter
/** /**
* The response status must be set before the response is written by Jackson (which will by default close and commit the response). * The response status must be set before the response is written by Jackson (which will by default close and commit the response).
* In a r/w txn, web script buffered responses ensure that it doesn't really matter but for r/o txns this is important. * In a r/w txn, web script buffered responses ensure that it doesn't really matter but for r/o txns this is important.
*
* If you set content information via the contentInfo object and ALSO the headers then "headers" will win because they are * If you set content information via the contentInfo object and ALSO the headers then "headers" will win because they are
* set last. * set last.
* *
@@ -153,7 +155,8 @@ public interface ResponseWriter
{ {
for (Map.Entry<String, List<String>> header : headers.entrySet()) for (Map.Entry<String, List<String>> header : headers.entrySet())
{ {
for (int i=0; i < header.getValue().size(); i++) { for (int i = 0; i < header.getValue().size(); i++)
{
if (i == 0) if (i == 0)
{ {
//If its the first one then set the header overwriting. //If its the first one then set the header overwriting.
@@ -171,6 +174,7 @@ public interface ResponseWriter
/** /**
* Sets the response using the WithResponse object * Sets the response using the WithResponse object
*
* @param res * @param res
* @param withResponse * @param withResponse
*/ */
@@ -181,11 +185,14 @@ public interface ResponseWriter
/** /**
* Renders a JSON error response * Renders a JSON error response
*
* @param errorResponse The error * @param errorResponse The error
* @param res web script response * @param res web script response
* @throws IOException * @throws IOException
*/ */
default void renderErrorResponse(final ErrorResponse errorResponse, final WebScriptResponse res, final JacksonHelper jsonHelper) throws IOException { default void renderErrorResponse(final ErrorResponse errorResponse, final WebScriptResponse res, final JacksonHelper jsonHelper)
throws IOException
{
String logId = ""; String logId = "";
@@ -197,13 +204,8 @@ public interface ResponseWriter
String stackMessage = I18NUtil.getMessage(DefaultExceptionResolver.STACK_MESSAGE_ID); String stackMessage = I18NUtil.getMessage(DefaultExceptionResolver.STACK_MESSAGE_ID);
final ErrorResponse errorToWrite = new ErrorResponse(errorResponse.getErrorKey(), final ErrorResponse errorToWrite = new ErrorResponse(errorResponse.getErrorKey(), errorResponse.getStatusCode(),
errorResponse.getStatusCode(), errorResponse.getBriefSummary(), stackMessage, logId, errorResponse.getAdditionalState(), DefaultExceptionResolver.ERROR_URL);
errorResponse.getBriefSummary(),
stackMessage,
logId,
errorResponse.getAdditionalState(),
DefaultExceptionResolver.ERROR_URL);
setContentInfoOnResponse(res, DEFAULT_JSON_CONTENT); setContentInfoOnResponse(res, DEFAULT_JSON_CONTENT);
@@ -225,14 +227,15 @@ public interface ResponseWriter
}); });
} }
/** /**
* Renders an exception to the output stream as Json. * Renders an exception to the output stream as Json.
*
* @param exception * @param exception
* @param response * @param response
* @throws IOException * @throws IOException
*/ */
default void renderException(final Exception exception, final WebScriptResponse response, final ApiAssistant assistant) throws IOException { default void renderException(final Exception exception, final WebScriptResponse response, final ApiAssistant assistant) throws IOException
{
renderErrorResponse(assistant.resolveException(exception), response, assistant.getJsonHelper()); renderErrorResponse(assistant.resolveException(exception), response, assistant.getJsonHelper());
} }
@@ -243,8 +246,7 @@ public interface ResponseWriter
* @param toSerialize result of an execution * @param toSerialize result of an execution
* @throws IOException * @throws IOException
*/ */
default void renderJsonResponse(final WebScriptResponse res, final Object toSerialize, final JacksonHelper jsonHelper) default void renderJsonResponse(final WebScriptResponse res, final Object toSerialize, final JacksonHelper jsonHelper) throws IOException
throws IOException
{ {
jsonHelper.withWriter(res.getOutputStream(), new JacksonHelper.Writer() jsonHelper.withWriter(res.getOutputStream(), new JacksonHelper.Writer()
{ {