You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by mc...@apache.org on 2016/04/29 22:32:21 UTC

[08/13] nifi git commit: NIFI-1554: - Populating component entities in the REST API to decouple key fields from the configuration DTOs. - Added initial support for components in UI when access isn't allowed. Formal styling to come later.

http://git-wip-us.apache.org/repos/asf/nifi/blob/ff98d823/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/LabelResource.java
----------------------------------------------------------------------
diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/LabelResource.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/LabelResource.java
index 411bad2..65dbb75 100644
--- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/LabelResource.java
+++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/LabelResource.java
@@ -25,9 +25,9 @@ import com.wordnik.swagger.annotations.Authorization;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.nifi.cluster.manager.impl.WebClusterManager;
 import org.apache.nifi.util.NiFiProperties;
-import org.apache.nifi.web.ConfigurationSnapshot;
 import org.apache.nifi.web.NiFiServiceFacade;
 import org.apache.nifi.web.Revision;
+import org.apache.nifi.web.UpdateResult;
 import org.apache.nifi.web.api.dto.LabelDTO;
 import org.apache.nifi.web.api.dto.RevisionDTO;
 import org.apache.nifi.web.api.entity.LabelEntity;
@@ -74,6 +74,32 @@ public class LabelResource extends ApplicationResource {
     /**
      * Populates the uri for the specified labels.
      *
+     * @param labelEntities labels
+     * @return entites
+     */
+    public Set<LabelEntity> populateRemainingLabelEntitiesContent(Set<LabelEntity> labelEntities) {
+        for (LabelEntity labelEntity : labelEntities) {
+            populateRemainingLabelEntityContent(labelEntity);
+        }
+        return labelEntities;
+    }
+
+    /**
+     * Populates the uri for the specified labels.
+     *
+     * @param labelEntity label
+     * @return entities
+     */
+    public LabelEntity populateRemainingLabelEntityContent(LabelEntity labelEntity) {
+        if (labelEntity.getComponent() != null) {
+            populateRemainingLabelContent(labelEntity.getComponent());
+        }
+        return labelEntity;
+    }
+
+    /**
+     * Populates the uri for the specified labels.
+     *
      * @param labels labels
      * @return dtos
      */
@@ -96,7 +122,6 @@ public class LabelResource extends ApplicationResource {
     /**
      * Retrieves the specified label.
      *
-     * @param clientId Optional client id. If the client id is not specified, a new one will be generated. This value (whether specified or generated) is included in the response.
      * @param id The id of the label to retrieve
      * @return A labelEntity.
      */
@@ -125,11 +150,6 @@ public class LabelResource extends ApplicationResource {
     )
     public Response getLabel(
             @ApiParam(
-                    value = "If the client id is not specified, new one will be generated. This value (whether specified or generated) is included in the response.",
-                    required = false
-            )
-            @QueryParam(CLIENT_ID) @DefaultValue(StringUtils.EMPTY) ClientIdParameter clientId,
-            @ApiParam(
                     value = "The label id.",
                     required = true
             )
@@ -141,16 +161,8 @@ public class LabelResource extends ApplicationResource {
         }
 
         // get the label
-        final LabelDTO label = serviceFacade.getLabel(id);
-
-        // create the revision
-        final RevisionDTO revision = new RevisionDTO();
-        revision.setClientId(clientId.getClientId());
-
-        // create the response entity
-        final LabelEntity entity = new LabelEntity();
-        entity.setRevision(revision);
-        entity.setLabel(populateRemainingLabelContent(label));
+        final LabelEntity entity = serviceFacade.getLabel(id);
+        populateRemainingLabelEntityContent(entity);
 
         return clusterContext(generateOkResponse(entity)).build();
     }
@@ -196,7 +208,7 @@ public class LabelResource extends ApplicationResource {
                     required = true
             ) LabelEntity labelEntity) {
 
-        if (labelEntity == null || labelEntity.getLabel() == null) {
+        if (labelEntity == null || labelEntity.getComponent() == null) {
             throw new IllegalArgumentException("Label details must be specified.");
         }
 
@@ -205,7 +217,7 @@ public class LabelResource extends ApplicationResource {
         }
 
         // ensure the ids are the same
-        final LabelDTO requestLabelDTO = labelEntity.getLabel();
+        final LabelDTO requestLabelDTO = labelEntity.getComponent();
         if (!id.equals(requestLabelDTO.getId())) {
             throw new IllegalArgumentException(String.format("The label id (%s) in the request body does not equal the "
                     + "label id of the requested resource (%s).", requestLabelDTO.getId(), id));
@@ -229,25 +241,13 @@ public class LabelResource extends ApplicationResource {
 
         // update the label
         final RevisionDTO revision = labelEntity.getRevision();
-        final ConfigurationSnapshot<LabelDTO> controllerResponse = serviceFacade.updateLabel(
+        final UpdateResult<LabelEntity> result = serviceFacade.updateLabel(
                 new Revision(revision.getVersion(), revision.getClientId()), requestLabelDTO);
+        final LabelEntity entity = result.getResult();
+        populateRemainingLabelEntityContent(entity);
 
-        // get the results
-        final LabelDTO responseLabelDTO = controllerResponse.getConfiguration();
-        populateRemainingLabelContent(responseLabelDTO);
-
-        // get the updated revision
-        final RevisionDTO updatedRevision = new RevisionDTO();
-        updatedRevision.setClientId(revision.getClientId());
-        updatedRevision.setVersion(controllerResponse.getVersion());
-
-        // build the response entity
-        final LabelEntity entity = new LabelEntity();
-        entity.setRevision(updatedRevision);
-        entity.setLabel(responseLabelDTO);
-
-        if (controllerResponse.isNew()) {
-            return clusterContext(generateCreatedResponse(URI.create(responseLabelDTO.getUri()), entity)).build();
+        if (result.isNew()) {
+            return clusterContext(generateCreatedResponse(URI.create(entity.getComponent().getUri()), entity)).build();
         } else {
             return clusterContext(generateOkResponse(entity)).build();
         }
@@ -319,17 +319,7 @@ public class LabelResource extends ApplicationResource {
         }
 
         // delete the specified label
-        final ConfigurationSnapshot<Void> controllerResponse = serviceFacade.deleteLabel(new Revision(clientVersion, clientId.getClientId()), id);
-
-        // get the updated revision
-        final RevisionDTO revision = new RevisionDTO();
-        revision.setClientId(clientId.getClientId());
-        revision.setVersion(controllerResponse.getVersion());
-
-        // build the response entity
-        final LabelEntity entity = new LabelEntity();
-        entity.setRevision(revision);
-
+        final LabelEntity entity = serviceFacade.deleteLabel(new Revision(clientVersion, clientId.getClientId()), id);
         return clusterContext(generateOkResponse(entity)).build();
     }
 

http://git-wip-us.apache.org/repos/asf/nifi/blob/ff98d823/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/OutputPortResource.java
----------------------------------------------------------------------
diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/OutputPortResource.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/OutputPortResource.java
index 1335390..398350f 100644
--- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/OutputPortResource.java
+++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/OutputPortResource.java
@@ -25,12 +25,12 @@ import com.wordnik.swagger.annotations.Authorization;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.nifi.cluster.manager.impl.WebClusterManager;
 import org.apache.nifi.util.NiFiProperties;
-import org.apache.nifi.web.ConfigurationSnapshot;
 import org.apache.nifi.web.NiFiServiceFacade;
 import org.apache.nifi.web.Revision;
+import org.apache.nifi.web.UpdateResult;
 import org.apache.nifi.web.api.dto.PortDTO;
 import org.apache.nifi.web.api.dto.RevisionDTO;
-import org.apache.nifi.web.api.entity.OutputPortEntity;
+import org.apache.nifi.web.api.entity.PortEntity;
 import org.apache.nifi.web.api.request.ClientIdParameter;
 import org.apache.nifi.web.api.request.LongParameter;
 import org.slf4j.Logger;
@@ -74,6 +74,32 @@ public class OutputPortResource extends ApplicationResource {
     /**
      * Populates the uri for the specified output ports.
      *
+     * @param outputPortEntities ports
+     * @return dtos
+     */
+    public Set<PortEntity> populateRemainingOutputPortEntitiesContent(Set<PortEntity> outputPortEntities) {
+        for (PortEntity outputPortEntity : outputPortEntities) {
+            populateRemainingOutputPortEntityContent(outputPortEntity);
+        }
+        return outputPortEntities;
+    }
+
+    /**
+     * Populates the uri for the specified output ports.
+     *
+     * @param outputPortEntity ports
+     * @return dtos
+     */
+    public PortEntity populateRemainingOutputPortEntityContent(PortEntity outputPortEntity) {
+        if (outputPortEntity.getComponent() != null) {
+            populateRemainingOutputPortContent(outputPortEntity.getComponent());
+        }
+        return outputPortEntity;
+    }
+
+    /**
+     * Populates the uri for the specified output ports.
+     *
      * @param outputPorts ports
      * @return dtos
      */
@@ -96,7 +122,6 @@ public class OutputPortResource extends ApplicationResource {
     /**
      * Retrieves the specified output port.
      *
-     * @param clientId Optional client id. If the client id is not specified, a new one will be generated. This value (whether specified or generated) is included in the response.
      * @param id The id of the output port to retrieve
      * @return A outputPortEntity.
      */
@@ -107,7 +132,7 @@ public class OutputPortResource extends ApplicationResource {
     // TODO - @PreAuthorize("hasAnyRole('ROLE_MONITOR', 'ROLE_DFM', 'ROLE_ADMIN')")
     @ApiOperation(
             value = "Gets an output port",
-            response = OutputPortEntity.class,
+            response = PortEntity.class,
             authorizations = {
                 @Authorization(value = "Read Only", type = "ROLE_MONITOR"),
                 @Authorization(value = "Data Flow Manager", type = "ROLE_DFM"),
@@ -125,11 +150,6 @@ public class OutputPortResource extends ApplicationResource {
     )
     public Response getOutputPort(
             @ApiParam(
-                    value = "If the client id is not specified, new one will be generated. This value (whether specified or generated) is included in the response.",
-                    required = false
-            )
-            @QueryParam(CLIENT_ID) @DefaultValue(StringUtils.EMPTY) ClientIdParameter clientId,
-            @ApiParam(
                     value = "The output port id.",
                     required = true
             )
@@ -141,16 +161,8 @@ public class OutputPortResource extends ApplicationResource {
         }
 
         // get the port
-        final PortDTO port = serviceFacade.getOutputPort(id);
-
-        // create the revision
-        final RevisionDTO revision = new RevisionDTO();
-        revision.setClientId(clientId.getClientId());
-
-        // create the response entity
-        final OutputPortEntity entity = new OutputPortEntity();
-        entity.setRevision(revision);
-        entity.setOutputPort(populateRemainingOutputPortContent(port));
+        final PortEntity entity = serviceFacade.getOutputPort(id);
+        populateRemainingOutputPortEntityContent(entity);
 
         return clusterContext(generateOkResponse(entity)).build();
     }
@@ -170,7 +182,7 @@ public class OutputPortResource extends ApplicationResource {
     // TODO - @PreAuthorize("hasRole('ROLE_DFM')")
     @ApiOperation(
             value = "Updates an output port",
-            response = OutputPortEntity.class,
+            response = PortEntity.class,
             authorizations = {
                 @Authorization(value = "Data Flow Manager", type = "ROLE_DFM")
             }
@@ -194,9 +206,9 @@ public class OutputPortResource extends ApplicationResource {
             @ApiParam(
                     value = "The output port configuration details.",
                     required = true
-            ) OutputPortEntity portEntity) {
+            ) PortEntity portEntity) {
 
-        if (portEntity == null || portEntity.getOutputPort() == null) {
+        if (portEntity == null || portEntity.getComponent() == null) {
             throw new IllegalArgumentException("Output port details must be specified.");
         }
 
@@ -205,7 +217,7 @@ public class OutputPortResource extends ApplicationResource {
         }
 
         // ensure the ids are the same
-        PortDTO requestPortDTO = portEntity.getOutputPort();
+        PortDTO requestPortDTO = portEntity.getComponent();
         if (!id.equals(requestPortDTO.getId())) {
             throw new IllegalArgumentException(String.format("The output port id (%s) in the request body does not equal the "
                     + "output port id of the requested resource (%s).", requestPortDTO.getId(), id));
@@ -230,25 +242,15 @@ public class OutputPortResource extends ApplicationResource {
 
         // update the output port
         final RevisionDTO revision = portEntity.getRevision();
-        final ConfigurationSnapshot<PortDTO> controllerResponse = serviceFacade.updateOutputPort(
+        final UpdateResult<PortEntity> updateResult = serviceFacade.updateOutputPort(
                 new Revision(revision.getVersion(), revision.getClientId()), requestPortDTO);
 
         // get the results
-        final PortDTO responsePortDTO = controllerResponse.getConfiguration();
-        populateRemainingOutputPortContent(responsePortDTO);
-
-        // get the updated revision
-        final RevisionDTO updatedRevision = new RevisionDTO();
-        updatedRevision.setClientId(revision.getClientId());
-        updatedRevision.setVersion(controllerResponse.getVersion());
-
-        // build the response entity
-        final OutputPortEntity entity = new OutputPortEntity();
-        entity.setRevision(updatedRevision);
-        entity.setOutputPort(responsePortDTO);
+        final PortEntity entity = updateResult.getResult();
+        populateRemainingOutputPortEntityContent(entity);
 
-        if (controllerResponse.isNew()) {
-            return clusterContext(generateCreatedResponse(URI.create(responsePortDTO.getUri()), entity)).build();
+        if (updateResult.isNew()) {
+            return clusterContext(generateCreatedResponse(URI.create(entity.getComponent().getUri()), entity)).build();
         } else {
             return clusterContext(generateOkResponse(entity)).build();
         }
@@ -270,7 +272,7 @@ public class OutputPortResource extends ApplicationResource {
     // TODO - @PreAuthorize("hasRole('ROLE_DFM')")
     @ApiOperation(
             value = "Deletes an output port",
-            response = OutputPortEntity.class,
+            response = PortEntity.class,
             authorizations = {
                 @Authorization(value = "Data Flow Manager", type = "ROLE_DFM")
             }
@@ -321,17 +323,7 @@ public class OutputPortResource extends ApplicationResource {
         }
 
         // delete the specified output port
-        final ConfigurationSnapshot<Void> controllerResponse = serviceFacade.deleteOutputPort(new Revision(clientVersion, clientId.getClientId()), id);
-
-        // get the updated revision
-        final RevisionDTO revision = new RevisionDTO();
-        revision.setClientId(clientId.getClientId());
-        revision.setVersion(controllerResponse.getVersion());
-
-        // build the response entity
-        final OutputPortEntity entity = new OutputPortEntity();
-        entity.setRevision(revision);
-
+        final PortEntity entity = serviceFacade.deleteOutputPort(new Revision(clientVersion, clientId.getClientId()), id);
         return clusterContext(generateOkResponse(entity)).build();
     }