Repository: atlas Updated Branches: refs/heads/master cd49be50a -> b714fe006
ATLAS-2025: Validation of classification/type in basic search query Signed-off-by: Madhan Neethiraj <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/atlas/repo Commit: http://git-wip-us.apache.org/repos/asf/atlas/commit/b714fe00 Tree: http://git-wip-us.apache.org/repos/asf/atlas/tree/b714fe00 Diff: http://git-wip-us.apache.org/repos/asf/atlas/diff/b714fe00 Branch: refs/heads/master Commit: b714fe00656f529cec8e4b467322a41729f0ddc5 Parents: cd49be5 Author: apoorvnaik <[email protected]> Authored: Sun Aug 13 00:53:38 2017 -0700 Committer: Madhan Neethiraj <[email protected]> Committed: Sun Aug 13 00:53:38 2017 -0700 ---------------------------------------------------------------------- .../java/org/apache/atlas/AtlasErrorCode.java | 7 ++-- .../atlas/discovery/EntityDiscoveryService.java | 21 ++++++----- .../apache/atlas/discovery/SearchContext.java | 38 ++++++++++++++------ .../apache/atlas/web/rest/DiscoveryREST.java | 6 +++- 4 files changed, 49 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/atlas/blob/b714fe00/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java ---------------------------------------------------------------------- diff --git a/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java b/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java index 36fcc03..6fa1f60 100644 --- a/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java +++ b/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java @@ -36,7 +36,7 @@ public enum AtlasErrorCode { CONSTRAINT_MISSING_PARAMS(400, "ATLAS-400-00-007", "{0}.{1} : invalid constraint. missing parameter {2} in {3}. params={4}"), CONSTRAINT_NOT_EXIST(400, "ATLAS-400-00-008", "{0}.{1} : invalid constraint. {2} {3}.{4} does not exist"), CONSTRAINT_NOT_MATCHED(400, "ATLAS-400-00-009", "{0}.{1} : invalid constraint. Data type of {2} {3}.{4} should be {5}, but found {6}"), - UNKNOWN_TYPENAME(400, "ATLAS-400-00-00A", "{0}: Unknown typename"), + UNKNOWN_TYPENAME(400, "ATLAS-400-00-00A", "{0}: Unknown/invalid typename"), CONSTRAINT_NOT_SUPPORTED_ON_MAP_TYPE(400, "ATLAS-400-00-00B", "{0}.{1} : constraints not supported on map type {2}"), CANNOT_ADD_MANDATORY_ATTRIBUTE(400, "ATLAS-400-00-00C", "{0}.{1} : can not add mandatory attribute"), ATTRIBUTE_DELETION_NOT_SUPPORTED(400, "ATLAS-400-00-00D", "{0}.{1} : attribute delete not supported"), @@ -90,6 +90,8 @@ public enum AtlasErrorCode { RELATIONSHIPDEF_NOT_DEFINED(400, "ATLAS-400-00-043", "No relationshipDef defined between {0} and {1} on attribute: {2}"), RELATIONSHIPDEF_INVALID(400, "ATLAS-400-00-044", "Invalid relationshipDef: {0}"), RELATIONSHIP_INVALID_ENDTYPE(400, "ATLAS-400-00-045", "Invalid entity-type for relationship attribute â{0}â: entity specified (guid={1}) is of type â{2}â, but expected type is â{3}â"), + UNKNOWN_CLASSIFICATION(400, "ATLAS-400-00-046", "{0}: Unknown/invalid classification"), + INVALID_SEARCH_PARAMS(400, "ATLAS-400-00-047", "No search parameter was found. One of the following MUST be specified in the request; typeName, classification or queryText"), // All Not found enums go here TYPE_NAME_NOT_FOUND(404, "ATLAS-404-00-001", "Given typename {0} was invalid"), TYPE_GUID_NOT_FOUND(404, "ATLAS-404-00-002", "Given type guid {0} was invalid"), @@ -104,6 +106,7 @@ public enum AtlasErrorCode { RELATIONSHIP_GUID_NOT_FOUND(404, "ATLAS-404-00-00C", "Given relationship guid {0} is invalid/not found"), RELATIONSHIP_CRUD_INVALID_PARAMS(404, "ATLAS-404-00-00D", "Invalid relationship creation/updation parameters passed : {0}"), RELATIONSHIPDEF_END_TYPE_NAME_NOT_FOUND(404, "ATLAS-404-00-00E", "RelationshipDef {0} endDef typename {0} cannot be found"), + // All data conflict errors go here TYPE_ALREADY_EXISTS(409, "ATLAS-409-00-001", "Given type {0} already exists"), TYPE_HAS_REFERENCES(409, "ATLAS-409-00-002", "Given type {0} has references"), @@ -165,4 +168,4 @@ public enum AtlasErrorCode { public String getErrorCode() { return errorCode; } -} \ No newline at end of file +} http://git-wip-us.apache.org/repos/asf/atlas/blob/b714fe00/repository/src/main/java/org/apache/atlas/discovery/EntityDiscoveryService.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/discovery/EntityDiscoveryService.java b/repository/src/main/java/org/apache/atlas/discovery/EntityDiscoveryService.java index 66dd748..a7aaefe 100644 --- a/repository/src/main/java/org/apache/atlas/discovery/EntityDiscoveryService.java +++ b/repository/src/main/java/org/apache/atlas/discovery/EntityDiscoveryService.java @@ -427,18 +427,21 @@ public class EntityDiscoveryService implements AtlasDiscoveryService { resultAttributes.addAll(context.getEntityAttributes()); } - for (String resultAttribute : resultAttributes) { - AtlasAttribute attribute = context.getEntityType().getAttribute(resultAttribute); + AtlasEntityType entityType = context.getEntityType(); + if (entityType != null) { + for (String resultAttribute : resultAttributes) { + AtlasAttribute attribute = entityType.getAttribute(resultAttribute); - if (attribute != null) { - AtlasType attributeType = attribute.getAttributeType(); + if (attribute != null) { + AtlasType attributeType = attribute.getAttributeType(); - if (attributeType instanceof AtlasArrayType) { - attributeType = ((AtlasArrayType) attributeType).getElementType(); - } + if (attributeType instanceof AtlasArrayType) { + attributeType = ((AtlasArrayType) attributeType).getElementType(); + } - if (attributeType instanceof AtlasEntityType || attributeType instanceof AtlasObjectIdType) { - entityAttributes.add(resultAttribute); + if (attributeType instanceof AtlasEntityType || attributeType instanceof AtlasObjectIdType) { + entityAttributes.add(resultAttribute); + } } } } http://git-wip-us.apache.org/repos/asf/atlas/blob/b714fe00/repository/src/main/java/org/apache/atlas/discovery/SearchContext.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/discovery/SearchContext.java b/repository/src/main/java/org/apache/atlas/discovery/SearchContext.java index 929f8d0..d42078a 100644 --- a/repository/src/main/java/org/apache/atlas/discovery/SearchContext.java +++ b/repository/src/main/java/org/apache/atlas/discovery/SearchContext.java @@ -18,6 +18,8 @@ package org.apache.atlas.discovery; +import org.apache.atlas.AtlasErrorCode; +import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.model.discovery.SearchParameters; import org.apache.atlas.model.discovery.SearchParameters.FilterCriteria; import org.apache.atlas.repository.graphdb.AtlasGraph; @@ -30,7 +32,11 @@ import org.apache.commons.lang3.StringUtils; import java.util.HashSet; import java.util.Set; - +/* + * Search context captures elements required for performing a basic search + * For every search request the search context will determine the execution sequence of the search processor(s) and the + * possible chaining of processor(s) + */ public class SearchContext { private final SearchParameters searchParameters; private final AtlasTypeRegistry typeRegistry; @@ -42,16 +48,26 @@ public class SearchContext { private SearchProcessor searchProcessor; private boolean terminateSearch = false; - public SearchContext(SearchParameters searchParameters, AtlasTypeRegistry typeRegistry, AtlasGraph graph, Set<String> indexedKeys) { + public SearchContext(SearchParameters searchParameters, AtlasTypeRegistry typeRegistry, AtlasGraph graph, Set<String> indexedKeys) throws AtlasBaseException { this.searchParameters = searchParameters; this.typeRegistry = typeRegistry; this.graph = graph; this.indexedKeys = indexedKeys; - this.entityAttributes = new HashSet<>(); - this.entityType = typeRegistry.getEntityTypeByName(searchParameters.getTypeName()); - this.classificationType = typeRegistry.getClassificationTypeByName(searchParameters.getClassification()); + entityAttributes = new HashSet<>(); + entityType = typeRegistry.getEntityTypeByName(searchParameters.getTypeName()); + classificationType = typeRegistry.getClassificationTypeByName(searchParameters.getClassification()); + + // Validate if the type name exists + if (StringUtils.isNotEmpty(searchParameters.getTypeName()) && entityType == null) { + throw new AtlasBaseException(AtlasErrorCode.UNKNOWN_TYPENAME, searchParameters.getTypeName()); + } + + // Validate if the classification exists + if (StringUtils.isNotEmpty(searchParameters.getClassification()) && classificationType == null) { + throw new AtlasBaseException(AtlasErrorCode.UNKNOWN_CLASSIFICATION, searchParameters.getClassification()); + } - if (needFullTextrocessor()) { + if (needFullTextProcessor()) { addProcessor(new FullTextSearchProcessor(this)); } @@ -80,7 +96,7 @@ public class SearchContext { public SearchProcessor getSearchProcessor() { return searchProcessor; } - public boolean terminateSearch() { return this.terminateSearch; } + public boolean terminateSearch() { return terminateSearch; } public void terminateSearch(boolean terminateSearch) { this.terminateSearch = terminateSearch; } @@ -103,7 +119,7 @@ public class SearchContext { return toString(new StringBuilder()).toString(); } - boolean needFullTextrocessor() { + boolean needFullTextProcessor() { return StringUtils.isNotEmpty(searchParameters.getQuery()); } @@ -121,10 +137,10 @@ public class SearchContext { } private void addProcessor(SearchProcessor processor) { - if (this.searchProcessor == null) { - this.searchProcessor = processor; + if (searchProcessor == null) { + searchProcessor = processor; } else { - this.searchProcessor.addProcessor(processor); + searchProcessor.addProcessor(processor); } } } http://git-wip-us.apache.org/repos/asf/atlas/blob/b714fe00/webapp/src/main/java/org/apache/atlas/web/rest/DiscoveryREST.java ---------------------------------------------------------------------- diff --git a/webapp/src/main/java/org/apache/atlas/web/rest/DiscoveryREST.java b/webapp/src/main/java/org/apache/atlas/web/rest/DiscoveryREST.java index 71860a1..303c4d8 100644 --- a/webapp/src/main/java/org/apache/atlas/web/rest/DiscoveryREST.java +++ b/webapp/src/main/java/org/apache/atlas/web/rest/DiscoveryREST.java @@ -213,7 +213,7 @@ public class DiscoveryREST { if (StringUtils.isEmpty(attrName) && StringUtils.isEmpty(attrValuePrefix)) { throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, - String.format("attrName : {0}, attrValue: {1} for attribute search.", attrName, attrValuePrefix)); + String.format("attrName : %s, attrValue: %s for attribute search.", attrName, attrValuePrefix)); } return atlasDiscoveryService.searchUsingBasicQuery(null, typeName, null, attrName, attrValuePrefix, true, limit, offset); @@ -256,6 +256,10 @@ public class DiscoveryREST { throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "TagFilters specified without tag name"); } + if (StringUtils.isEmpty(parameters.getTypeName()) && StringUtils.isEmpty(parameters.getClassification()) && StringUtils.isEmpty(parameters.getQuery())) { + throw new AtlasBaseException(AtlasErrorCode.INVALID_SEARCH_PARAMS); + } + return atlasDiscoveryService.searchWithParameters(parameters); } finally { AtlasPerfTracer.log(perf);
