ank19 commented on a change in pull request #6526: URL: https://github.com/apache/camel/pull/6526#discussion_r771969345
########## File path: components/camel-milo/src/main/java/org/apache/camel/component/milo/browse/MiloBrowseProducer.java ########## @@ -0,0 +1,138 @@ +/* + * 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.camel.component.milo.browse; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.apache.camel.AsyncCallback; +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.component.milo.client.MiloClientConnection; +import org.apache.camel.support.DefaultAsyncProducer; +import org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId; +import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId; +import org.eclipse.milo.opcua.stack.core.types.structured.BrowseResult; +import org.eclipse.milo.opcua.stack.core.types.structured.ReferenceDescription; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static java.lang.Boolean.TRUE; +import static org.apache.camel.component.milo.NodeIds.HEADER_NODE_IDS; + +public class MiloBrowseProducer extends DefaultAsyncProducer { + + private static final Logger LOG = LoggerFactory.getLogger(MiloBrowseProducer.class); + + private MiloClientConnection connection; + + private final boolean defaultAwaitWrites; + + public MiloBrowseProducer(final MiloBrowseEndpoint endpoint, final boolean defaultAwaitWrites) { + super(endpoint); + + this.defaultAwaitWrites = defaultAwaitWrites; + } + + @Override + public MiloBrowseEndpoint getEndpoint() { + return (MiloBrowseEndpoint) super.getEndpoint(); + } + + @Override + protected void doStart() throws Exception { + super.doStart(); + this.connection = getEndpoint().createConnection(); + } + + @Override + protected void doStop() throws Exception { + if (null != this.connection) { + getEndpoint().releaseConnection(connection); + } + super.doStop(); + } + + private ExpandedNodeId tryParse(String nodeString) { + final Optional<NodeId> nodeId = NodeId.parseSafe(nodeString); + return nodeId.map(NodeId::expanded).orElseGet(() -> ExpandedNodeId.parse(nodeString)); + } + + @Override + public boolean process(Exchange exchange, AsyncCallback async) { + + final Message message = exchange.getMessage(); + final List<ExpandedNodeId> expandedNodeIds = new ArrayList<>(); + + if (message.getHeaders().containsKey(HEADER_NODE_IDS)) { + + final List<?> nodes + = message.getHeader(HEADER_NODE_IDS, Collections.singletonList(getEndpoint().getNode()), List.class); + message.removeHeader(HEADER_NODE_IDS); + if (null == nodes) { + + return false; + } + + for (final Object node : nodes) { + expandedNodeIds.add(tryParse(node.toString())); + } + } else { + + expandedNodeIds.add(tryParse(this.getEndpoint().getNode())); + } + + final MiloBrowseEndpoint endpoint = this.getEndpoint(); + final int depth = endpoint.isRecursive() ? endpoint.getDepth() : -1; + final boolean subTypes = endpoint.isIncludeSubTypes() || endpoint.isRecursive(); + + final CompletableFuture<?> future = this.connection + .browse(expandedNodeIds, endpoint.getDirection(), endpoint.getNodeClassMask(), depth, endpoint.getFilter(), + subTypes, endpoint.getMaxNodeIdsPerRequest()) + .thenApply(browseResults -> { + + final List<String> expandedNodes = browseResults.values().stream() + .map(BrowseResult::getReferences) + .flatMap(Stream::of) + .map(ReferenceDescription::getNodeId) + .map(ExpandedNodeId::toParseableString) + .collect(Collectors.toList()); + + // For convenience, to be used with the milo-client producer + exchange.getMessage().setHeader(HEADER_NODE_IDS, expandedNodes); + + exchange.getMessage().setBody(browseResults); + + return browseResults; + }); + + final Boolean await = message.getHeader("await", this.defaultAwaitWrites, Boolean.class); + + if (TRUE.equals(await)) { + future.whenComplete((v, ex) -> async.done(false)); + return false; + } else { + return true; Review comment: I changed that, too. The thing is that I copied that code part from the existing milo-client component (admittedly without thinking enough). By the way, the test cases for the existing milo-client didn't seem to work, running the whole camel build indicated that the tests run into a timeout situation, which did not fail the overall build. I created an additional commit to change that in milo-client, too, alongside with some outdated surefire settings, apparently. Applying your proposed changes to milo-client, too, seems to fix the test cases. Or do you need another issue/ pull request for that? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org