Repository: nifi-registry
Updated Branches:
  refs/heads/master eb25bf9fe -> 57e80fc6a


NIFIREG-76 Adding exception mapper for ConstraintViolationException, and 
validating blank names on updates

This closes #67.

Signed-off-by: Bryan Bende <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/nifi-registry/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi-registry/commit/57e80fc6
Tree: http://git-wip-us.apache.org/repos/asf/nifi-registry/tree/57e80fc6
Diff: http://git-wip-us.apache.org/repos/asf/nifi-registry/diff/57e80fc6

Branch: refs/heads/master
Commit: 57e80fc6a800fd4ad01061dcd394dfb7793eafcf
Parents: eb25bf9
Author: Bryan Bende <[email protected]>
Authored: Thu Dec 21 17:08:31 2017 -0500
Committer: Bryan Bende <[email protected]>
Committed: Fri Dec 22 08:45:44 2017 -0500

----------------------------------------------------------------------
 .../nifi/registry/service/RegistryService.java  | 14 +++-
 .../ConstraintViolationExceptionMapper.java     | 68 ++++++++++++++++++++
 .../manage-bucket/nf-registry-manage-bucket.js  |  8 +++
 3 files changed, 87 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi-registry/blob/57e80fc6/nifi-registry-framework/src/main/java/org/apache/nifi/registry/service/RegistryService.java
----------------------------------------------------------------------
diff --git 
a/nifi-registry-framework/src/main/java/org/apache/nifi/registry/service/RegistryService.java
 
b/nifi-registry-framework/src/main/java/org/apache/nifi/registry/service/RegistryService.java
index 8a96d20..e805520 100644
--- 
a/nifi-registry-framework/src/main/java/org/apache/nifi/registry/service/RegistryService.java
+++ 
b/nifi-registry-framework/src/main/java/org/apache/nifi/registry/service/RegistryService.java
@@ -111,7 +111,7 @@ public class RegistryService {
         bucket.setIdentifier(UUID.randomUUID().toString());
         bucket.setCreatedTimestamp(System.currentTimeMillis());
 
-        validate(bucket, "Bucket is not valid");
+        validate(bucket, "Cannot create Bucket");
 
         writeLock.lock();
         try {
@@ -174,6 +174,10 @@ public class RegistryService {
             throw new IllegalArgumentException("Bucket identifier cannot be 
null");
         }
 
+        if (bucket.getName() != null && StringUtils.isBlank(bucket.getName())) 
{
+            throw new IllegalArgumentException("Bucket name cannot be blank");
+        }
+
         writeLock.lock();
         try {
             // ensure a bucket with the given id exists
@@ -312,7 +316,7 @@ public class RegistryService {
         versionedFlow.setCreatedTimestamp(timestamp);
         versionedFlow.setModifiedTimestamp(timestamp);
 
-        validate(versionedFlow, "Versioned flow is not valid");
+        validate(versionedFlow, "Cannot create versioned flow");
 
         writeLock.lock();
         try {
@@ -405,6 +409,10 @@ public class RegistryService {
             throw new IllegalArgumentException("Versioned flow bucket 
identifier cannot be null or blank");
         }
 
+        if (versionedFlow.getName() != null && 
StringUtils.isBlank(versionedFlow.getName())) {
+            throw new IllegalArgumentException("Versioned flow name cannot be 
blank");
+        }
+
         writeLock.lock();
         try {
             // ensure the bucket exists
@@ -506,7 +514,7 @@ public class RegistryService {
         flowSnapshot.setFlow(null);
         flowSnapshot.setBucket(null);
 
-        validate(flowSnapshot, "Versioned flow snapshot is not valid");
+        validate(flowSnapshot, "Cannot create versioned flow snapshot");
 
         writeLock.lock();
         try {

http://git-wip-us.apache.org/repos/asf/nifi-registry/blob/57e80fc6/nifi-registry-web-api/src/main/java/org/apache/nifi/registry/web/mapper/ConstraintViolationExceptionMapper.java
----------------------------------------------------------------------
diff --git 
a/nifi-registry-web-api/src/main/java/org/apache/nifi/registry/web/mapper/ConstraintViolationExceptionMapper.java
 
b/nifi-registry-web-api/src/main/java/org/apache/nifi/registry/web/mapper/ConstraintViolationExceptionMapper.java
new file mode 100644
index 0000000..b691775
--- /dev/null
+++ 
b/nifi-registry-web-api/src/main/java/org/apache/nifi/registry/web/mapper/ConstraintViolationExceptionMapper.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.registry.web.mapper;
+
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.ConstraintViolationException;
+import javax.validation.Path;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+
+@Component
+@Provider
+public class ConstraintViolationExceptionMapper implements 
ExceptionMapper<ConstraintViolationException> {
+
+    private static final Logger logger = 
LoggerFactory.getLogger(ConstraintViolationExceptionMapper.class);
+
+    @Override
+    public Response toResponse(ConstraintViolationException exception) {
+        logger.info(String.format("%s. Returning %s response.", exception, 
Response.Status.BAD_REQUEST));
+
+        if (logger.isDebugEnabled()) {
+            logger.debug(StringUtils.EMPTY, exception);
+        }
+
+        // start with the overall message which will be something like "Cannot 
create xyz"
+        final StringBuilder errorMessage = new 
StringBuilder(exception.getMessage()).append(" - ");
+
+        boolean first = true;
+        for (final ConstraintViolation violation : 
exception.getConstraintViolations()) {
+            if (!first) {
+                errorMessage.append(", ");
+            }
+            first = false;
+
+            // lastNode should end up as the field that failed validation
+            Path.Node lastNode = null;
+            for (final Path.Node node : violation.getPropertyPath()) {
+                lastNode = node;
+            }
+
+            // append something like "xyz must not be..."
+            errorMessage.append(lastNode.getName()).append(" 
").append(violation.getMessage());
+        }
+
+        return 
Response.status(Response.Status.BAD_REQUEST).entity(errorMessage.toString()).type("text/plain").build();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/nifi-registry/blob/57e80fc6/nifi-registry-web-ui/src/main/webapp/components/administration/workflow/sidenav/manage-bucket/nf-registry-manage-bucket.js
----------------------------------------------------------------------
diff --git 
a/nifi-registry-web-ui/src/main/webapp/components/administration/workflow/sidenav/manage-bucket/nf-registry-manage-bucket.js
 
b/nifi-registry-web-ui/src/main/webapp/components/administration/workflow/sidenav/manage-bucket/nf-registry-manage-bucket.js
index 8bb3286..38b09e5 100644
--- 
a/nifi-registry-web-ui/src/main/webapp/components/administration/workflow/sidenav/manage-bucket/nf-registry-manage-bucket.js
+++ 
b/nifi-registry-web-ui/src/main/webapp/components/administration/workflow/sidenav/manage-bucket/nf-registry-manage-bucket.js
@@ -390,6 +390,14 @@ NfRegistryManageBucket.prototype = {
                     acceptButton: 'Ok',
                     acceptButtonColor: 'fds-warn'
                 });
+            } else if (response.status === 400) {
+                self._bucketname = self.nfRegistryService.bucket.name;
+                self.dialogService.openConfirm({
+                    title: 'Error',
+                    message: response.error,
+                    acceptButton: 'Ok',
+                    acceptButtonColor: 'fds-warn'
+                });
             }
         });
     }

Reply via email to