Repository: atlas
Updated Branches:
  refs/heads/0.8-incubating fe69c849e -> b993f9227


ATLAS-2025: Validation of classification/type in basic search query

Signed-off-by: Madhan Neethiraj <[email protected]>
(cherry picked from commit b714fe00656f529cec8e4b467322a41729f0ddc5)


Project: http://git-wip-us.apache.org/repos/asf/atlas/repo
Commit: http://git-wip-us.apache.org/repos/asf/atlas/commit/b993f922
Tree: http://git-wip-us.apache.org/repos/asf/atlas/tree/b993f922
Diff: http://git-wip-us.apache.org/repos/asf/atlas/diff/b993f922

Branch: refs/heads/0.8-incubating
Commit: b993f92270a595775d319beabf1f753e1f296067
Parents: fe69c84
Author: apoorvnaik <[email protected]>
Authored: Sun Aug 13 00:53:38 2017 -0700
Committer: Madhan Neethiraj <[email protected]>
Committed: Sun Aug 13 01:14:17 2017 -0700

----------------------------------------------------------------------
 .../java/org/apache/atlas/AtlasErrorCode.java   |  8 +++--
 .../atlas/discovery/EntityDiscoveryService.java | 21 ++++++-----
 .../apache/atlas/discovery/SearchContext.java   | 38 ++++++++++++++------
 .../apache/atlas/web/rest/DiscoveryREST.java    |  6 +++-
 4 files changed, 49 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/atlas/blob/b993f922/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 f267a33..aa8e3f4 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"),
@@ -71,10 +71,12 @@ public enum AtlasErrorCode {
     BAD_REQUEST(400, "ATLAS-400-00-029", "{0}"),
     PARAMETER_PARSING_FAILED(400, "ATLAS-400-00-02A", "Parameter parsing 
failed at: {0}"),
     MISSING_MANDATORY_ATTRIBUTE(400, "ATLAS-400-00-02B", "Mandatory field 
{0}.{1} has empty/null value"),
-    INVALID_RELATIONSHIP_ATTRIBUTE(400, "ATLAS-400-00-02C", "Expected 
attribute {0} to be a relationship but found type {}"),
-    INVALID_RELATIONSHIP_TYPE(400, "ATLAS-400-00-02D", "Invalid entity type 
'{0}', guid '{1}' in relationship search"),
+       INVALID_RELATIONSHIP_ATTRIBUTE(400, "ATLAS-400-00-02C", "Expected 
attribute {0} to be a relationship but found type {}"),
+       INVALID_RELATIONSHIP_TYPE(400, "ATLAS-400-00-02D", "Invalid entity type 
'{0}', guid '{1}' in relationship search"),
 
     // All Not found enums go here
+    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"),
     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"),
     EMPTY_RESULTS(404, "ATLAS-404-00-004", "No result found for {0}"),

http://git-wip-us.apache.org/repos/asf/atlas/blob/b993f922/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 6f467e5..d5062a7 100644
--- 
a/repository/src/main/java/org/apache/atlas/discovery/EntityDiscoveryService.java
+++ 
b/repository/src/main/java/org/apache/atlas/discovery/EntityDiscoveryService.java
@@ -438,18 +438,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/b993f922/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/b993f922/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 cae5498..032bde8 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
@@ -214,7 +214,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);
@@ -257,6 +257,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);

Reply via email to