use of org.apache.commons.lang3.StringUtils.isBlank in project nifi by apache.
the class Wait method onTrigger.
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
final ComponentLog logger = getLogger();
// Signal id is computed from attribute 'RELEASE_SIGNAL_IDENTIFIER' with expression language support
final PropertyValue signalIdProperty = context.getProperty(RELEASE_SIGNAL_IDENTIFIER);
final Integer bufferCount = context.getProperty(WAIT_BUFFER_COUNT).asInteger();
final Map<Relationship, List<FlowFile>> processedFlowFiles = new HashMap<>();
final Function<Relationship, List<FlowFile>> getFlowFilesFor = r -> processedFlowFiles.computeIfAbsent(r, k -> new ArrayList<>());
final AtomicReference<String> targetSignalId = new AtomicReference<>();
final AtomicInteger bufferedCount = new AtomicInteger(0);
final List<FlowFile> failedFilteringFlowFiles = new ArrayList<>();
final Supplier<FlowFileFilter.FlowFileFilterResult> acceptResultSupplier = () -> bufferedCount.incrementAndGet() == bufferCount ? ACCEPT_AND_TERMINATE : ACCEPT_AND_CONTINUE;
final List<FlowFile> flowFiles = session.get(f -> {
final String fSignalId = signalIdProperty.evaluateAttributeExpressions(f).getValue();
// if the computed value is null, or empty, we transfer the FlowFile to failure relationship
if (StringUtils.isBlank(fSignalId)) {
// We can't penalize f before getting it from session, so keep it in a temporal list.
logger.error("FlowFile {} has no attribute for given Release Signal Identifier", new Object[] { f });
failedFilteringFlowFiles.add(f);
return ACCEPT_AND_CONTINUE;
}
final String targetSignalIdStr = targetSignalId.get();
if (targetSignalIdStr == null) {
// This is the first one.
targetSignalId.set(fSignalId);
return acceptResultSupplier.get();
}
if (targetSignalIdStr.equals(fSignalId)) {
return acceptResultSupplier.get();
}
return REJECT_AND_CONTINUE;
});
final String attributeCopyMode = context.getProperty(ATTRIBUTE_COPY_MODE).getValue();
final boolean replaceOriginalAttributes = ATTRIBUTE_COPY_REPLACE.getValue().equals(attributeCopyMode);
final AtomicReference<Signal> signalRef = new AtomicReference<>();
// This map contains original counts before those are consumed to release incoming FlowFiles.
final HashMap<String, Long> originalSignalCounts = new HashMap<>();
final Consumer<FlowFile> transferToFailure = flowFile -> {
flowFile = session.penalize(flowFile);
getFlowFilesFor.apply(REL_FAILURE).add(flowFile);
};
final Consumer<Entry<Relationship, List<FlowFile>>> transferFlowFiles = routedFlowFiles -> {
Relationship relationship = routedFlowFiles.getKey();
if (REL_WAIT.equals(relationship)) {
final String waitMode = context.getProperty(WAIT_MODE).getValue();
if (WAIT_MODE_KEEP_IN_UPSTREAM.getValue().equals(waitMode)) {
// Transfer to self.
relationship = Relationship.SELF;
}
}
final List<FlowFile> flowFilesWithSignalAttributes = routedFlowFiles.getValue().stream().map(f -> copySignalAttributes(session, f, signalRef.get(), originalSignalCounts, replaceOriginalAttributes)).collect(Collectors.toList());
session.transfer(flowFilesWithSignalAttributes, relationship);
};
failedFilteringFlowFiles.forEach(f -> {
flowFiles.remove(f);
transferToFailure.accept(f);
});
if (flowFiles.isEmpty()) {
// If there was nothing but failed FlowFiles while filtering, transfer those and end immediately.
processedFlowFiles.entrySet().forEach(transferFlowFiles);
return;
}
// the cache client used to interact with the distributed cache
final AtomicDistributedMapCacheClient cache = context.getProperty(DISTRIBUTED_CACHE_SERVICE).asControllerService(AtomicDistributedMapCacheClient.class);
final WaitNotifyProtocol protocol = new WaitNotifyProtocol(cache);
final String signalId = targetSignalId.get();
final Signal signal;
// get notifying signal
try {
signal = protocol.getSignal(signalId);
if (signal != null) {
originalSignalCounts.putAll(signal.getCounts());
}
signalRef.set(signal);
} catch (final IOException e) {
throw new ProcessException(String.format("Failed to get signal for %s due to %s", signalId, e), e);
}
String targetCounterName = null;
long targetCount = 1;
int releasableFlowFileCount = 1;
final List<FlowFile> candidates = new ArrayList<>();
for (FlowFile flowFile : flowFiles) {
// Set wait start timestamp if it's not set yet
String waitStartTimestamp = flowFile.getAttribute(WAIT_START_TIMESTAMP);
if (waitStartTimestamp == null) {
waitStartTimestamp = String.valueOf(System.currentTimeMillis());
flowFile = session.putAttribute(flowFile, WAIT_START_TIMESTAMP, waitStartTimestamp);
}
long lWaitStartTimestamp;
try {
lWaitStartTimestamp = Long.parseLong(waitStartTimestamp);
} catch (NumberFormatException nfe) {
logger.error("{} has an invalid value '{}' on FlowFile {}", new Object[] { WAIT_START_TIMESTAMP, waitStartTimestamp, flowFile });
transferToFailure.accept(flowFile);
continue;
}
// check for expiration
long expirationDuration = context.getProperty(EXPIRATION_DURATION).asTimePeriod(TimeUnit.MILLISECONDS);
long now = System.currentTimeMillis();
if (now > (lWaitStartTimestamp + expirationDuration)) {
logger.info("FlowFile {} expired after {}ms", new Object[] { flowFile, (now - lWaitStartTimestamp) });
getFlowFilesFor.apply(REL_EXPIRED).add(flowFile);
continue;
}
// If there's no signal yet, then we don't have to evaluate target counts. Return immediately.
if (signal == null) {
if (logger.isDebugEnabled()) {
logger.debug("No release signal found for {} on FlowFile {} yet", new Object[] { signalId, flowFile });
}
getFlowFilesFor.apply(REL_WAIT).add(flowFile);
continue;
}
// Fix target counter name and count from current FlowFile, if those are not set yet.
if (candidates.isEmpty()) {
targetCounterName = context.getProperty(SIGNAL_COUNTER_NAME).evaluateAttributeExpressions(flowFile).getValue();
try {
targetCount = Long.valueOf(context.getProperty(TARGET_SIGNAL_COUNT).evaluateAttributeExpressions(flowFile).getValue());
} catch (final NumberFormatException e) {
transferToFailure.accept(flowFile);
logger.error("Failed to parse targetCount when processing {} due to {}", new Object[] { flowFile, e }, e);
continue;
}
try {
releasableFlowFileCount = Integer.valueOf(context.getProperty(RELEASABLE_FLOWFILE_COUNT).evaluateAttributeExpressions(flowFile).getValue());
} catch (final NumberFormatException e) {
transferToFailure.accept(flowFile);
logger.error("Failed to parse releasableFlowFileCount when processing {} due to {}", new Object[] { flowFile, e }, e);
continue;
}
}
// FlowFile is now validated and added to candidates.
candidates.add(flowFile);
}
boolean waitCompleted = false;
boolean waitProgressed = false;
if (signal != null && !candidates.isEmpty()) {
if (releasableFlowFileCount > 0) {
signal.releaseCandidates(targetCounterName, targetCount, releasableFlowFileCount, candidates, released -> getFlowFilesFor.apply(REL_SUCCESS).addAll(released), waiting -> getFlowFilesFor.apply(REL_WAIT).addAll(waiting));
waitCompleted = signal.getTotalCount() == 0 && signal.getReleasableCount() == 0;
waitProgressed = !getFlowFilesFor.apply(REL_SUCCESS).isEmpty();
} else {
boolean reachedTargetCount = StringUtils.isBlank(targetCounterName) ? signal.isTotalCountReached(targetCount) : signal.isCountReached(targetCounterName, targetCount);
if (reachedTargetCount) {
getFlowFilesFor.apply(REL_SUCCESS).addAll(candidates);
} else {
getFlowFilesFor.apply(REL_WAIT).addAll(candidates);
}
}
}
// Transfer FlowFiles.
processedFlowFiles.entrySet().forEach(transferFlowFiles);
// Update signal if needed.
try {
if (waitCompleted) {
protocol.complete(signalId);
} else if (waitProgressed) {
protocol.replace(signal);
}
} catch (final IOException e) {
session.rollback();
throw new ProcessException(String.format("Unable to communicate with cache while updating %s due to %s", signalId, e), e);
}
}
use of org.apache.commons.lang3.StringUtils.isBlank in project nifi by apache.
the class GetRegistryClientId method doExecute.
@Override
public RegistryClientIDResult doExecute(final NiFiClient client, final Properties properties) throws NiFiClientException, IOException, CommandException {
final String regClientName = getArg(properties, CommandOption.REGISTRY_CLIENT_NAME);
final String regClientUrl = getArg(properties, CommandOption.REGISTRY_CLIENT_URL);
if (!StringUtils.isBlank(regClientName) && !StringUtils.isBlank(regClientUrl)) {
throw new CommandException("Name and URL cannot be specified at the same time");
}
if (StringUtils.isBlank(regClientName) && StringUtils.isBlank(regClientUrl)) {
throw new CommandException("Name or URL must be specified");
}
final RegistryClientsEntity registries = client.getControllerClient().getRegistryClients();
RegistryDTO registry;
if (!StringUtils.isBlank(regClientName)) {
registry = registries.getRegistries().stream().map(r -> r.getComponent()).filter(r -> r.getName().equalsIgnoreCase(regClientName.trim())).findFirst().orElse(null);
} else {
registry = registries.getRegistries().stream().map(r -> r.getComponent()).filter(r -> r.getUri().equalsIgnoreCase(regClientUrl.trim())).findFirst().orElse(null);
}
if (registry == null) {
throw new NiFiClientException("No registry client exists with the name '" + regClientName + "'");
} else {
return new RegistryClientIDResult(getResultType(properties), registry);
}
}
use of org.apache.commons.lang3.StringUtils.isBlank in project nifi by apache.
the class JerseyFlowClient method getSuggestedProcessGroupCoordinates.
@Override
public ProcessGroupBox getSuggestedProcessGroupCoordinates(final String parentId) throws NiFiClientException, IOException {
if (StringUtils.isBlank(parentId)) {
throw new IllegalArgumentException("Process group id cannot be null");
}
final ProcessGroupFlowEntity processGroup = getProcessGroup(parentId);
final ProcessGroupFlowDTO processGroupFlowDTO = processGroup.getProcessGroupFlow();
final FlowDTO flowDTO = processGroupFlowDTO.getFlow();
final List<ComponentEntity> pgComponents = new ArrayList<>();
pgComponents.addAll(flowDTO.getProcessGroups());
pgComponents.addAll(flowDTO.getProcessors());
pgComponents.addAll(flowDTO.getRemoteProcessGroups());
pgComponents.addAll(flowDTO.getConnections());
pgComponents.addAll(flowDTO.getFunnels());
pgComponents.addAll(flowDTO.getInputPorts());
pgComponents.addAll(flowDTO.getOutputPorts());
pgComponents.addAll(flowDTO.getLabels());
final Set<PositionDTO> positions = pgComponents.stream().map(ComponentEntity::getPosition).collect(Collectors.toSet());
if (positions.isEmpty()) {
return ProcessGroupBox.CANVAS_CENTER;
}
final List<ProcessGroupBox> coords = positions.stream().filter(Objects::nonNull).map(p -> new ProcessGroupBox(p.getX().intValue(), p.getY().intValue())).collect(Collectors.toList());
final ProcessGroupBox freeSpot = coords.get(0).findFreeSpace(coords);
return freeSpot;
}
use of org.apache.commons.lang3.StringUtils.isBlank in project nifi by apache.
the class ControllerFacade method setComponentDetails.
private void setComponentDetails(final ProvenanceEventDTO dto) {
final ProcessGroup root = flowController.getGroup(flowController.getRootGroupId());
final Connectable connectable = root.findLocalConnectable(dto.getComponentId());
if (connectable != null) {
dto.setGroupId(connectable.getProcessGroup().getIdentifier());
dto.setComponentName(connectable.getName());
return;
}
final RemoteGroupPort remoteGroupPort = root.findRemoteGroupPort(dto.getComponentId());
if (remoteGroupPort != null) {
dto.setGroupId(remoteGroupPort.getProcessGroupIdentifier());
dto.setComponentName(remoteGroupPort.getName());
return;
}
final Connection connection = root.findConnection(dto.getComponentId());
if (connection != null) {
dto.setGroupId(connection.getProcessGroup().getIdentifier());
String name = connection.getName();
final Collection<Relationship> relationships = connection.getRelationships();
if (StringUtils.isBlank(name) && CollectionUtils.isNotEmpty(relationships)) {
name = StringUtils.join(relationships.stream().map(relationship -> relationship.getName()).collect(Collectors.toSet()), ", ");
}
dto.setComponentName(name);
return;
}
}
use of org.apache.commons.lang3.StringUtils.isBlank in project cxf by apache.
the class OpenApiCustomizer method customize.
public void customize(final OpenAPI oas) {
if (replaceTags || javadocProvider != null) {
Map<String, ClassResourceInfo> operations = new HashMap<>();
Map<Pair<String, String>, OperationResourceInfo> methods = new HashMap<>();
cris.forEach(cri -> {
cri.getMethodDispatcher().getOperationResourceInfos().forEach(ori -> {
String normalizedPath = getNormalizedPath(cri.getURITemplate().getValue(), ori.getURITemplate().getValue());
operations.put(normalizedPath, cri);
methods.put(Pair.of(ori.getHttpMethod(), normalizedPath), ori);
});
});
List<Tag> tags = new ArrayList<>();
oas.getPaths().forEach((pathKey, pathItem) -> {
Tag tag = null;
if (replaceTags && operations.containsKey(pathKey)) {
ClassResourceInfo cri = operations.get(pathKey);
tag = new Tag();
tag.setName(cri.getURITemplate().getValue().replaceAll("/", "_"));
if (javadocProvider != null) {
tag.setDescription(javadocProvider.getClassDoc(cri));
}
if (!tags.contains(tag)) {
tags.add(tag);
}
}
for (Map.Entry<HttpMethod, Operation> subentry : pathItem.readOperationsMap().entrySet()) {
if (replaceTags && tag != null) {
subentry.getValue().setTags(Collections.singletonList(tag.getName()));
}
Pair<String, String> key = Pair.of(subentry.getKey().name(), pathKey);
if (methods.containsKey(key) && javadocProvider != null) {
OperationResourceInfo ori = methods.get(key);
if (StringUtils.isBlank(subentry.getValue().getSummary())) {
subentry.getValue().setSummary(javadocProvider.getMethodDoc(ori));
}
if (subentry.getValue().getParameters() == null) {
List<Parameter> parameters = new ArrayList<>();
addParameters(parameters);
subentry.getValue().setParameters(parameters);
} else {
for (int i = 0; i < subentry.getValue().getParameters().size(); i++) {
if (StringUtils.isBlank(subentry.getValue().getParameters().get(i).getDescription())) {
subentry.getValue().getParameters().get(i).setDescription(javadocProvider.getMethodParameterDoc(ori, i));
}
}
addParameters(subentry.getValue().getParameters());
}
if (subentry.getValue().getResponses() != null && !subentry.getValue().getResponses().isEmpty()) {
ApiResponse response = subentry.getValue().getResponses().entrySet().iterator().next().getValue();
if (StringUtils.isBlank(response.getDescription())) {
response.setDescription(javadocProvider.getMethodResponseDoc(ori));
}
}
}
}
});
if (replaceTags && oas.getTags() != null) {
oas.setTags(tags);
}
}
}
Aggregations