carlesarnal commented on code in PR #24844:
URL: https://github.com/apache/camel/pull/24844#discussion_r4024851123


##########
components/camel-apicurio-registry/src/main/java/org/apache/camel/component/apicurioregistry/ApicurioRegistryConsumer.java:
##########
@@ -0,0 +1,101 @@
+/*
+ * 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.apicurioregistry;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+
+import io.apicurio.registry.rest.client.RegistryClient;
+import io.apicurio.registry.rest.client.models.SearchedVersion;
+import io.apicurio.registry.rest.client.models.VersionSearchResults;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.support.ScheduledPollConsumer;
+
+public class ApicurioRegistryConsumer extends ScheduledPollConsumer {
+
+    private final ApicurioRegistryEndpoint endpoint;
+    private final ApicurioRegistryConfiguration configuration;
+    private volatile Long lastSeenGlobalId;
+
+    public ApicurioRegistryConsumer(ApicurioRegistryEndpoint endpoint, 
Processor processor,
+                                    ApicurioRegistryConfiguration 
configuration) {
+        super(endpoint, processor);
+        this.endpoint = endpoint;
+        this.configuration = configuration;
+    }
+
+    @Override
+    protected int poll() throws Exception {
+        String groupId = endpoint.getGroupId();
+        String artifactId = endpoint.getArtifactId();
+
+        if (groupId == null || artifactId == null) {
+            throw new IllegalArgumentException(
+                    "Both groupId and artifactId are required for the 
consumer");
+        }
+
+        RegistryClient client = endpoint.getRegistryClient();
+        VersionSearchResults results = client.groups().byGroupId(groupId)
+                .artifacts().byArtifactId(artifactId).versions().get();
+
+        if (results == null || results.getVersions() == null) {
+            return 0;
+        }
+
+        List<SearchedVersion> versions = new 
ArrayList<>(results.getVersions());
+        versions.sort(Comparator.comparingLong(SearchedVersion::getGlobalId));
+
+        int count = 0;
+        for (SearchedVersion version : versions) {
+            Long globalId = version.getGlobalId();
+            if (lastSeenGlobalId == null || globalId > lastSeenGlobalId) {
+                Exchange exchange = createExchange(true);
+                Message message = exchange.getIn();
+
+                message.setHeader(ApicurioRegistryConstants.HEADER_GROUP_ID, 
groupId);
+                
message.setHeader(ApicurioRegistryConstants.HEADER_ARTIFACT_ID, artifactId);
+                message.setHeader(ApicurioRegistryConstants.HEADER_VERSION, 
version.getVersion());
+                message.setHeader(ApicurioRegistryConstants.HEADER_GLOBAL_ID, 
globalId);
+                message.setHeader(ApicurioRegistryConstants.HEADER_CONTENT_ID, 
version.getContentId());
+                
message.setHeader(ApicurioRegistryConstants.HEADER_ARTIFACT_TYPE, 
version.getArtifactType());
+                if (version.getState() != null) {
+                    
message.setHeader(ApicurioRegistryConstants.HEADER_VERSION_STATE,
+                            version.getState().getValue());
+                }
+
+                if (configuration.isFetchContent()) {
+                    try (InputStream content = 
client.groups().byGroupId(groupId).artifacts()
+                            .byArtifactId(artifactId).versions()
+                            
.byVersionExpression(version.getVersion()).content().get()) {
+                        message.setBody(content.readAllBytes());
+                    }
+                } else {
+                    message.setBody(version);
+                }
+
+                getProcessor().process(exchange);

Review Comment:
   The cleanup is fixed in 5a4e13f66c4, including failures before the processor 
is invoked. I kept the existing behavior of aborting the poll without advancing 
the watermark when an exception is thrown: continuing and advancing past that 
version would make it ineligible for retry. The new pooled-exchange tests cover 
this retry behavior, and it is documented.
   
   _AI-assisted response prepared with OpenCode on behalf of @carlesarnal._



##########
components/camel-apicurio-registry/src/main/java/org/apache/camel/component/apicurioregistry/ApicurioRegistryConsumer.java:
##########
@@ -0,0 +1,101 @@
+/*
+ * 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.apicurioregistry;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+
+import io.apicurio.registry.rest.client.RegistryClient;
+import io.apicurio.registry.rest.client.models.SearchedVersion;
+import io.apicurio.registry.rest.client.models.VersionSearchResults;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.support.ScheduledPollConsumer;
+
+public class ApicurioRegistryConsumer extends ScheduledPollConsumer {
+
+    private final ApicurioRegistryEndpoint endpoint;
+    private final ApicurioRegistryConfiguration configuration;
+    private volatile Long lastSeenGlobalId;
+
+    public ApicurioRegistryConsumer(ApicurioRegistryEndpoint endpoint, 
Processor processor,
+                                    ApicurioRegistryConfiguration 
configuration) {
+        super(endpoint, processor);
+        this.endpoint = endpoint;
+        this.configuration = configuration;
+    }
+
+    @Override
+    protected int poll() throws Exception {
+        String groupId = endpoint.getGroupId();
+        String artifactId = endpoint.getArtifactId();
+
+        if (groupId == null || artifactId == null) {
+            throw new IllegalArgumentException(
+                    "Both groupId and artifactId are required for the 
consumer");
+        }
+
+        RegistryClient client = endpoint.getRegistryClient();
+        VersionSearchResults results = client.groups().byGroupId(groupId)
+                .artifacts().byArtifactId(artifactId).versions().get();
+
+        if (results == null || results.getVersions() == null) {
+            return 0;
+        }
+
+        List<SearchedVersion> versions = new 
ArrayList<>(results.getVersions());
+        versions.sort(Comparator.comparingLong(SearchedVersion::getGlobalId));
+
+        int count = 0;
+        for (SearchedVersion version : versions) {
+            Long globalId = version.getGlobalId();
+            if (lastSeenGlobalId == null || globalId > lastSeenGlobalId) {
+                Exchange exchange = createExchange(true);
+                Message message = exchange.getIn();
+
+                message.setHeader(ApicurioRegistryConstants.HEADER_GROUP_ID, 
groupId);
+                
message.setHeader(ApicurioRegistryConstants.HEADER_ARTIFACT_ID, artifactId);
+                message.setHeader(ApicurioRegistryConstants.HEADER_VERSION, 
version.getVersion());
+                message.setHeader(ApicurioRegistryConstants.HEADER_GLOBAL_ID, 
globalId);
+                message.setHeader(ApicurioRegistryConstants.HEADER_CONTENT_ID, 
version.getContentId());
+                
message.setHeader(ApicurioRegistryConstants.HEADER_ARTIFACT_TYPE, 
version.getArtifactType());
+                if (version.getState() != null) {
+                    
message.setHeader(ApicurioRegistryConstants.HEADER_VERSION_STATE,
+                            version.getState().getValue());
+                }
+
+                if (configuration.isFetchContent()) {
+                    try (InputStream content = 
client.groups().byGroupId(groupId).artifacts()
+                            .byArtifactId(artifactId).versions()
+                            
.byVersionExpression(version.getVersion()).content().get()) {
+                        message.setBody(content.readAllBytes());
+                    }
+                } else {

Review Comment:
   Fixed in 5a4e13f66c4. The consumer now uses createExchange(false) with 
releaseExchange(exchange, false) in a finally block around content retrieval 
and processing. Using manual release consistently avoids combining automatic 
and explicit release.
   
   Added tests with PooledExchangeFactory for both content-fetch and processor 
failures. They verify release counters, reuse of the same pooled allocation, 
and successful retry before advancing to the next version.
   
   _AI-assisted response prepared with OpenCode on behalf of @carlesarnal._



##########
components/camel-apicurio-registry/src/main/java/org/apache/camel/component/apicurioregistry/ApicurioRegistryComponent.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.apicurioregistry;
+
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Endpoint;
+import org.apache.camel.spi.Metadata;
+import org.apache.camel.spi.annotations.Component;
+import org.apache.camel.support.DefaultComponent;
+
+@Component("apicurio-registry")
+public class ApicurioRegistryComponent extends DefaultComponent {
+
+    @Metadata(label = "advanced", description = "The component configuration")
+    private ApicurioRegistryConfiguration configuration = new 
ApicurioRegistryConfiguration();
+
+    public ApicurioRegistryComponent() {
+    }
+
+    public ApicurioRegistryComponent(CamelContext context) {
+        super(context);
+    }
+
+    @Override
+    protected Endpoint createEndpoint(String uri, String remaining, 
Map<String, Object> parameters) throws Exception {
+        ApicurioRegistryConfiguration config = this.configuration.copy();
+
+        String groupId = null;
+        String artifactId = null;
+        if (remaining != null && !remaining.isEmpty()) {
+            String[] parts = remaining.split("/", 2);
+            groupId = parts[0];
+            if (parts.length > 1 && !parts[1].isEmpty()) {
+                artifactId = parts[1];
+            }

Review Comment:
   Fixed in 5a4e13f66c4: an empty group ID is normalized to null. Added a 
parsing test for /someArtifact and consumer tests for missing group/artifact 
IDs and trailing slashes. They now produce the clear missing-identifiers 
exception.
   
   _AI-assisted response prepared with OpenCode on behalf of @carlesarnal._



##########
components/camel-apicurio-registry/src/main/java/org/apache/camel/component/apicurioregistry/ApicurioRegistryProducer.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.apicurioregistry;
+
+import java.io.InputStream;
+
+import io.apicurio.registry.rest.client.RegistryClient;
+import io.apicurio.registry.rest.client.models.ArtifactMetaData;
+import io.apicurio.registry.rest.client.models.CreateArtifact;
+import io.apicurio.registry.rest.client.models.CreateArtifactResponse;
+import io.apicurio.registry.rest.client.models.CreateGroup;
+import io.apicurio.registry.rest.client.models.CreateVersion;
+import io.apicurio.registry.rest.client.models.GroupMetaData;
+import io.apicurio.registry.rest.client.models.IfArtifactExists;
+import io.apicurio.registry.rest.client.models.RuleViolationProblemDetails;
+import io.apicurio.registry.rest.client.models.VersionContent;
+import io.apicurio.registry.rest.client.models.VersionMetaData;
+import io.apicurio.registry.rest.client.models.VersionSearchResults;
+import org.apache.camel.Message;
+import org.apache.camel.spi.InvokeOnHeader;
+import org.apache.camel.support.HeaderSelectorProducer;
+
+public class ApicurioRegistryProducer extends HeaderSelectorProducer {
+
+    private final ApicurioRegistryEndpoint endpoint;
+    private final ApicurioRegistryConfiguration configuration;
+
+    public ApicurioRegistryProducer(ApicurioRegistryEndpoint endpoint,
+                                    ApicurioRegistryConfiguration 
configuration) {
+        super(endpoint, ApicurioRegistryConstants.HEADER_OPERATION, 
configuration::getOperation);
+        this.endpoint = endpoint;
+        this.configuration = configuration;
+    }
+
+    private RegistryClient getClient() {
+        return endpoint.getRegistryClient();
+    }
+
+    private String resolveGroupId(Message message) {
+        String gid = 
message.getHeader(ApicurioRegistryConstants.HEADER_GROUP_ID, String.class);
+        return gid != null ? gid : endpoint.getGroupId();
+    }
+
+    private String resolveArtifactId(Message message) {
+        String aid = 
message.getHeader(ApicurioRegistryConstants.HEADER_ARTIFACT_ID, String.class);
+        return aid != null ? aid : endpoint.getArtifactId();
+    }
+
+    @InvokeOnHeader(ApicurioRegistryConstants.OPERATION_CREATE_ARTIFACT)
+    public void createArtifact(Message message) {
+        String groupId = resolveGroupId(message);
+        String artifactId = resolveArtifactId(message);
+        String artifactType = message.getHeader(
+                ApicurioRegistryConstants.HEADER_ARTIFACT_TYPE, 
configuration.getArtifactType(), String.class);
+        String name = 
message.getHeader(ApicurioRegistryConstants.HEADER_ARTIFACT_NAME, String.class);
+        String description = 
message.getHeader(ApicurioRegistryConstants.HEADER_ARTIFACT_DESCRIPTION, 
String.class);
+        String content = message.getBody(String.class);
+        String contentType = message.getHeader(
+                ApicurioRegistryConstants.HEADER_CONTENT_TYPE, 
"application/json", String.class);
+        String ifExistsVal = message.getHeader(
+                ApicurioRegistryConstants.HEADER_IF_EXISTS, 
configuration.getIfExists(), String.class);
+
+        CreateArtifact createArtifact = new CreateArtifact();
+        createArtifact.setArtifactId(artifactId);
+        createArtifact.setArtifactType(artifactType);
+        createArtifact.setName(name);
+        createArtifact.setDescription(description);
+
+        if (content != null) {
+            CreateVersion firstVersion = new CreateVersion();
+            VersionContent vc = new VersionContent();
+            vc.setContent(content);
+            vc.setContentType(contentType);
+            firstVersion.setContent(vc);
+            createArtifact.setFirstVersion(firstVersion);
+        }
+
+        CreateArtifactResponse result = 
getClient().groups().byGroupId(groupId).artifacts()
+                .post(createArtifact, config -> {
+                    if (ifExistsVal != null) {
+                        config.queryParameters.ifExists = 
IfArtifactExists.forValue(ifExistsVal);
+                    }
+                });
+        message.setBody(result);
+    }
+
+    @InvokeOnHeader(ApicurioRegistryConstants.OPERATION_UPDATE_ARTIFACT)
+    public void updateArtifact(Message message) {
+        String groupId = resolveGroupId(message);
+        String artifactId = resolveArtifactId(message);
+        String content = message.getBody(String.class);
+        String version = 
message.getHeader(ApicurioRegistryConstants.HEADER_VERSION, String.class);
+        String contentType = message.getHeader(
+                ApicurioRegistryConstants.HEADER_CONTENT_TYPE, 
"application/json", String.class);
+
+        CreateVersion createVersion = new CreateVersion();
+        createVersion.setVersion(version);
+        VersionContent vc = new VersionContent();
+        vc.setContent(content);
+        vc.setContentType(contentType);
+        createVersion.setContent(vc);
+
+        VersionMetaData result = 
getClient().groups().byGroupId(groupId).artifacts()
+                .byArtifactId(artifactId).versions().post(createVersion);
+        message.setBody(result);
+    }
+
+    @InvokeOnHeader(ApicurioRegistryConstants.OPERATION_DELETE_ARTIFACT)
+    public void deleteArtifact(Message message) {
+        String groupId = resolveGroupId(message);
+        String artifactId = resolveArtifactId(message);
+        
getClient().groups().byGroupId(groupId).artifacts().byArtifactId(artifactId).delete();
+    }
+
+    @InvokeOnHeader(ApicurioRegistryConstants.OPERATION_GET_ARTIFACT_CONTENT)
+    public void getArtifactContent(Message message) throws Exception {
+        String groupId = resolveGroupId(message);
+        String artifactId = resolveArtifactId(message);
+        String version = message.getHeader(
+                ApicurioRegistryConstants.HEADER_VERSION, "branch=latest", 
String.class);
+
+        try (InputStream content = 
getClient().groups().byGroupId(groupId).artifacts()
+                
.byArtifactId(artifactId).versions().byVersionExpression(version).content().get())
 {
+            message.setBody(content.readAllBytes());
+        }
+    }
+
+    @InvokeOnHeader(ApicurioRegistryConstants.OPERATION_GET_ARTIFACT_METADATA)
+    public void getArtifactMetadata(Message message) {
+        String groupId = resolveGroupId(message);
+        String artifactId = resolveArtifactId(message);
+
+        ArtifactMetaData metadata = 
getClient().groups().byGroupId(groupId).artifacts()
+                .byArtifactId(artifactId).get();
+        message.setBody(metadata);
+    }
+
+    @InvokeOnHeader(ApicurioRegistryConstants.OPERATION_SEARCH_ARTIFACTS)
+    public void searchArtifacts(Message message) {
+        var results = getClient().search().artifacts().get(config -> {
+            String name = 
message.getHeader(ApicurioRegistryConstants.HEADER_ARTIFACT_NAME, String.class);
+            String groupId = resolveGroupId(message);
+            String description = message.getHeader(
+                    ApicurioRegistryConstants.HEADER_ARTIFACT_DESCRIPTION, 
String.class);
+            if (name != null) {
+                config.queryParameters.name = name;
+            }
+            if (groupId != null) {
+                config.queryParameters.groupId = groupId;
+            }
+            if (description != null) {
+                config.queryParameters.description = description;
+            }
+        });
+        message.setBody(results);
+    }
+
+    @InvokeOnHeader(ApicurioRegistryConstants.OPERATION_LIST_VERSIONS)
+    public void listVersions(Message message) {
+        String groupId = resolveGroupId(message);
+        String artifactId = resolveArtifactId(message);
+
+        VersionSearchResults results = 
getClient().groups().byGroupId(groupId).artifacts()
+                .byArtifactId(artifactId).versions().get();
+        message.setBody(results);
+    }
+
+    @InvokeOnHeader(ApicurioRegistryConstants.OPERATION_CREATE_GROUP)
+    public void createGroup(Message message) {
+        String groupId = resolveGroupId(message);
+        String description = message.getHeader(
+                ApicurioRegistryConstants.HEADER_ARTIFACT_DESCRIPTION, 
String.class);
+
+        CreateGroup createGroup = new CreateGroup();
+        createGroup.setGroupId(groupId);
+        createGroup.setDescription(description);
+
+        GroupMetaData result = getClient().groups().post(createGroup);
+        message.setBody(result);
+    }
+
+    @InvokeOnHeader(ApicurioRegistryConstants.OPERATION_TEST_COMPATIBILITY)
+    public void testCompatibility(Message message) throws Exception {
+        boolean compatible = doDryRun(message);
+        message.setBody(compatible);
+    }
+
+    @InvokeOnHeader(ApicurioRegistryConstants.OPERATION_VALIDATE)
+    public void validate(Message message) throws Exception {
+        boolean valid = doDryRun(message);

Review Comment:
   I kept the error details and documented them for both operations in the 
component page and header metadata. They are useful when a compatibility check 
returns false. The tests now explicitly verify that testCompatibility sets the 
details on failure and clears a stale error on success. Included in 5a4e13f66c4.
   
   _AI-assisted response prepared with OpenCode on behalf of @carlesarnal._



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to