gnodet-bot commented on code in PR #24844: URL: https://github.com/apache/camel/pull/24844#discussion_r4014391107
########## 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: **Side-effect header not documented.** `doDryRun()` sets `HEADER_VALIDATION_ERRORS` on the message when `RuleViolationProblemDetails` is thrown, and both `testCompatibility` and `validate` delegate to it. For `validate` this is intentional and documented. For `testCompatibility` it is a surprise: users who call `testCompatibility` will find `CamelApicurioRegistryValidationErrors` set on the exchange when the schema is incompatible, with no mention of it anywhere in the docs or the Javadoc of `testCompatibility`. Either (a) clear the header inside `testCompatibility` after calling `doDryRun`, or (b) document that `testCompatibility` also sets `HEADER_VALIDATION_ERRORS` on incompatibility. Option (a) avoids polluting the exchange with validation-specific state when the caller only asked for a boolean result: ```suggestion @InvokeOnHeader(ApicurioRegistryConstants.OPERATION_TEST_COMPATIBILITY) public void testCompatibility(Message message) throws Exception { boolean compatible = doDryRun(message); // doDryRun sets HEADER_VALIDATION_ERRORS as a side-effect; remove it here // since testCompatibility returns the result in the body, not via headers. message.removeHeader(ApicurioRegistryConstants.HEADER_VALIDATION_ERRORS); message.setBody(compatible); } ``` ########## 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: **Exchange auto-release is missing.** `createExchange(true)` creates an exchange that `ScheduledPollConsumer` will auto-release — but only if you use the base-class exchange tracking. When you call `getProcessor().process(exchange)` directly and the processor throws, the exchange is abandoned without going through `releaseExchange`. The safer Camel pattern used by most `ScheduledPollConsumer` implementations is to call `defaultPollStrategy` or at a minimum wrap in a `try/finally`: ```suggestion try { getProcessor().process(exchange); } catch (Exception e) { handleException("Failed to process exchange for artifact version globalId=" + globalId, e); } finally { releaseExchange(exchange, false); } lastSeenGlobalId = globalId; ``` With the current code, if `process()` throws, (1) the exchange is leaked, (2) `lastSeenGlobalId` is not updated, and (3) the entire `poll()` loop aborts — versions processed _after_ the failing one are never seen until the next poll cycle. Wrapping in `try/catch/finally` allows the loop to continue to the next version and lets the error handler decide how to report the failure. -- 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]
