Fixed NPE when fetching controller services of a specific type
@@ -28,7 +28,6 @@ import com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException;
import com.thinkbiganalytics.nifi.rest.client.NifiComponentNotFoundException;
import com.thinkbiganalytics.nifi.rest.support.NifiConstants;
-import org.apache.nifi.web.api.dto.BundleDTO;
import org.apache.nifi.web.api.dto.ControllerServiceApiDTO;
import org.apache.nifi.web.api.dto.ControllerServiceDTO;
import org.apache.nifi.web.api.dto.DocumentedTypeDTO;
@@ -39,7 +38,9 @@ import org.apache.nifi.web.api.entity.ControllerServicesEntity;
import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
@@ -141,26 +142,24 @@ public class NiFiControllerServicesRestClientV1 extends AbstractNiFiControllerSe
public Set<DocumentedTypeDTO> getTypes(@Nonnull final String serviceType) {
// Find the bundle that contains the API
final Optional<ControllerServiceApiDTO> controllerServiceApi = getTypes().stream()
- .flatMap(type -> type.getControllerServiceApis().stream())
+ .map(DocumentedTypeDTO::getControllerServiceApis)
+ .filter(Objects::nonNull)
+ .flatMap(List::stream)
.filter(api -> api.getType().equals(serviceType))
.findAny();
// Query for types implementing the API
- return controllerServiceApi
- .map(api -> {
final Map<String, Object> query = new HashMap<>();
query.put("serviceType", serviceType);
- final BundleDTO bundle = api.getBundle();
- if (bundle != null) {
+ controllerServiceApi.map(ControllerServiceApiDTO::getBundle)
+ .ifPresent(bundle -> {
query.put("serviceBundleGroup", bundle.getGroup());
query.put("serviceBundleArtifact", bundle.getArtifact());
query.put("serviceBundleVersion", bundle.getVersion());
- }
+ });
- return query;
- })
- .map(query -> client.get("/flow/controller-service-types", query, ControllerServiceTypesEntity.class))
+ return Optional.ofNullable(client.get("/flow/controller-service-types", query, ControllerServiceTypesEntity.class))
.map(ControllerServiceTypesEntity::getControllerServiceTypes)
.orElseGet(Collections::emptySet);
}
