All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.streamnative.pulsar.handlers.kop.utils.TopicNameUtils Maven / Gradle / Ivy

There is a newer version: 3.3.1.5
Show newest version
/**
 * Copyright (c) 2019 - 2024 StreamNative, Inc.. All Rights Reserved.
 */
/**
 * Licensed 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 io.streamnative.pulsar.handlers.kop.utils;

import static org.apache.pulsar.common.naming.TopicName.PARTITIONED_TOPIC_SUFFIX;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import lombok.NonNull;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.errors.InvalidTopicException;
import org.apache.kafka.common.internals.Topic;
import org.apache.pulsar.common.naming.NamespaceName;
import org.apache.pulsar.common.naming.TopicDomain;
import org.apache.pulsar.common.naming.TopicName;

/**
 * Utils for Pulsar TopicName.
 */
public class TopicNameUtils {

    private static final String persistentDomain = "persistent://";

    public static TopicName pulsarTopicName(TopicPartition topicPartition, NamespaceName namespace) {
        return pulsarTopicName(topicPartition.topic(), topicPartition.partition(), namespace);
    }

    public static TopicName pulsarTopicName(TopicPartition topicPartition) {
        return pulsarTopicName(topicPartition.topic(), topicPartition.partition());
    }

    private static TopicName pulsarTopicName(String topic, int partitionIndex) {
        return TopicName.get(topic + PARTITIONED_TOPIC_SUFFIX + partitionIndex);
    }

    public static TopicName pulsarTopicName(String topic, NamespaceName namespace) {
        return TopicName.get(TopicDomain.persistent.value(), namespace, topic);
    }

    public static TopicName pulsarTopicName(String topic) {
        return TopicName.get(topic);
    }

    public static TopicName pulsarTopicName(String topic, int partitionIndex, NamespaceName namespace) {
        if (topic.startsWith(TopicDomain.persistent.value())) {
            topic = topic.replace(TopicDomain.persistent.value() + "://", "");
        }

        if (topic.contains(namespace.getNamespaceObject().toString())) {
            topic = topic.replace(namespace.getNamespaceObject().toString() + "/", "");
        }
        return TopicName.get(TopicDomain.persistent.value(),
            namespace,
            topic + PARTITIONED_TOPIC_SUFFIX + partitionIndex);
    }

    public static String getPartitionedTopicNameWithoutPartitions(TopicName topicName) {
        String localName = topicName.getPartitionedTopicName();
        if (localName.contains(PARTITIONED_TOPIC_SUFFIX)) {
            return localName.substring(0, localName.lastIndexOf(PARTITIONED_TOPIC_SUFFIX));
        } else {
            return localName;
        }
    }

    /**
     * Get an url encoded topic name.
     */
    public static @NonNull String getTopicNameWithUrlEncoded(String topicName) {
        String encodedTopicName = "";
        try {
            encodedTopicName = URLEncoder.encode(topicName, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException ignore) {
            // The exception will never happen, because the charset always exists.
        }
        return encodedTopicName;
    }

    /**
     * Get an url decoded topic name.
     */
    public static @NonNull String getTopicNameWithUrlDecoded(String encodedTopicName) {
        String topicName = "";
        try {
            topicName = URLDecoder.decode(encodedTopicName, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException ignore) {
            // The exception will never happen, because the charset always exists.
        }
        return topicName;
    }

    /**
     * Convert the topic name from Pulsar topic to a valid dot-separated Kafka topic partition.
     * 

* Rules: * 1. The "persistent://" prefix will be discarded. * 2. The '/' delimiter will be replaced by '.'. * 3. The "-partition-N" suffix will be converted to the partition index if N is a valid integer. * 4. The converted Kafka topic name will discard the namespace prefix *

* @param topic the Pulsar topic name * @param namespacePrefix the Kafka namespace prefix, e.g. "public.default." * @return the Kafka topic partition */ public static TopicPartition pulsarToKafka(final String topic, final String namespacePrefix) { final String[] tokens; if (topic.startsWith(persistentDomain)) { tokens = topic.substring(persistentDomain.length()).split("/"); if (tokens.length < 3) { throw new IllegalArgumentException("Invalid Pulsar topic: " + topic); } } else { tokens = topic.split("/"); if (tokens.length == 2) { throw new IllegalArgumentException("Invalid Pulsar topic: " + topic); } } if (tokens.length >= 3) { if ((tokens[0] + "." + tokens[1] + ".").equals(namespacePrefix)) { return splitTopicPartition(String.join(".", Arrays.copyOfRange(tokens, 2, tokens.length))); } else { return splitTopicPartition(String.join(".", tokens)); } } else { return splitTopicPartition(topic); } } public static TopicPartition splitTopicPartition(final String topic) { final var index = topic.lastIndexOf(PARTITIONED_TOPIC_SUFFIX); if (index < 0) { return new TopicPartition(topic, 0); } final int partition; try { partition = Integer.parseInt(topic.substring(index + PARTITIONED_TOPIC_SUFFIX.length())); } catch (NumberFormatException __) { return new TopicPartition(topic, 0); } return new TopicPartition(topic.substring(0, index), partition); } /** * Convert the topic name from a dot-separated Kafka topic to a Pulsar topic. *

* Examples, for a namespace prefix "public/default/" * "tenant.ns.topic" => "persistent://tenant/ns/topic" * "topic" => "persistent://public/default/topic" * "user.topic" => "persistent://public/default/user.topic" * "tenant.ns.topic" => "persistent://tenant/ns/topic" * "tenant.ns.user.topic" => "persistent://tenant/ns/user.topic" *

* @param topic the Kafka topic name * @param namespacePrefix the Pulsar namespace prefix, e.g. "public/default/" * @return the Pulsar topic name * @throws InvalidTopicException if the topic name is invalid */ public static String kafkaToPulsar(final String topic, final String namespacePrefix) throws InvalidTopicException { final var prefix = (namespacePrefix.isEmpty() ? "" : persistentDomain + namespacePrefix); // '/' is an invalid character in Kafka topic name, in this case, treat it as a Pulsar topic name if (topic.contains("/")) { try { return TopicName.get(topic).toString(); } catch (Exception e) { throw new InvalidTopicException(e); } } final int index1 = topic.indexOf('.'); if (index1 < 0) { return prefix + topic; } final int index2 = topic.indexOf('.', index1 + 1); if (index2 < 0) { return prefix + topic; } // We don't allow Kafka clients to access tenant and namespace whose name has a dot character final var tenant = topic.substring(0, index1); final var namespace = topic.substring(index1 + 1, index2); final var shortTopic = topic.substring(index2 + 1); Topic.validate(shortTopic); return persistentDomain + tenant + "/" + namespace + "/" + shortTopic; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy