exceptionfactory commented on code in PR #10105:
URL: https://github.com/apache/nifi/pull/10105#discussion_r2274982687


##########
nifi-extension-bundles/nifi-confluent-platform-bundle/nifi-confluent-protobuf-message-name-resolver/src/main/java/org/apache/nifi/confluent/schemaregistry/ConfluentProtobufMessageNameResolver.java:
##########
@@ -0,0 +1,194 @@
+/*
+ * 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.nifi.confluent.schemaregistry;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.annotation.lifecycle.OnDisabled;
+import org.apache.nifi.annotation.lifecycle.OnEnabled;
+import org.apache.nifi.confluent.schema.AntlrProtobufMessageSchemaParser;
+import org.apache.nifi.confluent.schema.ProtobufMessageSchema;
+import org.apache.nifi.controller.AbstractControllerService;
+import org.apache.nifi.controller.ConfigurationContext;
+import org.apache.nifi.logging.ComponentLog;
+import org.apache.nifi.schemaregistry.services.MessageName;
+import org.apache.nifi.schemaregistry.services.MessageNameResolver;
+import org.apache.nifi.schemaregistry.services.SchemaDefinition;
+import org.apache.nifi.schemaregistry.services.StandardMessageName;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import static java.lang.String.format;
+import static java.util.Collections.singletonList;
+import static 
org.apache.nifi.confluent.schemaregistry.VarintUtils.decodeZigZag;
+import static 
org.apache.nifi.confluent.schemaregistry.VarintUtils.readVarintFromStream;
+import static 
org.apache.nifi.confluent.schemaregistry.VarintUtils.readVarintFromStreamAfterFirstByteConsumed;
+
+@Tags({"confluent", "schema", "registry", "protobuf", "message", "name", 
"resolver"})
+@CapabilityDescription("""
+    Resolves Protobuf message names from Confluent Schema Registry wire format 
by decoding message indexes and looking up the fully qualified name in the 
schema definition
+    For Confluent wire format reference see: 
https://docs.confluent.io/platform/current/schema-registry/fundamentals/serdes-develop/index.html#wire-format
+    """)
+public class ConfluentProtobufMessageNameResolver extends 
AbstractControllerService implements MessageNameResolver {
+
+    public static final int MAXIMUM_SUPPORTED_ARRAY_LENGTH = 100;
+    public static final int MAXIMUM_CACHE_SIZE = 1000;

Review Comment:
   These should be `private` values:
   ```suggestion
       private static final int MAXIMUM_SUPPORTED_ARRAY_LENGTH = 100;
       private static final int MAXIMUM_CACHE_SIZE = 1000;
   ```



##########
nifi-extension-bundles/nifi-confluent-platform-bundle/nifi-confluent-protobuf-message-name-resolver/src/main/java/org/apache/nifi/confluent/schemaregistry/ConfluentProtobufMessageNameResolver.java:
##########
@@ -0,0 +1,194 @@
+/*
+ * 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.nifi.confluent.schemaregistry;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.annotation.lifecycle.OnDisabled;
+import org.apache.nifi.annotation.lifecycle.OnEnabled;
+import org.apache.nifi.confluent.schema.AntlrProtobufMessageSchemaParser;
+import org.apache.nifi.confluent.schema.ProtobufMessageSchema;
+import org.apache.nifi.controller.AbstractControllerService;
+import org.apache.nifi.controller.ConfigurationContext;
+import org.apache.nifi.logging.ComponentLog;
+import org.apache.nifi.schemaregistry.services.MessageName;
+import org.apache.nifi.schemaregistry.services.MessageNameResolver;
+import org.apache.nifi.schemaregistry.services.SchemaDefinition;
+import org.apache.nifi.schemaregistry.services.StandardMessageName;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import static java.lang.String.format;
+import static java.util.Collections.singletonList;
+import static 
org.apache.nifi.confluent.schemaregistry.VarintUtils.decodeZigZag;
+import static 
org.apache.nifi.confluent.schemaregistry.VarintUtils.readVarintFromStream;
+import static 
org.apache.nifi.confluent.schemaregistry.VarintUtils.readVarintFromStreamAfterFirstByteConsumed;
+
+@Tags({"confluent", "schema", "registry", "protobuf", "message", "name", 
"resolver"})
+@CapabilityDescription("""
+    Resolves Protobuf message names from Confluent Schema Registry wire format 
by decoding message indexes and looking up the fully qualified name in the 
schema definition
+    For Confluent wire format reference see: 
https://docs.confluent.io/platform/current/schema-registry/fundamentals/serdes-develop/index.html#wire-format
+    """)
+public class ConfluentProtobufMessageNameResolver extends 
AbstractControllerService implements MessageNameResolver {
+
+    public static final int MAXIMUM_SUPPORTED_ARRAY_LENGTH = 100;
+    public static final int MAXIMUM_CACHE_SIZE = 1000;
+    private Cache<FindMessageNameArguments, MessageName> messageNameCache;
+
+    @OnEnabled
+    public void onEnabled(final ConfigurationContext context) {
+        messageNameCache = 
Caffeine.newBuilder().maximumSize(MAXIMUM_CACHE_SIZE).expireAfterWrite(Duration.ofHours(1)).build();
+    }
+
+    @OnDisabled
+    public void onDisabled(final ConfigurationContext context) {
+        if (messageNameCache != null) {
+            messageNameCache.invalidateAll();
+            messageNameCache = null;
+        }
+    }
+
+
+    @Override
+    public MessageName getMessageName(final Map<String, String> variables, 
final SchemaDefinition schemaDefinition, final InputStream inputStream) throws 
IOException {
+        final ComponentLog logger = getLogger();
+
+        // Read message indexes directly from stream (Confluent wire format)
+        final List<Integer> messageIndexes = 
readMessageIndexesFromStream(inputStream);
+        if (logger.isDebugEnabled()) {

Review Comment:
   This conditional is not needed since the log string does not include 
additional processing.



##########
nifi-extension-bundles/nifi-confluent-platform-bundle/nifi-confluent-protobuf-message-name-resolver/src/main/java/org/apache/nifi/confluent/schemaregistry/ConfluentProtobufMessageNameResolver.java:
##########
@@ -0,0 +1,194 @@
+/*
+ * 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.nifi.confluent.schemaregistry;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.annotation.lifecycle.OnDisabled;
+import org.apache.nifi.annotation.lifecycle.OnEnabled;
+import org.apache.nifi.confluent.schema.AntlrProtobufMessageSchemaParser;
+import org.apache.nifi.confluent.schema.ProtobufMessageSchema;
+import org.apache.nifi.controller.AbstractControllerService;
+import org.apache.nifi.controller.ConfigurationContext;
+import org.apache.nifi.logging.ComponentLog;
+import org.apache.nifi.schemaregistry.services.MessageName;
+import org.apache.nifi.schemaregistry.services.MessageNameResolver;
+import org.apache.nifi.schemaregistry.services.SchemaDefinition;
+import org.apache.nifi.schemaregistry.services.StandardMessageName;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import static java.lang.String.format;
+import static java.util.Collections.singletonList;
+import static 
org.apache.nifi.confluent.schemaregistry.VarintUtils.decodeZigZag;
+import static 
org.apache.nifi.confluent.schemaregistry.VarintUtils.readVarintFromStream;
+import static 
org.apache.nifi.confluent.schemaregistry.VarintUtils.readVarintFromStreamAfterFirstByteConsumed;
+
+@Tags({"confluent", "schema", "registry", "protobuf", "message", "name", 
"resolver"})
+@CapabilityDescription("""
+    Resolves Protobuf message names from Confluent Schema Registry wire format 
by decoding message indexes and looking up the fully qualified name in the 
schema definition
+    For Confluent wire format reference see: 
https://docs.confluent.io/platform/current/schema-registry/fundamentals/serdes-develop/index.html#wire-format
+    """)
+public class ConfluentProtobufMessageNameResolver extends 
AbstractControllerService implements MessageNameResolver {
+
+    public static final int MAXIMUM_SUPPORTED_ARRAY_LENGTH = 100;
+    public static final int MAXIMUM_CACHE_SIZE = 1000;
+    private Cache<FindMessageNameArguments, MessageName> messageNameCache;
+
+    @OnEnabled
+    public void onEnabled(final ConfigurationContext context) {
+        messageNameCache = 
Caffeine.newBuilder().maximumSize(MAXIMUM_CACHE_SIZE).expireAfterWrite(Duration.ofHours(1)).build();

Review Comment:
   Minor note, recommend moving the `Duration` to a static value along with the 
cache size.



##########
nifi-extension-bundles/nifi-confluent-platform-bundle/nifi-confluent-schema-registry-service/src/main/java/org/apache/nifi/confluent/schemaregistry/client/RestSchemaRegistryClient.java:
##########
@@ -211,6 +220,91 @@ public RecordSchema getSchema(final int schemaId) throws 
IOException, SchemaNotF
         return createRecordSchema(completeSchema);
     }
 
+    @Override
+    public SchemaDefinition getSchemaDefinition(SchemaIdentifier identifier) 
throws SchemaNotFoundException {
+        final JsonNode schemaJson;
+        String subject = null;
+        Integer version = null;
+
+        // If we have an ID, get the schema by ID first
+        // Using schemaVersionId, because that is what is set by 
ConfluentEncodedSchemaReferenceReader.
+        // probably identifier field should be used, but I'm not changing 
ConfluentEncodedSchemaReferenceReader for backward compatibility reasons.
+        if (identifier.getSchemaVersionId().isPresent()) {
+            long schemaId = identifier.getSchemaVersionId().getAsLong();
+            String schemaPath = getSchemaPath(schemaId);
+            schemaJson = fetchJsonResponse(schemaPath, "id " + schemaId);
+        } else if (identifier.getName().isPresent()) {
+            // If we have a name or (name and version), get the schema by those
+            subject = identifier.getName().get();
+            version = identifier.getVersion().isPresent() ? 
identifier.getVersion().getAsInt() : null;
+            // if no version was specified, the latest version will be used. 
See @getSubjectPath method.
+            String pathSuffix = getSubjectPath(subject, version);
+            schemaJson = fetchJsonResponse(pathSuffix, "name " + subject);
+        } else {
+            throw new SchemaNotFoundException("Schema identifier must contain 
either a version identifier or a subject name");
+        }
+
+        // Extract schema information
+        String schemaText = schemaJson.get(SCHEMA_TEXT_FIELD_NAME).asText();
+        String schemaTypeText = 
schemaJson.get(SCHEMA_TYPE_FIELD_NAME).asText();
+        SchemaType schemaType = toSchemaType(schemaTypeText);
+
+        long schemaId;
+        if (schemaJson.has(ID_FIELD_NAME)) {
+            schemaId = schemaJson.get(ID_FIELD_NAME).asLong();
+        } else {
+            schemaId = identifier.getSchemaVersionId().getAsLong();
+        }
+
+        if (subject == null && schemaJson.has(SUBJECT_FIELD_NAME)) {
+            subject = schemaJson.get(SUBJECT_FIELD_NAME).asText();
+        }
+
+        if (version == null && schemaJson.has(VERSION_FIELD_NAME)) {
+            version = schemaJson.get(VERSION_FIELD_NAME).asInt();
+        }
+
+        // Build schema identifier with all available information
+        SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder()
+            .id(schemaId)
+            .name(subject)
+            .version(version)
+            .build();
+
+        // Process references if present
+        Map<String, SchemaDefinition> references = new HashMap<>();
+        if (schemaJson.has(REFERENCES_FIELD_NAME) && 
!schemaJson.get(REFERENCES_FIELD_NAME).isNull()) {
+            ArrayNode refsArray = (ArrayNode) 
schemaJson.get(REFERENCES_FIELD_NAME);
+            for (JsonNode ref : refsArray) {
+                String refName = ref.get(REFERENCE_NAME_FIELD_NAME).asText();
+                String refSubject = 
ref.get(REFERENCE_SUBJECT_FIELD_NAME).asText();
+                int refVersion = ref.get(REFERENCE_VERSION_FIELD_NAME).asInt();
+
+                // Recursively get referenced schema
+                SchemaIdentifier refId = SchemaIdentifier.builder()
+                    .name(refSubject)
+                    .version(refVersion)
+                    .build();
+                SchemaDefinition refSchema = getSchemaDefinition(refId);
+                references.put(refName, refSchema);
+            }
+        }
+
+        return new StandardSchemaDefinition(schemaIdentifier, schemaText, 
schemaType, references);
+    }
+
+    private SchemaType toSchemaType(final String schemaTypeText) {
+        try {
+            if (schemaTypeText == null || schemaTypeText.isEmpty()) {
+                return SchemaType.AVRO; // Default schema type for confluent 
schema registry is AVRO.
+            }
+            return SchemaType.valueOf(schemaTypeText.toUpperCase().trim());
+        } catch (final Exception e) {
+            final String message = String.format("Could not convert schema 
type '%s' to SchemaType enum.", schemaTypeText);

Review Comment:
   Minor note, messages should not end with a `.` character.
   ```suggestion
               final String message = String.format("Could not convert schema 
type [%s] to SchemaType", schemaTypeText);
   ```



##########
nifi-extension-bundles/nifi-standard-services/nifi-schema-registry-service-api/src/main/java/org/apache/nifi/schemaregistry/services/StandardSchemaDefinition.java:
##########
@@ -0,0 +1,111 @@
+/*
+ * 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.nifi.schemaregistry.services;
+
+import org.apache.nifi.serialization.record.SchemaIdentifier;
+
+import java.util.Map;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Simple implementation of SchemaDefinition that holds a schema identifier, 
text, and references.
+ */
+public class StandardSchemaDefinition implements SchemaDefinition {
+
+    private final SchemaIdentifier identifier;
+    private final String text;
+    private final Map<String, SchemaDefinition> references;
+    private final SchemaType schemaType;
+
+    /**
+     * Creates a new StandardSchemaDefinition.
+     *
+     * @param identifier the schema identifier
+     * @param text       the schema text content
+     * @param references map of schema references (key = reference name, value 
= referenced schema)
+     */
+    public StandardSchemaDefinition(final SchemaIdentifier identifier,
+                                    final String text,
+                                    final SchemaType schemaType,
+                                    final Map<String, SchemaDefinition> 
references
+
+    ) {
+        this.identifier = requireNonNull(identifier, "Schema identifier cannot 
be null");
+        this.text = requireNonNull(text, "Schema text cannot be null");
+        this.schemaType = requireNonNull(schemaType, "Schema type cannot be 
null");
+        this.references = references != null ? Map.copyOf(references) : 
Map.of();
+    }
+
+    /**
+     * Creates a new StandardSchemaDefinition without references.
+     *
+     * @param identifier the schema identifier
+     * @param text       the schema text content
+     */
+    public StandardSchemaDefinition(final SchemaIdentifier identifier, final 
String text, final SchemaType schemaType) {
+        this(identifier, text, schemaType, null);
+    }
+
+    @Override
+    public SchemaType getSchemaType() {
+        return schemaType;
+    }
+
+    @Override
+    public SchemaIdentifier getIdentifier() {
+        return identifier;
+    }
+
+    @Override
+    public String getText() {
+        return text;
+    }
+
+    @Override
+    public Map<String, SchemaDefinition> getReferences() {
+        return references;
+    }
+
+    @Override
+    public final boolean equals(final Object o) {
+        if (!(o instanceof final StandardSchemaDefinition that)) {
+            return false;
+        }
+
+        return identifier.equals(that.identifier) && text.equals(that.text) && 
references.equals(that.references) && schemaType == that.schemaType;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = identifier.hashCode();
+        result = 31 * result + text.hashCode();
+        result = 31 * result + references.hashCode();
+        result = 31 * result + schemaType.hashCode();
+        return result;
+    }
+
+    @Override
+    public String toString() {
+        return "StandardSchemaDefinition{" +
+            "identifier=" + identifier +
+            ", text='" + text + '\'' +

Review Comment:
   Including the entire schema text in the toString() seems excessive, 
recommend removing the text property.



##########
nifi-extension-bundles/nifi-standard-services/nifi-schema-registry-service-api/src/main/java/org/apache/nifi/schemaregistry/services/StandardMessageName.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * 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.nifi.schemaregistry.services;
+
+import java.util.Objects;
+import java.util.Optional;
+
+public class StandardMessageName implements MessageName {
+
+    private final Optional<String> namespace;
+    private final String name;
+
+    public StandardMessageName(final Optional<String> namespace, final String 
name) {
+        this.name = Objects.requireNonNull(name, "name must not be null");
+        this.namespace = namespace;
+    }
+
+    @Override
+    public String getName() {
+        return name;
+    }
+
+    @Override
+    public Optional<String> getNamespace() {
+        return namespace;
+    }
+
+    @Override
+    public final boolean equals(final Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (!(o instanceof final StandardMessageName that)) {
+            return false;
+        }
+
+        return name.equals(that.name) && namespace.equals(that.namespace);
+    }
+
+    @Override
+    public int hashCode() {
+        int result = name.hashCode();
+        result = 31 * result + namespace.hashCode();
+        return result;
+    }
+
+    @Override
+    public String toString() {
+        return String.format("StandardMessageName{name='%s', namespace=%s}", 
name, namespace);

Review Comment:
   Minor detail, the quoting should be consistent for both values, recommend 
removing it in this case:
   ```suggestion
           return String.format("StandardMessageName{name=%s, namespace=%s}", 
name, namespace);
   ```



-- 
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